mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2024-11-16 11:42:43 +08:00
Compare commits
4 Commits
70fb352b9c
...
00d58f9972
Author | SHA1 | Date | |
---|---|---|---|
|
00d58f9972 | ||
|
50d60cb004 | ||
|
256e9d8c3d | ||
|
828ba83ef3 |
|
@ -10,6 +10,7 @@ import (
|
||||||
"github.com/metacubex/mihomo/component/loopback"
|
"github.com/metacubex/mihomo/component/loopback"
|
||||||
"github.com/metacubex/mihomo/component/resolver"
|
"github.com/metacubex/mihomo/component/resolver"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
|
"github.com/metacubex/mihomo/constant/features"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Direct struct {
|
type Direct struct {
|
||||||
|
@ -24,8 +25,10 @@ type DirectOption struct {
|
||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (d *Direct) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
func (d *Direct) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
||||||
if err := d.loopBack.CheckConn(metadata); err != nil {
|
if !features.CMFA {
|
||||||
return nil, err
|
if err := d.loopBack.CheckConn(metadata); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
opts = append(opts, dialer.WithResolver(resolver.DefaultResolver))
|
opts = append(opts, dialer.WithResolver(resolver.DefaultResolver))
|
||||||
c, err := dialer.DialContext(ctx, "tcp", metadata.RemoteAddress(), d.Base.DialOptions(opts...)...)
|
c, err := dialer.DialContext(ctx, "tcp", metadata.RemoteAddress(), d.Base.DialOptions(opts...)...)
|
||||||
|
@ -38,8 +41,10 @@ func (d *Direct) DialContext(ctx context.Context, metadata *C.Metadata, opts ...
|
||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
func (d *Direct) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
func (d *Direct) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
||||||
if err := d.loopBack.CheckPacketConn(metadata); err != nil {
|
if !features.CMFA {
|
||||||
return nil, err
|
if err := d.loopBack.CheckPacketConn(metadata); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// net.UDPConn.WriteTo only working with *net.UDPAddr, so we need a net.UDPAddr
|
// net.UDPConn.WriteTo only working with *net.UDPAddr, so we need a net.UDPAddr
|
||||||
if !metadata.Resolved() {
|
if !metadata.Resolved() {
|
||||||
|
|
|
@ -5,8 +5,8 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
"net"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/adapter/outbound"
|
"github.com/metacubex/mihomo/adapter/outbound"
|
||||||
|
@ -134,31 +134,31 @@ func (lb *LoadBalance) IsL3Protocol(metadata *C.Metadata) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func strategyRoundRobin(url string) strategyFn {
|
func strategyRoundRobin(url string) strategyFn {
|
||||||
|
var availableProxies []C.Proxy
|
||||||
idx := 0
|
idx := 0
|
||||||
idxMutex := sync.Mutex{}
|
idxMutex := sync.Mutex{}
|
||||||
|
|
||||||
return func(proxies []C.Proxy, metadata *C.Metadata, touch bool) C.Proxy {
|
return func(proxies []C.Proxy, metadata *C.Metadata, touch bool) C.Proxy {
|
||||||
idxMutex.Lock()
|
idxMutex.Lock()
|
||||||
defer idxMutex.Unlock()
|
defer idxMutex.Unlock()
|
||||||
|
|
||||||
i := 0
|
|
||||||
length := len(proxies)
|
|
||||||
|
|
||||||
if touch {
|
if touch {
|
||||||
defer func() {
|
// check list
|
||||||
idx = (idx + i) % length
|
availableProxies = []C.Proxy{}
|
||||||
}()
|
for _, proxy := range proxies {
|
||||||
}
|
if proxy.AliveForTestUrl(url) {
|
||||||
|
availableProxies = append(availableProxies, proxy)
|
||||||
for ; i < length; i++ {
|
}
|
||||||
id := (idx + i) % length
|
}
|
||||||
proxy := proxies[id]
|
// fallback
|
||||||
if proxy.AliveForTestUrl(url) {
|
if len(availableProxies) == 0 {
|
||||||
i++
|
return proxies[0]
|
||||||
return proxy
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
proxy := availableProxies[idx]
|
||||||
return proxies[0]
|
// reset idx
|
||||||
|
idx = (idx + 1) % len(availableProxies)
|
||||||
|
return proxy
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user