tcp_hub.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package internet
  2. import (
  3. "context"
  4. "github.com/v2fly/v2ray-core/v4/common/net"
  5. )
  6. var (
  7. transportListenerCache = make(map[string]ListenFunc)
  8. )
  9. func RegisterTransportListener(protocol string, listener ListenFunc) error {
  10. if _, found := transportListenerCache[protocol]; found {
  11. return newError(protocol, " listener already registered.").AtError()
  12. }
  13. transportListenerCache[protocol] = listener
  14. return nil
  15. }
  16. type ConnHandler func(Connection)
  17. type ListenFunc func(ctx context.Context, address net.Address, port net.Port, settings *MemoryStreamConfig, handler ConnHandler) (Listener, error)
  18. type Listener interface {
  19. Close() error
  20. Addr() net.Addr
  21. }
  22. // ListenUnix is the UDS version of ListenTCP
  23. func ListenUnix(ctx context.Context, address net.Address, settings *MemoryStreamConfig, handler ConnHandler) (Listener, error) {
  24. if settings == nil {
  25. s, err := ToMemoryStreamConfig(nil)
  26. if err != nil {
  27. return nil, newError("failed to create default unix stream settings").Base(err)
  28. }
  29. settings = s
  30. }
  31. protocol := settings.ProtocolName
  32. listenFunc := transportListenerCache[protocol]
  33. if listenFunc == nil {
  34. return nil, newError(protocol, " unix istener not registered.").AtError()
  35. }
  36. listener, err := listenFunc(ctx, address, net.Port(0), settings, handler)
  37. if err != nil {
  38. return nil, newError("failed to listen on unix address: ", address).Base(err)
  39. }
  40. return listener, nil
  41. }
  42. func ListenTCP(ctx context.Context, address net.Address, port net.Port, settings *MemoryStreamConfig, handler ConnHandler) (Listener, error) {
  43. if settings == nil {
  44. s, err := ToMemoryStreamConfig(nil)
  45. if err != nil {
  46. return nil, newError("failed to create default stream settings").Base(err)
  47. }
  48. settings = s
  49. }
  50. if address.Family().IsDomain() && address.Domain() == "localhost" {
  51. address = net.LocalHostIP
  52. }
  53. if address.Family().IsDomain() {
  54. return nil, newError("domain address is not allowed for listening: ", address.Domain())
  55. }
  56. protocol := settings.ProtocolName
  57. listenFunc := transportListenerCache[protocol]
  58. if listenFunc == nil {
  59. return nil, newError(protocol, " listener not registered.").AtError()
  60. }
  61. listener, err := listenFunc(ctx, address, port, settings, handler)
  62. if err != nil {
  63. return nil, newError("failed to listen on address: ", address, ":", port).Base(err)
  64. }
  65. return listener, nil
  66. }
  67. // ListenSystem listens on a local address for incoming TCP connections.
  68. //
  69. // v2ray:api:beta
  70. func ListenSystem(ctx context.Context, addr net.Addr, sockopt *SocketConfig) (net.Listener, error) {
  71. return effectiveListener.Listen(ctx, addr, sockopt)
  72. }
  73. // ListenSystemPacket listens on a local address for incoming UDP connections.
  74. //
  75. // v2ray:api:beta
  76. func ListenSystemPacket(ctx context.Context, addr net.Addr, sockopt *SocketConfig) (net.PacketConn, error) {
  77. return effectiveListener.ListenPacket(ctx, addr, sockopt)
  78. }