101 lines
2.8 KiB
Go
101 lines
2.8 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/logger"
|
|
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,
|
|
}
|
|
networks := []string{N.NetworkTCP}
|
|
if options.ProtocolType == "bedrock" {
|
|
networks = nil // Handled by separate raknet listener
|
|
}
|
|
inbound.listener = listener.New(listener.Options{
|
|
Context: ctx,
|
|
Logger: logger,
|
|
Network: networks,
|
|
Listen: options.ListenOptions,
|
|
ConnectionHandler: inbound,
|
|
PacketHandler: 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
|
|
}
|
|
if h.options.ProtocolType == "bedrock" {
|
|
go h.startBedrockListener()
|
|
return nil
|
|
}
|
|
return h.listener.Start()
|
|
}
|
|
|
|
func (h *Inbound) Close() error { return common.Close(h.listener) }
|
|
|
|
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) {
|
|
h.handleJavaConnection(ctx, conn, metadata, onClose)
|
|
}
|
|
|
|
func (h *Inbound) NewPacketConnectionEx(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
|
|
h.handleBedrockPacket(ctx, conn, metadata, onClose)
|
|
}
|
|
|
|
func (h *Inbound) NewPacketEx(ctx context.Context, conn N.PacketWriter, buffer *N.Buffer, metadata adapter.InboundContext) {
|
|
}
|