client.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package shadowsocks
  2. import (
  3. "v2ray.com/core/common/alloc"
  4. "v2ray.com/core/common/log"
  5. v2net "v2ray.com/core/common/net"
  6. "v2ray.com/core/common/protocol"
  7. "v2ray.com/core/common/retry"
  8. "v2ray.com/core/proxy"
  9. "v2ray.com/core/transport/internet"
  10. "v2ray.com/core/transport/ray"
  11. )
  12. type Client struct {
  13. serverPicker protocol.ServerPicker
  14. meta *proxy.OutboundHandlerMeta
  15. }
  16. func (this *Client) Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) error {
  17. defer payload.Release()
  18. defer ray.OutboundInput().Release()
  19. defer ray.OutboundOutput().Close()
  20. network := destination.Network
  21. var server *protocol.ServerSpec
  22. var conn internet.Connection
  23. err := retry.Timed(5, 100).On(func() error {
  24. server = this.serverPicker.PickServer()
  25. dest := server.Destination()
  26. dest.Network = network
  27. rawConn, err := internet.Dial(this.meta.Address, dest, this.meta.StreamSettings)
  28. if err != nil {
  29. return err
  30. }
  31. conn = rawConn
  32. return nil
  33. })
  34. if err != nil {
  35. log.Error("Shadowsocks|Client: Failed to find an available destination:", err)
  36. return err
  37. }
  38. log.Info("Shadowsocks|Client: Tunneling request to ", destination, " via ", server.Destination())
  39. return nil
  40. }