Compare commits

..
1 Commits
Author SHA1 Message Date
Fangliding a93737452e Print domain for lookup fail 2026-11-26 21:35:16 +08:00
2 changed files with 21 additions and 83 deletions
+2 -1
View File
@@ -255,10 +255,11 @@ func DialSystem(ctx context.Context, dest net.Destination, sockopt *SocketConfig
}
ips, err := LookupForIP(dest.Address.Domain(), finalStrategy, src)
if err != nil {
errors.LogErrorInner(ctx, err, "failed to resolve ip")
if sockopt.DomainStrategy.ForceIP() {
errors.LogErrorInner(ctx, err, "failed to resolve ip for: ", dest.Address.Domain())
return nil, err
}
errors.LogInfoInner(ctx, err, "failed to resolve ip for: ", dest.Address.Domain(), ", but continue due to USEIP* strategy")
} else if sockopt.HappyEyeballs == nil || sockopt.HappyEyeballs.TryDelayMs == 0 || sockopt.HappyEyeballs.MaxConcurrentTry == 0 || len(ips) < 2 || len(sockopt.DialerProxy) > 0 || dest.Network != net.Network_TCP {
dest.Address = net.IPAddress(ips[dice.Roll(len(ips))])
errors.LogInfo(ctx, "replace destination with "+dest.String())
+19 -82
View File
@@ -5,7 +5,6 @@ import (
_ "embed"
"encoding/base64"
"io"
"sync/atomic"
"time"
"github.com/gorilla/websocket"
@@ -25,7 +24,7 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me
if streamSettings.ProtocolSettings.(*Config).Ed > 0 {
ctx, cancel := context.WithCancel(ctx)
conn = &delayDialConn{
dialed: make(chan bool),
dialed: make(chan bool, 1),
cancel: cancel,
ctx: ctx,
dest: dest,
@@ -159,8 +158,8 @@ func dialWebSocket(ctx context.Context, dest net.Destination, streamSettings *in
}
type delayDialConn struct {
conn atomic.Value // net.Conn
closed atomic.Bool
net.Conn
closed bool
dialed chan bool
cancel context.CancelFunc
ctx context.Context
@@ -172,124 +171,62 @@ type delayDialConn struct {
// Without this method, Go promotes LocalAddr from the embedded net.Conn; the
// embedded interface is nil before the first Write, so the promoted call panics.
func (d *delayDialConn) LocalAddr() net.Addr {
conn := d.conn.Load()
if conn == nil {
if d.Conn == nil {
return nil
}
return (conn.(net.Conn)).LocalAddr()
return d.Conn.LocalAddr()
}
// RemoteAddr returns nil until the deferred WebSocket dial has completed.
// See LocalAddr for why an explicit method is required here.
func (d *delayDialConn) RemoteAddr() net.Addr {
conn := d.conn.Load()
if conn == nil {
if d.Conn == nil {
return nil
}
return (conn.(net.Conn)).RemoteAddr()
return d.Conn.RemoteAddr()
}
func (d *delayDialConn) Write(b []byte) (int, error) {
if d.closed.Load() {
if d.closed {
return 0, io.ErrClosedPipe
}
conn := d.conn.Load()
if conn == nil {
if d.Conn == nil {
ed := b
if len(ed) > int(d.streamSettings.ProtocolSettings.(*Config).Ed) {
ed = nil
}
c, err := dialWebSocket(d.ctx, d.dest, d.streamSettings, ed)
if err != nil {
var err error
if d.Conn, err = dialWebSocket(d.ctx, d.dest, d.streamSettings, ed); err != nil {
d.Close()
return 0, errors.New("failed to dial WebSocket").Base(err)
}
d.conn.Store(c)
// someone close the conn after dial success
if d.closed.Load() {
d.Close()
return 0, io.ErrClosedPipe
}
conn = c
close(d.dialed)
d.dialed <- true
if ed != nil {
return len(ed), nil
}
}
return (conn.(net.Conn)).Write(b)
return d.Conn.Write(b)
}
func (d *delayDialConn) Read(b []byte) (int, error) {
if d.closed.Load() {
if d.closed {
return 0, io.ErrClosedPipe
}
conn := d.conn.Load()
if conn == nil {
if d.Conn == nil {
select {
case <-d.ctx.Done():
return 0, io.ErrUnexpectedEOF
case <-d.dialed:
}
conn = d.conn.Load()
}
return (conn.(net.Conn)).Read(b)
}
func (d *delayDialConn) SetDeadline(t time.Time) error {
if d.closed.Load() {
return io.ErrClosedPipe
}
conn := d.conn.Load()
if conn == nil {
d.setDelayedDeadline(t, d.SetDeadline)
return nil
}
return (conn.(net.Conn)).SetDeadline(t)
}
func (d *delayDialConn) SetReadDeadline(t time.Time) error {
if d.closed.Load() {
return io.ErrClosedPipe
}
conn := d.conn.Load()
if conn == nil {
d.setDelayedDeadline(t, d.SetReadDeadline)
return nil
}
return (conn.(net.Conn)).SetReadDeadline(t)
}
func (d *delayDialConn) SetWriteDeadline(t time.Time) error {
if d.closed.Load() {
return io.ErrClosedPipe
}
conn := d.conn.Load()
if conn == nil {
d.setDelayedDeadline(t, d.SetWriteDeadline)
return nil
}
return (conn.(net.Conn)).SetWriteDeadline(t)
}
func (d *delayDialConn) setDelayedDeadline(t time.Time, f func(time.Time) error) {
ctx, cancel := context.WithTimeout(d.ctx, time.Until(t))
go func() {
defer cancel()
select {
case <-d.dialed:
f(t)
case <-ctx.Done():
return
}
}()
return d.Conn.Read(b)
}
func (d *delayDialConn) Close() error {
d.closed.Store(true)
d.closed = true
d.cancel()
conn := d.conn.Load()
if conn == nil {
if d.Conn == nil {
return nil
}
return (conn.(net.Conn)).Close()
return d.Conn.Close()
}