2024-11-02 00:39:02 +08:00
|
|
|
package hysteria
|
2022-08-19 15:42:57 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-10-21 12:00:00 +08:00
|
|
|
"net"
|
2023-12-20 19:56:50 +08:00
|
|
|
"time"
|
2022-08-19 15:42:57 +08:00
|
|
|
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
2024-11-02 00:39:02 +08:00
|
|
|
"github.com/sagernet/sing-box/adapter/inbound"
|
2023-10-21 12:00:00 +08:00
|
|
|
"github.com/sagernet/sing-box/common/humanize"
|
2024-11-02 00:39:02 +08:00
|
|
|
"github.com/sagernet/sing-box/common/listener"
|
2022-09-09 18:45:10 +08:00
|
|
|
"github.com/sagernet/sing-box/common/tls"
|
2022-08-19 15:42:57 +08:00
|
|
|
C "github.com/sagernet/sing-box/constant"
|
|
|
|
"github.com/sagernet/sing-box/log"
|
|
|
|
"github.com/sagernet/sing-box/option"
|
2023-10-21 12:00:00 +08:00
|
|
|
"github.com/sagernet/sing-quic/hysteria"
|
2022-08-19 15:42:57 +08:00
|
|
|
"github.com/sagernet/sing/common"
|
2022-11-09 21:00:08 +08:00
|
|
|
"github.com/sagernet/sing/common/auth"
|
2022-08-19 15:42:57 +08:00
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
2024-11-09 21:16:11 +08:00
|
|
|
M "github.com/sagernet/sing/common/metadata"
|
2022-08-19 15:42:57 +08:00
|
|
|
N "github.com/sagernet/sing/common/network"
|
|
|
|
)
|
|
|
|
|
2024-11-02 00:39:02 +08:00
|
|
|
func RegisterInbound(registry *inbound.Registry) {
|
|
|
|
inbound.Register[option.HysteriaInboundOptions](registry, C.TypeHysteria, NewInbound)
|
|
|
|
}
|
2022-08-19 15:42:57 +08:00
|
|
|
|
2024-11-02 00:39:02 +08:00
|
|
|
type Inbound struct {
|
|
|
|
inbound.Adapter
|
|
|
|
router adapter.Router
|
|
|
|
logger log.ContextLogger
|
|
|
|
listener *listener.Listener
|
2022-09-09 18:45:10 +08:00
|
|
|
tlsConfig tls.ServerConfig
|
2023-10-21 12:00:00 +08:00
|
|
|
service *hysteria.Service[int]
|
|
|
|
userNameList []string
|
2022-08-19 15:42:57 +08:00
|
|
|
}
|
|
|
|
|
2024-11-02 00:39:02 +08:00
|
|
|
func NewInbound(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.HysteriaInboundOptions) (adapter.Inbound, error) {
|
2023-01-19 10:35:26 +08:00
|
|
|
options.UDPFragmentDefault = true
|
2023-10-21 12:00:00 +08:00
|
|
|
if options.TLS == nil || !options.TLS.Enabled {
|
|
|
|
return nil, C.ErrTLSRequired
|
2022-08-19 15:42:57 +08:00
|
|
|
}
|
2023-10-21 12:00:00 +08:00
|
|
|
tlsConfig, err := tls.NewServer(ctx, logger, common.PtrValueOrDefault(options.TLS))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-08-19 15:42:57 +08:00
|
|
|
}
|
2024-11-02 00:39:02 +08:00
|
|
|
inbound := &Inbound{
|
|
|
|
Adapter: inbound.NewAdapter(C.TypeHysteria, tag),
|
|
|
|
router: router,
|
|
|
|
logger: logger,
|
|
|
|
listener: listener.New(listener.Options{
|
|
|
|
Context: ctx,
|
|
|
|
Logger: logger,
|
|
|
|
Listen: options.ListenOptions,
|
|
|
|
}),
|
2023-10-21 12:00:00 +08:00
|
|
|
tlsConfig: tlsConfig,
|
2022-08-19 15:42:57 +08:00
|
|
|
}
|
2023-10-21 12:00:00 +08:00
|
|
|
var sendBps, receiveBps uint64
|
|
|
|
if len(options.Up) > 0 {
|
|
|
|
sendBps, err = humanize.ParseBytes(options.Up)
|
2022-08-19 15:42:57 +08:00
|
|
|
if err != nil {
|
2023-10-21 12:00:00 +08:00
|
|
|
return nil, E.Cause(err, "invalid up speed format: ", options.Up)
|
2022-08-19 15:42:57 +08:00
|
|
|
}
|
2023-10-21 12:00:00 +08:00
|
|
|
} else {
|
|
|
|
sendBps = uint64(options.UpMbps) * hysteria.MbpsToBps
|
2022-08-19 15:42:57 +08:00
|
|
|
}
|
2023-10-21 12:00:00 +08:00
|
|
|
if len(options.Down) > 0 {
|
|
|
|
receiveBps, err = humanize.ParseBytes(options.Down)
|
2024-11-23 22:34:02 +08:00
|
|
|
if err != nil {
|
2023-10-21 12:00:00 +08:00
|
|
|
return nil, E.New("invalid down speed format: ", options.Down)
|
2023-03-20 19:22:46 +08:00
|
|
|
}
|
2022-11-09 21:00:08 +08:00
|
|
|
} else {
|
2023-10-21 12:00:00 +08:00
|
|
|
receiveBps = uint64(options.DownMbps) * hysteria.MbpsToBps
|
|
|
|
}
|
2023-12-20 19:56:50 +08:00
|
|
|
var udpTimeout time.Duration
|
|
|
|
if options.UDPTimeout != 0 {
|
|
|
|
udpTimeout = time.Duration(options.UDPTimeout)
|
|
|
|
} else {
|
|
|
|
udpTimeout = C.UDPTimeout
|
|
|
|
}
|
2023-10-21 12:00:00 +08:00
|
|
|
service, err := hysteria.NewService[int](hysteria.ServiceOptions{
|
|
|
|
Context: ctx,
|
|
|
|
Logger: logger,
|
|
|
|
SendBPS: sendBps,
|
|
|
|
ReceiveBPS: receiveBps,
|
|
|
|
XPlusPassword: options.Obfs,
|
|
|
|
TLSConfig: tlsConfig,
|
2023-12-20 19:56:50 +08:00
|
|
|
UDPTimeout: udpTimeout,
|
2024-11-09 21:16:11 +08:00
|
|
|
Handler: inbound,
|
2023-10-21 12:00:00 +08:00
|
|
|
|
|
|
|
// Legacy options
|
|
|
|
|
|
|
|
ConnReceiveWindow: options.ReceiveWindowConn,
|
|
|
|
StreamReceiveWindow: options.ReceiveWindowClient,
|
|
|
|
MaxIncomingStreams: int64(options.MaxConnClient),
|
|
|
|
DisableMTUDiscovery: options.DisableMTUDiscovery,
|
2022-08-19 15:42:57 +08:00
|
|
|
})
|
|
|
|
if err != nil {
|
2023-10-21 12:00:00 +08:00
|
|
|
return nil, err
|
2022-08-19 15:42:57 +08:00
|
|
|
}
|
2023-10-21 12:00:00 +08: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 15:42:57 +08:00
|
|
|
}
|
2023-10-21 12:00:00 +08:00
|
|
|
userPasswordList = append(userPasswordList, password)
|
2022-08-19 15:42:57 +08:00
|
|
|
}
|
2023-10-21 12:00:00 +08:00
|
|
|
service.UpdateUsers(userList, userPasswordList)
|
|
|
|
inbound.service = service
|
|
|
|
inbound.userNameList = userNameList
|
|
|
|
return inbound, nil
|
2022-08-19 15:42:57 +08:00
|
|
|
}
|
|
|
|
|
2024-11-09 21:16:11 +08:00
|
|
|
func (h *Inbound) NewConnectionEx(ctx context.Context, conn net.Conn, source M.Socksaddr, destination M.Socksaddr, onClose N.CloseHandlerFunc) {
|
2023-10-21 12:00:00 +08:00
|
|
|
ctx = log.ContextWithNewID(ctx)
|
2024-11-09 21:16:11 +08:00
|
|
|
var metadata adapter.InboundContext
|
2024-11-02 00:39:02 +08:00
|
|
|
metadata.Inbound = h.Tag()
|
|
|
|
metadata.InboundType = h.Type()
|
2024-11-23 22:34:02 +08:00
|
|
|
//nolint:staticcheck
|
2024-11-02 00:39:02 +08:00
|
|
|
metadata.InboundDetour = h.listener.ListenOptions().Detour
|
2024-11-23 22:34:02 +08:00
|
|
|
//nolint:staticcheck
|
2024-11-02 00:39:02 +08:00
|
|
|
metadata.InboundOptions = h.listener.ListenOptions().InboundOptions
|
2024-11-09 21:16:11 +08:00
|
|
|
metadata.OriginDestination = h.listener.UDPAddr()
|
|
|
|
metadata.Source = source
|
|
|
|
metadata.Destination = destination
|
2024-03-07 10:43:06 +08:00
|
|
|
h.logger.InfoContext(ctx, "inbound connection from ", metadata.Source)
|
2023-10-21 12:00:00 +08: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 15:42:57 +08:00
|
|
|
}
|
2024-11-09 21:16:11 +08:00
|
|
|
h.router.RouteConnectionEx(ctx, conn, metadata, onClose)
|
2022-08-19 15:42:57 +08:00
|
|
|
}
|
|
|
|
|
2024-11-09 21:16:11 +08:00
|
|
|
func (h *Inbound) NewPacketConnectionEx(ctx context.Context, conn N.PacketConn, source M.Socksaddr, destination M.Socksaddr, onClose N.CloseHandlerFunc) {
|
2023-10-21 12:00:00 +08:00
|
|
|
ctx = log.ContextWithNewID(ctx)
|
2024-11-09 21:16:11 +08:00
|
|
|
var metadata adapter.InboundContext
|
2024-11-02 00:39:02 +08:00
|
|
|
metadata.Inbound = h.Tag()
|
|
|
|
metadata.InboundType = h.Type()
|
2024-11-23 22:34:02 +08:00
|
|
|
//nolint:staticcheck
|
2024-11-02 00:39:02 +08:00
|
|
|
metadata.InboundDetour = h.listener.ListenOptions().Detour
|
2024-11-23 22:34:02 +08:00
|
|
|
//nolint:staticcheck
|
2024-11-02 00:39:02 +08:00
|
|
|
metadata.InboundOptions = h.listener.ListenOptions().InboundOptions
|
|
|
|
metadata.OriginDestination = h.listener.UDPAddr()
|
2024-11-09 21:16:11 +08:00
|
|
|
metadata.Source = source
|
|
|
|
metadata.Destination = destination
|
2024-03-07 10:43:06 +08:00
|
|
|
h.logger.InfoContext(ctx, "inbound packet connection from ", metadata.Source)
|
2023-10-21 12:00:00 +08: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 15:42:57 +08:00
|
|
|
} else {
|
|
|
|
h.logger.InfoContext(ctx, "inbound packet connection to ", metadata.Destination)
|
2023-10-21 12:00:00 +08:00
|
|
|
}
|
2024-11-09 21:16:11 +08:00
|
|
|
h.router.RoutePacketConnectionEx(ctx, conn, metadata, onClose)
|
2023-10-21 12:00:00 +08:00
|
|
|
}
|
|
|
|
|
2024-11-21 18:10:41 +08:00
|
|
|
func (h *Inbound) Start(stage adapter.StartStage) error {
|
|
|
|
if stage != adapter.StartStateStart {
|
|
|
|
return nil
|
|
|
|
}
|
2023-10-21 12:00:00 +08:00
|
|
|
if h.tlsConfig != nil {
|
|
|
|
err := h.tlsConfig.Start()
|
2022-09-12 18:32:43 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-19 15:42:57 +08:00
|
|
|
}
|
2024-11-02 00:39:02 +08:00
|
|
|
packetConn, err := h.listener.ListenUDP()
|
2023-10-21 12:00:00 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return h.service.Start(packetConn)
|
2022-08-19 15:42:57 +08:00
|
|
|
}
|
|
|
|
|
2024-11-02 00:39:02 +08:00
|
|
|
func (h *Inbound) Close() error {
|
2022-08-19 15:42:57 +08:00
|
|
|
return common.Close(
|
2024-11-09 21:16:11 +08:00
|
|
|
h.listener,
|
2022-09-09 18:45:10 +08:00
|
|
|
h.tlsConfig,
|
2023-10-21 12:00:00 +08:00
|
|
|
common.PtrOrNil(h.service),
|
2022-08-19 15:42:57 +08:00
|
|
|
)
|
|
|
|
}
|