sing-box/protocol/tor/proxy.go

122 lines
3.4 KiB
Go
Raw Normal View History

2024-11-02 00:39:02 +08:00
package tor
2022-08-21 00:59:49 +08:00
import (
2024-11-23 22:34:02 +08:00
std_bufio "bufio"
2022-08-21 00:59:49 +08:00
"context"
2023-02-07 14:53:08 +08:00
"crypto/rand"
2022-08-21 00:59:49 +08:00
"encoding/hex"
"net"
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/auth"
E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
"github.com/sagernet/sing/protocol/socks"
2024-11-23 22:34:02 +08:00
"github.com/sagernet/sing/service"
2022-08-21 00:59:49 +08:00
)
type ProxyListener struct {
ctx context.Context
logger log.ContextLogger
dialer N.Dialer
2024-11-23 22:34:02 +08:00
connection adapter.ConnectionManager
2022-08-21 00:59:49 +08:00
tcpListener *net.TCPListener
username string
password string
authenticator *auth.Authenticator
2022-08-21 00:59:49 +08:00
}
func NewProxyListener(ctx context.Context, logger log.ContextLogger, dialer N.Dialer) *ProxyListener {
var usernameB [64]byte
var passwordB [64]byte
rand.Read(usernameB[:])
rand.Read(passwordB[:])
username := hex.EncodeToString(usernameB[:])
password := hex.EncodeToString(passwordB[:])
return &ProxyListener{
ctx: ctx,
logger: logger,
dialer: dialer,
2024-11-23 22:34:02 +08:00
connection: service.FromContext[adapter.ConnectionManager](ctx),
2022-08-21 00:59:49 +08:00
authenticator: auth.NewAuthenticator([]auth.User{{Username: username, Password: password}}),
username: username,
password: password,
}
}
func (l *ProxyListener) Start() error {
tcpListener, err := net.ListenTCP("tcp", &net.TCPAddr{
IP: net.IPv4(127, 0, 0, 1),
})
if err != nil {
return err
}
l.tcpListener = tcpListener
go l.acceptLoop()
return nil
}
func (l *ProxyListener) Port() uint16 {
if l.tcpListener == nil {
panic("start listener first")
}
return M.SocksaddrFromNet(l.tcpListener.Addr()).Port
}
func (l *ProxyListener) Username() string {
return l.username
}
func (l *ProxyListener) Password() string {
return l.password
}
func (l *ProxyListener) Close() error {
return common.Close(l.tcpListener)
}
func (l *ProxyListener) acceptLoop() {
for {
tcpConn, err := l.tcpListener.AcceptTCP()
if err != nil {
return
}
ctx := log.ContextWithNewID(l.ctx)
go func() {
hErr := l.accept(ctx, tcpConn)
if hErr != nil {
if E.IsClosedOrCanceled(hErr) {
l.logger.DebugContext(ctx, E.Cause(hErr, "proxy connection closed"))
return
}
l.logger.ErrorContext(ctx, E.Cause(hErr, "proxy"))
}
}()
}
}
func (l *ProxyListener) accept(ctx context.Context, conn *net.TCPConn) error {
2024-11-23 22:34:02 +08:00
return socks.HandleConnectionEx(ctx, conn, std_bufio.NewReader(conn), l.authenticator, nil, l, M.SocksaddrFromNet(conn.RemoteAddr()), M.Socksaddr{}, nil)
2022-08-21 00:59:49 +08:00
}
2024-11-23 22:34:02 +08:00
func (l *ProxyListener) NewConnectionEx(ctx context.Context, conn net.Conn, source M.Socksaddr, destination M.Socksaddr, onClose N.CloseHandlerFunc) {
2022-08-21 00:59:49 +08:00
var metadata adapter.InboundContext
2024-11-23 22:34:02 +08:00
metadata.Source = source
metadata.Destination = destination
2022-08-21 00:59:49 +08:00
metadata.Network = N.NetworkTCP
l.logger.InfoContext(ctx, "proxy connection to ", metadata.Destination)
2024-11-23 22:34:02 +08:00
l.connection.NewConnection(ctx, l.dialer, conn, metadata, onClose)
2022-08-21 00:59:49 +08:00
}
2024-11-23 22:34:02 +08:00
func (l *ProxyListener) NewPacketConnectionEx(ctx context.Context, conn N.PacketConn, source M.Socksaddr, destination M.Socksaddr, onClose N.CloseHandlerFunc) {
2022-08-21 00:59:49 +08:00
var metadata adapter.InboundContext
2024-11-23 22:34:02 +08:00
metadata.Source = source
metadata.Destination = destination
2022-08-21 00:59:49 +08:00
metadata.Network = N.NetworkUDP
l.logger.InfoContext(ctx, "proxy packet connection to ", metadata.Destination)
2024-11-23 22:34:02 +08:00
l.connection.NewPacketConnection(ctx, l.dialer, conn, metadata, onClose)
2022-08-21 00:59:49 +08:00
}