2020-03-21 23:46:49 +08:00
|
|
|
package outboundgroup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2024-01-07 23:32:22 +08:00
|
|
|
|
2023-11-03 21:01:45 +08:00
|
|
|
"github.com/metacubex/mihomo/adapter/outbound"
|
|
|
|
"github.com/metacubex/mihomo/component/dialer"
|
|
|
|
"github.com/metacubex/mihomo/component/proxydialer"
|
|
|
|
C "github.com/metacubex/mihomo/constant"
|
|
|
|
"github.com/metacubex/mihomo/constant/provider"
|
2020-03-21 23:46:49 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Relay struct {
|
2022-04-28 19:01:13 +08:00
|
|
|
*GroupBase
|
2024-01-07 23:32:22 +08:00
|
|
|
Hidden bool
|
|
|
|
Icon string
|
2020-03-21 23:46:49 +08:00
|
|
|
}
|
|
|
|
|
2021-04-29 11:23:14 +08:00
|
|
|
// DialContext implements C.ProxyAdapter
|
2021-11-07 16:48:51 +08:00
|
|
|
func (r *Relay) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
2022-04-30 11:36:42 +08:00
|
|
|
proxies, chainProxies := r.proxies(metadata, true)
|
2021-09-06 21:39:28 +08:00
|
|
|
|
|
|
|
switch len(proxies) {
|
|
|
|
case 0:
|
2021-11-07 16:48:51 +08:00
|
|
|
return outbound.NewDirect().DialContext(ctx, metadata, r.Base.DialOptions(opts...)...)
|
2021-09-06 21:39:28 +08:00
|
|
|
case 1:
|
2021-11-07 16:48:51 +08:00
|
|
|
return proxies[0].DialContext(ctx, metadata, r.Base.DialOptions(opts...)...)
|
2020-03-21 23:46:49 +08:00
|
|
|
}
|
2022-12-19 21:34:07 +08:00
|
|
|
var d C.Dialer
|
2022-12-20 00:11:02 +08:00
|
|
|
d = dialer.NewDialer(r.Base.DialOptions(opts...)...)
|
2022-12-19 21:34:07 +08:00
|
|
|
for _, proxy := range proxies[:len(proxies)-1] {
|
2023-04-11 21:42:16 +08:00
|
|
|
d = proxydialer.New(proxy, d, false)
|
2020-03-21 23:46:49 +08:00
|
|
|
}
|
2022-12-19 21:34:07 +08:00
|
|
|
last := proxies[len(proxies)-1]
|
|
|
|
conn, err := last.DialContextWithDialer(ctx, d, metadata)
|
2020-03-21 23:46:49 +08:00
|
|
|
if err != nil {
|
2022-12-19 21:34:07 +08:00
|
|
|
return nil, err
|
2020-03-21 23:46:49 +08:00
|
|
|
}
|
|
|
|
|
2022-04-30 11:36:42 +08:00
|
|
|
for i := len(chainProxies) - 2; i >= 0; i-- {
|
|
|
|
conn.AppendToChains(chainProxies[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
conn.AppendToChains(r)
|
|
|
|
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListenPacketContext implements C.ProxyAdapter
|
|
|
|
func (r *Relay) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.PacketConn, err error) {
|
|
|
|
proxies, chainProxies := r.proxies(metadata, true)
|
|
|
|
|
|
|
|
switch len(proxies) {
|
|
|
|
case 0:
|
|
|
|
return outbound.NewDirect().ListenPacketContext(ctx, metadata, r.Base.DialOptions(opts...)...)
|
|
|
|
case 1:
|
|
|
|
return proxies[0].ListenPacketContext(ctx, metadata, r.Base.DialOptions(opts...)...)
|
|
|
|
}
|
|
|
|
|
2022-12-19 21:34:07 +08:00
|
|
|
var d C.Dialer
|
2022-12-20 00:11:02 +08:00
|
|
|
d = dialer.NewDialer(r.Base.DialOptions(opts...)...)
|
2022-12-19 21:34:07 +08:00
|
|
|
for _, proxy := range proxies[:len(proxies)-1] {
|
2023-04-11 21:42:16 +08:00
|
|
|
d = proxydialer.New(proxy, d, false)
|
2022-12-19 17:02:04 +08:00
|
|
|
}
|
2022-04-30 11:36:42 +08:00
|
|
|
last := proxies[len(proxies)-1]
|
2022-12-19 21:34:07 +08:00
|
|
|
pc, err := last.ListenPacketWithDialer(ctx, d, metadata)
|
2022-04-30 11:36:42 +08:00
|
|
|
if err != nil {
|
2022-12-19 21:34:07 +08:00
|
|
|
return nil, err
|
2022-04-30 11:36:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := len(chainProxies) - 2; i >= 0; i-- {
|
|
|
|
pc.AppendToChains(chainProxies[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
pc.AppendToChains(r)
|
|
|
|
|
|
|
|
return pc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SupportUDP implements C.ProxyAdapter
|
|
|
|
func (r *Relay) SupportUDP() bool {
|
|
|
|
proxies, _ := r.proxies(nil, false)
|
|
|
|
if len(proxies) == 0 { // C.Direct
|
|
|
|
return true
|
|
|
|
}
|
2022-12-19 17:02:04 +08:00
|
|
|
for i := len(proxies) - 1; i >= 0; i-- {
|
|
|
|
proxy := proxies[i]
|
|
|
|
if !proxy.SupportUDP() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if proxy.SupportUOT() {
|
|
|
|
return true
|
|
|
|
}
|
2023-04-11 12:51:24 +08:00
|
|
|
switch proxy.SupportWithDialer() {
|
|
|
|
case C.ALLNet:
|
|
|
|
case C.UDP:
|
2023-04-23 21:50:42 +08:00
|
|
|
default: // C.TCP and C.InvalidNet
|
2022-12-19 17:02:04 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
2020-03-21 23:46:49 +08:00
|
|
|
}
|
|
|
|
|
2021-04-29 11:23:14 +08:00
|
|
|
// MarshalJSON implements C.ProxyAdapter
|
2020-03-21 23:46:49 +08:00
|
|
|
func (r *Relay) MarshalJSON() ([]byte, error) {
|
2022-03-23 13:48:21 +08:00
|
|
|
all := []string{}
|
2022-04-28 19:01:13 +08:00
|
|
|
for _, proxy := range r.GetProxies(false) {
|
2020-03-21 23:46:49 +08:00
|
|
|
all = append(all, proxy.Name())
|
|
|
|
}
|
2022-03-16 12:10:13 +08:00
|
|
|
return json.Marshal(map[string]any{
|
2024-01-07 23:32:22 +08:00
|
|
|
"type": r.Type().String(),
|
|
|
|
"all": all,
|
|
|
|
"hidden": r.Hidden,
|
|
|
|
"icon": r.Icon,
|
2020-03-21 23:46:49 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-30 11:36:42 +08:00
|
|
|
func (r *Relay) proxies(metadata *C.Metadata, touch bool) ([]C.Proxy, []C.Proxy) {
|
|
|
|
rawProxies := r.GetProxies(touch)
|
|
|
|
|
|
|
|
var proxies []C.Proxy
|
|
|
|
var chainProxies []C.Proxy
|
|
|
|
var targetProxies []C.Proxy
|
2020-05-07 21:42:52 +08:00
|
|
|
|
2022-04-30 11:36:42 +08:00
|
|
|
for n, proxy := range rawProxies {
|
|
|
|
proxies = append(proxies, proxy)
|
|
|
|
chainProxies = append(chainProxies, proxy)
|
2022-10-30 23:08:18 +08:00
|
|
|
subproxy := proxy.Unwrap(metadata, touch)
|
2020-05-07 21:42:52 +08:00
|
|
|
for subproxy != nil {
|
2022-04-30 11:36:42 +08:00
|
|
|
chainProxies = append(chainProxies, subproxy)
|
2020-05-07 21:42:52 +08:00
|
|
|
proxies[n] = subproxy
|
2022-10-30 23:08:18 +08:00
|
|
|
subproxy = subproxy.Unwrap(metadata, touch)
|
2020-05-07 21:42:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-30 11:36:42 +08:00
|
|
|
for _, proxy := range proxies {
|
|
|
|
if proxy.Type() != C.Direct && proxy.Type() != C.Compatible {
|
|
|
|
targetProxies = append(targetProxies, proxy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return targetProxies, chainProxies
|
2020-05-07 21:42:52 +08:00
|
|
|
}
|
|
|
|
|
2022-05-04 16:57:08 +08:00
|
|
|
func (r *Relay) Addr() string {
|
2023-03-01 14:04:42 +08:00
|
|
|
proxies, _ := r.proxies(nil, false)
|
2022-05-04 16:57:08 +08:00
|
|
|
return proxies[len(proxies)-1].Addr()
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:48:51 +08:00
|
|
|
func NewRelay(option *GroupCommonOption, providers []provider.ProxyProvider) *Relay {
|
2020-03-21 23:46:49 +08:00
|
|
|
return &Relay{
|
2022-04-28 19:01:13 +08:00
|
|
|
GroupBase: NewGroupBase(GroupBaseOption{
|
|
|
|
outbound.BaseOption{
|
|
|
|
Name: option.Name,
|
|
|
|
Type: C.Relay,
|
|
|
|
Interface: option.Interface,
|
|
|
|
RoutingMark: option.RoutingMark,
|
|
|
|
},
|
|
|
|
"",
|
2022-11-09 08:06:37 +08:00
|
|
|
"",
|
2023-01-07 12:24:28 +08:00
|
|
|
"",
|
2024-03-07 03:35:11 +08:00
|
|
|
5000,
|
|
|
|
5,
|
2022-04-28 19:01:13 +08:00
|
|
|
providers,
|
2021-11-07 16:48:51 +08:00
|
|
|
}),
|
2024-01-07 23:32:22 +08:00
|
|
|
Hidden: option.Hidden,
|
|
|
|
Icon: option.Icon,
|
2020-03-21 23:46:49 +08:00
|
|
|
}
|
|
|
|
}
|