Skip to content

Commit 7a260f7

Browse files
HiMetrewwqgtxx
authored andcommitted
fix: udp dial support ip4p (#1377)
1 parent 8085c68 commit 7a260f7

File tree

4 files changed

+41
-24
lines changed

4 files changed

+41
-24
lines changed

adapter/outbound/util.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ func resolveUDPAddr(ctx context.Context, network, address string, prefer C.DNSPr
5454
if err != nil {
5555
return nil, err
5656
}
57+
58+
ip, port = resolver.LookupIP4P(ip, port)
5759
return net.ResolveUDPAddr(network, net.JoinHostPort(ip.String(), port))
5860
}
5961

component/dialer/dialer.go

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@ import (
77
"net"
88
"net/netip"
99
"os"
10-
"strconv"
1110
"strings"
1211
"sync"
1312
"time"
1413

1514
"github.com/metacubex/mihomo/component/keepalive"
1615
"github.com/metacubex/mihomo/component/resolver"
17-
"github.com/metacubex/mihomo/log"
1816
)
1917

2018
const (
@@ -138,9 +136,7 @@ func GetTcpConcurrent() bool {
138136

139137
func dialContext(ctx context.Context, network string, destination netip.Addr, port string, opt *option) (net.Conn, error) {
140138
var address string
141-
if IP4PEnable {
142-
destination, port = lookupIP4P(destination, port)
143-
}
139+
destination, port = resolver.LookupIP4P(destination, port)
144140
address = net.JoinHostPort(destination.String(), port)
145141

146142
netDialer := opt.netDialer
@@ -396,21 +392,3 @@ func NewDialer(options ...Option) Dialer {
396392
opt := applyOptions(options...)
397393
return Dialer{Opt: *opt}
398394
}
399-
400-
func GetIP4PEnable(enableIP4PConvert bool) {
401-
IP4PEnable = enableIP4PConvert
402-
}
403-
404-
// kanged from https://github.com/heiher/frp/blob/ip4p/client/ip4p.go
405-
406-
func lookupIP4P(addr netip.Addr, port string) (netip.Addr, string) {
407-
ip := addr.AsSlice()
408-
if ip[0] == 0x20 && ip[1] == 0x01 &&
409-
ip[2] == 0x00 && ip[3] == 0x00 {
410-
addr = netip.AddrFrom4([4]byte{ip[12], ip[13], ip[14], ip[15]})
411-
port = strconv.Itoa(int(ip[10])<<8 + int(ip[11]))
412-
log.Debugln("Convert IP4P address %s to %s", ip, net.JoinHostPort(addr.String(), port))
413-
return addr, port
414-
}
415-
return addr, port
416-
}

component/resolver/ip4p.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package resolver
2+
3+
import (
4+
"net"
5+
"net/netip"
6+
"strconv"
7+
8+
"github.com/metacubex/mihomo/log"
9+
)
10+
11+
var (
12+
ip4PEnable bool
13+
)
14+
15+
func GetIP4PEnable() bool {
16+
return ip4PEnable
17+
}
18+
19+
func SetIP4PEnable(enableIP4PConvert bool) {
20+
ip4PEnable = enableIP4PConvert
21+
}
22+
23+
// kanged from https://github.com/heiher/frp/blob/ip4p/client/ip4p.go
24+
25+
func LookupIP4P(addr netip.Addr, port string) (netip.Addr, string) {
26+
if ip4PEnable {
27+
ip := addr.AsSlice()
28+
if ip[0] == 0x20 && ip[1] == 0x01 &&
29+
ip[2] == 0x00 && ip[3] == 0x00 {
30+
addr = netip.AddrFrom4([4]byte{ip[12], ip[13], ip[14], ip[15]})
31+
port = strconv.Itoa(int(ip[10])<<8 + int(ip[11]))
32+
log.Debugln("Convert IP4P address %s to %s", ip, net.JoinHostPort(addr.String(), port))
33+
return addr, port
34+
}
35+
}
36+
return addr, port
37+
}

hub/executor/executor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ func updateExperimental(c *config.Experimental) {
221221
if c.QUICGoDisableECN {
222222
_ = os.Setenv("QUIC_GO_DISABLE_ECN", strconv.FormatBool(true))
223223
}
224-
dialer.GetIP4PEnable(c.IP4PEnable)
224+
resolver.SetIP4PEnable(c.IP4PEnable)
225225
}
226226

227227
func updateNTP(c *config.NTP) {

0 commit comments

Comments
 (0)