Skip to content

Commit

Permalink
Handle Basic Auth before HTTP RoundTrip
Browse files Browse the repository at this point in the history
  • Loading branch information
dyhkwong committed Nov 18, 2024
1 parent 0a21cc9 commit 47dd034
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions quic/transport_http3.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func init() {
type HTTP3Transport struct {
name string
destination string
serverURL *url.URL
transport *http3.RoundTripper
}

Expand All @@ -45,6 +46,7 @@ func NewHTTP3Transport(options dns.TransportOptions) (*HTTP3Transport, error) {
return &HTTP3Transport{
name: options.Name,
destination: serverURL.String(),
serverURL: options.ServerURL,
transport: &http3.RoundTripper{
Dial: func(ctx context.Context, addr string, tlsCfg *tls.Config, cfg *quic.Config) (quic.EarlyConnection, error) {
destinationAddr := M.ParseSocksaddr(addr)
Expand Down Expand Up @@ -98,6 +100,10 @@ func (t *HTTP3Transport) Exchange(ctx context.Context, message *mDNS.Msg) (*mDNS
}
request.Header.Set("Content-Type", dns.MimeType)
request.Header.Set("Accept", dns.MimeType)
if t.serverURL.User != nil {
password, _ := t.serverURL.User.Password()
request.SetBasicAuth(t.serverURL.User.Username(), password)
}
response, err := t.transport.RoundTrip(request)
requestBuffer.Release()
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions quic/transport_quic.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ func (t *Transport) exchange(ctx context.Context, message *mDNS.Msg, conn quic.C
if err != nil {
return nil, err
}
_ = stream.Close()
buffer.Reset()
_, err = buffer.ReadFullFrom(stream, 2)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type TransportOptions struct {
Name string
Dialer N.Dialer
Address string
ServerURL *url.URL
ClientSubnet netip.Prefix
}

Expand All @@ -52,6 +53,7 @@ func CreateTransport(options TransportOptions) (Transport, error) {
if serverURL != nil {
scheme = serverURL.Scheme
}
options.ServerURL = serverURL
constructor = transports[scheme]
}
if constructor == nil {
Expand Down
7 changes: 7 additions & 0 deletions transport_https.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net"
"net/http"
"net/netip"
"net/url"
"os"

"github.com/sagernet/sing/common/buf"
Expand All @@ -24,6 +25,7 @@ var _ Transport = (*HTTPSTransport)(nil)
type HTTPSTransport struct {
name string
destination string
serverURL *url.URL
transport *http.Transport
}

Expand All @@ -37,6 +39,7 @@ func NewHTTPSTransport(options TransportOptions) *HTTPSTransport {
return &HTTPSTransport{
name: options.Name,
destination: options.Address,
serverURL: options.ServerURL,
transport: &http.Transport{
ForceAttemptHTTP2: true,
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
Expand Down Expand Up @@ -88,6 +91,10 @@ func (t *HTTPSTransport) Exchange(ctx context.Context, message *dns.Msg) (*dns.M
}
request.Header.Set("Content-Type", MimeType)
request.Header.Set("Accept", MimeType)
if t.serverURL.User != nil {
password, _ := t.serverURL.User.Password()
request.SetBasicAuth(t.serverURL.User.Username(), password)
}
response, err := t.transport.RoundTrip(request)
requestBuffer.Release()
if err != nil {
Expand Down

0 comments on commit 47dd034

Please sign in to comment.