github.com/metacubex/mihomo@v1.18.5/adapter/inbound/util.go (about) 1 package inbound 2 3 import ( 4 "net" 5 "net/http" 6 "net/netip" 7 "strconv" 8 "strings" 9 10 "github.com/metacubex/mihomo/common/nnip" 11 C "github.com/metacubex/mihomo/constant" 12 "github.com/metacubex/mihomo/transport/socks5" 13 ) 14 15 func parseSocksAddr(target socks5.Addr) *C.Metadata { 16 metadata := &C.Metadata{} 17 18 switch target[0] { 19 case socks5.AtypDomainName: 20 // trim for FQDN 21 metadata.Host = strings.TrimRight(string(target[2:2+target[1]]), ".") 22 metadata.DstPort = uint16((int(target[2+target[1]]) << 8) | int(target[2+target[1]+1])) 23 case socks5.AtypIPv4: 24 metadata.DstIP = nnip.IpToAddr(net.IP(target[1 : 1+net.IPv4len])) 25 metadata.DstPort = uint16((int(target[1+net.IPv4len]) << 8) | int(target[1+net.IPv4len+1])) 26 case socks5.AtypIPv6: 27 ip6, _ := netip.AddrFromSlice(target[1 : 1+net.IPv6len]) 28 metadata.DstIP = ip6.Unmap() 29 metadata.DstPort = uint16((int(target[1+net.IPv6len]) << 8) | int(target[1+net.IPv6len+1])) 30 } 31 32 return metadata 33 } 34 35 func parseHTTPAddr(request *http.Request) *C.Metadata { 36 host := request.URL.Hostname() 37 port := request.URL.Port() 38 if port == "" { 39 port = "80" 40 } 41 42 // trim FQDN (#737) 43 host = strings.TrimRight(host, ".") 44 45 var uint16Port uint16 46 if port, err := strconv.ParseUint(port, 10, 16); err == nil { 47 uint16Port = uint16(port) 48 } 49 50 metadata := &C.Metadata{ 51 NetWork: C.TCP, 52 Host: host, 53 DstIP: netip.Addr{}, 54 DstPort: uint16Port, 55 } 56 57 ip, err := netip.ParseAddr(host) 58 if err == nil { 59 metadata.DstIP = ip 60 } 61 62 return metadata 63 }