Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Basic Authentication before HTTP RoundTrip #17

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/.idea/
/vendor/
.DS_Store
161 changes: 81 additions & 80 deletions client.go

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions client_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package dns

import "net/netip"

type QueryOptions struct {
Strategy DomainStrategy
DisableCache bool
RewriteTTL *uint32
ClientSubnet netip.Prefix
}
8 changes: 6 additions & 2 deletions dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ func (d *DialerWrapper) DialContext(ctx context.Context, network string, destina
if destination.IsIP() {
return d.dialer.DialContext(ctx, network, destination)
}
addresses, err := d.client.Lookup(ctx, d.transport, destination.Fqdn, d.strategy)
addresses, err := d.client.Lookup(ctx, d.transport, destination.Fqdn, QueryOptions{
Strategy: d.strategy,
})
if err != nil {
return nil, err
}
Expand All @@ -36,7 +38,9 @@ func (d *DialerWrapper) ListenPacket(ctx context.Context, destination M.Socksadd
if destination.IsIP() {
return d.dialer.ListenPacket(ctx, destination)
}
addresses, err := d.client.Lookup(ctx, d.transport, destination.Fqdn, d.strategy)
addresses, err := d.client.Lookup(ctx, d.transport, destination.Fqdn, QueryOptions{
Strategy: d.strategy,
})
if err != nil {
return nil, err
}
Expand Down
56 changes: 0 additions & 56 deletions extensions.go

This file was deleted.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.20
require (
github.com/miekg/dns v1.1.62
github.com/sagernet/quic-go v0.47.0-beta.2
github.com/sagernet/sing v0.5.0
github.com/sagernet/sing v0.6.0-alpha.10
github.com/stretchr/testify v1.9.0
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ github.com/quic-go/qtls-go1-20 v0.4.1 h1:D33340mCNDAIKBqXuAvexTNMUByrYmFYVfKfDN5
github.com/quic-go/qtls-go1-20 v0.4.1/go.mod h1:X9Nh97ZL80Z+bX/gUXMbipO6OxdiDi58b/fMC9mAL+k=
github.com/sagernet/quic-go v0.47.0-beta.2 h1:1tCGWFOSaXIeuQaHrwOMJIYvlupjTcaVInGQw5ArULU=
github.com/sagernet/quic-go v0.47.0-beta.2/go.mod h1:bLVKvElSEMNv7pu7SZHscW02TYigzQ5lQu3Nh4wNh8Q=
github.com/sagernet/sing v0.5.0 h1:soo2wVwLcieKWWKIksFNK6CCAojUgAppqQVwyRYGkEM=
github.com/sagernet/sing v0.5.0/go.mod h1:ARkL0gM13/Iv5VCZmci/NuoOlePoIsW0m7BWfln/Hak=
github.com/sagernet/sing v0.6.0-alpha.10 h1:gIUiFof6SDDcAg3m3pUOshOPZBLC7z9VCgDt4Tzs24g=
github.com/sagernet/sing v0.6.0-alpha.10/go.mod h1:ARkL0gM13/Iv5VCZmci/NuoOlePoIsW0m7BWfln/Hak=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
Expand Down
16 changes: 10 additions & 6 deletions quic/transport_http3.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ func init() {
}

type HTTP3Transport struct {
name string
destination string
transport *http3.RoundTripper
name string
serverURL *url.URL
transport *http3.RoundTripper
}

func NewHTTP3Transport(options dns.TransportOptions) (*HTTP3Transport, error) {
Expand All @@ -43,8 +43,8 @@ func NewHTTP3Transport(options dns.TransportOptions) (*HTTP3Transport, error) {
}
serverURL.Scheme = "https"
return &HTTP3Transport{
name: options.Name,
destination: serverURL.String(),
name: options.Name,
serverURL: 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 @@ -91,13 +91,17 @@ func (t *HTTP3Transport) Exchange(ctx context.Context, message *mDNS.Msg) (*mDNS
requestBuffer.Release()
return nil, err
}
request, err := http.NewRequestWithContext(ctx, http.MethodPost, t.destination, bytes.NewReader(rawMessage))
request, err := http.NewRequestWithContext(ctx, http.MethodPost, t.serverURL.String(), bytes.NewReader(rawMessage))
if err != nil {
requestBuffer.Release()
return nil, err
}
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
27 changes: 18 additions & 9 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 @@ -22,21 +23,25 @@ const MimeType = "application/dns-message"
var _ Transport = (*HTTPSTransport)(nil)

type HTTPSTransport struct {
name string
destination string
transport *http.Transport
name string
serverURL *url.URL
transport *http.Transport
}

func init() {
RegisterTransport([]string{"https"}, func(options TransportOptions) (Transport, error) {
return NewHTTPSTransport(options), nil
return NewHTTPSTransport(options)
})
}

func NewHTTPSTransport(options TransportOptions) *HTTPSTransport {
func NewHTTPSTransport(options TransportOptions) (*HTTPSTransport, error) {
serverURL, err := url.Parse(options.Address)
if err != nil {
return nil, err
}
return &HTTPSTransport{
name: options.Name,
destination: options.Address,
name: options.Name,
serverURL: serverURL,
transport: &http.Transport{
ForceAttemptHTTP2: true,
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
Expand All @@ -46,7 +51,7 @@ func NewHTTPSTransport(options TransportOptions) *HTTPSTransport {
NextProtos: []string{"dns"},
},
},
}
}, nil
}

func (t *HTTPSTransport) Name() string {
Expand Down Expand Up @@ -81,13 +86,17 @@ func (t *HTTPSTransport) Exchange(ctx context.Context, message *dns.Msg) (*dns.M
requestBuffer.Release()
return nil, err
}
request, err := http.NewRequestWithContext(ctx, http.MethodPost, t.destination, bytes.NewReader(rawMessage))
request, err := http.NewRequestWithContext(ctx, http.MethodPost, t.serverURL.String(), bytes.NewReader(rawMessage))
if err != nil {
requestBuffer.Release()
return nil, err
}
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
4 changes: 3 additions & 1 deletion transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ func TestTransports(t *testing.T) {
client := dns.NewClient(dns.ClientOptions{
Logger: logger.NOP(),
})
addresses, err := client.Lookup(context.Background(), transport, "cloudflare.com", dns.DomainStrategyAsIS)
addresses, err := client.Lookup(context.Background(), transport, "cloudflare.com", dns.QueryOptions{
Strategy: dns.DomainStrategyUseIPv4,
})
require.NoError(t, err)
require.NotEmpty(t, addresses)
})
Expand Down
Loading