- Lab app: proxy panel, node editor, configuration and detail pages, responsive layout, and related tests - Add meshport-socks crate and sing-box bridge scripts/docs - Add singbox_flutter binding and meshport_client_core package - FFI, VPN helper (unified mac path), and Android/macOS runner updates - Docs for architecture, client runtime, UI design, and validation - UI design output artifacts under outputs/ui Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
123 lines
4.1 KiB
Go
123 lines
4.1 KiB
Go
package bridge
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net"
|
|
"net/netip"
|
|
"time"
|
|
|
|
"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"
|
|
boxsocks "github.com/sagernet/sing-box/protocol/socks"
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
N "github.com/sagernet/sing/common/network"
|
|
"github.com/sagernet/sing/common/varbin"
|
|
"github.com/sagernet/sing/protocol/socks"
|
|
"github.com/sagernet/sing/protocol/socks/socks5"
|
|
)
|
|
|
|
type meshOutbound struct {
|
|
adapter.Outbound
|
|
dialer N.Dialer
|
|
server M.Socksaddr
|
|
logger log.ContextLogger
|
|
}
|
|
|
|
func registerMeshOutbound(registry *outbound.Registry) {
|
|
outbound.Register[option.SOCKSOutboundOptions](registry, C.TypeSOCKS,
|
|
func(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.SOCKSOutboundOptions) (adapter.Outbound, error) {
|
|
delegate, err := boxsocks.NewOutbound(ctx, router, logger, tag, options)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if tag != "meshport" {
|
|
return delegate, nil
|
|
}
|
|
d, err := dialer.New(ctx, options.DialerOptions, false)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &meshOutbound{Outbound: delegate, dialer: d, server: options.ServerOptions.Build(), logger: logger}, nil
|
|
})
|
|
}
|
|
|
|
func (o *meshOutbound) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
|
|
if N.NetworkName(network) == N.NetworkUDP {
|
|
return o.associate(ctx, destination)
|
|
}
|
|
return o.Outbound.DialContext(ctx, network, destination)
|
|
}
|
|
|
|
func (o *meshOutbound) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
|
|
return o.associate(ctx, destination)
|
|
}
|
|
|
|
// RFC 1928 UDP ASSOCIATE names the client's expected UDP endpoint, not the
|
|
// remote destination. That endpoint is not yet allocated, so request 0.0.0.0:0.
|
|
// Keep this normalization in our adapter instead of weakening the Rust bridge's
|
|
// source IP/port isolation to accept another client's private-address heuristic.
|
|
func (o *meshOutbound) associate(ctx context.Context, destination M.Socksaddr) (packet *socks.AssociatePacketConn, err error) {
|
|
control, err := o.dialer.DialContext(ctx, N.NetworkTCP, o.server)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer func() {
|
|
if err != nil {
|
|
_ = control.Close()
|
|
}
|
|
}()
|
|
stopCancel := context.AfterFunc(ctx, func() { _ = control.Close() })
|
|
defer func() {
|
|
if !stopCancel() {
|
|
if packet != nil {
|
|
_ = packet.Close()
|
|
packet = nil
|
|
}
|
|
err = ctx.Err()
|
|
}
|
|
}()
|
|
deadline := time.Now().Add(5 * time.Second)
|
|
if d, ok := ctx.Deadline(); ok && d.Before(deadline) {
|
|
deadline = d
|
|
}
|
|
if err = control.SetDeadline(deadline); err != nil {
|
|
return nil, err
|
|
}
|
|
reader := varbin.StubReader(control)
|
|
if err = socks5.WriteAuthRequest(control, socks5.AuthRequest{Methods: []byte{socks5.AuthTypeNotRequired}}); err != nil {
|
|
return nil, err
|
|
}
|
|
auth, err := socks5.ReadAuthResponse(reader)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if auth.Method != socks5.AuthTypeNotRequired {
|
|
return nil, errors.New("mesh bridge rejected authentication method")
|
|
}
|
|
if err = socks5.WriteRequest(control, socks5.Request{Command: socks5.CommandUDPAssociate, Destination: M.SocksaddrFrom(netip.IPv4Unspecified(), 0)}); err != nil {
|
|
return nil, err
|
|
}
|
|
o.logger.DebugContext(ctx, "mesh UDP association requested")
|
|
response, err := socks5.ReadResponse(reader)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if response.ReplyCode != socks5.ReplyCodeSuccess || !response.Bind.Addr.Is4() || response.Bind.Addr != o.server.Addr || response.Bind.Port == 0 {
|
|
return nil, errors.New("mesh bridge rejected UDP association or returned an invalid endpoint")
|
|
}
|
|
if err = control.SetDeadline(time.Time{}); err != nil {
|
|
return nil, err
|
|
}
|
|
udp, err := o.dialer.DialContext(ctx, N.NetworkUDP, response.Bind)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
o.logger.DebugContext(ctx, "mesh UDP association ready: ", udp.LocalAddr(), " -> ", response.Bind)
|
|
return socks.NewAssociatePacketConn(udp, destination, control), nil
|
|
}
|