2020-03-21 15:46:49 +00:00
|
|
|
package outboundgroup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2021-06-10 06:05:56 +00:00
|
|
|
"github.com/Dreamacro/clash/adapter/outbound"
|
2020-03-21 15:46:49 +00:00
|
|
|
"github.com/Dreamacro/clash/common/singledo"
|
|
|
|
"github.com/Dreamacro/clash/component/dialer"
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2021-07-04 12:32:59 +00:00
|
|
|
"github.com/Dreamacro/clash/constant/provider"
|
2020-03-21 15:46:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Relay struct {
|
|
|
|
*outbound.Base
|
|
|
|
single *singledo.Single
|
|
|
|
providers []provider.ProxyProvider
|
|
|
|
}
|
|
|
|
|
2021-04-29 03:23:14 +00:00
|
|
|
// DialContext implements C.ProxyAdapter
|
2020-03-21 15:46:49 +00:00
|
|
|
func (r *Relay) DialContext(ctx context.Context, metadata *C.Metadata) (C.Conn, error) {
|
2020-11-18 16:53:22 +00:00
|
|
|
proxies := r.proxies(metadata, true)
|
2020-03-21 15:46:49 +00:00
|
|
|
if len(proxies) == 0 {
|
2020-08-25 14:19:59 +00:00
|
|
|
return nil, errors.New("proxy does not exist")
|
2020-03-21 15:46:49 +00:00
|
|
|
}
|
|
|
|
first := proxies[0]
|
|
|
|
last := proxies[len(proxies)-1]
|
|
|
|
|
|
|
|
c, err := dialer.DialContext(ctx, "tcp", first.Addr())
|
|
|
|
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 03:23:14 +00:00
|
|
|
// MarshalJSON implements C.ProxyAdapter
|
2020-03-21 15:46:49 +00:00
|
|
|
func (r *Relay) MarshalJSON() ([]byte, error) {
|
|
|
|
var all []string
|
2020-11-18 16:53:22 +00:00
|
|
|
for _, proxy := range r.rawProxies(false) {
|
2020-03-21 15:46:49 +00:00
|
|
|
all = append(all, proxy.Name())
|
|
|
|
}
|
|
|
|
return json.Marshal(map[string]interface{}{
|
|
|
|
"type": r.Type().String(),
|
|
|
|
"all": all,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-18 16:53:22 +00:00
|
|
|
func (r *Relay) rawProxies(touch bool) []C.Proxy {
|
2020-03-21 15:46:49 +00:00
|
|
|
elm, _, _ := r.single.Do(func() (interface{}, error) {
|
2020-11-18 16:53:22 +00:00
|
|
|
return getProvidersProxies(r.providers, touch), nil
|
2020-03-21 15:46:49 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return elm.([]C.Proxy)
|
|
|
|
}
|
|
|
|
|
2020-11-18 16:53:22 +00:00
|
|
|
func (r *Relay) proxies(metadata *C.Metadata, touch bool) []C.Proxy {
|
|
|
|
proxies := r.rawProxies(touch)
|
2020-05-07 13:42:52 +00:00
|
|
|
|
|
|
|
for n, proxy := range proxies {
|
|
|
|
subproxy := proxy.Unwrap(metadata)
|
|
|
|
for subproxy != nil {
|
|
|
|
proxies[n] = subproxy
|
|
|
|
subproxy = subproxy.Unwrap(metadata)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return proxies
|
|
|
|
}
|
|
|
|
|
2020-11-13 13:48:52 +00:00
|
|
|
func NewRelay(options *GroupCommonOption, providers []provider.ProxyProvider) *Relay {
|
2020-03-21 15:46:49 +00:00
|
|
|
return &Relay{
|
2020-11-13 13:48:52 +00:00
|
|
|
Base: outbound.NewBase(options.Name, "", C.Relay, false),
|
2020-03-21 15:46:49 +00:00
|
|
|
single: singledo.NewSingle(defaultGetProxiesDuration),
|
|
|
|
providers: providers,
|
|
|
|
}
|
|
|
|
}
|