Documentation
¶
Index ¶
- Constants
- Variables
- func BackupScriptState()
- func DecodeLatin1(reader io.Reader) (string, error)
- func EncodeLatin1(str string) ([]byte, error)
- func GetScriptRuleProviders() map[string]Rule
- func NewErrorResponse(req *http.Request, err error) *http.Response
- func NewResponse(code int, body io.Reader, req *http.Request) *http.Response
- func ProxyURL(auth auth.Authenticator) func(*http.Request) (*url.URL, error)
- func ReadDecompressedBody(res *http.Response) ([]byte, error)
- func RestoreScriptState()
- func SetConfig(file string)
- func SetHomeDir(root string)
- func SetLastTunConf(conf *Tun)
- func SetProxyInbound(tcpInbounds map[string]Inbound)
- func SetScriptRuleProviders(rp map[string]Rule)
- type AdapterType
- type AuthenticatorListener
- type Chain
- type Conn
- type ConnContext
- type Connection
- type DNSAddrPort
- type DNSMode
- type DNSUrl
- type DelayHistory
- type EBpf
- type Inbound
- type InboundType
- type Listener
- type LogAddr
- type LogRule
- type Matcher
- type Metadata
- func (m *Metadata) AddrType() int
- func (m *Metadata) MarshalObject(e *log.Entry)
- func (m *Metadata) NetworkFromString(s string)
- func (m *Metadata) RemoteAddress() string
- func (m *Metadata) Resolved() bool
- func (m *Metadata) SourceAddress() string
- func (m *Metadata) String() string
- func (m *Metadata) TypeFromString(s string)
- func (m *Metadata) UDPAddr() *net.UDPAddr
- func (m *Metadata) Valid() bool
- type MitmOption
- type MitmSession
- func (s *MitmSession) GetProperties(key string) (any, bool)
- func (s *MitmSession) NewErrorResponse(err error) *http.Response
- func (s *MitmSession) NewResponse(code int, body io.Reader) *http.Response
- func (s *MitmSession) SetProperties(key string, val any)
- func (s *MitmSession) WriteResponse() (err error)
- type NetWork
- type PacketConn
- type PacketConnContext
- type PlainContext
- type Port
- type Proxy
- type ProxyAdapter
- type RawProxy
- type Rewrite
- type RewriteHandler
- type RewriteRule
- type RewriteType
- type Rule
- type RuleExtra
- type RuleGeoSite
- type RuleGroup
- type RuleType
- type TUNChangeCallback
- type TUNStack
- type TUNState
- type Tun
- type Type
- type UDPPacket
Constants ¶
const ( DefaultTCPTimeout = 8 * time.Second DefaultUDPTimeout = DefaultTCPTimeout DefaultTLSTimeout = DefaultTCPTimeout )
const ( BpfFSPath = "/sys/fs/bpf/clash" TcpAutoRedirPort = 't'<<8 | 'r'<<0 ClashTrafficMark = 'c'<<24 | 'l'<<16 | 't'<<8 | 'm'<<0 )
const IsNoGVisor = false
const Name = "clash"
const ScriptRuleGeoSiteTarget = "__WhateverTarget__"
Variables ¶
var ( ErrInvalidResponse = errors.New("invalid response") ErrInvalidURL = errors.New("invalid URL") )
var ( Version = "unknown version" BuildTime = "unknown time" )
var DNSModeMapping = map[string]DNSMode{ DNSNormal.String(): DNSNormal, DNSFakeIP.String(): DNSFakeIP, DNSSniffing.String(): DNSSniffing, }
DNSModeMapping is a mapping for EnhancedMode enum
var (
GetScriptProxyProviders = defaultProxyProvidersGetter
)
var MetadataTypeMapping = map[string]Type{ strings.ToLower(HTTP.String()): HTTP, strings.ToLower(HTTPCONNECT.String()): HTTPCONNECT, strings.ToLower(SOCKS4.String()): SOCKS4, strings.ToLower(SOCKS5.String()): SOCKS5, strings.ToLower(REDIR.String()): REDIR, strings.ToLower(TPROXY.String()): TPROXY, strings.ToLower(TUN.String()): TUN, strings.ToLower(MITM.String()): MITM, strings.ToLower(TUNNEL.String()): TUNNEL, }
var Path = func() *path { homeDir, err := os.UserHomeDir() if err != nil { homeDir, _ = os.Getwd() } homeDir = P.Join(homeDir, ".config", Name) if _, err = os.Stat(homeDir); err != nil { if home, ok := os.LookupEnv("XDG_CONFIG_HOME"); ok { homeDir = P.Join(home, Name) } } return &path{homeDir: homeDir, configFile: "config.yaml"} }()
Path is used to get the configuration path
var RewriteTypeMapping = map[string]RewriteType{ MitmReject.String(): MitmReject, MitmReject200.String(): MitmReject200, MitmReject204.String(): MitmReject204, MitmRejectImg.String(): MitmRejectImg, MitmRejectDict.String(): MitmRejectDict, MitmRejectArray.String(): MitmRejectArray, Mitm302.String(): Mitm302, Mitm307.String(): Mitm307, MitmRequestHeader.String(): MitmRequestHeader, MitmRequestBody.String(): MitmRequestBody, MitmResponseHeader.String(): MitmResponseHeader, MitmResponseBody.String(): MitmResponseBody, }
Functions ¶
func BackupScriptState ¶
func BackupScriptState()
func EncodeLatin1 ¶
func GetScriptRuleProviders ¶
func ProxyURL ¶
ProxyURL returns a proxy function (for use in a http.Transport), nil if no inbound ports are set
func RestoreScriptState ¶
func RestoreScriptState()
func SetLastTunConf ¶
func SetLastTunConf(conf *Tun)
func SetProxyInbound ¶
SetProxyInbound assigns a http or socks inbound to proxyInbound
func SetScriptRuleProviders ¶
Types ¶
type AdapterType ¶
type AdapterType int
AdapterType is enum of adapter type
const ( Direct AdapterType = iota Reject Mitm Shadowsocks ShadowsocksR Snell Socks5 Http Vmess Vless Trojan WireGuard Hysteria2 Relay Selector Fallback URLTest LoadBalance )
Adapter Type
func (AdapterType) String ¶
func (at AdapterType) String() string
type AuthenticatorListener ¶
type Conn ¶
type Conn interface { net.Conn Connection }
type ConnContext ¶
type Connection ¶
type Connection interface { Chains() Chain AppendToChains(adapter ProxyAdapter) SetChains(chains []string) String() string }
type DNSAddrPort ¶
func (DNSAddrPort) String ¶
func (p DNSAddrPort) String() string
func (*DNSAddrPort) UnmarshalText ¶
func (p *DNSAddrPort) UnmarshalText(text []byte) error
type DNSMode ¶
type DNSMode int
func (DNSMode) MarshalJSON ¶
MarshalJSON serialize EnhancedMode with json
func (DNSMode) MarshalYAML ¶
MarshalYAML serialize EnhancedMode with yaml
func (*DNSMode) UnmarshalJSON ¶
UnmarshalJSON unserialize EnhancedMode with json
type DNSUrl ¶
type DNSUrl struct { Network string AddrPort DNSAddrPort }
func (DNSUrl) MarshalJSON ¶
func (DNSUrl) MarshalYAML ¶
func (*DNSUrl) UnmarshalJSON ¶
type DelayHistory ¶
type Inbound ¶
type Inbound inbound
func (*Inbound) MarshalJSON ¶
MarshalJSON implements encoding/json.Marshaler
func (*Inbound) UnmarshalJSON ¶
UnmarshalJSON implements encoding/json.Unmarshaler
type InboundType ¶
type InboundType string
const ( InboundTypeSocks InboundType = "socks" InboundTypeSocks4 InboundType = "socks4" InboundTypeSocks5 InboundType = "socks5" InboundTypeRedir InboundType = "redir" InboundTypeTproxy InboundType = "tproxy" InboundTypeHTTP InboundType = "http" InboundTypeMixed InboundType = "mixed" InboundTypeMitm InboundType = "mitm" )
type Metadata ¶
type Metadata struct { NetWork NetWork `json:"network"` Type Type `json:"type"` SrcIP netip.Addr `json:"sourceIP"` DstIP netip.Addr `json:"destinationIP"` SrcPort Port `json:"sourcePort"` DstPort Port `json:"destinationPort"` Host string `json:"host"` DNSMode DNSMode `json:"dnsMode"` Process string `json:"process"` ProcessPath string `json:"processPath"` UserAgent string `json:"userAgent"` SpecialProxy string `json:"specialProxy"` OriginDst netip.AddrPort `json:"-"` }
Metadata is used to store connection address
func (*Metadata) MarshalObject ¶
func (*Metadata) NetworkFromString ¶
func (*Metadata) RemoteAddress ¶
func (*Metadata) SourceAddress ¶
func (*Metadata) TypeFromString ¶
type MitmOption ¶
type MitmSession ¶
type MitmSession struct { Conn net.Conn Request *http.Request Response *http.Response // contains filtered or unexported fields }
func NewMitmSession ¶
func (*MitmSession) GetProperties ¶
func (s *MitmSession) GetProperties(key string) (any, bool)
func (*MitmSession) NewErrorResponse ¶
func (s *MitmSession) NewErrorResponse(err error) *http.Response
func (*MitmSession) NewResponse ¶
func (*MitmSession) SetProperties ¶
func (s *MitmSession) SetProperties(key string, val any)
func (*MitmSession) WriteResponse ¶
func (s *MitmSession) WriteResponse() (err error)
type PacketConn ¶
type PacketConn interface { net.PacketConn Connection }
type PacketConnContext ¶
type PacketConnContext interface { PlainContext Metadata() *Metadata PacketConn() net.PacketConn }
type PlainContext ¶
type ProxyAdapter ¶
type ProxyAdapter interface { Name() string Type() AdapterType Addr() string SupportUDP() bool DisableDnsResolve() bool MarshalJSON() ([]byte, error) // StreamConn wraps a protocol around net.Conn with Metadata. // // Examples: // conn, _ := net.DialContext(context.Background(), "tcp", "host:port") // conn, _ = adapter.StreamConn(conn, metadata) // // It returns a C.Conn with protocol which start with // a new session (if any) StreamConn(c net.Conn, metadata *Metadata) (net.Conn, error) // StreamPacketConn wraps a UDP protocol around net.Conn with Metadata. StreamPacketConn(c net.Conn, metadata *Metadata) (net.Conn, error) // DialContext return a C.Conn with protocol which // contains multiplexing-related reuse logic (if any) DialContext(ctx context.Context, metadata *Metadata, opts ...dialer.Option) (Conn, error) // ListenPacketContext listen for a PacketConn ListenPacketContext(ctx context.Context, metadata *Metadata, opts ...dialer.Option) (PacketConn, error) // Unwrap extracts the proxy from a proxy-group. It returns nil when nothing to extract. Unwrap(metadata *Metadata) Proxy // Cleanup released resources. Cleanup() }
type RawProxy ¶
type RewriteHandler ¶
type RewriteHandler interface { HandleRequest(*MitmSession) (*http.Request, *http.Response) // session.Response maybe nil HandleResponse(*MitmSession) *http.Response HandleApiRequest(*MitmSession) bool HandleError(*MitmSession, error) // session maybe nil }
type RewriteRule ¶
type RewriteType ¶
type RewriteType int
const ( MitmReject RewriteType = iota + 1 MitmReject200 MitmReject204 MitmRejectImg MitmRejectDict MitmRejectArray Mitm302 Mitm307 MitmRequestHeader MitmRequestBody MitmResponseHeader MitmResponseBody )
func (RewriteType) String ¶
func (rt RewriteType) String() string
type RuleGeoSite ¶
type RuleGeoSite interface {
GetDomainMatcher() *router.DomainMatcher
}
type RuleType ¶
type RuleType int
type TUNChangeCallback ¶
type TUNChangeCallback interface { Pause() Resume() }
type TUNStack ¶
type TUNStack int
func (TUNStack) MarshalJSON ¶
MarshalJSON serialize TUNStack with json
func (TUNStack) MarshalYAML ¶
MarshalYAML serialize TUNStack with yaml
func (*TUNStack) UnmarshalJSON ¶
UnmarshalJSON unserialize TUNStack with json
type Tun ¶
type Tun struct { Enable bool `yaml:"enable" json:"enable"` Device string `yaml:"device" json:"device"` Stack TUNStack `yaml:"stack" json:"stack"` DNSHijack []DNSUrl `yaml:"dns-hijack" json:"dns-hijack"` AutoRoute bool `yaml:"auto-route" json:"auto-route"` AutoDetectInterface bool `yaml:"auto-detect-interface" json:"auto-detect-interface"` TunAddressPrefix *netip.Prefix `yaml:"-" json:"-"` RedirectToTun []string `yaml:"-" json:"-"` StopRouteListener bool `yaml:"-" json:"-"` }
Tun config
func GetLastTunConf ¶
func GetLastTunConf() *Tun
type UDPPacket ¶
type UDPPacket interface { // Data get the payload of UDP Packet Data() *[]byte // WriteBack writes the payload with source IP/Port equals addr // - variable source IP/Port is important to STUN // - if addr is not provided, WriteBack will write out UDP packet with SourceIP/Port equals to original Target, // this is important when using Fake-IP. WriteBack(b []byte, addr net.Addr) (n int, err error) // Drop call after packet is used, could recycle buffer in this function. Drop() // LocalAddr returns the source IP/Port of packet LocalAddr() net.Addr }
UDPPacket contains the data of UDP packet, and offers control/info of UDP packet's source