clash/constant/metadata.go

155 lines
2.7 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"
2020-01-31 06:43:54 +00:00
"strconv"
2018-06-11 10:36:39 +00:00
)
2018-06-10 14:50:03 +00:00
// Socks addr type
const (
AtypIPv4 = 1
AtypDomainName = 3
AtypIPv6 = 4
2018-06-13 17:00:58 +00:00
TCP NetWork = iota
2018-06-13 17:00:58 +00:00
UDP
2021-11-17 08:03:47 +00:00
ALLNet
2019-05-09 13:00:29 +00:00
HTTP Type = iota
HTTPCONNECT
SOCKS4
SOCKS5
2018-12-05 13:13:29 +00:00
REDIR
TPROXY
2021-11-17 08:03:47 +00:00
TUN
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
}
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 HTTPCONNECT:
return "HTTP Connect"
case SOCKS4:
return "Socks4"
case SOCKS5:
return "Socks5"
case REDIR:
return "Redir"
case TPROXY:
return "TProxy"
2021-11-17 08:03:47 +00:00
case TUN:
return "Tun"
case INNER:
return "Inner"
default:
return "Unknown"
}
}
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-03-12 17:21:23 +00: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"`
AddrType int `json:"-"`
Host string `json:"host"`
DNSMode DNSMode `json:"dnsMode"`
Process string `json:"process"`
ProcessPath string `json:"processPath"`
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 {
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 {
if m.Process != "" {
return fmt.Sprintf("%s(%s)", m.SourceAddress(), m.Process)
} else {
if m.Type == INNER {
return fmt.Sprintf("[Clash]")
}
return fmt.Sprintf("%s", m.SourceAddress())
}
}
func (m *Metadata) Resolved() bool {
return m.DstIP != nil
}
// 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 = ""
if copy.DstIP.To4() != nil {
copy.AddrType = AtypIPv4
} else {
copy.AddrType = AtypIPv6
}
return &copy
}
return m
}
2020-01-31 06:43:54 +00:00
func (m *Metadata) UDPAddr() *net.UDPAddr {
if m.NetWork != UDP || m.DstIP == nil {
return nil
}
port, _ := strconv.ParseUint(m.DstPort, 10, 16)
2020-01-31 06:43:54 +00:00
return &net.UDPAddr{
IP: m.DstIP,
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
} else if m.DstIP != nil {
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 {
2019-05-09 13:00:29 +00:00
return m.Host != "" || m.DstIP != nil
2019-02-02 12:47:38 +00:00
}