worker.go 5.2 KB

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