2019-12-08 12:17:24 +08:00
|
|
|
package outboundgroup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2022-05-23 00:40:27 +08:00
|
|
|
"errors"
|
2023-01-07 12:24:28 +08:00
|
|
|
"time"
|
|
|
|
|
2023-11-03 21:01:45 +08:00
|
|
|
"github.com/metacubex/mihomo/adapter/outbound"
|
|
|
|
"github.com/metacubex/mihomo/common/callback"
|
|
|
|
N "github.com/metacubex/mihomo/common/net"
|
|
|
|
"github.com/metacubex/mihomo/common/utils"
|
|
|
|
"github.com/metacubex/mihomo/component/dialer"
|
|
|
|
C "github.com/metacubex/mihomo/constant"
|
|
|
|
"github.com/metacubex/mihomo/constant/provider"
|
2019-12-08 12:17:24 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Fallback struct {
|
2022-04-28 19:01:13 +08:00
|
|
|
*GroupBase
|
2023-06-04 11:51:30 +08:00
|
|
|
disableUDP bool
|
|
|
|
testUrl string
|
|
|
|
selected string
|
|
|
|
expectedStatus string
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Fallback) Now() string {
|
2020-11-19 00:53:22 +08:00
|
|
|
proxy := f.findAliveProxy(false)
|
2019-12-08 12:17:24 +08:00
|
|
|
return proxy.Name()
|
|
|
|
}
|
|
|
|
|
2021-04-29 11:23:14 +08:00
|
|
|
// DialContext implements C.ProxyAdapter
|
2021-11-07 16:48:51 +08:00
|
|
|
func (f *Fallback) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
2020-11-19 00:53:22 +08:00
|
|
|
proxy := f.findAliveProxy(true)
|
2021-11-07 16:48:51 +08:00
|
|
|
c, err := proxy.DialContext(ctx, metadata, f.Base.DialOptions(opts...)...)
|
2019-12-08 12:17:24 +08:00
|
|
|
if err == nil {
|
|
|
|
c.AppendToChains(f)
|
2021-12-03 14:35:21 +08:00
|
|
|
} else {
|
2022-10-16 13:12:49 +08:00
|
|
|
f.onDialFailed(proxy.Type(), err)
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
2021-12-04 00:16:39 +08:00
|
|
|
|
2023-02-27 00:26:49 +08:00
|
|
|
if N.NeedHandshake(c) {
|
2023-04-01 20:56:49 +08:00
|
|
|
c = callback.NewFirstWriteCallBackConn(c, func(err error) {
|
|
|
|
if err == nil {
|
|
|
|
f.onDialSuccess()
|
|
|
|
} else {
|
|
|
|
f.onDialFailed(proxy.Type(), err)
|
|
|
|
}
|
|
|
|
})
|
2023-02-24 09:54:54 +08:00
|
|
|
}
|
|
|
|
|
2019-12-08 12:17:24 +08:00
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
|
2021-10-15 21:44:53 +08:00
|
|
|
// ListenPacketContext implements C.ProxyAdapter
|
2021-11-07 16:48:51 +08:00
|
|
|
func (f *Fallback) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
2020-11-19 00:53:22 +08:00
|
|
|
proxy := f.findAliveProxy(true)
|
2021-11-07 16:48:51 +08:00
|
|
|
pc, err := proxy.ListenPacketContext(ctx, metadata, f.Base.DialOptions(opts...)...)
|
2019-12-08 12:17:24 +08:00
|
|
|
if err == nil {
|
|
|
|
pc.AppendToChains(f)
|
|
|
|
}
|
2021-12-04 00:16:39 +08:00
|
|
|
|
2020-01-31 14:43:54 +08:00
|
|
|
return pc, err
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
|
2021-04-29 11:23:14 +08:00
|
|
|
// SupportUDP implements C.ProxyAdapter
|
2019-12-08 12:17:24 +08:00
|
|
|
func (f *Fallback) SupportUDP() bool {
|
2020-11-13 21:48:52 +08:00
|
|
|
if f.disableUDP {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-11-19 00:53:22 +08:00
|
|
|
proxy := f.findAliveProxy(false)
|
2019-12-08 12:17:24 +08:00
|
|
|
return proxy.SupportUDP()
|
|
|
|
}
|
|
|
|
|
2023-04-11 14:10:57 +08:00
|
|
|
// IsL3Protocol implements C.ProxyAdapter
|
|
|
|
func (f *Fallback) IsL3Protocol(metadata *C.Metadata) bool {
|
|
|
|
return f.findAliveProxy(false).IsL3Protocol(metadata)
|
|
|
|
}
|
|
|
|
|
2021-04-29 11:23:14 +08:00
|
|
|
// MarshalJSON implements C.ProxyAdapter
|
2019-12-08 12:17:24 +08:00
|
|
|
func (f *Fallback) MarshalJSON() ([]byte, error) {
|
2022-03-23 13:48:21 +08:00
|
|
|
all := []string{}
|
2022-04-28 19:01:13 +08:00
|
|
|
for _, proxy := range f.GetProxies(false) {
|
2019-12-08 12:17:24 +08:00
|
|
|
all = append(all, proxy.Name())
|
|
|
|
}
|
2022-03-16 12:10:13 +08:00
|
|
|
return json.Marshal(map[string]any{
|
2023-12-01 16:44:30 +08:00
|
|
|
"type": f.Type().String(),
|
|
|
|
"now": f.Now(),
|
|
|
|
"all": all,
|
|
|
|
"testUrl": f.testUrl,
|
|
|
|
"expectedStatus": f.expectedStatus,
|
2023-12-26 03:49:00 +08:00
|
|
|
"fixed": f.selected,
|
2019-12-08 12:17:24 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-29 11:23:14 +08:00
|
|
|
// Unwrap implements C.ProxyAdapter
|
2022-10-30 23:08:18 +08:00
|
|
|
func (f *Fallback) Unwrap(metadata *C.Metadata, touch bool) C.Proxy {
|
|
|
|
proxy := f.findAliveProxy(touch)
|
2020-05-07 21:42:52 +08:00
|
|
|
return proxy
|
|
|
|
}
|
|
|
|
|
2020-11-19 00:53:22 +08:00
|
|
|
func (f *Fallback) findAliveProxy(touch bool) C.Proxy {
|
2022-04-28 19:01:13 +08:00
|
|
|
proxies := f.GetProxies(touch)
|
2022-08-13 17:23:42 +08:00
|
|
|
for _, proxy := range proxies {
|
|
|
|
if len(f.selected) == 0 {
|
2023-12-22 21:18:17 +08:00
|
|
|
if proxy.AliveForTestUrl(f.testUrl) {
|
2022-08-13 17:23:42 +08:00
|
|
|
return proxy
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if proxy.Name() == f.selected {
|
2023-12-22 21:18:17 +08:00
|
|
|
if proxy.AliveForTestUrl(f.testUrl) {
|
2022-08-13 17:23:42 +08:00
|
|
|
return proxy
|
|
|
|
} else {
|
|
|
|
f.selected = ""
|
|
|
|
}
|
|
|
|
}
|
2022-05-23 00:40:27 +08:00
|
|
|
}
|
|
|
|
}
|
2022-08-13 17:23:42 +08:00
|
|
|
|
|
|
|
return proxies[0]
|
2022-05-23 00:40:27 +08:00
|
|
|
}
|
|
|
|
|
2022-06-03 16:50:05 +08:00
|
|
|
func (f *Fallback) Set(name string) error {
|
2022-06-03 13:31:56 +08:00
|
|
|
var p C.Proxy
|
2022-05-23 00:40:27 +08:00
|
|
|
for _, proxy := range f.GetProxies(false) {
|
|
|
|
if proxy.Name() == name {
|
2022-06-03 13:31:56 +08:00
|
|
|
p = proxy
|
|
|
|
break
|
2022-05-23 00:40:27 +08:00
|
|
|
}
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
|
2022-06-03 13:31:56 +08:00
|
|
|
if p == nil {
|
|
|
|
return errors.New("proxy not exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
f.selected = name
|
2023-12-22 21:18:17 +08:00
|
|
|
if !p.AliveForTestUrl(f.testUrl) {
|
2022-06-03 13:31:56 +08:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*time.Duration(5000))
|
|
|
|
defer cancel()
|
2023-06-04 11:51:30 +08:00
|
|
|
expectedStatus, _ := utils.NewIntRanges[uint16](f.expectedStatus)
|
2023-12-01 16:44:30 +08:00
|
|
|
_, _ = p.URLTest(ctx, f.testUrl, expectedStatus)
|
2022-06-03 13:31:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
|
2023-04-24 08:07:17 +08:00
|
|
|
func (f *Fallback) ForceSet(name string) {
|
|
|
|
f.selected = name
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:48:51 +08:00
|
|
|
func NewFallback(option *GroupCommonOption, providers []provider.ProxyProvider) *Fallback {
|
2019-12-08 12:17:24 +08:00
|
|
|
return &Fallback{
|
2022-04-28 19:01:13 +08:00
|
|
|
GroupBase: NewGroupBase(GroupBaseOption{
|
|
|
|
outbound.BaseOption{
|
|
|
|
Name: option.Name,
|
|
|
|
Type: C.Fallback,
|
|
|
|
Interface: option.Interface,
|
|
|
|
RoutingMark: option.RoutingMark,
|
|
|
|
},
|
|
|
|
option.Filter,
|
2022-11-09 08:06:37 +08:00
|
|
|
option.ExcludeFilter,
|
2023-01-07 12:24:28 +08:00
|
|
|
option.ExcludeType,
|
2022-04-28 19:01:13 +08:00
|
|
|
providers,
|
2021-11-07 16:48:51 +08:00
|
|
|
}),
|
2023-06-04 11:51:30 +08:00
|
|
|
disableUDP: option.DisableUDP,
|
|
|
|
testUrl: option.URL,
|
|
|
|
expectedStatus: option.ExpectedStatus,
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
}
|