2018-06-10 22:50:03 +08:00
|
|
|
package constant
|
|
|
|
|
2018-06-11 18:36:39 +08:00
|
|
|
import (
|
2019-10-27 21:44:07 +08:00
|
|
|
"encoding/json"
|
2018-06-11 18:36:39 +08:00
|
|
|
"net"
|
2020-01-31 14:43:54 +08:00
|
|
|
"strconv"
|
2022-07-05 20:26:43 +08:00
|
|
|
|
|
|
|
"github.com/Dreamacro/clash/transport/socks5"
|
2018-06-11 18:36:39 +08:00
|
|
|
)
|
|
|
|
|
2018-06-10 22:50:03 +08:00
|
|
|
// Socks addr type
|
|
|
|
const (
|
2018-07-26 00:04:59 +08:00
|
|
|
TCP NetWork = iota
|
2018-06-14 01:00:58 +08:00
|
|
|
UDP
|
2018-07-26 00:04:59 +08:00
|
|
|
|
2019-05-09 21:00:29 +08:00
|
|
|
HTTP Type = iota
|
2019-10-27 21:44:07 +08:00
|
|
|
HTTPCONNECT
|
2021-07-27 13:58:29 +08:00
|
|
|
SOCKS4
|
|
|
|
SOCKS5
|
2018-12-05 21:13:29 +08:00
|
|
|
REDIR
|
2020-11-09 10:46:10 +08:00
|
|
|
TPROXY
|
2018-06-10 22:50:03 +08:00
|
|
|
)
|
|
|
|
|
2018-06-14 01:00:58 +08:00
|
|
|
type NetWork int
|
|
|
|
|
2020-06-07 16:54:41 +08:00
|
|
|
func (n NetWork) String() string {
|
|
|
|
if n == TCP {
|
2018-06-14 01:00:58 +08:00
|
|
|
return "tcp"
|
|
|
|
}
|
|
|
|
return "udp"
|
|
|
|
}
|
|
|
|
|
2019-10-27 21:44:07 +08:00
|
|
|
func (n NetWork) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(n.String())
|
|
|
|
}
|
|
|
|
|
2019-05-09 21:00:29 +08:00
|
|
|
type Type int
|
2018-07-26 00:04:59 +08:00
|
|
|
|
2019-10-27 21:44:07 +08:00
|
|
|
func (t Type) String() string {
|
|
|
|
switch t {
|
|
|
|
case HTTP:
|
|
|
|
return "HTTP"
|
|
|
|
case HTTPCONNECT:
|
|
|
|
return "HTTP Connect"
|
2021-07-27 13:58:29 +08:00
|
|
|
case SOCKS4:
|
|
|
|
return "Socks4"
|
|
|
|
case SOCKS5:
|
2019-10-27 21:44:07 +08:00
|
|
|
return "Socks5"
|
|
|
|
case REDIR:
|
|
|
|
return "Redir"
|
2020-11-09 10:46:10 +08:00
|
|
|
case TPROXY:
|
|
|
|
return "TProxy"
|
2019-10-27 21:44:07 +08:00
|
|
|
default:
|
|
|
|
return "Unknown"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t Type) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(t.String())
|
|
|
|
}
|
|
|
|
|
2018-09-30 12:25:52 +08:00
|
|
|
// Metadata is used to store connection address
|
|
|
|
type Metadata struct {
|
2022-03-12 19:07:53 +08:00
|
|
|
NetWork NetWork `json:"network"`
|
|
|
|
Type Type `json:"type"`
|
|
|
|
SrcIP net.IP `json:"sourceIP"`
|
|
|
|
DstIP net.IP `json:"destinationIP"`
|
|
|
|
SrcPort string `json:"sourcePort"`
|
|
|
|
DstPort string `json:"destinationPort"`
|
|
|
|
Host string `json:"host"`
|
|
|
|
DNSMode DNSMode `json:"dnsMode"`
|
|
|
|
ProcessPath string `json:"processPath"`
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
2018-06-11 18:36:39 +08:00
|
|
|
|
2019-10-11 20:11:18 +08:00
|
|
|
func (m *Metadata) RemoteAddress() string {
|
|
|
|
return net.JoinHostPort(m.String(), m.DstPort)
|
|
|
|
}
|
|
|
|
|
2020-01-31 14:58:54 +08:00
|
|
|
func (m *Metadata) SourceAddress() string {
|
|
|
|
return net.JoinHostPort(m.SrcIP.String(), m.SrcPort)
|
|
|
|
}
|
|
|
|
|
2022-07-05 20:26:43 +08:00
|
|
|
func (m *Metadata) AddrType() int {
|
|
|
|
switch true {
|
|
|
|
case m.Host != "" || m.DstIP == nil:
|
|
|
|
return socks5.AtypDomainName
|
|
|
|
case m.DstIP.To4() != nil:
|
|
|
|
return socks5.AtypIPv4
|
|
|
|
default:
|
|
|
|
return socks5.AtypIPv6
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-07 20:53:43 +08:00
|
|
|
func (m *Metadata) Resolved() bool {
|
|
|
|
return m.DstIP != nil
|
|
|
|
}
|
|
|
|
|
2021-10-18 21:08:27 +08:00
|
|
|
// Pure is used to solve unexpected behavior
|
|
|
|
// when dialing proxy connection in DNSMapping mode.
|
|
|
|
func (m *Metadata) Pure() *Metadata {
|
|
|
|
if m.DNSMode == DNSMapping && m.DstIP != nil {
|
|
|
|
copy := *m
|
|
|
|
copy.Host = ""
|
|
|
|
return ©
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2020-01-31 14:43:54 +08:00
|
|
|
func (m *Metadata) UDPAddr() *net.UDPAddr {
|
|
|
|
if m.NetWork != UDP || m.DstIP == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2022-01-02 01:09:29 +08:00
|
|
|
port, _ := strconv.ParseUint(m.DstPort, 10, 16)
|
2020-01-31 14:43:54 +08:00
|
|
|
return &net.UDPAddr{
|
|
|
|
IP: m.DstIP,
|
2021-11-08 00:31:08 +08:00
|
|
|
Port: int(port),
|
2020-01-31 14:43:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-02 20:47:38 +08:00
|
|
|
func (m *Metadata) String() string {
|
2019-10-11 20:11:18 +08:00
|
|
|
if m.Host != "" {
|
|
|
|
return m.Host
|
|
|
|
} else if m.DstIP != nil {
|
2019-05-09 21:00:29 +08:00
|
|
|
return m.DstIP.String()
|
2019-10-11 20:11:18 +08:00
|
|
|
} else {
|
|
|
|
return "<nil>"
|
2018-06-11 18:36:39 +08:00
|
|
|
}
|
2019-02-02 20:47:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Metadata) Valid() bool {
|
2019-05-09 21:00:29 +08:00
|
|
|
return m.Host != "" || m.DstIP != nil
|
2019-02-02 20:47:38 +08:00
|
|
|
}
|