2018-07-25 16:04:59 +00:00
|
|
|
package adapters
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
2018-08-12 08:18:58 +00:00
|
|
|
"net/http"
|
2018-07-25 16:04:59 +00:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2018-09-21 07:27:51 +00:00
|
|
|
"github.com/Dreamacro/go-shadowsocks2/socks"
|
2018-07-25 16:04:59 +00:00
|
|
|
)
|
|
|
|
|
2018-09-30 04:25:52 +00:00
|
|
|
func parseSocksAddr(target socks.Addr) *C.Metadata {
|
2018-12-05 13:13:29 +00:00
|
|
|
metadata := &C.Metadata{
|
|
|
|
NetWork: C.TCP,
|
|
|
|
AddrType: int(target[0]),
|
|
|
|
}
|
2018-07-25 16:04:59 +00:00
|
|
|
|
|
|
|
switch target[0] {
|
|
|
|
case socks.AtypDomainName:
|
2018-12-05 13:13:29 +00:00
|
|
|
metadata.Host = string(target[2 : 2+target[1]])
|
|
|
|
metadata.Port = strconv.Itoa((int(target[2+target[1]]) << 8) | int(target[2+target[1]+1]))
|
2018-07-25 16:04:59 +00:00
|
|
|
case socks.AtypIPv4:
|
2018-12-05 13:13:29 +00:00
|
|
|
ip := net.IP(target[1 : 1+net.IPv4len])
|
|
|
|
metadata.IP = &ip
|
|
|
|
metadata.Port = strconv.Itoa((int(target[1+net.IPv4len]) << 8) | int(target[1+net.IPv4len+1]))
|
2018-07-25 16:04:59 +00:00
|
|
|
case socks.AtypIPv6:
|
2018-12-05 13:13:29 +00:00
|
|
|
ip := net.IP(target[1 : 1+net.IPv6len])
|
|
|
|
metadata.IP = &ip
|
|
|
|
metadata.Port = strconv.Itoa((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
|
|
|
|
2018-12-05 13:13:29 +00:00
|
|
|
metadata := &C.Metadata{
|
2018-07-25 16:04:59 +00:00
|
|
|
NetWork: C.TCP,
|
2018-12-05 13:13:29 +00:00
|
|
|
Source: C.HTTP,
|
|
|
|
AddrType: C.AtypDomainName,
|
2018-07-25 16:04:59 +00:00
|
|
|
Host: host,
|
2018-12-05 13:13:29 +00:00
|
|
|
IP: nil,
|
2018-07-25 16:04:59 +00:00
|
|
|
Port: port,
|
|
|
|
}
|
2018-12-05 13:13:29 +00:00
|
|
|
|
|
|
|
ip := net.ParseIP(host)
|
|
|
|
if ip != nil {
|
|
|
|
switch {
|
|
|
|
case ip.To4() == nil:
|
|
|
|
metadata.AddrType = C.AtypIPv6
|
|
|
|
default:
|
|
|
|
metadata.AddrType = C.AtypIPv4
|
|
|
|
}
|
|
|
|
metadata.IP = &ip
|
|
|
|
}
|
|
|
|
|
|
|
|
return metadata
|
2018-07-25 16:04:59 +00:00
|
|
|
}
|
2019-02-02 13:03:13 +00:00
|
|
|
|
|
|
|
func parseSourceIP(conn net.Conn) *net.IP {
|
|
|
|
if addr, ok := conn.RemoteAddr().(*net.TCPAddr); ok {
|
|
|
|
return &addr.IP
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|