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. 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. ctx = proxy.ContextWithSource(ctx, v2net.DestinationFromAddr(conn.RemoteAddr()))
  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. w.hub.Close()
  68. w.cancel()
  69. }
  70. func (w *tcpWorker) Port() v2net.Port {
  71. return w.port
  72. }
  73. type udpConn struct {
  74. lastActivityTime int64 // in seconds
  75. input chan *buf.Buffer
  76. output func([]byte) (int, error)
  77. remote net.Addr
  78. local net.Addr
  79. cancel context.CancelFunc
  80. }
  81. func (c *udpConn) updateActivity() {
  82. atomic.StoreInt64(&c.lastActivityTime, time.Now().Unix())
  83. }
  84. func (c *udpConn) Read(buf []byte) (int, error) {
  85. in, open := <-c.input
  86. if !open {
  87. return 0, io.EOF
  88. }
  89. defer in.Release()
  90. c.updateActivity()
  91. return copy(buf, in.Bytes()), 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 *buf.Buffer, 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. select {
  161. case conn.input <- b:
  162. default:
  163. b.Release()
  164. }
  165. if !existing {
  166. go func() {
  167. ctx := w.ctx
  168. ctx, cancel := context.WithCancel(ctx)
  169. conn.cancel = cancel
  170. if originalDest.IsValid() {
  171. ctx = proxy.ContextWithOriginalDestination(ctx, originalDest)
  172. }
  173. if len(w.tag) > 0 {
  174. ctx = proxy.ContextWithInboundTag(ctx, w.tag)
  175. }
  176. ctx = proxy.ContextWithSource(ctx, source)
  177. ctx = proxy.ContextWithInboundDestination(ctx, v2net.UDPDestination(w.address, w.port))
  178. w.proxy.Process(ctx, v2net.Network_UDP, conn)
  179. w.removeConn(source)
  180. cancel()
  181. }()
  182. }
  183. }
  184. func (w *udpWorker) removeConn(src v2net.Destination) {
  185. w.Lock()
  186. delete(w.activeConn, src)
  187. w.Unlock()
  188. }
  189. func (w *udpWorker) Start() error {
  190. w.activeConn = make(map[v2net.Destination]*udpConn)
  191. ctx, cancel := context.WithCancel(context.Background())
  192. w.ctx = ctx
  193. w.cancel = cancel
  194. h, err := udp.ListenUDP(w.address, w.port, udp.ListenOption{
  195. Callback: w.callback,
  196. ReceiveOriginalDest: w.recvOrigDest,
  197. })
  198. if err != nil {
  199. return err
  200. }
  201. go w.monitor()
  202. w.hub = h
  203. return nil
  204. }
  205. func (w *udpWorker) Close() {
  206. w.hub.Close()
  207. w.cancel()
  208. }
  209. func (w *udpWorker) monitor() {
  210. for {
  211. select {
  212. case <-w.ctx.Done():
  213. return
  214. case <-time.After(time.Second * 16):
  215. nowSec := time.Now().Unix()
  216. w.Lock()
  217. for addr, conn := range w.activeConn {
  218. if nowSec-conn.lastActivityTime > 8 {
  219. delete(w.activeConn, addr)
  220. conn.cancel()
  221. }
  222. }
  223. w.Unlock()
  224. }
  225. }
  226. }
  227. func (w *udpWorker) Port() v2net.Port {
  228. return w.port
  229. }
  230. func (w *udpWorker) Proxy() proxy.Inbound {
  231. return w.proxy
  232. }