2024-11-02 00:39:02 +08:00
|
|
|
package hysteria
|
2022-08-19 15:42:57 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
2023-10-21 12:00:00 +08:00
|
|
|
"os"
|
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/outbound"
|
2022-08-19 15:42:57 +08:00
|
|
|
"github.com/sagernet/sing-box/common/dialer"
|
2023-10-21 12:00:00 +08:00
|
|
|
"github.com/sagernet/sing-box/common/humanize"
|
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"
|
2024-11-02 00:39:02 +08:00
|
|
|
"github.com/sagernet/sing-box/protocol/tuic"
|
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"
|
|
|
|
"github.com/sagernet/sing/common/bufio"
|
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
2024-11-02 00:39:02 +08:00
|
|
|
"github.com/sagernet/sing/common/logger"
|
2022-08-19 15:42:57 +08:00
|
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
|
|
N "github.com/sagernet/sing/common/network"
|
|
|
|
)
|
|
|
|
|
2024-11-02 00:39:02 +08:00
|
|
|
func RegisterOutbound(registry *outbound.Registry) {
|
|
|
|
outbound.Register[option.HysteriaOutboundOptions](registry, C.TypeHysteria, NewOutbound)
|
|
|
|
}
|
|
|
|
|
2022-11-06 10:36:19 +08:00
|
|
|
var (
|
2024-11-02 00:39:02 +08:00
|
|
|
_ adapter.Outbound = (*tuic.Outbound)(nil)
|
|
|
|
_ adapter.InterfaceUpdateListener = (*tuic.Outbound)(nil)
|
2022-11-06 10:36:19 +08:00
|
|
|
)
|
2022-08-19 15:42:57 +08:00
|
|
|
|
2024-11-02 00:39:02 +08:00
|
|
|
type Outbound struct {
|
|
|
|
outbound.Adapter
|
|
|
|
logger logger.ContextLogger
|
2023-10-21 12:00:00 +08:00
|
|
|
client *hysteria.Client
|
2022-08-19 15:42:57 +08:00
|
|
|
}
|
|
|
|
|
2024-11-02 00:39:02 +08:00
|
|
|
func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.HysteriaOutboundOptions) (adapter.Outbound, error) {
|
2022-10-18 17:27:41 +08:00
|
|
|
options.UDPFragmentDefault = true
|
2022-08-19 15:42:57 +08:00
|
|
|
if options.TLS == nil || !options.TLS.Enabled {
|
2022-08-22 22:19:25 +08:00
|
|
|
return nil, C.ErrTLSRequired
|
2022-08-19 15:42:57 +08:00
|
|
|
}
|
2023-08-31 11:37:26 +08:00
|
|
|
tlsConfig, err := tls.NewClient(ctx, options.Server, common.PtrValueOrDefault(options.TLS))
|
2022-09-09 18:45:10 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-10-21 12:00:00 +08:00
|
|
|
outboundDialer, err := dialer.New(router, options.DialerOptions)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-08-19 15:42:57 +08:00
|
|
|
}
|
2023-10-21 12:00:00 +08:00
|
|
|
networkList := options.Network.Build()
|
|
|
|
var password string
|
|
|
|
if options.AuthString != "" {
|
|
|
|
password = options.AuthString
|
2022-08-19 15:42:57 +08:00
|
|
|
} else {
|
2023-10-21 12:00:00 +08:00
|
|
|
password = string(options.Auth)
|
2022-08-19 15:42:57 +08:00
|
|
|
}
|
2023-10-21 12:00:00 +08:00
|
|
|
var sendBps, receiveBps uint64
|
2022-08-19 15:42:57 +08:00
|
|
|
if len(options.Up) > 0 {
|
2023-10-21 12:00:00 +08:00
|
|
|
sendBps, err = humanize.ParseBytes(options.Up)
|
|
|
|
if err != nil {
|
|
|
|
return nil, E.Cause(err, "invalid up speed format: ", options.Up)
|
2022-08-19 15:42:57 +08:00
|
|
|
}
|
|
|
|
} else {
|
2023-10-21 12:00:00 +08:00
|
|
|
sendBps = uint64(options.UpMbps) * hysteria.MbpsToBps
|
2022-08-19 15:42:57 +08:00
|
|
|
}
|
|
|
|
if len(options.Down) > 0 {
|
2023-10-21 12:00:00 +08:00
|
|
|
receiveBps, err = humanize.ParseBytes(options.Down)
|
|
|
|
if receiveBps == 0 {
|
2022-08-19 15:42:57 +08:00
|
|
|
return nil, E.New("invalid down speed format: ", options.Down)
|
|
|
|
}
|
|
|
|
} else {
|
2023-10-21 12:00:00 +08:00
|
|
|
receiveBps = uint64(options.DownMbps) * hysteria.MbpsToBps
|
|
|
|
}
|
|
|
|
client, err := hysteria.NewClient(hysteria.ClientOptions{
|
|
|
|
Context: ctx,
|
|
|
|
Dialer: outboundDialer,
|
|
|
|
Logger: logger,
|
|
|
|
ServerAddress: options.ServerOptions.Build(),
|
|
|
|
SendBPS: sendBps,
|
|
|
|
ReceiveBPS: receiveBps,
|
|
|
|
XPlusPassword: options.Obfs,
|
|
|
|
Password: password,
|
|
|
|
TLSConfig: tlsConfig,
|
|
|
|
UDPDisabled: !common.Contains(networkList, N.NetworkUDP),
|
|
|
|
|
|
|
|
ConnReceiveWindow: options.ReceiveWindowConn,
|
|
|
|
StreamReceiveWindow: options.ReceiveWindow,
|
|
|
|
DisableMTUDiscovery: options.DisableMTUDiscovery,
|
|
|
|
})
|
2023-08-08 16:14:03 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-11-02 00:39:02 +08:00
|
|
|
return &Outbound{
|
|
|
|
Adapter: outbound.NewAdapterWithDialerOptions(C.TypeHysteria, networkList, tag, options.DialerOptions),
|
|
|
|
logger: logger,
|
|
|
|
client: client,
|
2022-08-19 15:42:57 +08:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-11-02 00:39:02 +08:00
|
|
|
func (h *Outbound) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
|
2022-08-19 15:42:57 +08:00
|
|
|
switch N.NetworkName(network) {
|
|
|
|
case N.NetworkTCP:
|
|
|
|
h.logger.InfoContext(ctx, "outbound connection to ", destination)
|
2023-10-21 12:00:00 +08:00
|
|
|
return h.client.DialConn(ctx, destination)
|
2022-08-19 15:42:57 +08:00
|
|
|
case N.NetworkUDP:
|
|
|
|
conn, err := h.ListenPacket(ctx, destination)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-10-21 12:00:00 +08:00
|
|
|
return bufio.NewBindPacketConn(conn, destination), nil
|
2022-08-19 15:42:57 +08:00
|
|
|
default:
|
|
|
|
return nil, E.New("unsupported network: ", network)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-02 00:39:02 +08:00
|
|
|
func (h *Outbound) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
|
2022-08-19 15:42:57 +08:00
|
|
|
h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
|
2023-10-21 12:00:00 +08:00
|
|
|
return h.client.ListenPacket(ctx, destination)
|
2022-08-19 15:42:57 +08:00
|
|
|
}
|
|
|
|
|
2024-11-02 00:39:02 +08:00
|
|
|
func (h *Outbound) InterfaceUpdated() {
|
2024-07-07 15:45:50 +08:00
|
|
|
h.client.CloseWithError(E.New("network changed"))
|
2023-10-21 12:00:00 +08:00
|
|
|
}
|
|
|
|
|
2024-11-02 00:39:02 +08:00
|
|
|
func (h *Outbound) Close() error {
|
2023-10-21 12:00:00 +08:00
|
|
|
return h.client.CloseWithError(os.ErrClosed)
|
|
|
|
}
|