sing-box/transport/v2ray/transport.go

69 lines
2.9 KiB
Go
Raw Normal View History

2022-08-22 18:53:47 +08:00
package v2ray
import (
"context"
"github.com/sagernet/sing-box/adapter"
2022-09-09 18:45:10 +08:00
"github.com/sagernet/sing-box/common/tls"
2022-08-22 18:53:47 +08:00
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/option"
2022-08-22 22:19:25 +08:00
"github.com/sagernet/sing-box/transport/v2rayhttp"
"github.com/sagernet/sing-box/transport/v2rayhttpupgrade"
2022-08-22 20:20:56 +08:00
"github.com/sagernet/sing-box/transport/v2raywebsocket"
2022-08-22 18:53:47 +08:00
E "github.com/sagernet/sing/common/exceptions"
2024-10-21 23:38:34 +08:00
"github.com/sagernet/sing/common/logger"
2022-08-22 18:53:47 +08:00
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
)
2022-09-26 19:37:06 +08:00
type (
2024-10-21 23:38:34 +08:00
ServerConstructor[O any] func(ctx context.Context, logger logger.ContextLogger, options O, tlsConfig tls.ServerConfig, handler adapter.V2RayServerTransportHandler) (adapter.V2RayServerTransport, error)
2022-09-26 19:37:06 +08:00
ClientConstructor[O any] func(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, options O, tlsConfig tls.Config) (adapter.V2RayClientTransport, error)
)
2024-10-21 23:38:34 +08:00
func NewServerTransport(ctx context.Context, logger logger.ContextLogger, options option.V2RayTransportOptions, tlsConfig tls.ServerConfig, handler adapter.V2RayServerTransportHandler) (adapter.V2RayServerTransport, error) {
2022-08-22 18:53:47 +08:00
if options.Type == "" {
return nil, nil
}
switch options.Type {
2022-08-22 22:19:25 +08:00
case C.V2RayTransportTypeHTTP:
2024-10-21 23:38:34 +08:00
return v2rayhttp.NewServer(ctx, logger, options.HTTPOptions, tlsConfig, handler)
2022-08-22 20:20:56 +08:00
case C.V2RayTransportTypeWebsocket:
2024-10-21 23:38:34 +08:00
return v2raywebsocket.NewServer(ctx, logger, options.WebsocketOptions, tlsConfig, handler)
2022-08-22 21:20:05 +08:00
case C.V2RayTransportTypeQUIC:
2022-08-22 22:19:25 +08:00
if tlsConfig == nil {
return nil, C.ErrTLSRequired
}
2024-10-21 23:38:34 +08:00
return NewQUICServer(ctx, logger, options.QUICOptions, tlsConfig, handler)
2022-08-22 22:19:25 +08:00
case C.V2RayTransportTypeGRPC:
2024-10-21 23:38:34 +08:00
return NewGRPCServer(ctx, logger, options.GRPCOptions, tlsConfig, handler)
case C.V2RayTransportTypeHTTPUpgrade:
2024-10-21 23:38:34 +08:00
return v2rayhttpupgrade.NewServer(ctx, logger, options.HTTPUpgradeOptions, tlsConfig, handler)
2022-08-22 18:53:47 +08:00
default:
return nil, E.New("unknown transport type: " + options.Type)
}
}
2022-09-09 18:45:10 +08:00
func NewClientTransport(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, options option.V2RayTransportOptions, tlsConfig tls.Config) (adapter.V2RayClientTransport, error) {
2022-08-22 18:53:47 +08:00
if options.Type == "" {
return nil, nil
}
switch options.Type {
2022-08-22 22:19:25 +08:00
case C.V2RayTransportTypeHTTP:
2023-04-12 15:14:55 +08:00
return v2rayhttp.NewClient(ctx, dialer, serverAddr, options.HTTPOptions, tlsConfig)
2022-08-22 18:53:47 +08:00
case C.V2RayTransportTypeGRPC:
2022-08-22 20:20:56 +08:00
return NewGRPCClient(ctx, dialer, serverAddr, options.GRPCOptions, tlsConfig)
case C.V2RayTransportTypeWebsocket:
2023-11-09 16:59:44 +08:00
return v2raywebsocket.NewClient(ctx, dialer, serverAddr, options.WebsocketOptions, tlsConfig)
2022-08-22 21:20:05 +08:00
case C.V2RayTransportTypeQUIC:
2022-08-22 22:19:25 +08:00
if tlsConfig == nil {
return nil, C.ErrTLSRequired
}
2022-08-22 21:20:05 +08:00
return NewQUICClient(ctx, dialer, serverAddr, options.QUICOptions, tlsConfig)
case C.V2RayTransportTypeHTTPUpgrade:
return v2rayhttpupgrade.NewClient(ctx, dialer, serverAddr, options.HTTPUpgradeOptions, tlsConfig)
2022-08-22 18:53:47 +08:00
default:
return nil, E.New("unknown transport type: " + options.Type)
}
}