worker.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. 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.Inbound
  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. w.activeConn[src] = conn
  164. conn.updateActivity()
  165. return conn, false
  166. }
  167. func (w *udpWorker) callback(b *buf.Buffer, source v2net.Destination, originalDest v2net.Destination) {
  168. conn, existing := w.getConnection(source)
  169. conn.input <- b.Bytes()
  170. if !existing {
  171. go func() {
  172. ctx := w.ctx
  173. ctx, cancel := context.WithCancel(ctx)
  174. conn.cancel = cancel
  175. if originalDest.IsValid() {
  176. ctx = proxy.ContextWithOriginalDestination(ctx, originalDest)
  177. }
  178. if len(w.tag) > 0 {
  179. ctx = proxy.ContextWithInboundTag(ctx, w.tag)
  180. }
  181. ctx = proxy.ContextWithSource(ctx, source)
  182. ctx = proxy.ContextWithInboundDestination(ctx, v2net.UDPDestination(w.address, w.port))
  183. w.proxy.Process(ctx, v2net.Network_UDP, conn)
  184. conn.cancel()
  185. }()
  186. }
  187. }
  188. func (w *udpWorker) removeConn(src v2net.Destination) {
  189. w.Lock()
  190. delete(w.activeConn, src)
  191. w.Unlock()
  192. }
  193. func (w *udpWorker) Start() error {
  194. w.activeConn = make(map[v2net.Destination]*udpConn)
  195. ctx, cancel := context.WithCancel(context.Background())
  196. w.ctx = ctx
  197. w.cancel = cancel
  198. h, err := udp.ListenUDP(w.address, w.port, udp.ListenOption{
  199. Callback: w.callback,
  200. ReceiveOriginalDest: w.recvOrigDest,
  201. })
  202. if err != nil {
  203. return err
  204. }
  205. w.hub = h
  206. return nil
  207. }
  208. func (w *udpWorker) Close() {
  209. w.hub.Close()
  210. w.cancel()
  211. }
  212. func (w *udpWorker) monitor() {
  213. for {
  214. select {
  215. case <-w.ctx.Done():
  216. return
  217. case <-time.After(time.Second * 16):
  218. nowSec := time.Now().Unix()
  219. w.Lock()
  220. for addr, conn := range w.activeConn {
  221. if nowSec-conn.lastActivityTime > 8 {
  222. w.removeConn(addr)
  223. conn.Close()
  224. }
  225. }
  226. }
  227. }
  228. }
  229. func (w *udpWorker) Port() v2net.Port {
  230. return w.port
  231. }
  232. func (w *udpWorker) Proxy() proxy.Inbound {
  233. return w.proxy
  234. }