worker.go 5.2 KB

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