2019-12-08 12:17:24 +08:00
|
|
|
package inbound
|
2018-07-26 00:04:59 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
2018-08-12 16:18:58 +08:00
|
|
|
"net/http"
|
2018-07-26 00:04:59 +08:00
|
|
|
"strconv"
|
2020-06-11 11:10:08 +08:00
|
|
|
"strings"
|
2018-07-26 00:04:59 +08:00
|
|
|
|
2019-04-25 13:48:47 +08:00
|
|
|
"github.com/Dreamacro/clash/component/socks5"
|
2018-07-26 00:04:59 +08:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
)
|
|
|
|
|
2019-04-25 13:48:47 +08:00
|
|
|
func parseSocksAddr(target socks5.Addr) *C.Metadata {
|
2018-12-05 21:13:29 +08:00
|
|
|
metadata := &C.Metadata{
|
|
|
|
AddrType: int(target[0]),
|
|
|
|
}
|
2018-07-26 00:04:59 +08:00
|
|
|
|
|
|
|
switch target[0] {
|
2019-04-25 13:48:47 +08:00
|
|
|
case socks5.AtypDomainName:
|
2018-12-05 21:13:29 +08:00
|
|
|
metadata.Host = string(target[2 : 2+target[1]])
|
2019-05-09 21:00:29 +08:00
|
|
|
metadata.DstPort = strconv.Itoa((int(target[2+target[1]]) << 8) | int(target[2+target[1]+1]))
|
2019-04-25 13:48:47 +08:00
|
|
|
case socks5.AtypIPv4:
|
2018-12-05 21:13:29 +08:00
|
|
|
ip := net.IP(target[1 : 1+net.IPv4len])
|
2019-10-27 21:44:07 +08:00
|
|
|
metadata.DstIP = ip
|
2019-05-09 21:00:29 +08:00
|
|
|
metadata.DstPort = strconv.Itoa((int(target[1+net.IPv4len]) << 8) | int(target[1+net.IPv4len+1]))
|
2019-04-25 13:48:47 +08:00
|
|
|
case socks5.AtypIPv6:
|
2018-12-05 21:13:29 +08:00
|
|
|
ip := net.IP(target[1 : 1+net.IPv6len])
|
2019-10-27 21:44:07 +08:00
|
|
|
metadata.DstIP = ip
|
2019-05-09 21:00:29 +08:00
|
|
|
metadata.DstPort = strconv.Itoa((int(target[1+net.IPv6len]) << 8) | int(target[1+net.IPv6len+1]))
|
2018-07-26 00:04:59 +08:00
|
|
|
}
|
|
|
|
|
2018-12-05 21:13:29 +08:00
|
|
|
return metadata
|
2018-07-26 00:04:59 +08:00
|
|
|
}
|
|
|
|
|
2018-09-30 12:25:52 +08:00
|
|
|
func parseHTTPAddr(request *http.Request) *C.Metadata {
|
2018-08-27 00:06:40 +08:00
|
|
|
host := request.URL.Hostname()
|
|
|
|
port := request.URL.Port()
|
|
|
|
if port == "" {
|
|
|
|
port = "80"
|
|
|
|
}
|
2018-07-26 00:04:59 +08:00
|
|
|
|
2020-06-11 11:10:08 +08:00
|
|
|
// trim FQDN (#737)
|
|
|
|
host = strings.TrimRight(host, ".")
|
|
|
|
|
2018-12-05 21:13:29 +08:00
|
|
|
metadata := &C.Metadata{
|
2018-07-26 00:04:59 +08:00
|
|
|
NetWork: C.TCP,
|
2018-12-05 21:13:29 +08:00
|
|
|
AddrType: C.AtypDomainName,
|
2018-07-26 00:04:59 +08:00
|
|
|
Host: host,
|
2019-05-09 21:00:29 +08:00
|
|
|
DstIP: nil,
|
|
|
|
DstPort: port,
|
2018-07-26 00:04:59 +08:00
|
|
|
}
|
2018-12-05 21:13:29 +08:00
|
|
|
|
|
|
|
ip := net.ParseIP(host)
|
|
|
|
if ip != nil {
|
|
|
|
switch {
|
|
|
|
case ip.To4() == nil:
|
|
|
|
metadata.AddrType = C.AtypIPv6
|
|
|
|
default:
|
|
|
|
metadata.AddrType = C.AtypIPv4
|
|
|
|
}
|
2019-10-27 21:44:07 +08:00
|
|
|
metadata.DstIP = ip
|
2018-12-05 21:13:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return metadata
|
2018-07-26 00:04:59 +08:00
|
|
|
}
|
2019-02-02 21:03:13 +08:00
|
|
|
|
2019-10-27 21:44:07 +08:00
|
|
|
func parseAddr(addr string) (net.IP, string, error) {
|
2019-05-09 21:00:29 +08:00
|
|
|
host, port, err := net.SplitHostPort(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
2019-02-02 21:03:13 +08:00
|
|
|
}
|
2019-05-09 21:00:29 +08:00
|
|
|
|
|
|
|
ip := net.ParseIP(host)
|
2019-10-27 21:44:07 +08:00
|
|
|
return ip, port, nil
|
2019-02-02 21:03:13 +08:00
|
|
|
}
|