tcp_hub.go 3.0 KB

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