2022-08-19 07:42:57 +00:00
|
|
|
//go:build with_quic
|
|
|
|
|
|
|
|
package outbound
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
2023-10-21 04:00:00 +00:00
|
|
|
"os"
|
2022-08-19 07:42:57 +00:00
|
|
|
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
|
|
|
"github.com/sagernet/sing-box/common/dialer"
|
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"
|
|
|
|
"github.com/sagernet/sing/common/bufio"
|
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
|
|
N "github.com/sagernet/sing/common/network"
|
|
|
|
)
|
|
|
|
|
2022-11-06 02:36:19 +00:00
|
|
|
var (
|
2023-10-21 04:00:00 +00:00
|
|
|
_ adapter.Outbound = (*TUIC)(nil)
|
|
|
|
_ adapter.InterfaceUpdateListener = (*TUIC)(nil)
|
2022-11-06 02:36:19 +00:00
|
|
|
)
|
2022-08-19 07:42:57 +00:00
|
|
|
|
|
|
|
type Hysteria struct {
|
|
|
|
myOutboundAdapter
|
2023-10-21 04:00:00 +00:00
|
|
|
client *hysteria.Client
|
2022-08-19 07:42:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewHysteria(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.HysteriaOutboundOptions) (*Hysteria, error) {
|
2022-10-18 09:27:41 +00:00
|
|
|
options.UDPFragmentDefault = true
|
2022-08-19 07:42:57 +00:00
|
|
|
if options.TLS == nil || !options.TLS.Enabled {
|
2022-08-22 14:19:25 +00:00
|
|
|
return nil, C.ErrTLSRequired
|
2022-08-19 07:42:57 +00:00
|
|
|
}
|
2023-08-31 03:37:26 +00:00
|
|
|
tlsConfig, err := tls.NewClient(ctx, options.Server, common.PtrValueOrDefault(options.TLS))
|
2022-09-09 10:45:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-10-21 04:00:00 +00:00
|
|
|
outboundDialer, err := dialer.New(router, options.DialerOptions)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-08-19 07:42:57 +00:00
|
|
|
}
|
2023-10-21 04:00:00 +00:00
|
|
|
networkList := options.Network.Build()
|
|
|
|
var password string
|
|
|
|
if options.AuthString != "" {
|
|
|
|
password = options.AuthString
|
2022-08-19 07:42:57 +00:00
|
|
|
} else {
|
2023-10-21 04:00:00 +00:00
|
|
|
password = string(options.Auth)
|
2022-08-19 07:42:57 +00:00
|
|
|
}
|
2023-10-21 04:00:00 +00:00
|
|
|
var sendBps, receiveBps uint64
|
2022-08-19 07:42:57 +00:00
|
|
|
if len(options.Up) > 0 {
|
2023-10-21 04:00:00 +00:00
|
|
|
sendBps, err = humanize.ParseBytes(options.Up)
|
|
|
|
if err != nil {
|
|
|
|
return nil, E.Cause(err, "invalid up speed format: ", options.Up)
|
2022-08-19 07:42:57 +00:00
|
|
|
}
|
|
|
|
} else {
|
2023-10-21 04:00:00 +00:00
|
|
|
sendBps = uint64(options.UpMbps) * hysteria.MbpsToBps
|
2022-08-19 07:42:57 +00:00
|
|
|
}
|
|
|
|
if len(options.Down) > 0 {
|
2023-10-21 04:00:00 +00:00
|
|
|
receiveBps, err = humanize.ParseBytes(options.Down)
|
|
|
|
if receiveBps == 0 {
|
2022-08-19 07:42:57 +00:00
|
|
|
return nil, E.New("invalid down speed format: ", options.Down)
|
|
|
|
}
|
|
|
|
} else {
|
2023-10-21 04:00:00 +00: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 08:14:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-08-19 07:42:57 +00:00
|
|
|
return &Hysteria{
|
|
|
|
myOutboundAdapter: myOutboundAdapter{
|
2023-06-13 14:38:05 +00:00
|
|
|
protocol: C.TypeHysteria,
|
2023-10-21 04:00:00 +00:00
|
|
|
network: networkList,
|
2023-06-13 14:38:05 +00:00
|
|
|
router: router,
|
|
|
|
logger: logger,
|
|
|
|
tag: tag,
|
|
|
|
dependencies: withDialerDependency(options.DialerOptions),
|
2022-08-19 07:42:57 +00:00
|
|
|
},
|
2023-10-21 04:00:00 +00:00
|
|
|
client: client,
|
2022-08-19 07:42:57 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Hysteria) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
|
|
|
|
switch N.NetworkName(network) {
|
|
|
|
case N.NetworkTCP:
|
|
|
|
h.logger.InfoContext(ctx, "outbound connection to ", destination)
|
2023-10-21 04:00:00 +00:00
|
|
|
return h.client.DialConn(ctx, destination)
|
2022-08-19 07:42:57 +00:00
|
|
|
case N.NetworkUDP:
|
|
|
|
conn, err := h.ListenPacket(ctx, destination)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-10-21 04:00:00 +00:00
|
|
|
return bufio.NewBindPacketConn(conn, destination), nil
|
2022-08-19 07:42:57 +00:00
|
|
|
default:
|
|
|
|
return nil, E.New("unsupported network: ", network)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Hysteria) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
|
|
|
|
h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
|
2023-10-21 04:00:00 +00:00
|
|
|
return h.client.ListenPacket(ctx, destination)
|
2022-08-19 07:42:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Hysteria) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
|
|
|
|
return NewConnection(ctx, h, conn, metadata)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Hysteria) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
|
|
|
|
return NewPacketConnection(ctx, h, conn, metadata)
|
|
|
|
}
|
2023-10-21 04:00:00 +00:00
|
|
|
|
|
|
|
func (h *Hysteria) InterfaceUpdated() error {
|
|
|
|
return h.client.CloseWithError(E.New("network changed"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Hysteria) Close() error {
|
|
|
|
return h.client.CloseWithError(os.ErrClosed)
|
|
|
|
}
|