2022-07-01 19:34:02 +08:00
|
|
|
package inbound
|
|
|
|
|
|
|
|
import (
|
|
|
|
std_bufio "bufio"
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
|
2022-07-08 23:03:57 +08:00
|
|
|
"github.com/sagernet/sing-box/adapter"
|
2023-11-08 12:09:22 +08:00
|
|
|
"github.com/sagernet/sing-box/common/uot"
|
2022-07-08 23:03:57 +08:00
|
|
|
C "github.com/sagernet/sing-box/constant"
|
|
|
|
"github.com/sagernet/sing-box/log"
|
|
|
|
"github.com/sagernet/sing-box/option"
|
2022-07-01 19:34:02 +08:00
|
|
|
"github.com/sagernet/sing/common/auth"
|
2024-10-21 23:38:34 +08:00
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
2022-07-30 00:29:22 +08:00
|
|
|
N "github.com/sagernet/sing/common/network"
|
2022-07-01 19:34:02 +08:00
|
|
|
"github.com/sagernet/sing/protocol/http"
|
|
|
|
"github.com/sagernet/sing/protocol/socks"
|
|
|
|
"github.com/sagernet/sing/protocol/socks/socks4"
|
|
|
|
"github.com/sagernet/sing/protocol/socks/socks5"
|
|
|
|
)
|
|
|
|
|
2022-08-29 19:43:13 +08:00
|
|
|
var (
|
2024-10-21 23:38:34 +08:00
|
|
|
_ adapter.Inbound = (*Mixed)(nil)
|
|
|
|
_ adapter.TCPInjectableInbound = (*Mixed)(nil)
|
2022-08-29 19:43:13 +08:00
|
|
|
)
|
2022-07-01 19:34:02 +08:00
|
|
|
|
|
|
|
type Mixed struct {
|
|
|
|
myInboundAdapter
|
2023-12-20 20:00:00 +08:00
|
|
|
authenticator *auth.Authenticator
|
2022-07-01 19:34:02 +08:00
|
|
|
}
|
|
|
|
|
2022-07-14 14:04:57 +08:00
|
|
|
func NewMixed(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.HTTPMixedInboundOptions) *Mixed {
|
2022-07-01 19:34:02 +08:00
|
|
|
inbound := &Mixed{
|
|
|
|
myInboundAdapter{
|
2022-07-14 14:04:57 +08:00
|
|
|
protocol: C.TypeMixed,
|
2022-07-30 00:29:22 +08:00
|
|
|
network: []string{N.NetworkTCP},
|
2022-07-14 14:04:57 +08:00
|
|
|
ctx: ctx,
|
2023-11-08 12:09:22 +08:00
|
|
|
router: uot.NewRouter(router, logger),
|
2022-07-14 14:04:57 +08:00
|
|
|
logger: logger,
|
|
|
|
tag: tag,
|
|
|
|
listenOptions: options.ListenOptions,
|
|
|
|
setSystemProxy: options.SetSystemProxy,
|
2022-07-01 19:34:02 +08:00
|
|
|
},
|
|
|
|
auth.NewAuthenticator(options.Users),
|
|
|
|
}
|
|
|
|
inbound.connHandler = inbound
|
|
|
|
return inbound
|
|
|
|
}
|
|
|
|
|
2024-10-21 23:38:34 +08:00
|
|
|
func (h *Mixed) NewConnectionEx(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
|
|
|
|
err := h.newConnection(ctx, conn, metadata, onClose)
|
|
|
|
N.CloseOnHandshakeFailure(conn, onClose, err)
|
|
|
|
if err != nil {
|
|
|
|
h.logger.ErrorContext(ctx, E.Cause(err, "process connection from ", metadata.Source))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Mixed) newConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) error {
|
2024-06-24 09:49:15 +08:00
|
|
|
reader := std_bufio.NewReader(conn)
|
|
|
|
headerBytes, err := reader.Peek(1)
|
2022-07-01 19:34:02 +08:00
|
|
|
if err != nil {
|
2024-10-21 23:38:34 +08:00
|
|
|
return E.Cause(err, "peek first byte")
|
2022-07-01 19:34:02 +08:00
|
|
|
}
|
2024-06-24 09:49:15 +08:00
|
|
|
switch headerBytes[0] {
|
2022-07-01 19:34:02 +08:00
|
|
|
case socks4.Version, socks5.Version:
|
2024-10-21 23:38:34 +08:00
|
|
|
return socks.HandleConnectionEx(ctx, conn, reader, h.authenticator, nil, h.upstreamUserHandlerEx(metadata), metadata.Source, metadata.Destination, onClose)
|
2024-06-24 09:49:15 +08:00
|
|
|
default:
|
2024-10-21 23:38:34 +08:00
|
|
|
return http.HandleConnectionEx(ctx, conn, reader, h.authenticator, nil, h.upstreamUserHandlerEx(metadata), metadata.Source, onClose)
|
2022-07-01 19:34:02 +08:00
|
|
|
}
|
|
|
|
}
|