@@ -3,6 +3,8 @@ package hysteria2
33import (
44 "context"
55 "net"
6+ "net/http"
7+ "net/netip"
68 "net/url"
79 "os"
810 "time"
@@ -18,12 +20,14 @@ import (
1820 qtls "github.com/sagernet/sing-quic"
1921 "github.com/sagernet/sing-quic/hysteria"
2022 "github.com/sagernet/sing-quic/hysteria2"
23+ "github.com/sagernet/sing-quic/hysteria2/realm"
2124 "github.com/sagernet/sing/common"
2225 "github.com/sagernet/sing/common/bufio"
2326 E "github.com/sagernet/sing/common/exceptions"
2427 "github.com/sagernet/sing/common/logger"
2528 M "github.com/sagernet/sing/common/metadata"
2629 N "github.com/sagernet/sing/common/network"
30+ "github.com/sagernet/sing/service"
2731)
2832
2933func RegisterOutbound (registry * outbound.Registry ) {
@@ -66,13 +70,43 @@ func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextL
6670 return nil , E .New ("unknown obfs type: " , options .Obfs .Type )
6771 }
6872 }
69- outboundDialer , err := dialer .New (ctx , options .DialerOptions , options .ServerIsDomain ())
73+ outboundDialer , err := dialer .NewWithOptions (dialer.Options {
74+ Context : ctx ,
75+ Options : options .DialerOptions ,
76+ RemoteIsDomain : options .ServerIsDomain (),
77+ })
7078 if err != nil {
7179 return nil , err
7280 }
73- realmOptions , err := buildRealmOptions (ctx , logger , options .Realm )
74- if err != nil {
75- return nil , err
81+ var realmOptions * realm.Options
82+ if options .Realm != nil {
83+ queryOptions , err := adapter .DNSQueryOptionsFrom (ctx , options .DialerOptions .DomainResolver )
84+ if err != nil {
85+ return nil , err
86+ }
87+ httpClientTransport , err := service.FromContext [adapter.HTTPClientManager ](ctx ).ResolveTransport (ctx , logger , common .PtrValueOrDefault (options .Realm .HTTPClient ))
88+ if err != nil {
89+ return nil , E .Cause (err , "create realm http client" )
90+ }
91+ dnsRouter := service.FromContext [adapter.DNSRouter ](ctx )
92+ realmOptions = & realm.Options {
93+ ServerURL : options .Realm .ServerURL ,
94+ Token : options .Realm .Token ,
95+ RealmID : options .Realm .RealmID ,
96+ STUNServers : options .Realm .STUNServers ,
97+ HTTPClient : & http.Client {Transport : httpClientTransport },
98+ Resolver : func (ctx context.Context , host string , ipv4 , ipv6 bool ) ([]netip.Addr , error ) {
99+ dnsOptions := queryOptions
100+ switch {
101+ case ipv4 && ! ipv6 :
102+ dnsOptions .Strategy = C .DomainStrategyIPv4Only
103+ case ! ipv4 && ipv6 :
104+ dnsOptions .Strategy = C .DomainStrategyIPv6Only
105+ }
106+ return dnsRouter .Lookup (ctx , host , dnsOptions )
107+ },
108+ Logger : logger ,
109+ }
76110 }
77111 networkList := options .Network .Build ()
78112 client , err := hysteria2 .NewClient (hysteria2.ClientOptions {
0 commit comments