2018-07-25 16:04:59 +00:00
|
|
|
package config
|
2018-06-10 14:50:03 +00:00
|
|
|
|
|
|
|
import (
|
2018-08-11 18:23:46 +00:00
|
|
|
"fmt"
|
2022-12-02 16:10:50 +00:00
|
|
|
"net"
|
2018-06-10 14:50:03 +00:00
|
|
|
"strings"
|
2018-10-18 15:24:04 +00:00
|
|
|
|
2021-06-10 06:05:56 +00:00
|
|
|
"github.com/Dreamacro/clash/adapter/outboundgroup"
|
2019-09-13 07:04:51 +00:00
|
|
|
"github.com/Dreamacro/clash/common/structure"
|
2018-06-10 14:50:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func trimArr(arr []string) (r []string) {
|
|
|
|
for _, e := range arr {
|
|
|
|
r = append(r, strings.Trim(e, " "))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2018-08-11 18:23:46 +00:00
|
|
|
|
2019-08-12 02:11:44 +00:00
|
|
|
// Check if ProxyGroups form DAG(Directed Acyclic Graph), and sort all ProxyGroups by dependency order.
|
|
|
|
// Meanwhile, record the original index in the config file.
|
|
|
|
// If loop is detected, return an error with location of loop.
|
2022-03-16 04:10:13 +00:00
|
|
|
func proxyGroupsDagSort(groupsConfig []map[string]any) error {
|
2019-09-13 07:04:51 +00:00
|
|
|
type graphNode struct {
|
2019-08-12 02:11:44 +00:00
|
|
|
indegree int
|
|
|
|
// topological order
|
|
|
|
topo int
|
2020-10-14 11:56:02 +00:00
|
|
|
// the original data in `groupsConfig`
|
2022-03-16 04:10:13 +00:00
|
|
|
data map[string]any
|
2019-08-12 02:11:44 +00:00
|
|
|
// `outdegree` and `from` are used in loop locating
|
|
|
|
outdegree int
|
2019-12-08 04:17:24 +00:00
|
|
|
option *outboundgroup.GroupCommonOption
|
2019-08-12 02:11:44 +00:00
|
|
|
from []string
|
|
|
|
}
|
|
|
|
|
2019-12-08 04:17:24 +00:00
|
|
|
decoder := structure.NewDecoder(structure.Option{TagName: "group", WeaklyTypedInput: true})
|
2019-09-13 07:04:51 +00:00
|
|
|
graph := make(map[string]*graphNode)
|
2019-08-12 02:11:44 +00:00
|
|
|
|
|
|
|
// Step 1.1 build dependency graph
|
2019-09-13 07:04:51 +00:00
|
|
|
for _, mapping := range groupsConfig {
|
2019-12-08 04:17:24 +00:00
|
|
|
option := &outboundgroup.GroupCommonOption{}
|
|
|
|
if err := decoder.Decode(mapping, option); err != nil {
|
|
|
|
return fmt.Errorf("ProxyGroup %s: %s", option.Name, err.Error())
|
2019-08-12 02:11:44 +00:00
|
|
|
}
|
2019-09-13 07:04:51 +00:00
|
|
|
|
2019-12-08 04:17:24 +00:00
|
|
|
groupName := option.Name
|
2019-08-12 02:11:44 +00:00
|
|
|
if node, ok := graph[groupName]; ok {
|
|
|
|
if node.data != nil {
|
|
|
|
return fmt.Errorf("ProxyGroup %s: duplicate group name", groupName)
|
|
|
|
}
|
|
|
|
node.data = mapping
|
2019-12-08 04:17:24 +00:00
|
|
|
node.option = option
|
2019-08-12 02:11:44 +00:00
|
|
|
} else {
|
2019-12-08 04:17:24 +00:00
|
|
|
graph[groupName] = &graphNode{0, -1, mapping, 0, option, nil}
|
2019-08-12 02:11:44 +00:00
|
|
|
}
|
2019-09-13 07:04:51 +00:00
|
|
|
|
|
|
|
for _, proxy := range option.Proxies {
|
2019-08-12 02:11:44 +00:00
|
|
|
if node, ex := graph[proxy]; ex {
|
|
|
|
node.indegree++
|
|
|
|
} else {
|
2019-12-08 04:17:24 +00:00
|
|
|
graph[proxy] = &graphNode{1, -1, nil, 0, nil, nil}
|
2019-08-12 02:11:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Step 1.2 Topological Sort
|
|
|
|
// topological index of **ProxyGroup**
|
|
|
|
index := 0
|
|
|
|
queue := make([]string, 0)
|
|
|
|
for name, node := range graph {
|
2020-10-14 11:56:02 +00:00
|
|
|
// in the beginning, put nodes that have `node.indegree == 0` into queue.
|
2019-08-12 02:11:44 +00:00
|
|
|
if node.indegree == 0 {
|
|
|
|
queue = append(queue, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// every element in queue have indegree == 0
|
|
|
|
for ; len(queue) > 0; queue = queue[1:] {
|
|
|
|
name := queue[0]
|
|
|
|
node := graph[name]
|
2019-12-08 04:17:24 +00:00
|
|
|
if node.option != nil {
|
2019-08-12 02:11:44 +00:00
|
|
|
index++
|
|
|
|
groupsConfig[len(groupsConfig)-index] = node.data
|
2019-12-08 04:17:24 +00:00
|
|
|
if len(node.option.Proxies) == 0 {
|
|
|
|
delete(graph, name)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, proxy := range node.option.Proxies {
|
|
|
|
child := graph[proxy]
|
2019-08-12 02:11:44 +00:00
|
|
|
child.indegree--
|
|
|
|
if child.indegree == 0 {
|
2019-12-08 04:17:24 +00:00
|
|
|
queue = append(queue, proxy)
|
2019-08-12 02:11:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete(graph, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// no loop is detected, return sorted ProxyGroup
|
|
|
|
if len(graph) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// if loop is detected, locate the loop and throw an error
|
|
|
|
// Step 2.1 rebuild the graph, fill `outdegree` and `from` filed
|
|
|
|
for name, node := range graph {
|
2019-12-08 04:17:24 +00:00
|
|
|
if node.option == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(node.option.Proxies) == 0 {
|
2019-08-12 02:11:44 +00:00
|
|
|
continue
|
|
|
|
}
|
2019-12-08 04:17:24 +00:00
|
|
|
|
|
|
|
for _, proxy := range node.option.Proxies {
|
2019-08-12 02:11:44 +00:00
|
|
|
node.outdegree++
|
2019-12-08 04:17:24 +00:00
|
|
|
child := graph[proxy]
|
2019-08-12 02:11:44 +00:00
|
|
|
if child.from == nil {
|
|
|
|
child.from = make([]string, 0, child.indegree)
|
|
|
|
}
|
|
|
|
child.from = append(child.from, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Step 2.2 remove nodes outside the loop. so that we have only the loops remain in `graph`
|
|
|
|
queue = make([]string, 0)
|
|
|
|
// initialize queue with node have outdegree == 0
|
|
|
|
for name, node := range graph {
|
|
|
|
if node.outdegree == 0 {
|
|
|
|
queue = append(queue, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// every element in queue have outdegree == 0
|
|
|
|
for ; len(queue) > 0; queue = queue[1:] {
|
|
|
|
name := queue[0]
|
|
|
|
node := graph[name]
|
|
|
|
for _, f := range node.from {
|
|
|
|
graph[f].outdegree--
|
|
|
|
if graph[f].outdegree == 0 {
|
|
|
|
queue = append(queue, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete(graph, name)
|
|
|
|
}
|
|
|
|
// Step 2.3 report the elements in loop
|
|
|
|
loopElements := make([]string, 0, len(graph))
|
|
|
|
for name := range graph {
|
|
|
|
loopElements = append(loopElements, name)
|
|
|
|
delete(graph, name)
|
|
|
|
}
|
2020-08-25 14:19:59 +00:00
|
|
|
return fmt.Errorf("loop is detected in ProxyGroup, please check following ProxyGroups: %v", loopElements)
|
2019-08-12 02:11:44 +00:00
|
|
|
}
|
2022-12-02 16:10:50 +00:00
|
|
|
|
|
|
|
func verifyIP6() bool {
|
|
|
|
addrs, err := net.InterfaceAddrs()
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, addr := range addrs {
|
|
|
|
ipNet, isIpNet := addr.(*net.IPNet)
|
|
|
|
if isIpNet && !ipNet.IP.IsLoopback() {
|
|
|
|
if ipNet.IP.To16() != nil {
|
|
|
|
s := ipNet.IP.String()
|
|
|
|
for i := 0; i < len(s); i++ {
|
|
|
|
switch s[i] {
|
|
|
|
case ':':
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|