clash/constant/metadata.go

256 lines
4.8 KiB
Go
Raw Normal View History

2018-06-10 14:50:03 +00:00
package constant
2018-06-11 10:36:39 +00:00
import (
"encoding/json"
"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 (
TCP NetWork = iota
2018-06-13 17:00:58 +00:00
UDP
2021-11-17 08:03:47 +00:00
ALLNet
InvalidNet = 0xff
)
const (
2019-05-09 13:00:29 +00:00
HTTP Type = iota
HTTPS
SOCKS4
SOCKS5
SHADOWSOCKS
VMESS
2018-12-05 13:13:29 +00:00
REDIR
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
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 {
switch n {
case TCP:
2018-06-13 17:00:58 +00:00
return "tcp"
case UDP:
2021-11-17 08:03:47 +00:00
return "udp"
case ALLNet:
return "all"
default:
return "invalid"
2018-06-13 17:00:58 +00:00
}
}
func (n NetWork) MarshalJSON() ([]byte, error) {
return json.Marshal(n.String())
}
2019-05-09 13:00:29 +00:00
type Type int
func (t Type) String() string {
switch t {
case HTTP:
return "HTTP"
case HTTPS:
return "HTTPS"
case SOCKS4:
return "Socks4"
case SOCKS5:
return "Socks5"
case SHADOWSOCKS:
return "ShadowSocks"
case VMESS:
return "Vmess"
case REDIR:
return "Redir"
case TPROXY:
return "TProxy"
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"
case INNER:
return "Inner"
default:
return "Unknown"
}
}
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
case "REDIR":
res = REDIR
case "TPROXY":
res = TPROXY
case "TUNNEL":
res = TUNNEL
case "TUN":
res = TUN
2022-11-28 09:09:25 +00:00
case "TUIC":
res = TUIC
case "INNER":
res = INNER
default:
return nil, fmt.Errorf("unknown type: %s", t)
}
return &res, nil
}
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"`
InUser string `json:"inboundUser"`
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"`
// Only domain rule
SniffHost string `json:"sniffHost"`
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)
}
func (m *Metadata) SourceDetail() string {
2022-04-21 10:56:33 +00:00
if m.Type == INNER {
2023-02-07 13:29:40 +00:00
return fmt.Sprintf("%s", ClashName)
2022-04-21 10:56:33 +00:00
}
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 != "":
return fmt.Sprintf("%s(%s)", m.SourceAddress(), m.Process)
2023-01-13 18:23:30 +00:00
default:
return fmt.Sprintf("%s", m.SourceAddress())
}
}
func (m *Metadata) SourceValid() bool {
return m.SrcPort != "" && m.SrcIP.IsValid()
}
2022-11-11 01:19:28 +00:00
func (m *Metadata) AddrType() int {
switch true {
2023-02-18 09:18:58 +00:00
case m.Host != "" || !m.DstIP.IsValid():
2022-11-11 01:19:28 +00:00
return socks5.AtypDomainName
case m.DstIP.Is4():
return socks5.AtypIPv4
default:
return socks5.AtypIPv6
}
}
func (m *Metadata) Resolved() bool {
2022-04-19 17:52:51 +00:00
return m.DstIP.IsValid()
}
func (m *Metadata) RuleHost() string {
if len(m.SniffHost) == 0 {
return m.Host
} else {
return m.SniffHost
}
}
// Pure is used to solve unexpected behavior
// when dialing proxy connection in DNSMapping mode.
func (m *Metadata) Pure() *Metadata {
if (m.DNSMode == DNSMapping || m.DNSMode == DNSHosts) && m.DstIP.IsValid() {
2022-04-19 17:52:51 +00:00
copyM := *m
copyM.Host = ""
return &copyM
}
return m
}
func (m *Metadata) AddrPort() netip.AddrPort {
port, _ := strconv.ParseUint(m.DstPort, 10, 16)
return netip.AddrPortFrom(m.DstIP.Unmap(), uint16(port))
}
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
}
return net.UDPAddrFromAddrPort(m.AddrPort())
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
}
func (m *Metadata) SetRemoteAddress(rawAddress string) error {
host, port, err := net.SplitHostPort(rawAddress)
if err != nil {
return err
}
if ip, err := netip.ParseAddr(host); err != nil {
m.Host = host
m.DstIP = netip.Addr{}
} else {
m.Host = ""
m.DstIP = ip.Unmap()
}
m.DstPort = port
return nil
}