From f805a9f4c694d384b689a977a0acf86cf6fba020 Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Tue, 26 Nov 2024 10:04:41 +0800 Subject: [PATCH] chore: cleaned up some weird code --- adapter/adapter.go | 17 ++++++----- adapter/outbound/base.go | 39 ++++++-------------------- adapter/outbound/http.go | 8 ++++-- adapter/outbound/hysteria.go | 8 ++++-- adapter/outbound/hysteria2.go | 8 ++++-- adapter/outbound/shadowsocks.go | 8 ++++-- adapter/outbound/shadowsocksr.go | 8 ++++-- adapter/outbound/singmux.go | 6 ++-- adapter/outbound/snell.go | 8 ++++-- adapter/outbound/socks5.go | 8 ++++-- adapter/outbound/ssh.go | 8 ++++-- adapter/outbound/trojan.go | 8 ++++-- adapter/outbound/tuic.go | 8 ++++-- adapter/outbound/vless.go | 8 ++++-- adapter/outbound/vmess.go | 7 +++-- adapter/outbound/wireguard.go | 48 ++------------------------------ constant/adapters.go | 20 ++++++++----- 17 files changed, 99 insertions(+), 126 deletions(-) diff --git a/adapter/adapter.go b/adapter/adapter.go index befe2113..31706a2e 100644 --- a/adapter/adapter.go +++ b/adapter/adapter.go @@ -164,13 +164,16 @@ func (p *Proxy) MarshalJSON() ([]byte, error) { mapping["name"] = p.Name() mapping["udp"] = p.SupportUDP() mapping["uot"] = p.SupportUOT() - mapping["xudp"] = p.SupportXUDP() - mapping["tfo"] = p.SupportTFO() - mapping["mptcp"] = p.SupportMPTCP() - mapping["smux"] = p.SupportSMUX() - mapping["interface"] = p.SupportInterface() - mapping["dialer-proxy"] = p.SupportDialerProxy() - mapping["routing-mark"] = p.SupportRoutingMark() + + proxyInfo := p.ProxyInfo() + mapping["xudp"] = proxyInfo.XUDP + mapping["tfo"] = proxyInfo.TFO + mapping["mptcp"] = proxyInfo.MPTCP + mapping["smux"] = proxyInfo.SMUX + mapping["interface"] = proxyInfo.Interface + mapping["dialer-proxy"] = proxyInfo.DialerProxy + mapping["routing-mark"] = proxyInfo.RoutingMark + return json.Marshal(mapping) } diff --git a/adapter/outbound/base.go b/adapter/outbound/base.go index c15e7115..dd226f74 100644 --- a/adapter/outbound/base.go +++ b/adapter/outbound/base.go @@ -85,36 +85,15 @@ func (b *Base) SupportUDP() bool { return b.udp } -// SupportXUDP implements C.ProxyAdapter -func (b *Base) SupportXUDP() bool { - return b.xudp -} - -// SupportTFO implements C.ProxyAdapter -func (b *Base) SupportTFO() bool { - return b.tfo -} - -// SupportMPTCP implements C.ProxyAdapter -func (b *Base) SupportMPTCP() bool { - return b.mpTcp -} - -// SupportSMUX implements C.ProxyAdapter -func (b *Base) SupportSMUX() bool { - return false -} - -func (b *Base) SupportDialerProxy() string { - return "" -} - -func (b *Base) SupportInterface() string { - return b.iface -} - -func (b *Base) SupportRoutingMark() int { - return b.rmark +// ProxyInfo implements C.ProxyAdapter +func (b *Base) ProxyInfo() (info C.ProxyInfo) { + info.XUDP = b.xudp + info.TFO = b.tfo + info.MPTCP = b.mpTcp + info.SMUX = false + info.Interface = b.iface + info.RoutingMark = b.rmark + return } // IsL3Protocol implements C.ProxyAdapter diff --git a/adapter/outbound/http.go b/adapter/outbound/http.go index 0f72f759..54e05f9d 100644 --- a/adapter/outbound/http.go +++ b/adapter/outbound/http.go @@ -92,9 +92,11 @@ func (h *Http) SupportWithDialer() C.NetWork { return C.TCP } -// SupportDialerProxy implements C.ProxyAdapter -func (h *Http) SupportDialerProxy() string { - return h.option.DialerProxy +// ProxyInfo implements C.ProxyAdapter +func (h *Http) ProxyInfo() C.ProxyInfo { + info := h.Base.ProxyInfo() + info.DialerProxy = h.option.DialerProxy + return info } func (h *Http) shakeHand(metadata *C.Metadata, rw io.ReadWriter) error { diff --git a/adapter/outbound/hysteria.go b/adapter/outbound/hysteria.go index 964acb96..b0edab02 100644 --- a/adapter/outbound/hysteria.go +++ b/adapter/outbound/hysteria.go @@ -87,9 +87,11 @@ func (h *Hysteria) genHdc(ctx context.Context, opts ...dialer.Option) utils.Pack } } -// SupportDialerProxy implements C.ProxyAdapter -func (h *Hysteria) SupportDialerProxy() string { - return h.option.DialerProxy +// ProxyInfo implements C.ProxyAdapter +func (h *Hysteria) ProxyInfo() C.ProxyInfo { + info := h.Base.ProxyInfo() + info.DialerProxy = h.option.DialerProxy + return info } type HysteriaOption struct { diff --git a/adapter/outbound/hysteria2.go b/adapter/outbound/hysteria2.go index fbf0c2ec..e7a9adae 100644 --- a/adapter/outbound/hysteria2.go +++ b/adapter/outbound/hysteria2.go @@ -96,9 +96,11 @@ func closeHysteria2(h *Hysteria2) { } } -// SupportDialerProxy implements C.ProxyAdapter -func (h *Hysteria2) SupportDialerProxy() string { - return h.option.DialerProxy +// ProxyInfo implements C.ProxyAdapter +func (h *Hysteria2) ProxyInfo() C.ProxyInfo { + info := h.Base.ProxyInfo() + info.DialerProxy = h.option.DialerProxy + return info } func NewHysteria2(option Hysteria2Option) (*Hysteria2, error) { diff --git a/adapter/outbound/shadowsocks.go b/adapter/outbound/shadowsocks.go index a7c8f5bb..f73d5302 100644 --- a/adapter/outbound/shadowsocks.go +++ b/adapter/outbound/shadowsocks.go @@ -196,9 +196,11 @@ func (ss *ShadowSocks) SupportWithDialer() C.NetWork { return C.ALLNet } -// SupportDialerProxy implements C.ProxyAdapter -func (ss *ShadowSocks) SupportDialerProxy() string { - return ss.option.DialerProxy +// ProxyInfo implements C.ProxyAdapter +func (ss *ShadowSocks) ProxyInfo() C.ProxyInfo { + info := ss.Base.ProxyInfo() + info.DialerProxy = ss.option.DialerProxy + return info } // ListenPacketOnStreamConn implements C.ProxyAdapter diff --git a/adapter/outbound/shadowsocksr.go b/adapter/outbound/shadowsocksr.go index d217dd16..d0752e79 100644 --- a/adapter/outbound/shadowsocksr.go +++ b/adapter/outbound/shadowsocksr.go @@ -122,9 +122,11 @@ func (ssr *ShadowSocksR) SupportWithDialer() C.NetWork { return C.ALLNet } -// SupportDialerProxy implements C.ProxyAdapter -func (ssr *ShadowSocksR) SupportDialerProxy() string { - return ssr.option.DialerProxy +// ProxyInfo implements C.ProxyAdapter +func (ssr *ShadowSocksR) ProxyInfo() C.ProxyInfo { + info := ssr.Base.ProxyInfo() + info.DialerProxy = ssr.option.DialerProxy + return info } func NewShadowSocksR(option ShadowSocksROption) (*ShadowSocksR, error) { diff --git a/adapter/outbound/singmux.go b/adapter/outbound/singmux.go index 5cc4e748..26a8d26e 100644 --- a/adapter/outbound/singmux.go +++ b/adapter/outbound/singmux.go @@ -97,8 +97,10 @@ func (s *SingMux) SupportUOT() bool { return true } -func (s *SingMux) SupportSMUX() bool { - return true +func (s *SingMux) ProxyInfo() C.ProxyInfo { + info := s.ProxyAdapter.ProxyInfo() + info.SMUX = true + return info } func closeSingMux(s *SingMux) { diff --git a/adapter/outbound/snell.go b/adapter/outbound/snell.go index 7df3dd48..f4b94787 100644 --- a/adapter/outbound/snell.go +++ b/adapter/outbound/snell.go @@ -141,9 +141,11 @@ func (s *Snell) SupportUOT() bool { return true } -// SupportDialerProxy implements C.ProxyAdapter -func (s *Snell) SupportDialerProxy() string { - return s.option.DialerProxy +// ProxyInfo implements C.ProxyAdapter +func (s *Snell) ProxyInfo() C.ProxyInfo { + info := s.Base.ProxyInfo() + info.DialerProxy = s.option.DialerProxy + return info } func NewSnell(option SnellOption) (*Snell, error) { diff --git a/adapter/outbound/socks5.go b/adapter/outbound/socks5.go index 1b3f93c5..b8dd3b39 100644 --- a/adapter/outbound/socks5.go +++ b/adapter/outbound/socks5.go @@ -171,9 +171,11 @@ func (ss *Socks5) ListenPacketContext(ctx context.Context, metadata *C.Metadata, return newPacketConn(&socksPacketConn{PacketConn: pc, rAddr: bindUDPAddr, tcpConn: c}, ss), nil } -// SupportDialerProxy implements C.ProxyAdapter -func (ss *Socks5) SupportDialerProxy() string { - return ss.option.DialerProxy +// ProxyInfo implements C.ProxyAdapter +func (ss *Socks5) ProxyInfo() C.ProxyInfo { + info := ss.Base.ProxyInfo() + info.DialerProxy = ss.option.DialerProxy + return info } func NewSocks5(option Socks5Option) (*Socks5, error) { diff --git a/adapter/outbound/ssh.go b/adapter/outbound/ssh.go index 5ea9b29c..d746842d 100644 --- a/adapter/outbound/ssh.go +++ b/adapter/outbound/ssh.go @@ -121,9 +121,11 @@ func closeSsh(s *Ssh) { _ = s.client.Close() } -// SupportWithDialer implements C.ProxyAdapter -func (s *Ssh) SupportDialerProxy() string { - return s.option.DialerProxy +// ProxyInfo implements C.ProxyAdapter +func (s *Ssh) ProxyInfo() C.ProxyInfo { + info := s.Base.ProxyInfo() + info.DialerProxy = s.option.DialerProxy + return info } func NewSsh(option SshOption) (*Ssh, error) { diff --git a/adapter/outbound/trojan.go b/adapter/outbound/trojan.go index a23a52f3..b6265439 100644 --- a/adapter/outbound/trojan.go +++ b/adapter/outbound/trojan.go @@ -244,9 +244,11 @@ func (t *Trojan) SupportUOT() bool { return true } -// SupportDialerProxy implements C.ProxyAdapter -func (t *Trojan) SupportDialerProxy() string { - return t.option.DialerProxy +// ProxyInfo implements C.ProxyAdapter +func (t *Trojan) ProxyInfo() C.ProxyInfo { + info := t.Base.ProxyInfo() + info.DialerProxy = t.option.DialerProxy + return info } func NewTrojan(option TrojanOption) (*Trojan, error) { diff --git a/adapter/outbound/tuic.go b/adapter/outbound/tuic.go index f2c218db..eaacb817 100644 --- a/adapter/outbound/tuic.go +++ b/adapter/outbound/tuic.go @@ -146,9 +146,11 @@ func (t *Tuic) dialWithDialer(ctx context.Context, dialer C.Dialer) (transport * return } -// SupportDialerProxy implements C.ProxyAdapter -func (t *Tuic) SupportDialerProxy() string { - return t.option.DialerProxy +// ProxyInfo implements C.ProxyAdapter +func (t *Tuic) ProxyInfo() C.ProxyInfo { + info := t.Base.ProxyInfo() + info.DialerProxy = t.option.DialerProxy + return info } func NewTuic(option TuicOption) (*Tuic, error) { diff --git a/adapter/outbound/vless.go b/adapter/outbound/vless.go index 7fb409ae..7ab61ff6 100644 --- a/adapter/outbound/vless.go +++ b/adapter/outbound/vless.go @@ -379,9 +379,11 @@ func (v *Vless) SupportUOT() bool { return true } -// SupportDialerProxy implements C.ProxyAdapter -func (v *Vless) SupportDialerProxy() string { - return v.option.DialerProxy +// ProxyInfo implements C.ProxyAdapter +func (v *Vless) ProxyInfo() C.ProxyInfo { + info := v.Base.ProxyInfo() + info.DialerProxy = v.option.DialerProxy + return info } func parseVlessAddr(metadata *C.Metadata, xudp bool) *vless.DstAddr { diff --git a/adapter/outbound/vmess.go b/adapter/outbound/vmess.go index a10ac2e1..f7de4e8e 100644 --- a/adapter/outbound/vmess.go +++ b/adapter/outbound/vmess.go @@ -388,8 +388,11 @@ func (v *Vmess) SupportWithDialer() C.NetWork { return C.ALLNet } -func (v *Vmess) SupportDialerProxy() string { - return v.option.DialerProxy +// ProxyInfo implements C.ProxyAdapter +func (v *Vmess) ProxyInfo() C.ProxyInfo { + info := v.Base.ProxyInfo() + info.DialerProxy = v.option.DialerProxy + return info } // ListenPacketOnStreamConn implements C.ProxyAdapter diff --git a/adapter/outbound/wireguard.go b/adapter/outbound/wireguard.go index 470dc523..2834d324 100644 --- a/adapter/outbound/wireguard.go +++ b/adapter/outbound/wireguard.go @@ -611,32 +611,11 @@ func (r *refProxyAdapter) SupportUDP() bool { return false } -func (r *refProxyAdapter) SupportXUDP() bool { +func (r *refProxyAdapter) ProxyInfo() C.ProxyInfo { if r.proxyAdapter != nil { - return r.proxyAdapter.SupportXUDP() + return r.proxyAdapter.ProxyInfo() } - return false -} - -func (r *refProxyAdapter) SupportTFO() bool { - if r.proxyAdapter != nil { - return r.proxyAdapter.SupportTFO() - } - return false -} - -func (r *refProxyAdapter) SupportMPTCP() bool { - if r.proxyAdapter != nil { - return r.proxyAdapter.SupportMPTCP() - } - return false -} - -func (r *refProxyAdapter) SupportSMUX() bool { - if r.proxyAdapter != nil { - return r.proxyAdapter.SupportSMUX() - } - return false + return C.ProxyInfo{} } func (r *refProxyAdapter) MarshalJSON() ([]byte, error) { @@ -674,27 +653,6 @@ func (r *refProxyAdapter) SupportUOT() bool { return false } -func (r *refProxyAdapter) SupportDialerProxy() string { - if r.proxyAdapter != nil { - return r.proxyAdapter.SupportDialerProxy() - } - return "" -} - -func (r *refProxyAdapter) SupportInterface() string { - if r.proxyAdapter != nil { - return r.proxyAdapter.SupportInterface() - } - return "" -} - -func (r *refProxyAdapter) SupportRoutingMark() int { - if r.proxyAdapter != nil { - return r.proxyAdapter.SupportRoutingMark() - } - return 0 -} - func (r *refProxyAdapter) SupportWithDialer() C.NetWork { if r.proxyAdapter != nil { return r.proxyAdapter.SupportWithDialer() diff --git a/constant/adapters.go b/constant/adapters.go index ade0100c..664054d7 100644 --- a/constant/adapters.go +++ b/constant/adapters.go @@ -99,18 +99,24 @@ type Dialer interface { ListenPacket(ctx context.Context, network, address string, rAddrPort netip.AddrPort) (net.PacketConn, error) } +type ProxyInfo struct { + XUDP bool + TFO bool + MPTCP bool + SMUX bool + Interface string + RoutingMark int + DialerProxy string +} + type ProxyAdapter interface { Name() string Type() AdapterType Addr() string SupportUDP() bool - SupportXUDP() bool - SupportTFO() bool - SupportMPTCP() bool - SupportSMUX() bool - SupportInterface() string - SupportRoutingMark() int - SupportDialerProxy() string + + // ProxyInfo contains some extra information maybe useful for MarshalJSON + ProxyInfo() ProxyInfo MarshalJSON() ([]byte, error) // Deprecated: use DialContextWithDialer and ListenPacketWithDialer instead.