Add dial parallel for outbound dialer

This commit is contained in:
世界
2022-07-08 12:58:43 +08:00
parent d45007b501
commit 3699a57847
17 changed files with 253 additions and 39 deletions

View File

@@ -4,6 +4,7 @@ import (
"context"
"net"
"net/netip"
"strings"
"time"
"github.com/sagernet/sing/common"
@@ -71,11 +72,14 @@ func (c *Client) Exchange(ctx context.Context, transport adapter.DNSTransport, m
if !c.disableCache {
c.storeCache(question, response)
}
return message, err
return response, err
}
func (c *Client) Lookup(ctx context.Context, transport adapter.DNSTransport, domain string, strategy C.DomainStrategy) ([]netip.Addr, error) {
dnsName, err := dnsmessage.NewName(domain)
if strings.HasPrefix(domain, ".") {
domain = domain[:len(domain)-1]
}
dnsName, err := dnsmessage.NewName(domain + ".")
if err != nil {
return nil, wrapError(err)
}

View File

@@ -22,8 +22,15 @@ func NewTransport(ctx context.Context, dialer N.Dialer, logger log.Logger, addre
}
host := serverURL.Hostname()
port := serverURL.Port()
if port == "" {
port = "53"
switch serverURL.Scheme {
case "tls":
if port == "" {
port = "853"
}
default:
if port == "" {
port = "53"
}
}
destination := M.ParseSocksaddrHostPortStr(host, port)
switch serverURL.Scheme {

View File

@@ -77,10 +77,9 @@ func (t *TCPTransport) offer() (*dnsConnection, error) {
func (t *TCPTransport) newConnection(conn *dnsConnection) {
defer close(conn.done)
defer conn.Close()
ctx, cancel := context.WithCancel(t.ctx)
err := task.Any(t.ctx, func() error {
err := task.Any(t.ctx, func(ctx context.Context) error {
return t.loopIn(conn)
}, func() error {
}, func(ctx context.Context) error {
select {
case <-ctx.Done():
return nil
@@ -88,7 +87,6 @@ func (t *TCPTransport) newConnection(conn *dnsConnection) {
return os.ErrClosed
}
})
cancel()
conn.err = err
if err != nil {
t.logger.Debug("connection closed: ", err)

View File

@@ -85,10 +85,9 @@ func (t *TLSTransport) offer(ctx context.Context) (*dnsConnection, error) {
func (t *TLSTransport) newConnection(conn *dnsConnection) {
defer close(conn.done)
defer conn.Close()
ctx, cancel := context.WithCancel(t.ctx)
err := task.Any(t.ctx, func() error {
err := task.Any(t.ctx, func(ctx context.Context) error {
return t.loopIn(conn)
}, func() error {
}, func(ctx context.Context) error {
select {
case <-ctx.Done():
return nil
@@ -96,7 +95,6 @@ func (t *TLSTransport) newConnection(conn *dnsConnection) {
return os.ErrClosed
}
})
cancel()
conn.err = err
if err != nil {
t.logger.Debug("connection closed: ", err)

View File

@@ -73,10 +73,9 @@ func (t *UDPTransport) offer() (*dnsConnection, error) {
func (t *UDPTransport) newConnection(conn *dnsConnection) {
defer close(conn.done)
defer conn.Close()
ctx, cancel := context.WithCancel(t.ctx)
err := task.Any(t.ctx, func() error {
err := task.Any(t.ctx, func(ctx context.Context) error {
return t.loopIn(conn)
}, func() error {
}, func(ctx context.Context) error {
select {
case <-ctx.Done():
return nil
@@ -84,7 +83,6 @@ func (t *UDPTransport) newConnection(conn *dnsConnection) {
return os.ErrClosed
}
})
cancel()
conn.err = err
if err != nil {
t.logger.Debug("connection closed: ", err)