shadowsocks.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // R.I.P Shadowsocks
  2. package shadowsocks
  3. import (
  4. "sync"
  5. "github.com/v2ray/v2ray-core/app"
  6. "github.com/v2ray/v2ray-core/common/alloc"
  7. "github.com/v2ray/v2ray-core/common/log"
  8. v2net "github.com/v2ray/v2ray-core/common/net"
  9. "github.com/v2ray/v2ray-core/proxy"
  10. "github.com/v2ray/v2ray-core/proxy/internal"
  11. "github.com/v2ray/v2ray-core/transport/listener"
  12. )
  13. type Shadowsocks struct {
  14. space app.Space
  15. config *Config
  16. port v2net.Port
  17. accepting bool
  18. tcpListener *listener.TCPListener
  19. }
  20. func (this *Shadowsocks) Port() v2net.Port {
  21. return this.port
  22. }
  23. func (this *Shadowsocks) Close() {
  24. this.accepting = false
  25. this.tcpListener.Close()
  26. this.tcpListener = nil
  27. }
  28. func (this *Shadowsocks) Listen(port v2net.Port) error {
  29. if this.accepting {
  30. if this.port == port {
  31. return nil
  32. } else {
  33. return proxy.ErrorAlreadyListening
  34. }
  35. }
  36. tcpListener, err := listener.ListenTCP(port, this.handleConnection)
  37. if err != nil {
  38. log.Error("Shadowsocks: Failed to listen on port ", port, ": ", err)
  39. return err
  40. }
  41. this.tcpListener = tcpListener
  42. this.accepting = true
  43. return nil
  44. }
  45. func (this *Shadowsocks) handleConnection(conn *listener.TCPConn) {
  46. defer conn.Close()
  47. buffer := alloc.NewSmallBuffer()
  48. defer buffer.Release()
  49. _, err := v2net.ReadAllBytes(conn, buffer.Value[:this.config.Cipher.IVSize()])
  50. if err != nil {
  51. log.Error("Shadowsocks: Failed to read IV: ", err)
  52. return
  53. }
  54. iv := buffer.Value[:this.config.Cipher.IVSize()]
  55. key := this.config.Key
  56. reader, err := this.config.Cipher.NewDecodingStream(key, iv, conn)
  57. if err != nil {
  58. log.Error("Shadowsocks: Failed to create decoding stream: ", err)
  59. return
  60. }
  61. request, err := ReadRequest(reader)
  62. if err != nil {
  63. return
  64. }
  65. packet := v2net.NewPacket(v2net.TCPDestination(request.Address, request.Port), nil, true)
  66. ray := this.space.PacketDispatcher().DispatchToOutbound(packet)
  67. writer, err := this.config.Cipher.NewEncodingStream(key, iv, conn)
  68. if err != nil {
  69. log.Error("Shadowsocks: Failed to create encoding stream: ", err)
  70. return
  71. }
  72. var writeFinish sync.Mutex
  73. writeFinish.Lock()
  74. go func() {
  75. v2net.ChanToWriter(writer, ray.InboundOutput())
  76. writeFinish.Unlock()
  77. }()
  78. v2net.ReaderToChan(ray.InboundInput(), reader)
  79. close(ray.InboundInput())
  80. writeFinish.Lock()
  81. }
  82. func init() {
  83. internal.MustRegisterInboundHandlerCreator("shadowsocks",
  84. func(space app.Space, rawConfig interface{}) (proxy.InboundHandler, error) {
  85. config := rawConfig.(*Config)
  86. return &Shadowsocks{
  87. space: space,
  88. config: config,
  89. }, nil
  90. })
  91. }