mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-06-26 20:29:03 +03:00
Add Snell protocol. Refactor MASQUE HTTP/2, Fair Queue. Update XHTTP, OpenVPN, Sudoku, Fallback. Fixes
This commit is contained in:
130
protocol/snell/inbound.go
Normal file
130
protocol/snell/inbound.go
Normal file
@@ -0,0 +1,130 @@
|
||||
package snell
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
|
||||
"github.com/sagernet/sing-box/adapter"
|
||||
"github.com/sagernet/sing-box/adapter/inbound"
|
||||
"github.com/sagernet/sing-box/common/listener"
|
||||
C "github.com/sagernet/sing-box/constant"
|
||||
"github.com/sagernet/sing-box/log"
|
||||
"github.com/sagernet/sing-box/option"
|
||||
"github.com/sagernet/sing-box/transport/snell"
|
||||
"github.com/sagernet/sing/common"
|
||||
"github.com/sagernet/sing/common/bufio"
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
"github.com/sagernet/sing/common/logger"
|
||||
M "github.com/sagernet/sing/common/metadata"
|
||||
N "github.com/sagernet/sing/common/network"
|
||||
)
|
||||
|
||||
func RegisterInbound(registry *inbound.Registry) {
|
||||
inbound.Register[option.SnellInboundOptions](registry, C.TypeSnell, NewInbound)
|
||||
}
|
||||
|
||||
type Inbound struct {
|
||||
inbound.Adapter
|
||||
router adapter.ConnectionRouterEx
|
||||
logger logger.ContextLogger
|
||||
listener *listener.Listener
|
||||
service *snell.Service
|
||||
}
|
||||
|
||||
func NewInbound(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.SnellInboundOptions) (adapter.Inbound, error) {
|
||||
if options.PSK == "" {
|
||||
return nil, E.New("snell requires psk")
|
||||
}
|
||||
udpEnabled := common.Contains(options.Network.Build(), N.NetworkUDP)
|
||||
obfsMode := ""
|
||||
if options.Obfs != nil {
|
||||
obfsMode = options.Obfs.Mode
|
||||
}
|
||||
in := &Inbound{
|
||||
Adapter: inbound.NewAdapter(C.TypeSnell, tag),
|
||||
router: router,
|
||||
logger: logger,
|
||||
}
|
||||
service, err := snell.NewService(snell.ServiceOptions{
|
||||
PSK: []byte(options.PSK),
|
||||
Version: options.Version,
|
||||
ObfsMode: obfsMode,
|
||||
UDP: udpEnabled,
|
||||
Logger: logger,
|
||||
Handler: (*inboundHandler)(in),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
in.service = service
|
||||
in.listener = listener.New(listener.Options{
|
||||
Context: ctx,
|
||||
Logger: logger,
|
||||
Network: []string{N.NetworkTCP},
|
||||
Listen: options.ListenOptions,
|
||||
ConnectionHandler: in,
|
||||
})
|
||||
return in, nil
|
||||
}
|
||||
|
||||
func (h *Inbound) Start(stage adapter.StartStage) error {
|
||||
if stage != adapter.StartStateStart {
|
||||
return nil
|
||||
}
|
||||
return h.listener.Start()
|
||||
}
|
||||
|
||||
func (h *Inbound) Close() error {
|
||||
return h.listener.Close()
|
||||
}
|
||||
|
||||
func (h *Inbound) NewConnectionEx(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
|
||||
err := h.service.NewConnection(ctx, conn, metadata.Source)
|
||||
N.CloseOnHandshakeFailure(conn, onClose, err)
|
||||
if err != nil {
|
||||
h.logger.ErrorContext(ctx, E.Cause(err, "process connection from ", metadata.Source))
|
||||
}
|
||||
}
|
||||
|
||||
var _ adapter.TCPInjectableInbound = (*Inbound)(nil)
|
||||
|
||||
type inboundHandler Inbound
|
||||
|
||||
func (h *inboundHandler) NewConnection(ctx context.Context, conn net.Conn, source M.Socksaddr, destination M.Socksaddr, clientID string) {
|
||||
var metadata adapter.InboundContext
|
||||
metadata.Inbound = h.Tag()
|
||||
metadata.InboundType = h.Type()
|
||||
metadata.InboundDetour = h.listener.ListenOptions().Detour
|
||||
metadata.Source = source
|
||||
metadata.Destination = destination
|
||||
if clientID != "" {
|
||||
metadata.User = clientID
|
||||
h.logger.InfoContext(ctx, "[", clientID, "] inbound connection to ", destination)
|
||||
} else {
|
||||
h.logger.InfoContext(ctx, "inbound connection to ", destination)
|
||||
}
|
||||
done := make(chan struct{})
|
||||
h.router.RouteConnectionEx(ctx, conn, metadata, N.OnceClose(func(error) {
|
||||
close(done)
|
||||
}))
|
||||
<-done
|
||||
}
|
||||
|
||||
func (h *inboundHandler) NewPacketConnection(ctx context.Context, conn net.PacketConn, source M.Socksaddr, clientID string) {
|
||||
var metadata adapter.InboundContext
|
||||
metadata.Inbound = h.Tag()
|
||||
metadata.InboundType = h.Type()
|
||||
metadata.InboundDetour = h.listener.ListenOptions().Detour
|
||||
metadata.Source = source
|
||||
if clientID != "" {
|
||||
metadata.User = clientID
|
||||
h.logger.InfoContext(ctx, "[", clientID, "] inbound packet connection")
|
||||
} else {
|
||||
h.logger.InfoContext(ctx, "inbound packet connection")
|
||||
}
|
||||
done := make(chan struct{})
|
||||
h.router.RoutePacketConnectionEx(ctx, bufio.NewPacketConn(conn), metadata, N.OnceClose(func(error) {
|
||||
close(done)
|
||||
}))
|
||||
<-done
|
||||
}
|
||||
114
protocol/snell/outbound.go
Normal file
114
protocol/snell/outbound.go
Normal file
@@ -0,0 +1,114 @@
|
||||
package snell
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"github.com/sagernet/sing-box/adapter"
|
||||
"github.com/sagernet/sing-box/adapter/outbound"
|
||||
"github.com/sagernet/sing-box/common/dialer"
|
||||
C "github.com/sagernet/sing-box/constant"
|
||||
"github.com/sagernet/sing-box/log"
|
||||
"github.com/sagernet/sing-box/option"
|
||||
"github.com/sagernet/sing-box/transport/snell"
|
||||
"github.com/sagernet/sing/common"
|
||||
"github.com/sagernet/sing/common/bufio"
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
"github.com/sagernet/sing/common/logger"
|
||||
M "github.com/sagernet/sing/common/metadata"
|
||||
N "github.com/sagernet/sing/common/network"
|
||||
)
|
||||
|
||||
func RegisterOutbound(registry *outbound.Registry) {
|
||||
outbound.Register[option.SnellOutboundOptions](registry, C.TypeSnell, NewOutbound)
|
||||
}
|
||||
|
||||
type Outbound struct {
|
||||
outbound.Adapter
|
||||
logger logger.ContextLogger
|
||||
client *snell.Client
|
||||
}
|
||||
|
||||
func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.SnellOutboundOptions) (adapter.Outbound, error) {
|
||||
if options.PSK == "" {
|
||||
return nil, E.New("snell requires psk")
|
||||
}
|
||||
version := options.Version
|
||||
if version == 0 {
|
||||
version = snell.DefaultSnellVersion
|
||||
}
|
||||
if version == snell.Version5 {
|
||||
version = snell.Version4
|
||||
}
|
||||
udpEnabled := common.Contains(options.Network.Build(), N.NetworkUDP)
|
||||
switch version {
|
||||
case snell.Version1, snell.Version2:
|
||||
if udpEnabled {
|
||||
return nil, fmt.Errorf("snell version %d does not support UDP", version)
|
||||
}
|
||||
case snell.Version3, snell.Version4:
|
||||
default:
|
||||
return nil, fmt.Errorf("snell version error: %d", version)
|
||||
}
|
||||
reuse := version == snell.Version2 || (version == snell.Version4 && options.Reuse)
|
||||
obfsMode := ""
|
||||
obfsHost := "bing.com"
|
||||
if options.Obfs != nil {
|
||||
switch options.Obfs.Mode {
|
||||
case "", "tls", "http":
|
||||
obfsMode = options.Obfs.Mode
|
||||
default:
|
||||
return nil, fmt.Errorf("snell obfs mode error: %s", options.Obfs.Mode)
|
||||
}
|
||||
if options.Obfs.Host != "" {
|
||||
obfsHost = options.Obfs.Host
|
||||
}
|
||||
}
|
||||
outboundDialer, err := dialer.New(ctx, options.DialerOptions, options.ServerIsDomain())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
client := snell.NewClient(snell.ClientOptions{
|
||||
Dialer: outboundDialer,
|
||||
Server: options.ServerOptions.Build(),
|
||||
PSK: []byte(options.PSK),
|
||||
Version: version,
|
||||
Reuse: reuse,
|
||||
ObfsMode: obfsMode,
|
||||
ObfsHost: obfsHost,
|
||||
})
|
||||
return &Outbound{
|
||||
Adapter: outbound.NewAdapterWithDialerOptions(C.TypeSnell, tag, options.Network.Build(), options.DialerOptions),
|
||||
logger: logger,
|
||||
client: client,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (h *Outbound) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
|
||||
ctx, metadata := adapter.ExtendContext(ctx)
|
||||
metadata.Outbound = h.Tag()
|
||||
metadata.Destination = destination
|
||||
switch N.NetworkName(network) {
|
||||
case N.NetworkTCP:
|
||||
h.logger.InfoContext(ctx, "outbound connection to ", destination)
|
||||
return h.client.DialContext(ctx, destination)
|
||||
case N.NetworkUDP:
|
||||
h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
|
||||
conn, err := h.client.ListenPacket(ctx, destination)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return bufio.NewBindPacketConn(conn, destination), nil
|
||||
default:
|
||||
return nil, E.Extend(N.ErrUnknownNetwork, network)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Outbound) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
|
||||
ctx, metadata := adapter.ExtendContext(ctx)
|
||||
metadata.Outbound = h.Tag()
|
||||
metadata.Destination = destination
|
||||
h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
|
||||
return h.client.ListenPacket(ctx, destination)
|
||||
}
|
||||
Reference in New Issue
Block a user