Skip to content

Commit c0f452b

Browse files
committed
chore: more unmap for 4in6 address
1 parent 6c9abe1 commit c0f452b

File tree

6 files changed

+24
-41
lines changed

6 files changed

+24
-41
lines changed

adapter/adapter.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ import (
77
"fmt"
88
"net"
99
"net/http"
10-
"net/netip"
1110
"net/url"
12-
"strconv"
1311
"strings"
1412
"time"
1513

@@ -316,15 +314,7 @@ func urlToMetadata(rawURL string) (addr C.Metadata, err error) {
316314
return
317315
}
318316
}
319-
uintPort, err := strconv.ParseUint(port, 10, 16)
320-
if err != nil {
321-
return
322-
}
323317

324-
addr = C.Metadata{
325-
Host: u.Hostname(),
326-
DstIP: netip.Addr{},
327-
DstPort: uint16(uintPort),
328-
}
318+
err = addr.SetRemoteAddress(net.JoinHostPort(u.Hostname(), port))
329319
return
330320
}

adapter/inbound/util.go

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"net"
55
"net/http"
66
"net/netip"
7-
"strconv"
87
"strings"
98

109
C "github.com/metacubex/mihomo/constant"
@@ -41,23 +40,8 @@ func parseHTTPAddr(request *http.Request) *C.Metadata {
4140
// trim FQDN (#737)
4241
host = strings.TrimRight(host, ".")
4342

44-
var uint16Port uint16
45-
if port, err := strconv.ParseUint(port, 10, 16); err == nil {
46-
uint16Port = uint16(port)
47-
}
48-
49-
metadata := &C.Metadata{
50-
NetWork: C.TCP,
51-
Host: host,
52-
DstIP: netip.Addr{},
53-
DstPort: uint16Port,
54-
}
55-
56-
ip, err := netip.ParseAddr(host)
57-
if err == nil {
58-
metadata.DstIP = ip
59-
}
60-
43+
metadata := &C.Metadata{}
44+
_ = metadata.SetRemoteAddress(net.JoinHostPort(host, port))
6145
return metadata
6246
}
6347

component/resolver/host.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,15 @@ func NewHostValue(value any) (HostValue, error) {
7777
isDomain = false
7878
for _, str := range valueArr {
7979
if ip, err := netip.ParseAddr(str); err == nil {
80-
ips = append(ips, ip)
80+
ips = append(ips, ip.Unmap())
8181
} else {
8282
return HostValue{}, err
8383
}
8484
}
8585
} else if len(valueArr) == 1 {
8686
host := valueArr[0]
8787
if ip, err := netip.ParseAddr(host); err == nil {
88-
ips = append(ips, ip)
88+
ips = append(ips, ip.Unmap())
8989
isDomain = false
9090
} else {
9191
domain = host

component/resolver/resolver.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"errors"
66
"fmt"
77
"net/netip"
8-
"strings"
98
"time"
109

1110
"github.com/metacubex/mihomo/common/utils"
@@ -68,7 +67,8 @@ func LookupIPv4WithResolver(ctx context.Context, host string, r Resolver) ([]net
6867

6968
ip, err := netip.ParseAddr(host)
7069
if err == nil {
71-
if ip.Is4() || ip.Is4In6() {
70+
ip = ip.Unmap()
71+
if ip.Is4() {
7272
return []netip.Addr{ip}, nil
7373
}
7474
return []netip.Addr{}, ErrIPVersion
@@ -117,7 +117,8 @@ func LookupIPv6WithResolver(ctx context.Context, host string, r Resolver) ([]net
117117
}
118118

119119
if ip, err := netip.ParseAddr(host); err == nil {
120-
if strings.Contains(host, ":") {
120+
ip = ip.Unmap()
121+
if ip.Is6() {
121122
return []netip.Addr{ip}, nil
122123
}
123124
return nil, ErrIPVersion
@@ -166,6 +167,7 @@ func LookupIPWithResolver(ctx context.Context, host string, r Resolver) ([]netip
166167
}
167168

168169
if ip, err := netip.ParseAddr(host); err == nil {
170+
ip = ip.Unmap()
169171
return []netip.Addr{ip}, nil
170172
}
171173

dns/resolver.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,8 @@ func (r *Resolver) ipExchange(ctx context.Context, m *D.Msg) (msg *D.Msg, err er
348348
func (r *Resolver) lookupIP(ctx context.Context, host string, dnsType uint16) (ips []netip.Addr, err error) {
349349
ip, err := netip.ParseAddr(host)
350350
if err == nil {
351-
isIPv4 := ip.Is4() || ip.Is4In6()
351+
ip = ip.Unmap()
352+
isIPv4 := ip.Is4()
352353
if dnsType == D.TypeAAAA && !isIPv4 {
353354
return []netip.Addr{ip}, nil
354355
} else if dnsType == D.TypeA && isIPv4 {

tunnel/tunnel.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -287,17 +287,21 @@ func isHandle(t C.Type) bool {
287287
return status == Running || (status == Inner && t == C.INNER)
288288
}
289289

290-
func needLookupIP(metadata *C.Metadata) bool {
291-
return resolver.MappingEnabled() && metadata.Host == "" && metadata.DstIP.IsValid()
292-
}
293-
294-
func preHandleMetadata(metadata *C.Metadata) error {
290+
func fixMetadata(metadata *C.Metadata) {
291+
// first unmap dstIP
292+
metadata.DstIP = metadata.DstIP.Unmap()
295293
// handle IP string on host
296294
if ip, err := netip.ParseAddr(metadata.Host); err == nil {
297-
metadata.DstIP = ip
295+
metadata.DstIP = ip.Unmap()
298296
metadata.Host = ""
299297
}
298+
}
300299

300+
func needLookupIP(metadata *C.Metadata) bool {
301+
return resolver.MappingEnabled() && metadata.Host == "" && metadata.DstIP.IsValid()
302+
}
303+
304+
func preHandleMetadata(metadata *C.Metadata) error {
301305
// preprocess enhanced-mode metadata
302306
if needLookupIP(metadata) {
303307
host, exist := resolver.FindHostByIP(metadata.DstIP)
@@ -365,6 +369,7 @@ func handleUDPConn(packet C.PacketAdapter) {
365369
log.Warnln("[Metadata] not valid: %#v", metadata)
366370
return
367371
}
372+
fixMetadata(metadata) // fix some metadata not set via metadata.SetRemoteAddr or metadata.SetRemoteAddress
368373

369374
if err := preHandleMetadata(metadata.Clone()); err != nil { // precheck without modify metadata
370375
packet.Drop()
@@ -449,6 +454,7 @@ func handleTCPConn(connCtx C.ConnContext) {
449454
log.Warnln("[Metadata] not valid: %#v", metadata)
450455
return
451456
}
457+
fixMetadata(metadata) // fix some metadata not set via metadata.SetRemoteAddr or metadata.SetRemoteAddress
452458

453459
preHandleFailed := false
454460
if err := preHandleMetadata(metadata); err != nil {

0 commit comments

Comments
 (0)