github.com/sagernet/sing-box@v1.9.0-rc.20/transport/v2ray/transport.go (about) 1 package v2ray 2 3 import ( 4 "context" 5 6 "github.com/sagernet/sing-box/adapter" 7 "github.com/sagernet/sing-box/common/tls" 8 C "github.com/sagernet/sing-box/constant" 9 "github.com/sagernet/sing-box/option" 10 "github.com/sagernet/sing-box/transport/v2rayhttp" 11 "github.com/sagernet/sing-box/transport/v2rayhttpupgrade" 12 "github.com/sagernet/sing-box/transport/v2raywebsocket" 13 E "github.com/sagernet/sing/common/exceptions" 14 M "github.com/sagernet/sing/common/metadata" 15 N "github.com/sagernet/sing/common/network" 16 ) 17 18 type ( 19 ServerConstructor[O any] func(ctx context.Context, options O, tlsConfig tls.ServerConfig, handler adapter.V2RayServerTransportHandler) (adapter.V2RayServerTransport, error) 20 ClientConstructor[O any] func(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, options O, tlsConfig tls.Config) (adapter.V2RayClientTransport, error) 21 ) 22 23 func NewServerTransport(ctx context.Context, options option.V2RayTransportOptions, tlsConfig tls.ServerConfig, handler adapter.V2RayServerTransportHandler) (adapter.V2RayServerTransport, error) { 24 if options.Type == "" { 25 return nil, nil 26 } 27 switch options.Type { 28 case C.V2RayTransportTypeHTTP: 29 return v2rayhttp.NewServer(ctx, options.HTTPOptions, tlsConfig, handler) 30 case C.V2RayTransportTypeWebsocket: 31 return v2raywebsocket.NewServer(ctx, options.WebsocketOptions, tlsConfig, handler) 32 case C.V2RayTransportTypeQUIC: 33 if tlsConfig == nil { 34 return nil, C.ErrTLSRequired 35 } 36 return NewQUICServer(ctx, options.QUICOptions, tlsConfig, handler) 37 case C.V2RayTransportTypeGRPC: 38 return NewGRPCServer(ctx, options.GRPCOptions, tlsConfig, handler) 39 case C.V2RayTransportTypeHTTPUpgrade: 40 return v2rayhttpupgrade.NewServer(ctx, options.HTTPUpgradeOptions, tlsConfig, handler) 41 default: 42 return nil, E.New("unknown transport type: " + options.Type) 43 } 44 } 45 46 func NewClientTransport(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, options option.V2RayTransportOptions, tlsConfig tls.Config) (adapter.V2RayClientTransport, error) { 47 if options.Type == "" { 48 return nil, nil 49 } 50 switch options.Type { 51 case C.V2RayTransportTypeHTTP: 52 return v2rayhttp.NewClient(ctx, dialer, serverAddr, options.HTTPOptions, tlsConfig) 53 case C.V2RayTransportTypeGRPC: 54 return NewGRPCClient(ctx, dialer, serverAddr, options.GRPCOptions, tlsConfig) 55 case C.V2RayTransportTypeWebsocket: 56 return v2raywebsocket.NewClient(ctx, dialer, serverAddr, options.WebsocketOptions, tlsConfig) 57 case C.V2RayTransportTypeQUIC: 58 if tlsConfig == nil { 59 return nil, C.ErrTLSRequired 60 } 61 return NewQUICClient(ctx, dialer, serverAddr, options.QUICOptions, tlsConfig) 62 case C.V2RayTransportTypeHTTPUpgrade: 63 return v2rayhttpupgrade.NewClient(ctx, dialer, serverAddr, options.HTTPUpgradeOptions, tlsConfig) 64 default: 65 return nil, E.New("unknown transport type: " + options.Type) 66 } 67 }