tcp_hub.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. listenFunc := transportListenerCache[protocol]
  56. if listenFunc == nil {
  57. return nil, newError(protocol, " listener not registered.").AtError()
  58. }
  59. listener, err := listenFunc(ctx, address, port, settings, handler)
  60. if err != nil {
  61. return nil, newError("failed to listen on address: ", address, ":", port).Base(err)
  62. }
  63. return listener, nil
  64. }
  65. // ListenSystem listens on a local address for incoming TCP connections.
  66. //
  67. // v2ray:api:beta
  68. func ListenSystem(ctx context.Context, addr net.Addr, sockopt *SocketConfig) (net.Listener, error) {
  69. return effectiveListener.Listen(ctx, addr, sockopt)
  70. }
  71. // ListenSystemPacket listens on a local address for incoming UDP connections.
  72. //
  73. // v2ray:api:beta
  74. func ListenSystemPacket(ctx context.Context, addr net.Addr, sockopt *SocketConfig) (net.PacketConn, error) {
  75. return effectiveListener.ListenPacket(ctx, addr, sockopt)
  76. }