2020-03-21 23:46:49 +08:00
|
|
|
package outboundgroup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
2021-06-10 14:05:56 +08:00
|
|
|
"github.com/Dreamacro/clash/adapter/outbound"
|
2020-03-21 23:46:49 +08:00
|
|
|
"github.com/Dreamacro/clash/component/dialer"
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2021-07-04 20:32:59 +08:00
|
|
|
"github.com/Dreamacro/clash/constant/provider"
|
2020-03-21 23:46:49 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Relay struct {
|
2022-04-28 19:01:13 +08:00
|
|
|
*GroupBase
|
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) {
|
2021-09-06 21:39:28 +08:00
|
|
|
var proxies []C.Proxy
|
|
|
|
for _, proxy := range r.proxies(metadata, true) {
|
2022-01-18 21:09:36 +08:00
|
|
|
if proxy.Type() != C.Direct && proxy.Type() != C.Compatible {
|
2021-09-06 21:39:28 +08:00
|
|
|
proxies = append(proxies, proxy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2021-09-06 21:39:28 +08:00
|
|
|
|
2020-03-21 23:46:49 +08:00
|
|
|
first := proxies[0]
|
|
|
|
last := proxies[len(proxies)-1]
|
|
|
|
|
2021-11-07 16:48:51 +08:00
|
|
|
c, err := dialer.DialContext(ctx, "tcp", first.Addr(), r.Base.DialOptions(opts...)...)
|
2020-03-21 23:46:49 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s connect error: %w", first.Addr(), err)
|
|
|
|
}
|
|
|
|
tcpKeepAlive(c)
|
|
|
|
|
|
|
|
var currentMeta *C.Metadata
|
|
|
|
for _, proxy := range proxies[1:] {
|
|
|
|
currentMeta, err = addrToMetadata(proxy.Addr())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err = first.StreamConn(c, currentMeta)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s connect error: %w", first.Addr(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
first = proxy
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err = last.StreamConn(c, metadata)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s connect error: %w", last.Addr(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return outbound.NewConn(c, r), nil
|
|
|
|
}
|
|
|
|
|
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{
|
2020-03-21 23:46:49 +08:00
|
|
|
"type": r.Type().String(),
|
|
|
|
"all": all,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-19 00:53:22 +08:00
|
|
|
func (r *Relay) proxies(metadata *C.Metadata, touch bool) []C.Proxy {
|
2022-04-28 19:01:13 +08:00
|
|
|
proxies := r.GetProxies(touch)
|
2020-05-07 21:42:52 +08:00
|
|
|
|
|
|
|
for n, proxy := range proxies {
|
|
|
|
subproxy := proxy.Unwrap(metadata)
|
|
|
|
for subproxy != nil {
|
|
|
|
proxies[n] = subproxy
|
|
|
|
subproxy = subproxy.Unwrap(metadata)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return proxies
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
"",
|
|
|
|
providers,
|
2021-11-07 16:48:51 +08:00
|
|
|
}),
|
2020-03-21 23:46:49 +08:00
|
|
|
}
|
|
|
|
}
|