worker.go 5.8 KB

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