common_inet_proxy_handle.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package common
  2. import (
  3. "context"
  4. "github.com/cnlh/nps/bridge"
  5. "github.com/cnlh/nps/core"
  6. "net"
  7. )
  8. type Proxy struct {
  9. clientConn net.Conn
  10. ctx context.Context
  11. }
  12. func (proxy *Proxy) GetConfigName() *core.NpsConfigs {
  13. return core.NewNpsConfigs("socks5_proxy", "proxy to inet")
  14. }
  15. func (proxy *Proxy) GetStage() core.Stage {
  16. return core.STAGE_RUN
  17. }
  18. func (proxy *Proxy) Start(ctx context.Context, config map[string]string) error {
  19. return nil
  20. }
  21. func (proxy *Proxy) Run(ctx context.Context, config map[string]string) error {
  22. clientCtxConn := ctx.Value(core.CLIENT_CONNECTION)
  23. if clientCtxConn == nil {
  24. return core.CLIENT_CONNECTION_NOT_EXIST
  25. }
  26. proxy.clientConn = clientCtxConn.(net.Conn)
  27. proxy.ctx = ctx
  28. bg := ctx.Value(core.BRIDGE)
  29. if bg == nil {
  30. return core.BRIDGE_NOT_EXIST
  31. }
  32. clientCtxConn := ctx.Value(core.CLIENT_CONNECTION)
  33. if clientCtxConn == nil {
  34. return core.CLIENT_CONNECTION_NOT_EXIST
  35. }
  36. clientId := ctx.Value(core.CLIENT_ID)
  37. if clientId == nil {
  38. return core.CLIENT_ID_NOT_EXIST
  39. }
  40. brg := bg.(*bridge.Bridge)
  41. severConn, err := brg.GetConnByClientId(clientId.(int))
  42. if err != nil {
  43. return err
  44. }
  45. go core.CopyBuffer(severConn, clientCtxConn.(net.Conn))
  46. core.CopyBuffer(clientCtxConn.(net.Conn), severConn)
  47. return nil
  48. }
  49. func (proxy *Proxy) End(ctx context.Context, config map[string]string) error {
  50. return nil
  51. }