2023-08-31 12:07:32 +00:00
|
|
|
//go:build with_quic
|
|
|
|
|
|
|
|
package inbound
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httputil"
|
|
|
|
"net/url"
|
2023-12-20 11:56:50 +00:00
|
|
|
"time"
|
2023-08-31 12:07:32 +00:00
|
|
|
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
|
|
|
"github.com/sagernet/sing-box/common/tls"
|
|
|
|
C "github.com/sagernet/sing-box/constant"
|
|
|
|
"github.com/sagernet/sing-box/log"
|
|
|
|
"github.com/sagernet/sing-box/option"
|
2023-10-21 04:00:00 +00:00
|
|
|
"github.com/sagernet/sing-quic/hysteria"
|
2023-09-12 13:14:11 +00:00
|
|
|
"github.com/sagernet/sing-quic/hysteria2"
|
2023-08-31 12:07:32 +00:00
|
|
|
"github.com/sagernet/sing/common"
|
|
|
|
"github.com/sagernet/sing/common/auth"
|
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
|
|
N "github.com/sagernet/sing/common/network"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ adapter.Inbound = (*Hysteria2)(nil)
|
|
|
|
|
|
|
|
type Hysteria2 struct {
|
|
|
|
myInboundAdapter
|
2023-09-12 13:14:11 +00:00
|
|
|
tlsConfig tls.ServerConfig
|
|
|
|
service *hysteria2.Service[int]
|
|
|
|
userNameList []string
|
2023-08-31 12:07:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewHysteria2(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.Hysteria2InboundOptions) (*Hysteria2, error) {
|
2023-10-21 04:00:00 +00:00
|
|
|
options.UDPFragmentDefault = true
|
2023-08-31 12:07:32 +00:00
|
|
|
if options.TLS == nil || !options.TLS.Enabled {
|
|
|
|
return nil, C.ErrTLSRequired
|
|
|
|
}
|
|
|
|
tlsConfig, err := tls.NewServer(ctx, logger, common.PtrValueOrDefault(options.TLS))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var salamanderPassword string
|
|
|
|
if options.Obfs != nil {
|
|
|
|
if options.Obfs.Password == "" {
|
|
|
|
return nil, E.New("missing obfs password")
|
|
|
|
}
|
|
|
|
switch options.Obfs.Type {
|
|
|
|
case hysteria2.ObfsTypeSalamander:
|
|
|
|
salamanderPassword = options.Obfs.Password
|
|
|
|
default:
|
|
|
|
return nil, E.New("unknown obfs type: ", options.Obfs.Type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var masqueradeHandler http.Handler
|
|
|
|
if options.Masquerade != "" {
|
|
|
|
masqueradeURL, err := url.Parse(options.Masquerade)
|
|
|
|
if err != nil {
|
|
|
|
return nil, E.Cause(err, "parse masquerade URL")
|
|
|
|
}
|
|
|
|
switch masqueradeURL.Scheme {
|
|
|
|
case "file":
|
|
|
|
masqueradeHandler = http.FileServer(http.Dir(masqueradeURL.Path))
|
|
|
|
case "http", "https":
|
|
|
|
masqueradeHandler = &httputil.ReverseProxy{
|
|
|
|
Rewrite: func(r *httputil.ProxyRequest) {
|
|
|
|
r.SetURL(masqueradeURL)
|
|
|
|
r.Out.Host = r.In.Host
|
|
|
|
},
|
|
|
|
ErrorHandler: func(w http.ResponseWriter, r *http.Request, err error) {
|
|
|
|
w.WriteHeader(http.StatusBadGateway)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil, E.New("unknown masquerade URL scheme: ", masqueradeURL.Scheme)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
inbound := &Hysteria2{
|
|
|
|
myInboundAdapter: myInboundAdapter{
|
|
|
|
protocol: C.TypeHysteria2,
|
|
|
|
network: []string{N.NetworkUDP},
|
|
|
|
ctx: ctx,
|
|
|
|
router: router,
|
|
|
|
logger: logger,
|
|
|
|
tag: tag,
|
|
|
|
listenOptions: options.ListenOptions,
|
|
|
|
},
|
|
|
|
tlsConfig: tlsConfig,
|
|
|
|
}
|
2023-12-20 11:56:50 +00:00
|
|
|
var udpTimeout time.Duration
|
|
|
|
if options.UDPTimeout != 0 {
|
|
|
|
udpTimeout = time.Duration(options.UDPTimeout)
|
|
|
|
} else {
|
|
|
|
udpTimeout = C.UDPTimeout
|
|
|
|
}
|
2023-09-12 13:14:11 +00:00
|
|
|
service, err := hysteria2.NewService[int](hysteria2.ServiceOptions{
|
|
|
|
Context: ctx,
|
|
|
|
Logger: logger,
|
2023-10-21 04:00:00 +00:00
|
|
|
BrutalDebug: options.BrutalDebug,
|
2023-09-16 16:25:53 +00:00
|
|
|
SendBPS: uint64(options.UpMbps * hysteria.MbpsToBps),
|
|
|
|
ReceiveBPS: uint64(options.DownMbps * hysteria.MbpsToBps),
|
2023-09-12 13:14:11 +00:00
|
|
|
SalamanderPassword: salamanderPassword,
|
|
|
|
TLSConfig: tlsConfig,
|
2023-08-31 12:07:32 +00:00
|
|
|
IgnoreClientBandwidth: options.IgnoreClientBandwidth,
|
2023-12-20 11:56:50 +00:00
|
|
|
UDPTimeout: udpTimeout,
|
2023-08-31 12:07:32 +00:00
|
|
|
Handler: adapter.NewUpstreamHandler(adapter.InboundContext{}, inbound.newConnection, inbound.newPacketConnection, nil),
|
|
|
|
MasqueradeHandler: masqueradeHandler,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-09-12 13:14:11 +00:00
|
|
|
userList := make([]int, 0, len(options.Users))
|
|
|
|
userNameList := make([]string, 0, len(options.Users))
|
|
|
|
userPasswordList := make([]string, 0, len(options.Users))
|
|
|
|
for index, user := range options.Users {
|
|
|
|
userList = append(userList, index)
|
|
|
|
userNameList = append(userNameList, user.Name)
|
|
|
|
userPasswordList = append(userPasswordList, user.Password)
|
|
|
|
}
|
|
|
|
service.UpdateUsers(userList, userPasswordList)
|
|
|
|
inbound.service = service
|
|
|
|
inbound.userNameList = userNameList
|
2023-08-31 12:07:32 +00:00
|
|
|
return inbound, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Hysteria2) newConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
|
|
|
|
ctx = log.ContextWithNewID(ctx)
|
|
|
|
metadata = h.createMetadata(conn, metadata)
|
2024-03-07 02:43:06 +00:00
|
|
|
h.logger.InfoContext(ctx, "inbound connection from ", metadata.Source)
|
2023-09-12 13:14:11 +00:00
|
|
|
userID, _ := auth.UserFromContext[int](ctx)
|
|
|
|
if userName := h.userNameList[userID]; userName != "" {
|
|
|
|
metadata.User = userName
|
2023-09-24 04:00:00 +00:00
|
|
|
h.logger.InfoContext(ctx, "[", userName, "] inbound connection to ", metadata.Destination)
|
|
|
|
} else {
|
|
|
|
h.logger.InfoContext(ctx, "inbound connection to ", metadata.Destination)
|
2023-09-12 13:14:11 +00:00
|
|
|
}
|
2023-08-31 12:07:32 +00:00
|
|
|
return h.router.RouteConnection(ctx, conn, metadata)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Hysteria2) newPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
|
|
|
|
ctx = log.ContextWithNewID(ctx)
|
|
|
|
metadata = h.createPacketMetadata(conn, metadata)
|
2024-03-07 02:43:06 +00:00
|
|
|
h.logger.InfoContext(ctx, "inbound packet connection from ", metadata.Source)
|
2023-09-12 13:14:11 +00:00
|
|
|
userID, _ := auth.UserFromContext[int](ctx)
|
|
|
|
if userName := h.userNameList[userID]; userName != "" {
|
|
|
|
metadata.User = userName
|
2023-09-24 04:00:00 +00:00
|
|
|
h.logger.InfoContext(ctx, "[", userName, "] inbound packet connection to ", metadata.Destination)
|
|
|
|
} else {
|
|
|
|
h.logger.InfoContext(ctx, "inbound packet connection to ", metadata.Destination)
|
2023-09-12 13:14:11 +00:00
|
|
|
}
|
2023-08-31 12:07:32 +00:00
|
|
|
return h.router.RoutePacketConnection(ctx, conn, metadata)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Hysteria2) Start() error {
|
|
|
|
if h.tlsConfig != nil {
|
|
|
|
err := h.tlsConfig.Start()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
packetConn, err := h.myInboundAdapter.ListenUDP()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-12 13:14:11 +00:00
|
|
|
return h.service.Start(packetConn)
|
2023-08-31 12:07:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Hysteria2) Close() error {
|
|
|
|
return common.Close(
|
|
|
|
&h.myInboundAdapter,
|
|
|
|
h.tlsConfig,
|
2023-09-12 13:14:11 +00:00
|
|
|
common.PtrOrNil(h.service),
|
2023-08-31 12:07:32 +00:00
|
|
|
)
|
|
|
|
}
|