mirror of
https://gitclone.com/github.com/MetaCubeX/Clash.Meta
synced 2025-05-12 13:08:09 +08:00
fix: udp dial support ip4p (#1377)
This commit is contained in:
parent
8085c68b6d
commit
7a260f7bcf
@ -54,6 +54,8 @@ func resolveUDPAddr(ctx context.Context, network, address string, prefer C.DNSPr
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ip, port = resolver.LookupIP4P(ip, port)
|
||||||
return net.ResolveUDPAddr(network, net.JoinHostPort(ip.String(), port))
|
return net.ResolveUDPAddr(network, net.JoinHostPort(ip.String(), port))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,14 +7,12 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/component/keepalive"
|
"github.com/metacubex/mihomo/component/keepalive"
|
||||||
"github.com/metacubex/mihomo/component/resolver"
|
"github.com/metacubex/mihomo/component/resolver"
|
||||||
"github.com/metacubex/mihomo/log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -138,9 +136,7 @@ func GetTcpConcurrent() bool {
|
|||||||
|
|
||||||
func dialContext(ctx context.Context, network string, destination netip.Addr, port string, opt *option) (net.Conn, error) {
|
func dialContext(ctx context.Context, network string, destination netip.Addr, port string, opt *option) (net.Conn, error) {
|
||||||
var address string
|
var address string
|
||||||
if IP4PEnable {
|
destination, port = resolver.LookupIP4P(destination, port)
|
||||||
destination, port = lookupIP4P(destination, port)
|
|
||||||
}
|
|
||||||
address = net.JoinHostPort(destination.String(), port)
|
address = net.JoinHostPort(destination.String(), port)
|
||||||
|
|
||||||
netDialer := opt.netDialer
|
netDialer := opt.netDialer
|
||||||
@ -396,21 +392,3 @@ func NewDialer(options ...Option) Dialer {
|
|||||||
opt := applyOptions(options...)
|
opt := applyOptions(options...)
|
||||||
return Dialer{Opt: *opt}
|
return Dialer{Opt: *opt}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetIP4PEnable(enableIP4PConvert bool) {
|
|
||||||
IP4PEnable = enableIP4PConvert
|
|
||||||
}
|
|
||||||
|
|
||||||
// kanged from https://github.com/heiher/frp/blob/ip4p/client/ip4p.go
|
|
||||||
|
|
||||||
func lookupIP4P(addr netip.Addr, port string) (netip.Addr, string) {
|
|
||||||
ip := addr.AsSlice()
|
|
||||||
if ip[0] == 0x20 && ip[1] == 0x01 &&
|
|
||||||
ip[2] == 0x00 && ip[3] == 0x00 {
|
|
||||||
addr = netip.AddrFrom4([4]byte{ip[12], ip[13], ip[14], ip[15]})
|
|
||||||
port = strconv.Itoa(int(ip[10])<<8 + int(ip[11]))
|
|
||||||
log.Debugln("Convert IP4P address %s to %s", ip, net.JoinHostPort(addr.String(), port))
|
|
||||||
return addr, port
|
|
||||||
}
|
|
||||||
return addr, port
|
|
||||||
}
|
|
||||||
|
37
component/resolver/ip4p.go
Normal file
37
component/resolver/ip4p.go
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
package resolver
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
"net/netip"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/metacubex/mihomo/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ip4PEnable bool
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetIP4PEnable() bool {
|
||||||
|
return ip4PEnable
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetIP4PEnable(enableIP4PConvert bool) {
|
||||||
|
ip4PEnable = enableIP4PConvert
|
||||||
|
}
|
||||||
|
|
||||||
|
// kanged from https://github.com/heiher/frp/blob/ip4p/client/ip4p.go
|
||||||
|
|
||||||
|
func LookupIP4P(addr netip.Addr, port string) (netip.Addr, string) {
|
||||||
|
if ip4PEnable {
|
||||||
|
ip := addr.AsSlice()
|
||||||
|
if ip[0] == 0x20 && ip[1] == 0x01 &&
|
||||||
|
ip[2] == 0x00 && ip[3] == 0x00 {
|
||||||
|
addr = netip.AddrFrom4([4]byte{ip[12], ip[13], ip[14], ip[15]})
|
||||||
|
port = strconv.Itoa(int(ip[10])<<8 + int(ip[11]))
|
||||||
|
log.Debugln("Convert IP4P address %s to %s", ip, net.JoinHostPort(addr.String(), port))
|
||||||
|
return addr, port
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return addr, port
|
||||||
|
}
|
@ -221,7 +221,7 @@ func updateExperimental(c *config.Experimental) {
|
|||||||
if c.QUICGoDisableECN {
|
if c.QUICGoDisableECN {
|
||||||
_ = os.Setenv("QUIC_GO_DISABLE_ECN", strconv.FormatBool(true))
|
_ = os.Setenv("QUIC_GO_DISABLE_ECN", strconv.FormatBool(true))
|
||||||
}
|
}
|
||||||
dialer.GetIP4PEnable(c.IP4PEnable)
|
resolver.SetIP4PEnable(c.IP4PEnable)
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateNTP(c *config.NTP) {
|
func updateNTP(c *config.NTP) {
|
||||||
|
Loading…
Reference in New Issue
Block a user