tcp_hub.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package internet
  2. import (
  3. "context"
  4. "github.com/v2fly/v2ray-core/v4/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. listenFunc := transportListenerCache[protocol]
  31. if listenFunc == nil {
  32. return nil, newError(protocol, " unix istener not registered.").AtError()
  33. }
  34. listener, err := listenFunc(ctx, address, net.Port(0), settings, handler)
  35. if err != nil {
  36. return nil, newError("failed to listen on unix address: ", address).Base(err)
  37. }
  38. return listener, nil
  39. }
  40. func ListenTCP(ctx context.Context, address net.Address, port net.Port, settings *MemoryStreamConfig, handler ConnHandler) (Listener, error) {
  41. if settings == nil {
  42. s, err := ToMemoryStreamConfig(nil)
  43. if err != nil {
  44. return nil, newError("failed to create default stream settings").Base(err)
  45. }
  46. settings = s
  47. }
  48. if address.Family().IsDomain() && address.Domain() == "localhost" {
  49. address = net.LocalHostIP
  50. }
  51. if address.Family().IsDomain() {
  52. return nil, newError("domain address is not allowed for listening: ", address.Domain())
  53. }
  54. protocol := settings.ProtocolName
  55. if originalProtocolName := getOriginalMessageName(settings); originalProtocolName != "" {
  56. protocol = originalProtocolName
  57. }
  58. listenFunc := transportListenerCache[protocol]
  59. if listenFunc == nil {
  60. return nil, newError(protocol, " listener not registered.").AtError()
  61. }
  62. listener, err := listenFunc(ctx, address, port, settings, handler)
  63. if err != nil {
  64. return nil, newError("failed to listen on address: ", address, ":", port).Base(err)
  65. }
  66. return listener, nil
  67. }
  68. // ListenSystem listens on a local address for incoming TCP connections.
  69. //
  70. // v2ray:api:beta
  71. func ListenSystem(ctx context.Context, addr net.Addr, sockopt *SocketConfig) (net.Listener, error) {
  72. return effectiveListener.Listen(ctx, addr, sockopt)
  73. }
  74. // ListenSystemPacket listens on a local address for incoming UDP connections.
  75. //
  76. // v2ray:api:beta
  77. func ListenSystemPacket(ctx context.Context, addr net.Addr, sockopt *SocketConfig) (net.PacketConn, error) {
  78. return effectiveListener.ListenPacket(ctx, addr, sockopt)
  79. }