Feature: skip DIRECT proxies in relay (#1583)

This commit is contained in:
Excited Codes 2021-09-06 21:39:28 +08:00 committed by GitHub
parent 8ef5cdb8be
commit a2d59d6ef5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -3,7 +3,6 @@ package outboundgroup
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"github.com/Dreamacro/clash/adapter/outbound" "github.com/Dreamacro/clash/adapter/outbound"
@ -21,10 +20,20 @@ type Relay struct {
// DialContext implements C.ProxyAdapter // DialContext implements C.ProxyAdapter
func (r *Relay) DialContext(ctx context.Context, metadata *C.Metadata) (C.Conn, error) { func (r *Relay) DialContext(ctx context.Context, metadata *C.Metadata) (C.Conn, error) {
proxies := r.proxies(metadata, true) var proxies []C.Proxy
if len(proxies) == 0 { for _, proxy := range r.proxies(metadata, true) {
return nil, errors.New("proxy does not exist") if proxy.Type() != C.Direct {
proxies = append(proxies, proxy)
} }
}
switch len(proxies) {
case 0:
return outbound.NewDirect().DialContext(ctx, metadata)
case 1:
return proxies[0].DialContext(ctx, metadata)
}
first := proxies[0] first := proxies[0]
last := proxies[len(proxies)-1] last := proxies[len(proxies)-1]