worker.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. package inbound
  2. import (
  3. "context"
  4. "io"
  5. "net"
  6. "sync"
  7. "sync/atomic"
  8. "time"
  9. "v2ray.com/core/common/buf"
  10. "v2ray.com/core/common/log"
  11. v2net "v2ray.com/core/common/net"
  12. "v2ray.com/core/proxy"
  13. "v2ray.com/core/transport/internet"
  14. "v2ray.com/core/transport/internet/tcp"
  15. "v2ray.com/core/transport/internet/udp"
  16. )
  17. type worker interface {
  18. Start() error
  19. Close()
  20. Port() v2net.Port
  21. Proxy() proxy.InboundHandler
  22. }
  23. type tcpWorker struct {
  24. address v2net.Address
  25. port v2net.Port
  26. proxy proxy.InboundHandler
  27. stream *internet.StreamConfig
  28. recvOrigDest bool
  29. tag string
  30. allowPassiveConn bool
  31. ctx context.Context
  32. cancel context.CancelFunc
  33. hub *internet.TCPHub
  34. }
  35. func (w *tcpWorker) callback(conn internet.Connection) {
  36. ctx, cancel := context.WithCancel(w.ctx)
  37. if w.recvOrigDest {
  38. dest := tcp.GetOriginalDestination(conn)
  39. if dest.IsValid() {
  40. ctx = proxy.ContextWithOriginalDestination(ctx, dest)
  41. }
  42. }
  43. if len(w.tag) > 0 {
  44. ctx = proxy.ContextWithInboundTag(ctx, w.tag)
  45. }
  46. ctx = proxy.ContextWithAllowPassiveConnection(ctx, w.allowPassiveConn)
  47. ctx = proxy.ContextWithInboundDestination(ctx, v2net.TCPDestination(w.address, w.port))
  48. w.proxy.Process(ctx, v2net.Network_TCP, conn)
  49. cancel()
  50. conn.Close()
  51. }
  52. func (w *tcpWorker) Proxy() proxy.InboundHandler {
  53. return w.proxy
  54. }
  55. func (w *tcpWorker) Start() error {
  56. ctx, cancel := context.WithCancel(context.Background())
  57. w.ctx = ctx
  58. w.cancel = cancel
  59. hub, err := internet.ListenTCP(w.address, w.port, w.callback, w.stream)
  60. if err != nil {
  61. return err
  62. }
  63. w.hub = hub
  64. return nil
  65. }
  66. func (w *tcpWorker) Close() {
  67. log.Debug("Proxyman|TCPWorker: Closed. ", w.port)
  68. w.hub.Close()
  69. w.cancel()
  70. }
  71. func (w *tcpWorker) Port() v2net.Port {
  72. return w.port
  73. }
  74. type udpConn struct {
  75. cancel context.CancelFunc
  76. lastActivityTime int64 // in seconds
  77. input chan []byte
  78. output func([]byte) (int, error)
  79. closer func() error
  80. remote net.Addr
  81. local net.Addr
  82. }
  83. func (c *udpConn) updateActivity() {
  84. atomic.StoreInt64(&c.lastActivityTime, time.Now().Unix())
  85. }
  86. func (c *udpConn) Read(buf []byte) (int, error) {
  87. in, open := <-c.input
  88. if !open {
  89. return 0, io.EOF
  90. }
  91. c.updateActivity()
  92. return copy(buf, in), nil
  93. }
  94. func (c *udpConn) Write(buf []byte) (int, error) {
  95. n, err := c.output(buf)
  96. if err == nil {
  97. c.updateActivity()
  98. }
  99. return n, err
  100. }
  101. func (c *udpConn) Close() error {
  102. close(c.input)
  103. c.cancel()
  104. return nil
  105. }
  106. func (c *udpConn) RemoteAddr() net.Addr {
  107. return c.remote
  108. }
  109. func (c *udpConn) LocalAddr() net.Addr {
  110. return c.remote
  111. }
  112. func (*udpConn) SetDeadline(time.Time) error {
  113. return nil
  114. }
  115. func (*udpConn) SetReadDeadline(time.Time) error {
  116. return nil
  117. }
  118. func (*udpConn) SetWriteDeadline(time.Time) error {
  119. return nil
  120. }
  121. func (*udpConn) Reusable() bool {
  122. return false
  123. }
  124. func (*udpConn) SetReusable(bool) {}
  125. type udpWorker struct {
  126. sync.RWMutex
  127. proxy proxy.InboundHandler
  128. hub *udp.Hub
  129. address v2net.Address
  130. port v2net.Port
  131. recvOrigDest bool
  132. tag string
  133. ctx context.Context
  134. cancel context.CancelFunc
  135. activeConn map[v2net.Destination]*udpConn
  136. }
  137. func (w *udpWorker) getConnection(src v2net.Destination) (*udpConn, bool) {
  138. w.Lock()
  139. defer w.Unlock()
  140. if conn, found := w.activeConn[src]; found {
  141. return conn, true
  142. }
  143. conn := &udpConn{
  144. input: make(chan []byte, 32),
  145. output: func(b []byte) (int, error) {
  146. return w.hub.WriteTo(b, src)
  147. },
  148. closer: func() error {
  149. w.Lock()
  150. delete(w.activeConn, src)
  151. w.Unlock()
  152. return nil
  153. },
  154. remote: &net.UDPAddr{
  155. IP: src.Address.IP(),
  156. Port: int(src.Port),
  157. },
  158. local: &net.UDPAddr{
  159. IP: w.address.IP(),
  160. Port: int(w.port),
  161. },
  162. }
  163. conn.updateActivity()
  164. return conn, false
  165. }
  166. func (w *udpWorker) callback(b *buf.Buffer, source v2net.Destination, originalDest v2net.Destination) {
  167. conn, existing := w.getConnection(source)
  168. conn.input <- b.Bytes()
  169. if !existing {
  170. go func() {
  171. ctx := w.ctx
  172. ctx, cancel := context.WithCancel(ctx)
  173. conn.cancel = cancel
  174. if originalDest.IsValid() {
  175. ctx = proxy.ContextWithOriginalDestination(ctx, originalDest)
  176. }
  177. if len(w.tag) > 0 {
  178. ctx = proxy.ContextWithInboundTag(ctx, w.tag)
  179. }
  180. ctx = proxy.ContextWithSource(ctx, source)
  181. ctx = proxy.ContextWithInboundDestination(ctx, v2net.UDPDestination(w.address, w.port))
  182. w.proxy.Process(ctx, v2net.Network_UDP, conn)
  183. conn.cancel()
  184. }()
  185. }
  186. }
  187. func (w *udpWorker) removeConn(src v2net.Destination) {
  188. w.Lock()
  189. delete(w.activeConn, src)
  190. w.Unlock()
  191. }
  192. func (w *udpWorker) Start() error {
  193. ctx, cancel := context.WithCancel(context.Background())
  194. w.ctx = ctx
  195. w.cancel = cancel
  196. h, err := udp.ListenUDP(w.address, w.port, udp.ListenOption{
  197. Callback: w.callback,
  198. ReceiveOriginalDest: w.recvOrigDest,
  199. })
  200. if err != nil {
  201. return err
  202. }
  203. w.hub = h
  204. return nil
  205. }
  206. func (w *udpWorker) Close() {
  207. w.hub.Close()
  208. w.cancel()
  209. }
  210. func (w *udpWorker) monitor() {
  211. for {
  212. select {
  213. case <-w.ctx.Done():
  214. return
  215. case <-time.After(time.Second * 16):
  216. nowSec := time.Now().Unix()
  217. w.Lock()
  218. for addr, conn := range w.activeConn {
  219. if nowSec-conn.lastActivityTime > 8 {
  220. w.removeConn(addr)
  221. conn.Close()
  222. }
  223. }
  224. }
  225. }
  226. }
  227. func (w *udpWorker) Port() v2net.Port {
  228. return w.port
  229. }
  230. func (w *udpWorker) Proxy() proxy.InboundHandler {
  231. return w.proxy
  232. }