tcp_hub.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package internet
  2. import (
  3. "context"
  4. "github.com/xtls/xray-core/common/errors"
  5. "github.com/xtls/xray-core/common/net"
  6. "github.com/xtls/xray-core/transport/internet/stat"
  7. )
  8. var transportListenerCache = make(map[string]ListenFunc)
  9. func RegisterTransportListener(protocol string, listener ListenFunc) error {
  10. if _, found := transportListenerCache[protocol]; found {
  11. return errors.New(protocol, " listener already registered.").AtError()
  12. }
  13. transportListenerCache[protocol] = listener
  14. return nil
  15. }
  16. type ConnHandler func(stat.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, errors.New("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, errors.New(protocol, " unix listener not registered.").AtError()
  35. }
  36. listener, err := listenFunc(ctx, address, net.Port(0), settings, handler)
  37. if err != nil {
  38. return nil, errors.New("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, errors.New("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, errors.New("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, errors.New(protocol, " listener not registered.").AtError()
  60. }
  61. listener, err := listenFunc(ctx, address, port, settings, handler)
  62. if err != nil {
  63. return nil, errors.New("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. // xray: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. // xray:api:beta
  76. func ListenSystemPacket(ctx context.Context, addr net.Addr, sockopt *SocketConfig) (net.PacketConn, error) {
  77. return effectiveListener.ListenPacket(ctx, addr, sockopt)
  78. }