Browse Source

fix: ipv6 connect error

pull/20/head v2.0.0
xjasonlyu 4 years ago
parent
commit
fcf9d280b8
  1. 8
      internal/dns/resolver.go
  2. 5
      internal/dns/server.go
  3. 14
      internal/proxy/socks5.go

8
internal/dns/resolver.go

@ -4,9 +4,15 @@ import (
"net"
"github.com/xjasonlyu/clash/component/dialer"
"github.com/xjasonlyu/clash/component/resolver"
)
func init() { /* use bound dialer to resolve DNS */
func init() {
// enable ipv6
resolver.DisableIPv6 = false
// use bound dialer to resolve DNS
net.DefaultResolver.PreferGo = true
net.DefaultResolver.Dial = dialer.DialContext
}

5
internal/dns/server.go

@ -31,7 +31,10 @@ func Start(dnsURL string, rawHosts []string) error {
return errors.New("unsupported scheme")
}
var serverAddr = u.Host
serverAddr := u.Host
if serverAddr == "" {
return nil
}
fakeIPRange := u.Query().Get("fake-ip-range")
if fakeIPRange == "" {

14
internal/proxy/socks5.go

@ -92,7 +92,7 @@ func (ss *Socks5) DialUDP(_ *adapter.Metadata) (_ net.PacketConn, err error) {
// zeros. RFC1928
var targetAddr socks5.Addr = []byte{socks5.AtypIPv4, 0, 0, 0, 0, 0, 0}
bindAddr, err := socks5.ClientHandshake(c, targetAddr, socks5.CmdUDPAssociate, user)
addr, err := socks5.ClientHandshake(c, targetAddr, socks5.CmdUDPAssociate, user)
if err != nil {
return nil, fmt.Errorf("client hanshake: %w", err)
}
@ -110,7 +110,17 @@ func (ss *Socks5) DialUDP(_ *adapter.Metadata) (_ net.PacketConn, err error) {
pc.Close()
}()
return &socksPacketConn{PacketConn: pc, rAddr: bindAddr.UDPAddr(), tcpConn: c}, nil
bindAddr := addr.UDPAddr()
if bindAddr.IP.IsUnspecified() {
udpAddr, err := resolveUDPAddr("udp", ss.Addr())
if err != nil {
return nil, err
}
bindAddr.IP = udpAddr.IP
}
return &socksPacketConn{PacketConn: pc, rAddr: bindAddr, tcpConn: c}, nil
}
type socksPacketConn struct {

Loading…
Cancel
Save