worker.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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.Inbound
  22. }
  23. type tcpWorker struct {
  24. address v2net.Address
  25. port v2net.Port
  26. proxy proxy.Inbound
  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.Inbound {
  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. lastActivityTime int64 // in seconds
  76. input chan []byte
  77. output func([]byte) (int, error)
  78. remote net.Addr
  79. local net.Addr
  80. cancel context.CancelFunc
  81. }
  82. func (c *udpConn) updateActivity() {
  83. atomic.StoreInt64(&c.lastActivityTime, time.Now().Unix())
  84. }
  85. func (c *udpConn) Read(buf []byte) (int, error) {
  86. in, open := <-c.input
  87. if !open {
  88. return 0, io.EOF
  89. }
  90. c.updateActivity()
  91. return copy(buf, in), nil
  92. }
  93. func (c *udpConn) Write(buf []byte) (int, error) {
  94. n, err := c.output(buf)
  95. if err == nil {
  96. c.updateActivity()
  97. }
  98. return n, err
  99. }
  100. func (c *udpConn) Close() error {
  101. return nil
  102. }
  103. func (c *udpConn) RemoteAddr() net.Addr {
  104. return c.remote
  105. }
  106. func (c *udpConn) LocalAddr() net.Addr {
  107. return c.remote
  108. }
  109. func (*udpConn) SetDeadline(time.Time) error {
  110. return nil
  111. }
  112. func (*udpConn) SetReadDeadline(time.Time) error {
  113. return nil
  114. }
  115. func (*udpConn) SetWriteDeadline(time.Time) error {
  116. return nil
  117. }
  118. func (*udpConn) Reusable() bool {
  119. return false
  120. }
  121. func (*udpConn) SetReusable(bool) {}
  122. type udpWorker struct {
  123. sync.RWMutex
  124. proxy proxy.Inbound
  125. hub *udp.Hub
  126. address v2net.Address
  127. port v2net.Port
  128. recvOrigDest bool
  129. tag string
  130. ctx context.Context
  131. cancel context.CancelFunc
  132. activeConn map[v2net.Destination]*udpConn
  133. }
  134. func (w *udpWorker) getConnection(src v2net.Destination) (*udpConn, bool) {
  135. w.Lock()
  136. defer w.Unlock()
  137. if conn, found := w.activeConn[src]; found {
  138. return conn, true
  139. }
  140. conn := &udpConn{
  141. input: make(chan []byte, 32),
  142. output: func(b []byte) (int, error) {
  143. return w.hub.WriteTo(b, src)
  144. },
  145. remote: &net.UDPAddr{
  146. IP: src.Address.IP(),
  147. Port: int(src.Port),
  148. },
  149. local: &net.UDPAddr{
  150. IP: w.address.IP(),
  151. Port: int(w.port),
  152. },
  153. }
  154. w.activeConn[src] = conn
  155. conn.updateActivity()
  156. return conn, false
  157. }
  158. func (w *udpWorker) callback(b *buf.Buffer, source v2net.Destination, originalDest v2net.Destination) {
  159. conn, existing := w.getConnection(source)
  160. conn.input <- b.Bytes()
  161. if !existing {
  162. go func() {
  163. ctx := w.ctx
  164. ctx, cancel := context.WithCancel(ctx)
  165. conn.cancel = cancel
  166. if originalDest.IsValid() {
  167. ctx = proxy.ContextWithOriginalDestination(ctx, originalDest)
  168. }
  169. if len(w.tag) > 0 {
  170. ctx = proxy.ContextWithInboundTag(ctx, w.tag)
  171. }
  172. ctx = proxy.ContextWithSource(ctx, source)
  173. ctx = proxy.ContextWithInboundDestination(ctx, v2net.UDPDestination(w.address, w.port))
  174. w.proxy.Process(ctx, v2net.Network_UDP, conn)
  175. w.removeConn(source)
  176. cancel()
  177. }()
  178. }
  179. }
  180. func (w *udpWorker) removeConn(src v2net.Destination) {
  181. w.Lock()
  182. delete(w.activeConn, src)
  183. w.Unlock()
  184. }
  185. func (w *udpWorker) Start() error {
  186. w.activeConn = make(map[v2net.Destination]*udpConn)
  187. ctx, cancel := context.WithCancel(context.Background())
  188. w.ctx = ctx
  189. w.cancel = cancel
  190. h, err := udp.ListenUDP(w.address, w.port, udp.ListenOption{
  191. Callback: w.callback,
  192. ReceiveOriginalDest: w.recvOrigDest,
  193. })
  194. if err != nil {
  195. return err
  196. }
  197. go w.monitor()
  198. w.hub = h
  199. return nil
  200. }
  201. func (w *udpWorker) Close() {
  202. w.hub.Close()
  203. w.cancel()
  204. }
  205. func (w *udpWorker) monitor() {
  206. for {
  207. select {
  208. case <-w.ctx.Done():
  209. return
  210. case <-time.After(time.Second * 16):
  211. nowSec := time.Now().Unix()
  212. w.Lock()
  213. for addr, conn := range w.activeConn {
  214. if nowSec-conn.lastActivityTime > 8 {
  215. delete(w.activeConn, addr)
  216. conn.cancel()
  217. }
  218. }
  219. w.Unlock()
  220. }
  221. }
  222. }
  223. func (w *udpWorker) Port() v2net.Port {
  224. return w.port
  225. }
  226. func (w *udpWorker) Proxy() proxy.Inbound {
  227. return w.proxy
  228. }