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"
|
2022-01-05 00:33:42 +08:00
|
|
|
"fmt"
|
2018-06-11 18:36:39 +08:00
|
|
|
"net"
|
2022-04-20 01:52:51 +08:00
|
|
|
"net/netip"
|
2020-01-31 14:43:54 +08:00
|
|
|
"strconv"
|
2022-11-11 09:19:28 +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
|
2021-11-17 16:03:47 +08:00
|
|
|
ALLNet
|
2023-04-11 12:51:24 +08:00
|
|
|
InvalidNet = 0xff
|
|
|
|
)
|
2018-07-26 00:04:59 +08:00
|
|
|
|
2023-04-11 12:51:24 +08:00
|
|
|
const (
|
2019-05-09 21:00:29 +08:00
|
|
|
HTTP Type = iota
|
2022-05-20 23:17:16 +08:00
|
|
|
HTTPS
|
2021-07-27 13:58:29 +08:00
|
|
|
SOCKS4
|
|
|
|
SOCKS5
|
2022-11-11 20:56:08 +08:00
|
|
|
SHADOWSOCKS
|
|
|
|
VMESS
|
2018-12-05 21:13:29 +08:00
|
|
|
REDIR
|
2020-11-09 10:46:10 +08:00
|
|
|
TPROXY
|
2022-11-18 22:57:33 +08:00
|
|
|
TUNNEL
|
2021-11-17 16:03:47 +08:00
|
|
|
TUN
|
2022-11-28 17:09:25 +08:00
|
|
|
TUIC
|
2022-01-18 21:09:36 +08:00
|
|
|
INNER
|
2022-04-10 03:59:27 +08:00
|
|
|
MITM
|
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 {
|
2023-04-11 12:51:24 +08:00
|
|
|
switch n {
|
|
|
|
case TCP:
|
2018-06-14 01:00:58 +08:00
|
|
|
return "tcp"
|
2023-04-11 12:51:24 +08:00
|
|
|
case UDP:
|
2021-11-17 16:03:47 +08:00
|
|
|
return "udp"
|
2023-04-11 12:51:24 +08:00
|
|
|
case ALLNet:
|
|
|
|
return "all"
|
|
|
|
default:
|
|
|
|
return "invalid"
|
2018-06-14 01:00:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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"
|
2022-05-20 23:17:16 +08:00
|
|
|
case HTTPS:
|
|
|
|
return "HTTPS"
|
2021-07-27 13:58:29 +08:00
|
|
|
case SOCKS4:
|
|
|
|
return "Socks4"
|
|
|
|
case SOCKS5:
|
2019-10-27 21:44:07 +08:00
|
|
|
return "Socks5"
|
2022-11-11 20:56:08 +08:00
|
|
|
case SHADOWSOCKS:
|
|
|
|
return "ShadowSocks"
|
|
|
|
case VMESS:
|
|
|
|
return "Vmess"
|
2019-10-27 21:44:07 +08:00
|
|
|
case REDIR:
|
|
|
|
return "Redir"
|
2020-11-09 10:46:10 +08:00
|
|
|
case TPROXY:
|
|
|
|
return "TProxy"
|
2022-11-28 20:04:56 +08:00
|
|
|
case TUNNEL:
|
|
|
|
return "Tunnel"
|
2021-11-17 16:03:47 +08:00
|
|
|
case TUN:
|
|
|
|
return "Tun"
|
2022-11-28 17:09:25 +08:00
|
|
|
case TUIC:
|
|
|
|
return "Tuic"
|
2022-01-18 21:09:36 +08:00
|
|
|
case INNER:
|
|
|
|
return "Inner"
|
2022-04-10 03:59:27 +08:00
|
|
|
case MITM:
|
|
|
|
return "Mitm"
|
2019-10-27 21:44:07 +08:00
|
|
|
default:
|
|
|
|
return "Unknown"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-20 23:17:16 +08: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 17:09:25 +08:00
|
|
|
case "SHADOWSOCKS":
|
|
|
|
res = SHADOWSOCKS
|
|
|
|
case "VMESS":
|
|
|
|
res = VMESS
|
2022-05-20 23:17:16 +08:00
|
|
|
case "REDIR":
|
|
|
|
res = REDIR
|
|
|
|
case "TPROXY":
|
|
|
|
res = TPROXY
|
2022-11-28 20:04:56 +08:00
|
|
|
case "TUNNEL":
|
|
|
|
res = TUNNEL
|
2022-05-20 23:17:16 +08:00
|
|
|
case "TUN":
|
|
|
|
res = TUN
|
2022-11-28 17:09:25 +08:00
|
|
|
case "TUIC":
|
|
|
|
res = TUIC
|
2022-05-20 23:17:16 +08:00
|
|
|
case "INNER":
|
|
|
|
res = INNER
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown type: %s", t)
|
|
|
|
}
|
|
|
|
return &res, nil
|
|
|
|
}
|
|
|
|
|
2019-10-27 21:44:07 +08:00
|
|
|
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-12-04 22:08:20 +08:00
|
|
|
NetWork NetWork `json:"network"`
|
|
|
|
Type Type `json:"type"`
|
|
|
|
SrcIP netip.Addr `json:"sourceIP"`
|
|
|
|
DstIP netip.Addr `json:"destinationIP"`
|
2023-08-09 13:51:02 +08:00
|
|
|
SrcPort uint16 `json:"sourcePort,string"` // `,string` is used to compatible with old version json output
|
|
|
|
DstPort uint16 `json:"destinationPort,string"` // `,string` is used to compatible with old version json output
|
2022-12-04 22:08:20 +08:00
|
|
|
InIP netip.Addr `json:"inboundIP"`
|
2023-08-09 13:51:02 +08:00
|
|
|
InPort uint16 `json:"inboundPort,string"` // `,string` is used to compatible with old version json output
|
2022-12-06 08:23:30 +08:00
|
|
|
InName string `json:"inboundName"`
|
2023-05-28 17:19:57 +08:00
|
|
|
InUser string `json:"inboundUser"`
|
2022-12-04 22:08:20 +08:00
|
|
|
Host string `json:"host"`
|
|
|
|
DNSMode DNSMode `json:"dnsMode"`
|
2023-01-14 02:23:30 +08:00
|
|
|
Uid uint32 `json:"uid"`
|
2022-12-04 22:08:20 +08:00
|
|
|
Process string `json:"process"`
|
|
|
|
ProcessPath string `json:"processPath"`
|
|
|
|
SpecialProxy string `json:"specialProxy"`
|
|
|
|
SpecialRules string `json:"specialRules"`
|
2022-12-06 08:23:30 +08:00
|
|
|
RemoteDst string `json:"remoteDestination"`
|
2023-01-23 14:08:11 +08:00
|
|
|
// Only domain rule
|
2023-02-10 12:48:02 +08:00
|
|
|
SniffHost string `json:"sniffHost"`
|
2022-04-10 03:59:27 +08:00
|
|
|
// Only Mitm rule
|
|
|
|
UserAgent string `json:"userAgent"`
|
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 {
|
2023-08-09 13:51:02 +08:00
|
|
|
return net.JoinHostPort(m.String(), strconv.FormatUint(uint64(m.DstPort), 10))
|
2019-10-11 20:11:18 +08:00
|
|
|
}
|
|
|
|
|
2020-01-31 14:58:54 +08:00
|
|
|
func (m *Metadata) SourceAddress() string {
|
2023-08-09 13:51:02 +08:00
|
|
|
return net.JoinHostPort(m.SrcIP.String(), strconv.FormatUint(uint64(m.SrcPort), 10))
|
2020-01-31 14:58:54 +08:00
|
|
|
}
|
|
|
|
|
2022-01-05 00:33:42 +08:00
|
|
|
func (m *Metadata) SourceDetail() string {
|
2022-04-21 18:56:33 +08:00
|
|
|
if m.Type == INNER {
|
2023-02-07 21:29:40 +08:00
|
|
|
return fmt.Sprintf("%s", ClashName)
|
2022-04-21 18:56:33 +08:00
|
|
|
}
|
|
|
|
|
2023-01-14 02:23:30 +08: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-05 00:33:42 +08:00
|
|
|
return fmt.Sprintf("%s(%s)", m.SourceAddress(), m.Process)
|
2023-01-14 02:23:30 +08:00
|
|
|
default:
|
2022-01-05 00:33:42 +08:00
|
|
|
return fmt.Sprintf("%s", m.SourceAddress())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-08 15:57:51 +08:00
|
|
|
func (m *Metadata) SourceValid() bool {
|
2023-08-09 13:51:02 +08:00
|
|
|
return m.SrcPort != 0 && m.SrcIP.IsValid()
|
2023-06-08 15:57:51 +08:00
|
|
|
}
|
|
|
|
|
2022-11-11 09:19:28 +08:00
|
|
|
func (m *Metadata) AddrType() int {
|
|
|
|
switch true {
|
2023-02-18 17:18:58 +08:00
|
|
|
case m.Host != "" || !m.DstIP.IsValid():
|
2022-11-11 09:19:28 +08:00
|
|
|
return socks5.AtypDomainName
|
|
|
|
case m.DstIP.Is4():
|
|
|
|
return socks5.AtypIPv4
|
|
|
|
default:
|
|
|
|
return socks5.AtypIPv6
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-07 20:53:43 +08:00
|
|
|
func (m *Metadata) Resolved() bool {
|
2022-04-20 01:52:51 +08:00
|
|
|
return m.DstIP.IsValid()
|
2020-02-07 20:53:43 +08:00
|
|
|
}
|
|
|
|
|
2023-01-23 14:08:11 +08:00
|
|
|
func (m *Metadata) RuleHost() string {
|
|
|
|
if len(m.SniffHost) == 0 {
|
|
|
|
return m.Host
|
|
|
|
} else {
|
|
|
|
return m.SniffHost
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2022-10-29 09:02:38 +08:00
|
|
|
if (m.DNSMode == DNSMapping || m.DNSMode == DNSHosts) && m.DstIP.IsValid() {
|
2022-04-20 01:52:51 +08:00
|
|
|
copyM := *m
|
|
|
|
copyM.Host = ""
|
|
|
|
return ©M
|
2021-10-18 21:08:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2023-05-27 13:43:41 +08:00
|
|
|
func (m *Metadata) AddrPort() netip.AddrPort {
|
2023-08-09 13:51:02 +08:00
|
|
|
return netip.AddrPortFrom(m.DstIP.Unmap(), m.DstPort)
|
2023-05-27 13:43:41 +08:00
|
|
|
}
|
|
|
|
|
2020-01-31 14:43:54 +08:00
|
|
|
func (m *Metadata) UDPAddr() *net.UDPAddr {
|
2022-04-20 01:52:51 +08:00
|
|
|
if m.NetWork != UDP || !m.DstIP.IsValid() {
|
2020-01-31 14:43:54 +08:00
|
|
|
return nil
|
|
|
|
}
|
2023-05-27 13:43:41 +08:00
|
|
|
return net.UDPAddrFromAddrPort(m.AddrPort())
|
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
|
2022-04-20 01:52:51 +08:00
|
|
|
} else if m.DstIP.IsValid() {
|
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 {
|
2022-04-20 01:52:51 +08:00
|
|
|
return m.Host != "" || m.DstIP.IsValid()
|
2019-02-02 20:47:38 +08:00
|
|
|
}
|
2023-04-12 12:49:53 +08:00
|
|
|
|
|
|
|
func (m *Metadata) SetRemoteAddress(rawAddress string) error {
|
|
|
|
host, port, err := net.SplitHostPort(rawAddress)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-09 13:51:02 +08:00
|
|
|
var uint16Port uint16
|
|
|
|
if port, err := strconv.ParseUint(port, 10, 16); err == nil {
|
|
|
|
uint16Port = uint16(port)
|
|
|
|
}
|
|
|
|
|
2023-04-12 12:49:53 +08:00
|
|
|
if ip, err := netip.ParseAddr(host); err != nil {
|
|
|
|
m.Host = host
|
|
|
|
m.DstIP = netip.Addr{}
|
|
|
|
} else {
|
|
|
|
m.Host = ""
|
|
|
|
m.DstIP = ip.Unmap()
|
|
|
|
}
|
2023-08-09 13:51:02 +08:00
|
|
|
m.DstPort = uint16Port
|
2023-04-12 12:49:53 +08:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|