2019-12-08 04:17:24 +00:00
|
|
|
package inbound
|
2018-07-25 16:04:59 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
2018-08-12 08:18:58 +00:00
|
|
|
"net/http"
|
2022-04-19 17:52:51 +00:00
|
|
|
"net/netip"
|
2018-07-25 16:04:59 +00:00
|
|
|
"strconv"
|
2020-06-11 03:10:08 +00:00
|
|
|
"strings"
|
2018-07-25 16:04:59 +00:00
|
|
|
|
2023-01-17 07:41:51 +00:00
|
|
|
"github.com/Dreamacro/clash/common/nnip"
|
2018-07-25 16:04:59 +00:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2021-05-13 14:18:49 +00:00
|
|
|
"github.com/Dreamacro/clash/transport/socks5"
|
2018-07-25 16:04:59 +00:00
|
|
|
)
|
|
|
|
|
2019-04-25 05:48:47 +00:00
|
|
|
func parseSocksAddr(target socks5.Addr) *C.Metadata {
|
2022-11-11 01:19:28 +00:00
|
|
|
metadata := &C.Metadata{}
|
2018-07-25 16:04:59 +00:00
|
|
|
|
|
|
|
switch target[0] {
|
2019-04-25 05:48:47 +00:00
|
|
|
case socks5.AtypDomainName:
|
2020-06-11 04:11:44 +00:00
|
|
|
// trim for FQDN
|
|
|
|
metadata.Host = strings.TrimRight(string(target[2:2+target[1]]), ".")
|
2023-08-09 05:51:02 +00:00
|
|
|
metadata.DstPort = uint16((int(target[2+target[1]]) << 8) | int(target[2+target[1]+1]))
|
2019-04-25 05:48:47 +00:00
|
|
|
case socks5.AtypIPv4:
|
2022-04-19 17:52:51 +00:00
|
|
|
metadata.DstIP = nnip.IpToAddr(net.IP(target[1 : 1+net.IPv4len]))
|
2023-08-09 05:51:02 +00:00
|
|
|
metadata.DstPort = uint16((int(target[1+net.IPv4len]) << 8) | int(target[1+net.IPv4len+1]))
|
2019-04-25 05:48:47 +00:00
|
|
|
case socks5.AtypIPv6:
|
2022-07-05 13:09:29 +00:00
|
|
|
ip6, _ := netip.AddrFromSlice(target[1 : 1+net.IPv6len])
|
|
|
|
metadata.DstIP = ip6.Unmap()
|
2023-08-09 05:51:02 +00:00
|
|
|
metadata.DstPort = uint16((int(target[1+net.IPv6len]) << 8) | int(target[1+net.IPv6len+1]))
|
2018-07-25 16:04:59 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 13:13:29 +00:00
|
|
|
return metadata
|
2018-07-25 16:04:59 +00:00
|
|
|
}
|
|
|
|
|
2018-09-30 04:25:52 +00:00
|
|
|
func parseHTTPAddr(request *http.Request) *C.Metadata {
|
2018-08-26 16:06:40 +00:00
|
|
|
host := request.URL.Hostname()
|
|
|
|
port := request.URL.Port()
|
|
|
|
if port == "" {
|
|
|
|
port = "80"
|
|
|
|
}
|
2018-07-25 16:04:59 +00:00
|
|
|
|
2020-06-11 03:10:08 +00:00
|
|
|
// trim FQDN (#737)
|
|
|
|
host = strings.TrimRight(host, ".")
|
|
|
|
|
2023-08-09 05:51:02 +00:00
|
|
|
var uint16Port uint16
|
|
|
|
if port, err := strconv.ParseUint(port, 10, 16); err == nil {
|
|
|
|
uint16Port = uint16(port)
|
|
|
|
}
|
|
|
|
|
2018-12-05 13:13:29 +00:00
|
|
|
metadata := &C.Metadata{
|
2022-11-11 01:19:28 +00:00
|
|
|
NetWork: C.TCP,
|
|
|
|
Host: host,
|
|
|
|
DstIP: netip.Addr{},
|
2023-08-09 05:51:02 +00:00
|
|
|
DstPort: uint16Port,
|
2018-07-25 16:04:59 +00:00
|
|
|
}
|
2018-12-05 13:13:29 +00:00
|
|
|
|
2022-04-19 17:52:51 +00:00
|
|
|
ip, err := netip.ParseAddr(host)
|
|
|
|
if err == nil {
|
2019-10-27 13:44:07 +00:00
|
|
|
metadata.DstIP = ip
|
2018-12-05 13:13:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return metadata
|
2018-07-25 16:04:59 +00:00
|
|
|
}
|