worker.go 5.7 KB

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