2018-06-10 14:50:03 +00:00
|
|
|
package constant
|
|
|
|
|
2018-06-11 10:36:39 +00:00
|
|
|
import (
|
2019-10-27 13:44:07 +00:00
|
|
|
"encoding/json"
|
2022-01-04 16:33:42 +00:00
|
|
|
"fmt"
|
2018-06-11 10:36:39 +00:00
|
|
|
"net"
|
2022-04-19 17:52:51 +00:00
|
|
|
"net/netip"
|
2020-01-31 06:43:54 +00:00
|
|
|
"strconv"
|
2022-11-11 01:19:28 +00:00
|
|
|
|
|
|
|
"github.com/Dreamacro/clash/transport/socks5"
|
2018-06-11 10:36:39 +00:00
|
|
|
)
|
|
|
|
|
2018-06-10 14:50:03 +00:00
|
|
|
// Socks addr type
|
|
|
|
const (
|
2018-07-25 16:04:59 +00:00
|
|
|
TCP NetWork = iota
|
2018-06-13 17:00:58 +00:00
|
|
|
UDP
|
2021-11-17 08:03:47 +00:00
|
|
|
ALLNet
|
2018-07-25 16:04:59 +00:00
|
|
|
|
2019-05-09 13:00:29 +00:00
|
|
|
HTTP Type = iota
|
2022-05-20 15:17:16 +00:00
|
|
|
HTTPS
|
2021-07-27 05:58:29 +00:00
|
|
|
SOCKS4
|
|
|
|
SOCKS5
|
2022-11-11 12:56:08 +00:00
|
|
|
SHADOWSOCKS
|
|
|
|
VMESS
|
2018-12-05 13:13:29 +00:00
|
|
|
REDIR
|
2020-11-09 02:46:10 +00:00
|
|
|
TPROXY
|
2022-11-18 14:57:33 +00:00
|
|
|
TUNNEL
|
2021-11-17 08:03:47 +00:00
|
|
|
TUN
|
2022-11-28 09:09:25 +00:00
|
|
|
TUIC
|
2022-01-18 13:09:36 +00:00
|
|
|
INNER
|
2018-06-10 14:50:03 +00:00
|
|
|
)
|
|
|
|
|
2018-06-13 17:00:58 +00:00
|
|
|
type NetWork int
|
|
|
|
|
2020-06-07 08:54:41 +00:00
|
|
|
func (n NetWork) String() string {
|
|
|
|
if n == TCP {
|
2018-06-13 17:00:58 +00:00
|
|
|
return "tcp"
|
2021-11-17 08:03:47 +00:00
|
|
|
} else if n == UDP {
|
|
|
|
return "udp"
|
2018-06-13 17:00:58 +00:00
|
|
|
}
|
2021-11-17 08:03:47 +00:00
|
|
|
return "all"
|
2018-06-13 17:00:58 +00:00
|
|
|
}
|
|
|
|
|
2019-10-27 13:44:07 +00:00
|
|
|
func (n NetWork) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(n.String())
|
|
|
|
}
|
|
|
|
|
2019-05-09 13:00:29 +00:00
|
|
|
type Type int
|
2018-07-25 16:04:59 +00:00
|
|
|
|
2019-10-27 13:44:07 +00:00
|
|
|
func (t Type) String() string {
|
|
|
|
switch t {
|
|
|
|
case HTTP:
|
|
|
|
return "HTTP"
|
2022-05-20 15:17:16 +00:00
|
|
|
case HTTPS:
|
|
|
|
return "HTTPS"
|
2021-07-27 05:58:29 +00:00
|
|
|
case SOCKS4:
|
|
|
|
return "Socks4"
|
|
|
|
case SOCKS5:
|
2019-10-27 13:44:07 +00:00
|
|
|
return "Socks5"
|
2022-11-11 12:56:08 +00:00
|
|
|
case SHADOWSOCKS:
|
|
|
|
return "ShadowSocks"
|
|
|
|
case VMESS:
|
|
|
|
return "Vmess"
|
2019-10-27 13:44:07 +00:00
|
|
|
case REDIR:
|
|
|
|
return "Redir"
|
2020-11-09 02:46:10 +00:00
|
|
|
case TPROXY:
|
|
|
|
return "TProxy"
|
2022-11-28 12:04:56 +00:00
|
|
|
case TUNNEL:
|
|
|
|
return "Tunnel"
|
2021-11-17 08:03:47 +00:00
|
|
|
case TUN:
|
|
|
|
return "Tun"
|
2022-11-28 09:09:25 +00:00
|
|
|
case TUIC:
|
|
|
|
return "Tuic"
|
2022-01-18 13:09:36 +00:00
|
|
|
case INNER:
|
|
|
|
return "Inner"
|
2019-10-27 13:44:07 +00:00
|
|
|
default:
|
|
|
|
return "Unknown"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-20 15:17:16 +00:00
|
|
|
func ParseType(t string) (*Type, error) {
|
|
|
|
var res Type
|
|
|
|
switch t {
|
|
|
|
case "HTTP":
|
|
|
|
res = HTTP
|
|
|
|
case "HTTPS":
|
|
|
|
res = HTTPS
|
|
|
|
case "SOCKS4":
|
|
|
|
res = SOCKS4
|
|
|
|
case "SOCKS5":
|
|
|
|
res = SOCKS5
|
2022-11-28 09:09:25 +00:00
|
|
|
case "SHADOWSOCKS":
|
|
|
|
res = SHADOWSOCKS
|
|
|
|
case "VMESS":
|
|
|
|
res = VMESS
|
2022-05-20 15:17:16 +00:00
|
|
|
case "REDIR":
|
|
|
|
res = REDIR
|
|
|
|
case "TPROXY":
|
|
|
|
res = TPROXY
|
2022-11-28 12:04:56 +00:00
|
|
|
case "TUNNEL":
|
|
|
|
res = TUNNEL
|
2022-05-20 15:17:16 +00:00
|
|
|
case "TUN":
|
|
|
|
res = TUN
|
2022-11-28 09:09:25 +00:00
|
|
|
case "TUIC":
|
|
|
|
res = TUIC
|
2022-05-20 15:17:16 +00:00
|
|
|
case "INNER":
|
|
|
|
res = INNER
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown type: %s", t)
|
|
|
|
}
|
|
|
|
return &res, nil
|
|
|
|
}
|
|
|
|
|
2019-10-27 13:44:07 +00:00
|
|
|
func (t Type) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(t.String())
|
|
|
|
}
|
|
|
|
|
2018-09-30 04:25:52 +00:00
|
|
|
// Metadata is used to store connection address
|
|
|
|
type Metadata struct {
|
2022-12-04 14:08:20 +00:00
|
|
|
NetWork NetWork `json:"network"`
|
|
|
|
Type Type `json:"type"`
|
|
|
|
SrcIP netip.Addr `json:"sourceIP"`
|
|
|
|
DstIP netip.Addr `json:"destinationIP"`
|
|
|
|
SrcPort string `json:"sourcePort"`
|
|
|
|
DstPort string `json:"destinationPort"`
|
|
|
|
InIP netip.Addr `json:"inboundIP"`
|
|
|
|
InPort string `json:"inboundPort"`
|
2022-12-06 00:23:30 +00:00
|
|
|
InName string `json:"inboundName"`
|
2022-12-04 14:08:20 +00:00
|
|
|
Host string `json:"host"`
|
|
|
|
DNSMode DNSMode `json:"dnsMode"`
|
2023-01-13 18:23:30 +00:00
|
|
|
Uid uint32 `json:"uid"`
|
2022-12-04 14:08:20 +00:00
|
|
|
Process string `json:"process"`
|
|
|
|
ProcessPath string `json:"processPath"`
|
|
|
|
SpecialProxy string `json:"specialProxy"`
|
|
|
|
SpecialRules string `json:"specialRules"`
|
2022-12-06 00:23:30 +00:00
|
|
|
RemoteDst string `json:"remoteDestination"`
|
2023-01-23 06:08:11 +00:00
|
|
|
// Only domain rule
|
|
|
|
SniffHost string
|
2018-06-10 14:50:03 +00:00
|
|
|
}
|
2018-06-11 10:36:39 +00:00
|
|
|
|
2019-10-11 12:11:18 +00:00
|
|
|
func (m *Metadata) RemoteAddress() string {
|
2022-04-20 19:54:34 +00:00
|
|
|
return net.JoinHostPort(m.String(), m.DstPort)
|
2019-10-11 12:11:18 +00:00
|
|
|
}
|
|
|
|
|
2020-01-31 06:58:54 +00:00
|
|
|
func (m *Metadata) SourceAddress() string {
|
|
|
|
return net.JoinHostPort(m.SrcIP.String(), m.SrcPort)
|
|
|
|
}
|
|
|
|
|
2022-01-04 16:33:42 +00:00
|
|
|
func (m *Metadata) SourceDetail() string {
|
2022-04-21 10:56:33 +00:00
|
|
|
if m.Type == INNER {
|
|
|
|
return fmt.Sprintf("[%s]", ClashName)
|
|
|
|
}
|
|
|
|
|
2023-01-13 18:23:30 +00:00
|
|
|
switch {
|
|
|
|
case m.Process != "" && m.Uid != 0:
|
|
|
|
return fmt.Sprintf("%s(%s, uid=%d)", m.SourceAddress(), m.Process, m.Uid)
|
|
|
|
case m.Uid != 0:
|
|
|
|
return fmt.Sprintf("%s(uid=%d)", m.SourceAddress(), m.Uid)
|
|
|
|
case m.Process != "":
|
2022-01-04 16:33:42 +00:00
|
|
|
return fmt.Sprintf("%s(%s)", m.SourceAddress(), m.Process)
|
2023-01-13 18:23:30 +00:00
|
|
|
default:
|
2022-01-04 16:33:42 +00:00
|
|
|
return fmt.Sprintf("%s", m.SourceAddress())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-11 01:19:28 +00:00
|
|
|
func (m *Metadata) AddrType() int {
|
|
|
|
switch true {
|
|
|
|
case m.Host != "" || !m.DstIP.IsValid():
|
|
|
|
return socks5.AtypDomainName
|
|
|
|
case m.DstIP.Is4():
|
|
|
|
return socks5.AtypIPv4
|
|
|
|
default:
|
|
|
|
return socks5.AtypIPv6
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-07 12:53:43 +00:00
|
|
|
func (m *Metadata) Resolved() bool {
|
2022-04-19 17:52:51 +00:00
|
|
|
return m.DstIP.IsValid()
|
2020-02-07 12:53:43 +00:00
|
|
|
}
|
|
|
|
|
2023-01-23 06:08:11 +00:00
|
|
|
func (m *Metadata) RuleHost() string {
|
|
|
|
if len(m.SniffHost) == 0 {
|
|
|
|
return m.Host
|
|
|
|
} else {
|
|
|
|
return m.SniffHost
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-18 13:08:27 +00:00
|
|
|
// Pure is used to solve unexpected behavior
|
|
|
|
// when dialing proxy connection in DNSMapping mode.
|
|
|
|
func (m *Metadata) Pure() *Metadata {
|
2022-10-29 01:02:38 +00:00
|
|
|
if (m.DNSMode == DNSMapping || m.DNSMode == DNSHosts) && m.DstIP.IsValid() {
|
2022-04-19 17:52:51 +00:00
|
|
|
copyM := *m
|
|
|
|
copyM.Host = ""
|
|
|
|
return ©M
|
2021-10-18 13:08:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2020-01-31 06:43:54 +00:00
|
|
|
func (m *Metadata) UDPAddr() *net.UDPAddr {
|
2022-04-19 17:52:51 +00:00
|
|
|
if m.NetWork != UDP || !m.DstIP.IsValid() {
|
2020-01-31 06:43:54 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-01-01 17:09:29 +00:00
|
|
|
port, _ := strconv.ParseUint(m.DstPort, 10, 16)
|
2020-01-31 06:43:54 +00:00
|
|
|
return &net.UDPAddr{
|
2022-04-19 17:52:51 +00:00
|
|
|
IP: m.DstIP.AsSlice(),
|
2021-11-07 16:31:08 +00:00
|
|
|
Port: int(port),
|
2020-01-31 06:43:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-02 12:47:38 +00:00
|
|
|
func (m *Metadata) String() string {
|
2019-10-11 12:11:18 +00:00
|
|
|
if m.Host != "" {
|
|
|
|
return m.Host
|
2022-04-19 17:52:51 +00:00
|
|
|
} else if m.DstIP.IsValid() {
|
2019-05-09 13:00:29 +00:00
|
|
|
return m.DstIP.String()
|
2019-10-11 12:11:18 +00:00
|
|
|
} else {
|
|
|
|
return "<nil>"
|
2018-06-11 10:36:39 +00:00
|
|
|
}
|
2019-02-02 12:47:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Metadata) Valid() bool {
|
2022-04-19 17:52:51 +00:00
|
|
|
return m.Host != "" || m.DstIP.IsValid()
|
2019-02-02 12:47:38 +00:00
|
|
|
}
|