2022-08-19 07:42:57 +00:00
|
|
|
//go:build with_quic
|
|
|
|
|
|
|
|
package inbound
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-10-21 04:00:00 +00:00
|
|
|
"net"
|
2023-12-20 11:56:50 +00:00
|
|
|
"time"
|
2022-08-19 07:42:57 +00:00
|
|
|
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
2023-10-21 04:00:00 +00:00
|
|
|
"github.com/sagernet/sing-box/common/humanize"
|
2022-09-09 10:45:10 +00:00
|
|
|
"github.com/sagernet/sing-box/common/tls"
|
2022-08-19 07:42:57 +00:00
|
|
|
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"
|
2022-08-19 07:42:57 +00:00
|
|
|
"github.com/sagernet/sing/common"
|
2022-11-09 13:00:08 +00:00
|
|
|
"github.com/sagernet/sing/common/auth"
|
2022-08-19 07:42:57 +00:00
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
|
|
N "github.com/sagernet/sing/common/network"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ adapter.Inbound = (*Hysteria)(nil)
|
|
|
|
|
|
|
|
type Hysteria struct {
|
2022-08-23 13:07:35 +00:00
|
|
|
myInboundAdapter
|
2022-09-09 10:45:10 +00:00
|
|
|
tlsConfig tls.ServerConfig
|
2023-10-21 04:00:00 +00:00
|
|
|
service *hysteria.Service[int]
|
|
|
|
userNameList []string
|
2022-08-19 07:42:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewHysteria(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.HysteriaInboundOptions) (*Hysteria, error) {
|
2023-01-19 02:35:26 +00:00
|
|
|
options.UDPFragmentDefault = true
|
2023-10-21 04:00:00 +00:00
|
|
|
if options.TLS == nil || !options.TLS.Enabled {
|
|
|
|
return nil, C.ErrTLSRequired
|
2022-08-19 07:42:57 +00:00
|
|
|
}
|
2023-10-21 04:00:00 +00:00
|
|
|
tlsConfig, err := tls.NewServer(ctx, logger, common.PtrValueOrDefault(options.TLS))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-08-19 07:42:57 +00:00
|
|
|
}
|
|
|
|
inbound := &Hysteria{
|
2022-08-23 13:07:35 +00:00
|
|
|
myInboundAdapter: myInboundAdapter{
|
|
|
|
protocol: C.TypeHysteria,
|
|
|
|
network: []string{N.NetworkUDP},
|
|
|
|
ctx: ctx,
|
|
|
|
router: router,
|
|
|
|
logger: logger,
|
|
|
|
tag: tag,
|
|
|
|
listenOptions: options.ListenOptions,
|
|
|
|
},
|
2023-10-21 04:00:00 +00:00
|
|
|
tlsConfig: tlsConfig,
|
2022-08-19 07:42:57 +00:00
|
|
|
}
|
2023-10-21 04:00:00 +00:00
|
|
|
var sendBps, receiveBps uint64
|
|
|
|
if len(options.Up) > 0 {
|
|
|
|
sendBps, err = humanize.ParseBytes(options.Up)
|
2022-08-19 07:42:57 +00:00
|
|
|
if err != nil {
|
2023-10-21 04:00:00 +00:00
|
|
|
return nil, E.Cause(err, "invalid up speed format: ", options.Up)
|
2022-08-19 07:42:57 +00:00
|
|
|
}
|
2023-10-21 04:00:00 +00:00
|
|
|
} else {
|
|
|
|
sendBps = uint64(options.UpMbps) * hysteria.MbpsToBps
|
2022-08-19 07:42:57 +00:00
|
|
|
}
|
2023-10-21 04:00:00 +00:00
|
|
|
if len(options.Down) > 0 {
|
|
|
|
receiveBps, err = humanize.ParseBytes(options.Down)
|
|
|
|
if receiveBps == 0 {
|
|
|
|
return nil, E.New("invalid down speed format: ", options.Down)
|
2023-03-20 11:22:46 +00:00
|
|
|
}
|
2022-11-09 13:00:08 +00:00
|
|
|
} else {
|
2023-10-21 04:00:00 +00:00
|
|
|
receiveBps = uint64(options.DownMbps) * hysteria.MbpsToBps
|
|
|
|
}
|
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-10-21 04:00:00 +00:00
|
|
|
service, err := hysteria.NewService[int](hysteria.ServiceOptions{
|
|
|
|
Context: ctx,
|
|
|
|
Logger: logger,
|
|
|
|
SendBPS: sendBps,
|
|
|
|
ReceiveBPS: receiveBps,
|
|
|
|
XPlusPassword: options.Obfs,
|
|
|
|
TLSConfig: tlsConfig,
|
2023-12-20 11:56:50 +00:00
|
|
|
UDPTimeout: udpTimeout,
|
2023-10-21 04:00:00 +00:00
|
|
|
Handler: adapter.NewUpstreamHandler(adapter.InboundContext{}, inbound.newConnection, inbound.newPacketConnection, nil),
|
|
|
|
|
|
|
|
// Legacy options
|
|
|
|
|
|
|
|
ConnReceiveWindow: options.ReceiveWindowConn,
|
|
|
|
StreamReceiveWindow: options.ReceiveWindowClient,
|
|
|
|
MaxIncomingStreams: int64(options.MaxConnClient),
|
|
|
|
DisableMTUDiscovery: options.DisableMTUDiscovery,
|
2022-08-19 07:42:57 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2023-10-21 04:00:00 +00:00
|
|
|
return nil, err
|
2022-08-19 07:42:57 +00:00
|
|
|
}
|
2023-10-21 04:00:00 +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)
|
|
|
|
var password string
|
|
|
|
if user.AuthString != "" {
|
|
|
|
password = user.AuthString
|
|
|
|
} else {
|
|
|
|
password = string(user.Auth)
|
2022-08-19 07:42:57 +00:00
|
|
|
}
|
2023-10-21 04:00:00 +00:00
|
|
|
userPasswordList = append(userPasswordList, password)
|
2022-08-19 07:42:57 +00:00
|
|
|
}
|
2023-10-21 04:00:00 +00:00
|
|
|
service.UpdateUsers(userList, userPasswordList)
|
|
|
|
inbound.service = service
|
|
|
|
inbound.userNameList = userNameList
|
|
|
|
return inbound, nil
|
2022-08-19 07:42:57 +00:00
|
|
|
}
|
|
|
|
|
2023-10-21 04:00:00 +00:00
|
|
|
func (h *Hysteria) 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-10-21 04:00:00 +00:00
|
|
|
userID, _ := auth.UserFromContext[int](ctx)
|
|
|
|
if userName := h.userNameList[userID]; userName != "" {
|
|
|
|
metadata.User = userName
|
|
|
|
h.logger.InfoContext(ctx, "[", userName, "] inbound connection to ", metadata.Destination)
|
|
|
|
} else {
|
|
|
|
h.logger.InfoContext(ctx, "inbound connection to ", metadata.Destination)
|
2022-08-19 07:42:57 +00:00
|
|
|
}
|
2023-10-21 04:00:00 +00:00
|
|
|
return h.router.RouteConnection(ctx, conn, metadata)
|
2022-08-19 07:42:57 +00:00
|
|
|
}
|
|
|
|
|
2023-10-21 04:00:00 +00:00
|
|
|
func (h *Hysteria) 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-10-21 04:00:00 +00:00
|
|
|
userID, _ := auth.UserFromContext[int](ctx)
|
|
|
|
if userName := h.userNameList[userID]; userName != "" {
|
|
|
|
metadata.User = userName
|
|
|
|
h.logger.InfoContext(ctx, "[", userName, "] inbound packet connection to ", metadata.Destination)
|
2022-08-19 07:42:57 +00:00
|
|
|
} else {
|
|
|
|
h.logger.InfoContext(ctx, "inbound packet connection to ", metadata.Destination)
|
2023-10-21 04:00:00 +00:00
|
|
|
}
|
|
|
|
return h.router.RoutePacketConnection(ctx, conn, metadata)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Hysteria) Start() error {
|
|
|
|
if h.tlsConfig != nil {
|
|
|
|
err := h.tlsConfig.Start()
|
2022-09-12 10:32:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-19 07:42:57 +00:00
|
|
|
}
|
2023-10-21 04:00:00 +00:00
|
|
|
packetConn, err := h.myInboundAdapter.ListenUDP()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return h.service.Start(packetConn)
|
2022-08-19 07:42:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Hysteria) Close() error {
|
|
|
|
return common.Close(
|
2022-08-23 13:07:35 +00:00
|
|
|
&h.myInboundAdapter,
|
2022-09-09 10:45:10 +00:00
|
|
|
h.tlsConfig,
|
2023-10-21 04:00:00 +00:00
|
|
|
common.PtrOrNil(h.service),
|
2022-08-19 07:42:57 +00:00
|
|
|
)
|
|
|
|
}
|