Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
snakem982 committed Nov 28, 2024
2 parents 129fe9f + 9de9f1e commit a2da994
Show file tree
Hide file tree
Showing 26 changed files with 223 additions and 108 deletions.
15 changes: 11 additions & 4 deletions adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,17 @@ func (p *Proxy) MarshalJSON() ([]byte, error) {
mapping["alive"] = p.alive.Load()
mapping["name"] = p.Name()
mapping["udp"] = p.SupportUDP()
mapping["xudp"] = p.SupportXUDP()
mapping["tfo"] = p.SupportTFO()
mapping["mptcp"] = p.SupportMPTCP()
mapping["smux"] = p.SupportSMUX()
mapping["uot"] = p.SupportUOT()

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)
}

Expand Down
23 changes: 23 additions & 0 deletions adapter/inbound/listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package inbound

import (
"context"
"fmt"
"net"
"net/netip"
"sync"

"github.com/metacubex/mihomo/component/keepalive"
Expand Down Expand Up @@ -42,6 +44,27 @@ func MPTCP() bool {
}

func ListenContext(ctx context.Context, network, address string) (net.Listener, error) {
switch network { // like net.Resolver.internetAddrList but filter domain to avoid call net.Resolver.lookupIPAddr
case "tcp", "tcp4", "tcp6", "udp", "udp4", "udp6", "ip", "ip4", "ip6":
if host, port, err := net.SplitHostPort(address); err == nil {
switch host {
case "localhost":
switch network {
case "tcp6", "udp6", "ip6":
address = net.JoinHostPort("::1", port)
default:
address = net.JoinHostPort("127.0.0.1", port)
}
case "": // internetAddrList can handle this special case
break
default:
if _, err := netip.ParseAddr(host); err != nil { // not ip
return nil, fmt.Errorf("invalid network address: %s", address)
}
}
}
}

mutex.RLock()
defer mutex.RUnlock()
return lc.Listen(ctx, network, address)
Expand Down
27 changes: 9 additions & 18 deletions adapter/outbound/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +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
// 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
Expand Down
7 changes: 7 additions & 0 deletions adapter/outbound/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ func (h *Http) SupportWithDialer() C.NetWork {
return C.TCP
}

// 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 {
addr := metadata.RemoteAddress()
HeaderString := "CONNECT " + addr + " HTTP/1.1\r\n"
Expand Down
7 changes: 7 additions & 0 deletions adapter/outbound/hysteria.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ func (h *Hysteria) genHdc(ctx context.Context, opts ...dialer.Option) utils.Pack
}
}

// ProxyInfo implements C.ProxyAdapter
func (h *Hysteria) ProxyInfo() C.ProxyInfo {
info := h.Base.ProxyInfo()
info.DialerProxy = h.option.DialerProxy
return info
}

type HysteriaOption struct {
BasicOption
Name string `proxy:"name"`
Expand Down
7 changes: 7 additions & 0 deletions adapter/outbound/hysteria2.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ func closeHysteria2(h *Hysteria2) {
}
}

// 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) {
addr := net.JoinHostPort(option.Server, strconv.Itoa(option.Port))
var salamanderPassword string
Expand Down
7 changes: 7 additions & 0 deletions adapter/outbound/mieru.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ func (m *Mieru) DialContext(ctx context.Context, metadata *C.Metadata, _ ...dial
return NewConn(c, m), nil
}

// ProxyInfo implements C.ProxyAdapter
func (m *Mieru) ProxyInfo() C.ProxyInfo {
info := m.Base.ProxyInfo()
info.DialerProxy = m.option.DialerProxy
return info
}

