- 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>
108 lines
3.0 KiB
Go
108 lines
3.0 KiB
Go
package bridge
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"sync"
|
|
|
|
"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/protocol/socks"
|
|
N "github.com/sagernet/sing/common/network"
|
|
)
|
|
|
|
// Track accepted controls before SOCKS negotiation, including connections that
|
|
// have not entered sing-box's routed connection manager yet.
|
|
type connectionSet struct {
|
|
mu sync.Mutex
|
|
closed bool
|
|
connections map[*trackedConnection]struct{}
|
|
}
|
|
|
|
type trackedConnection struct {
|
|
net.Conn
|
|
owner *connectionSet
|
|
}
|
|
|
|
func (c *trackedConnection) Close() error {
|
|
err := c.Conn.Close()
|
|
c.owner.mu.Lock()
|
|
delete(c.owner.connections, c)
|
|
c.owner.mu.Unlock()
|
|
return err
|
|
}
|
|
|
|
func (s *connectionSet) track(conn net.Conn) *trackedConnection {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
if s.closed || len(s.connections) >= 128 {
|
|
return nil
|
|
}
|
|
c := &trackedConnection{Conn: conn, owner: s}
|
|
s.connections[c] = struct{}{}
|
|
return c
|
|
}
|
|
|
|
func (s *connectionSet) closeAll() {
|
|
s.mu.Lock()
|
|
s.closed = true
|
|
connections := make([]*trackedConnection, 0, len(s.connections))
|
|
for conn := range s.connections {
|
|
connections = append(connections, conn)
|
|
}
|
|
s.mu.Unlock()
|
|
for _, conn := range connections {
|
|
_ = conn.Close()
|
|
}
|
|
}
|
|
|
|
type trackedInbound struct {
|
|
*socks.Inbound
|
|
listener *listener.Listener
|
|
connections connectionSet
|
|
}
|
|
|
|
func registerTrackedInbound(registry *inbound.Registry) {
|
|
inbound.Register[option.SocksInboundOptions](registry, C.TypeSOCKS,
|
|
func(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.SocksInboundOptions) (adapter.Inbound, error) {
|
|
delegate, err := socks.NewInbound(ctx, router, logger, tag, options)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
tracked := &trackedInbound{Inbound: delegate.(*socks.Inbound), connections: connectionSet{connections: make(map[*trackedConnection]struct{})}}
|
|
// The delegate only handles the protocol. This listener owns the
|
|
// accepted TCP controls; its options also constrain UDP association
|
|
// allocation through the delegate's original packet-listener helper.
|
|
tracked.listener = listener.New(listener.Options{Context: ctx, Logger: logger, Network: []string{N.NetworkTCP}, Listen: options.ListenOptions, ConnectionHandler: tracked})
|
|
return tracked, nil
|
|
})
|
|
}
|
|
|
|
func (h *trackedInbound) Start(stage adapter.StartStage, scope *adapter.Scope) error {
|
|
if stage != adapter.StartStateStart {
|
|
return nil
|
|
}
|
|
if err := h.listener.Start(); err != nil {
|
|
return err
|
|
}
|
|
scope.Add(func() error {
|
|
err := h.listener.Close()
|
|
h.connections.closeAll()
|
|
return err
|
|
})
|
|
return nil
|
|
}
|
|
|
|
func (h *trackedInbound) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
|
|
tracked := h.connections.track(conn)
|
|
if tracked == nil {
|
|
_ = conn.Close()
|
|
return
|
|
}
|
|
h.Inbound.NewConnection(ctx, tracked, metadata, onClose)
|
|
}
|