111 lines
3.0 KiB
Go
111 lines
3.0 KiB
Go
package sbproxy
|
|
|
|
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/common"
|
|
"github.com/sagernet/sing/common/buf"
|
|
"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.SBProxyInboundOptions](registry, C.TypeSBProxy, NewInbound)
|
|
}
|
|
|
|
type Inbound struct {
|
|
inbound.Adapter
|
|
ctx context.Context
|
|
router adapter.Router
|
|
logger logger.ContextLogger
|
|
options option.SBProxyInboundOptions
|
|
listener *listener.Listener
|
|
users []option.SBProxyUser
|
|
ssmMutex sync.RWMutex
|
|
tracker adapter.SSMTracker
|
|
}
|
|
|
|
func NewInbound(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.SBProxyInboundOptions) (adapter.Inbound, error) {
|
|
inbound := &Inbound{
|
|
Adapter: inbound.NewAdapter(C.TypeSBProxy, tag),
|
|
ctx: ctx,
|
|
router: router,
|
|
logger: logger,
|
|
options: options,
|
|
users: options.Users,
|
|
}
|
|
|
|
// SBProxy always supports both TCP (Java MC) and UDP (Bedrock MC)
|
|
inbound.listener = listener.New(listener.Options{
|
|
Context: ctx,
|
|
Logger: logger,
|
|
Network: []string{N.NetworkTCP}, // Handle Java MC (TCP) via standard listener
|
|
Listen: options.ListenOptions,
|
|
ConnectionHandler: inbound,
|
|
})
|
|
|
|
return inbound, nil
|
|
}
|
|
|
|
func (h *Inbound) Type() string { return C.TypeSBProxy }
|
|
|
|
func (h *Inbound) Start(stage adapter.StartStage) error {
|
|
if stage != adapter.StartStateStart {
|
|
return nil
|
|
}
|
|
|
|
// Start Java MC (TCP) Listener
|
|
err := h.listener.Start()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Always start Bedrock MC (UDP) Listener on the same port (UDP)
|
|
go h.startBedrockListener()
|
|
|
|
return nil
|
|
}
|
|
|
|
func (h *Inbound) Close() error {
|
|
return common.Close(h.listener)
|
|
// RakNet listener in bedrock_server.go should be closed too if we keep a reference
|
|
}
|
|
|
|
func (h *Inbound) SetTracker(tracker adapter.SSMTracker) {
|
|
h.ssmMutex.Lock()
|
|
defer h.ssmMutex.Unlock()
|
|
h.tracker = tracker
|
|
}
|
|
|
|
func (h *Inbound) UpdateUsers(users, keys, flows []string) error {
|
|
h.ssmMutex.Lock()
|
|
defer h.ssmMutex.Unlock()
|
|
newUsers := make([]option.SBProxyUser, len(users))
|
|
for i := range users {
|
|
newUsers[i] = option.SBProxyUser{Name: users[i], Password: keys[i]}
|
|
}
|
|
h.users = newUsers
|
|
return nil
|
|
}
|
|
|
|
func (h *Inbound) NewConnectionEx(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
|
|
// TCP traffic always handled as Java MC
|
|
h.handleJavaConnection(ctx, conn, metadata, onClose)
|
|
}
|
|
|
|
func (h *Inbound) NewPacketConnectionEx(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
|
|
// UDP traffic from standard listener (if any)
|
|
}
|
|
|
|
func (h *Inbound) NewPacketEx(buffer *buf.Buffer, source M.Socksaddr) {
|
|
}
|