func NewMieru(option MieruOption) (*Mieru, error) {
config, err := buildMieruClientConfig(option)
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions adapter/outbound/shadowsocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,13 @@ func (ss *ShadowSocks) SupportWithDialer() C.NetWork {
return C.ALLNet
}

// 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
func (ss *ShadowSocks) ListenPacketOnStreamConn(ctx context.Context, c net.Conn, metadata *C.Metadata) (_ C.PacketConn, err error) {
if ss.option.UDPOverTCP {
Expand Down
7 changes: 7 additions & 0 deletions adapter/outbound/shadowsocksr.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,13 @@ func (ssr *ShadowSocksR) SupportWithDialer() C.NetWork {
return C.ALLNet
}

// 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) {
// SSR protocol compatibility
// https://github.com/metacubex/mihomo/pull/2056
Expand Down
6 changes: 4 additions & 2 deletions adapter/outbound/singmux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
9 changes: 8 additions & 1 deletion adapter/outbound/snell.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ func (s *Snell) SupportUOT() bool {
return true
}

// 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) {
addr := net.JoinHostPort(option.Server, strconv.Itoa(option.Port))
psk := []byte(option.Psk)
Expand Down Expand Up @@ -204,7 +211,7 @@ func NewSnell(option SnellOption) (*Snell, error) {
if err != nil {
return nil, err
}

return streamConn(c, streamOption{psk, option.Version, addr, obfsOption}), nil
})
}
Expand Down
7 changes: 7 additions & 0 deletions adapter/outbound/socks5.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,13 @@ func (ss *Socks5) ListenPacketContext(ctx context.Context, metadata *C.Metadata,
return newPacketConn(&socksPacketConn{PacketConn: pc, rAddr: bindUDPAddr, tcpConn: c}, ss), nil
}

// 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) {
var tlsConfig *tls.Config
if option.TLS {
Expand Down
7 changes: 7 additions & 0 deletions adapter/outbound/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ func closeSsh(s *Ssh) {
_ = s.client.Close()
}

// 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) {
addr := net.JoinHostPort(option.Server, strconv.Itoa(option.Port))

Expand Down
7 changes: 7 additions & 0 deletions adapter/outbound/trojan.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,13 @@ func (t *Trojan) SupportUOT() bool {
return true
}

// 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) {
addr := net.JoinHostPort(option.Server, strconv.Itoa(option.Port))

Expand Down
7 changes: 7 additions & 0 deletions adapter/outbound/tuic.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ func (t *Tuic) dialWithDialer(ctx context.Context, dialer C.Dialer) (transport *
return
}

// 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) {
addr := net.JoinHostPort(option.Server, strconv.Itoa(option.Port))
serverName := option.Server
Expand Down
7 changes: 7 additions & 0 deletions adapter/outbound/vless.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,13 @@ func (v *Vless) SupportUOT() bool {
return true
}

// 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 {
var addrType byte
var addr []byte
Expand Down
7 changes: 7 additions & 0 deletions adapter/outbound/vmess.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,13 @@ func (v *Vmess) SupportWithDialer() C.NetWork {
return C.ALLNet
}

// 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
func (v *Vmess) ListenPacketOnStreamConn(ctx context.Context, c net.Conn, metadata *C.Metadata) (_ C.PacketConn, err error) {
// vmess use stream-oriented udp with a special address, so we need a net.UDPAddr
Expand Down
27 changes: 3 additions & 24 deletions adapter/outbound/wireguard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion adapter/provider/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type proxyProviderSchema struct {
ExcludeFilter string `provider:"exclude-filter,omitempty"`
ExcludeType string `provider:"exclude-type,omitempty"`
DialerProxy string `provider:"dialer-proxy,omitempty"`
SizeLimit int64 `provider:"size-limit,omitempty"`

HealthCheck healthCheckSchema `provider:"health-check,omitempty"`
Override OverrideSchema `provider:"override,omitempty"`
Expand Down Expand Up @@ -111,7 +112,7 @@ func ParseProxyProvider(name string, mapping map[string]any) (types.ProxyProvide
return nil, fmt.Errorf("%w: %s", errSubPath, path)
}
}
vehicle = resource.NewHTTPVehicle(schema.URL, path, schema.Proxy, schema.Header, resource.DefaultHttpTimeout)
vehicle = resource.NewHTTPVehicle(schema.URL, path, schema.Proxy, schema.Header, resource.DefaultHttpTimeout, schema.SizeLimit)
default:
return nil, fmt.Errorf("%w: %s", errVehicleType, schema.Type)
}
Expand Down
Loading

0 comments on commit a2da994

Please sign in to comment.