sing-box/inbound/mixed.go

67 lines
1.9 KiB
Go
Raw Normal View History

2022-07-01 11:34:02 +00:00
package inbound
import (
std_bufio "bufio"
"context"
"net"
2022-08-29 11:43:13 +00:00
"os"
2022-07-01 11:34:02 +00:00
2022-07-08 15:03:57 +00:00
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/common/uot"
2022-07-08 15:03:57 +00:00
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"
2022-07-01 11:34:02 +00:00
"github.com/sagernet/sing/common/auth"
2022-07-29 16:29:22 +00:00
N "github.com/sagernet/sing/common/network"
2022-07-01 11:34:02 +00: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 11:43:13 +00:00
var (
_ adapter.Inbound = (*Mixed)(nil)
_ adapter.InjectableInbound = (*Mixed)(nil)
)
2022-07-01 11:34:02 +00:00
type Mixed struct {
myInboundAdapter
authenticator *auth.Authenticator
2022-07-01 11:34:02 +00:00
}
func NewMixed(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.HTTPMixedInboundOptions) *Mixed {
2022-07-01 11:34:02 +00:00
inbound := &Mixed{
myInboundAdapter{
protocol: C.TypeMixed,
2022-07-29 16:29:22 +00:00
network: []string{N.NetworkTCP},
ctx: ctx,
router: uot.NewRouter(router, logger),
logger: logger,
tag: tag,
listenOptions: options.ListenOptions,
setSystemProxy: options.SetSystemProxy,
2022-07-01 11:34:02 +00:00
},
auth.NewAuthenticator(options.Users),
}
inbound.connHandler = inbound
return inbound
}
func (h *Mixed) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
2024-06-24 01:49:15 +00:00
reader := std_bufio.NewReader(conn)
headerBytes, err := reader.Peek(1)
2022-07-01 11:34:02 +00:00
if err != nil {
return err
}
2024-06-24 01:49:15 +00:00
switch headerBytes[0] {
2022-07-01 11:34:02 +00:00
case socks4.Version, socks5.Version:
2024-06-24 01:49:15 +00:00
return socks.HandleConnection0(ctx, conn, reader, h.authenticator, h.upstreamUserHandler(metadata), adapter.UpstreamMetadata(metadata))
default:
return http.HandleConnection(ctx, conn, reader, h.authenticator, h.upstreamUserHandler(metadata), adapter.UpstreamMetadata(metadata))
2022-07-01 11:34:02 +00:00
}
}
2022-08-29 11:43:13 +00:00
func (h *Mixed) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
return os.ErrInvalid
}