Skip to content

Commit 4b15568

Browse files
committed
chore: cleanup metadata code
1 parent cac2bf7 commit 4b15568

File tree

5 files changed

+40
-23
lines changed

5 files changed

+40
-23
lines changed

adapter/outbound/util.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,19 @@ import (
1717
func serializesSocksAddr(metadata *C.Metadata) []byte {
1818
var buf [][]byte
1919
addrType := metadata.AddrType()
20-
aType := uint8(addrType)
2120
p := uint(metadata.DstPort)
2221
port := []byte{uint8(p >> 8), uint8(p & 0xff)}
2322
switch addrType {
24-
case socks5.AtypDomainName:
23+
case C.AtypDomainName:
2524
lenM := uint8(len(metadata.Host))
2625
host := []byte(metadata.Host)
27-
buf = [][]byte{{aType, lenM}, host, port}
28-
case socks5.AtypIPv4:
26+
buf = [][]byte{{socks5.AtypDomainName, lenM}, host, port}
27+
case C.AtypIPv4:
2928
host := metadata.DstIP.AsSlice()
30-
buf = [][]byte{{aType}, host, port}
31-
case socks5.AtypIPv6:
29+
buf = [][]byte{{socks5.AtypIPv4}, host, port}
30+
case C.AtypIPv6:
3231
host := metadata.DstIP.AsSlice()
33-
buf = [][]byte{{aType}, host, port}
32+
buf = [][]byte{{socks5.AtypIPv6}, host, port}
3433
}
3534
return bytes.Join(buf, nil)
3635
}

adapter/outbound/vless.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
tlsC "github.com/metacubex/mihomo/component/tls"
2323
C "github.com/metacubex/mihomo/constant"
2424
"github.com/metacubex/mihomo/transport/gun"
25-
"github.com/metacubex/mihomo/transport/socks5"
2625
"github.com/metacubex/mihomo/transport/vless"
2726
"github.com/metacubex/mihomo/transport/vmess"
2827

@@ -397,15 +396,15 @@ func parseVlessAddr(metadata *C.Metadata, xudp bool) *vless.DstAddr {
397396
var addrType byte
398397
var addr []byte
399398
switch metadata.AddrType() {
400-
case socks5.AtypIPv4:
399+
case C.AtypIPv4:
401400
addrType = vless.AtypIPv4
402401
addr = make([]byte, net.IPv4len)
403402
copy(addr[:], metadata.DstIP.AsSlice())
404-
case socks5.AtypIPv6:
403+
case C.AtypIPv6:
405404
addrType = vless.AtypIPv6
406405
addr = make([]byte, net.IPv6len)
407406
copy(addr[:], metadata.DstIP.AsSlice())
408-
case socks5.AtypDomainName:
407+
case C.AtypDomainName:
409408
addrType = vless.AtypDomainName
410409
addr = make([]byte, len(metadata.Host)+1)
411410
addr[0] = byte(len(metadata.Host))

constant/metadata.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@ import (
66
"net"
77
"net/netip"
88
"strconv"
9+
)
910

10-
"github.com/metacubex/mihomo/transport/socks5"
11+
// SOCKS address types as defined in RFC 1928 section 5.
12+
const (
13+
AtypIPv4 AddrType = 1
14+
AtypDomainName AddrType = 3
15+
AtypIPv6 AddrType = 4
1116
)
1217

13-
// Socks addr type
1418
const (
1519
TCP NetWork = iota
1620
UDP
@@ -37,6 +41,21 @@ const (
3741
INNER
3842
)
3943

44+
type AddrType byte
45+
46+
func (a AddrType) String() string {
47+
switch a {
48+
case AtypIPv4:
49+
return "IPv4"
50+
case AtypDomainName:
51+
return "DomainName"
52+
case AtypIPv6:
53+
return "IPv6"
54+
default:
55+
return "Unknown"
56+
}
57+
}
58+
4059
type NetWork int
4160

4261
func (n NetWork) String() string {
@@ -207,14 +226,14 @@ func (m *Metadata) SourceValid() bool {
207226
return m.SrcPort != 0 && m.SrcIP.IsValid()
208227
}
209228

210-
func (m *Metadata) AddrType() int {
229+
func (m *Metadata) AddrType() AddrType {
211230
switch true {
212231
case m.Host != "" || !m.DstIP.IsValid():
213-
return socks5.AtypDomainName
232+
return AtypDomainName
214233
case m.DstIP.Is4():
215-
return socks5.AtypIPv4
234+
return AtypIPv4
216235
default:
217-
return socks5.AtypIPv6
236+
return AtypIPv6
218237
}
219238
}
220239

transport/tuic/v4/protocol.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -444,13 +444,13 @@ func NewAddress(metadata *C.Metadata) Address {
444444
var addrType byte
445445
var addr []byte
446446
switch metadata.AddrType() {
447-
case socks5.AtypIPv4:
447+
case C.AtypIPv4:
448448
addrType = AtypIPv4
449449
addr = metadata.DstIP.AsSlice()
450-
case socks5.AtypIPv6:
450+
case C.AtypIPv6:
451451
addrType = AtypIPv6
452452
addr = metadata.DstIP.AsSlice()
453-
case socks5.AtypDomainName:
453+
case C.AtypDomainName:
454454
addrType = AtypDomainName
455455
addr = make([]byte, len(metadata.Host)+1)
456456
addr[0] = byte(len(metadata.Host))

transport/tuic/v5/protocol.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -423,13 +423,13 @@ func NewAddress(metadata *C.Metadata) Address {
423423
var addrType byte
424424
var addr []byte
425425
switch metadata.AddrType() {
426-
case socks5.AtypIPv4:
426+
case C.AtypIPv4:
427427
addrType = AtypIPv4
428428
addr = metadata.DstIP.AsSlice()
429-
case socks5.AtypIPv6:
429+
case C.AtypIPv6:
430430
addrType = AtypIPv6
431431
addr = metadata.DstIP.AsSlice()
432-
case socks5.AtypDomainName:
432+
case C.AtypDomainName:
433433
addrType = AtypDomainName
434434
addr = make([]byte, len(metadata.Host)+1)
435435
addr[0] = byte(len(metadata.Host))

0 commit comments

Comments
 (0)