diff --git a/config/utils.go b/config/utils.go index 7b413847..ed460514 100644 --- a/config/utils.go +++ b/config/utils.go @@ -163,45 +163,28 @@ func validateDialerProxies(proxies map[string]C.Proxy) error { } } - // perform depth-first search to detect cycles for each proxy + // every proxy has at most one dialer-proxy, so walk each chain once: reaching a proxy + // of the current walk means a cycle, reaching one an earlier walk finished does not + const onPath, acyclic = 1, 2 + state := make(map[string]uint8, len(graph)) + var path []string for name := range graph { - visited := make(map[string]bool, len(graph)) - path := make([]string, 0, len(graph)) - if validateDialerProxiesHasCycle(name, graph, visited, path) { - return fmt.Errorf("proxy [%s] has circular dialer-proxy dependency", name) + path = path[:0] + for current, ok := name, true; ok && state[current] != acyclic; current, ok = graph[current] { + if state[current] == onPath { + return fmt.Errorf("proxy [%s] has circular dialer-proxy dependency", name) + } + state[current] = onPath + path = append(path, current) + } + for _, p := range path { + state[p] = acyclic } } return nil } -// validateDialerProxiesHasCycle performs DFS to detect if there's a cycle starting from current proxy -func validateDialerProxiesHasCycle(current string, graph map[string]string, visited map[string]bool, path []string) bool { - // check if current is already in path (cycle detected) - for _, p := range path { - if p == current { - return true - } - } - - // already visited and no cycle - if visited[current] { - return false - } - - visited[current] = true - path = append(path, current) - - // check dialer-proxy of current proxy - if dialerProxy, exists := graph[current]; exists { - if validateDialerProxiesHasCycle(dialerProxy, graph, visited, path) { - return true - } - } - - return false -} - func verifyIP6() bool { if skip, _ := strconv.ParseBool(os.Getenv("SKIP_SYSTEM_IPV6_CHECK")); skip { return true diff --git a/config/utils_test.go b/config/utils_test.go index 542c2330..aba0ddc6 100644 --- a/config/utils_test.go +++ b/config/utils_test.go @@ -8,9 +8,11 @@ import ( func TestValidateDialerProxies(t *testing.T) { testCases := []struct { - testName string - proxy []map[string]any - errContains string + testName string + proxy []map[string]any + // the proxy named in a cycle error depends on map iteration order, + // so any of these errors is accepted; nil means no error + errs []string }{ { testName: "ValidReference", @@ -18,61 +20,129 @@ func TestValidateDialerProxies(t *testing.T) { {"name": "base-proxy", "type": "socks5", "server": "127.0.0.1", "port": 1080}, {"name": "proxy-with-dialer", "type": "socks5", "server": "127.0.0.1", "port": 1081, "dialer-proxy": "base-proxy"}, }, - errContains: "", }, { testName: "NotFoundReference", proxy: []map[string]any{ // create proxy with non-existent dialer-proxy reference + {"name": "base-proxy", "type": "socks5", "server": "127.0.0.1", "port": 1080}, + {"name": "valid-proxy-with-dialer", "type": "socks5", "server": "127.0.0.1", "port": 1081, "dialer-proxy": "base-proxy"}, {"name": "proxy-with-dialer", "type": "socks5", "server": "127.0.0.1", "port": 1081, "dialer-proxy": "non-existent-proxy"}, }, - errContains: "not found", + errs: []string{"proxy [proxy-with-dialer] dialer-proxy [non-existent-proxy] not found"}, }, { testName: "CircularDependency", - proxy: []map[string]any{ - // create proxy A that references B + proxy: []map[string]any{ // proxy-b -> proxy-a <-> proxy-c {"name": "proxy-a", "type": "socks5", "server": "127.0.0.1", "port": 1080, "dialer-proxy": "proxy-c"}, - // create proxy B that references C {"name": "proxy-b", "type": "socks5", "server": "127.0.0.1", "port": 1081, "dialer-proxy": "proxy-a"}, - // create proxy C that references A (creates cycle) {"name": "proxy-c", "type": "socks5", "server": "127.0.0.1", "port": 1082, "dialer-proxy": "proxy-a"}, }, - errContains: "circular", + errs: []string{ + "proxy [proxy-a] has circular dialer-proxy dependency", + "proxy [proxy-b] has circular dialer-proxy dependency", + "proxy [proxy-c] has circular dialer-proxy dependency", + }, }, { testName: "ComplexChain", - proxy: []map[string]any{ // create a valid chain: proxy-d -> proxy-c -> proxy-b -> proxy-a + proxy: []map[string]any{ // proxy-d, proxy-e and proxy-f share the chain proxy-c -> proxy-b -> proxy-a {"name": "proxy-a", "type": "socks5", "server": "127.0.0.1", "port": 1080}, {"name": "proxy-b", "type": "socks5", "server": "127.0.0.1", "port": 1081, "dialer-proxy": "proxy-a"}, {"name": "proxy-c", "type": "socks5", "server": "127.0.0.1", "port": 1082, "dialer-proxy": "proxy-b"}, {"name": "proxy-d", "type": "socks5", "server": "127.0.0.1", "port": 1083, "dialer-proxy": "proxy-c"}, + {"name": "proxy-e", "type": "socks5", "server": "127.0.0.1", "port": 1084, "dialer-proxy": "proxy-c"}, + {"name": "proxy-f", "type": "socks5", "server": "127.0.0.1", "port": 1085, "dialer-proxy": "proxy-c"}, }, - errContains: "", }, { testName: "EmptyDialerProxy", proxy: []map[string]any{ // create proxy without dialer-proxy {"name": "simple-proxy", "type": "socks5", "server": "127.0.0.1", "port": 1080}, + {"name": "another-simple-proxy", "type": "socks5", "server": "127.0.0.1", "port": 1081}, }, - errContains: "", }, { testName: "SelfReference", proxy: []map[string]any{ // create proxy that references itself + {"name": "base-proxy", "type": "socks5", "server": "127.0.0.1", "port": 1080}, {"name": "self-proxy", "type": "socks5", "server": "127.0.0.1", "port": 1080, "dialer-proxy": "self-proxy"}, }, - errContains: "circular", + errs: []string{"proxy [self-proxy] has circular dialer-proxy dependency"}, + }, + { + testName: "NotFoundBeforeCycle", + proxy: []map[string]any{ + {"name": "a", "type": "socks5", "server": "127.0.0.1", "port": 1080, "dialer-proxy": "b"}, + {"name": "b", "type": "socks5", "server": "127.0.0.1", "port": 1080, "dialer-proxy": "a"}, + {"name": "c", "type": "socks5", "server": "127.0.0.1", "port": 1080, "dialer-proxy": "missing"}, + }, + errs: []string{"proxy [c] dialer-proxy [missing] not found"}, + }, + { + testName: "TwoProxyCycle", + proxy: []map[string]any{ + {"name": "a", "type": "socks5", "server": "127.0.0.1", "port": 1080, "dialer-proxy": "b"}, + {"name": "b", "type": "socks5", "server": "127.0.0.1", "port": 1080, "dialer-proxy": "a"}, + {"name": "c", "type": "socks5", "server": "127.0.0.1", "port": 1080}, + }, + errs: []string{ + "proxy [a] has circular dialer-proxy dependency", + "proxy [b] has circular dialer-proxy dependency", + }, + }, + { + testName: "LongChainIntoCycle", + // x -> a -> b -> c -> d -> e -> c, beside an acyclic chain y -> z, w -> z + proxy: []map[string]any{ + {"name": "x", "type": "socks5", "server": "127.0.0.1", "port": 1080, "dialer-proxy": "a"}, + {"name": "a", "type": "socks5", "server": "127.0.0.1", "port": 1080, "dialer-proxy": "b"}, + {"name": "b", "type": "socks5", "server": "127.0.0.1", "port": 1080, "dialer-proxy": "c"}, + {"name": "c", "type": "socks5", "server": "127.0.0.1", "port": 1080, "dialer-proxy": "d"}, + {"name": "d", "type": "socks5", "server": "127.0.0.1", "port": 1080, "dialer-proxy": "e"}, + {"name": "e", "type": "socks5", "server": "127.0.0.1", "port": 1080, "dialer-proxy": "c"}, + {"name": "y", "type": "socks5", "server": "127.0.0.1", "port": 1080, "dialer-proxy": "z"}, + {"name": "z", "type": "socks5", "server": "127.0.0.1", "port": 1080}, + {"name": "w", "type": "socks5", "server": "127.0.0.1", "port": 1080, "dialer-proxy": "z"}, + }, + errs: []string{ + "proxy [x] has circular dialer-proxy dependency", + "proxy [a] has circular dialer-proxy dependency", + "proxy [b] has circular dialer-proxy dependency", + "proxy [c] has circular dialer-proxy dependency", + "proxy [d] has circular dialer-proxy dependency", + "proxy [e] has circular dialer-proxy dependency", + }, + }, + { + testName: "SharedRelayBesideCycle", + // p and q share an acyclic relay, t leads into the cycle r <-> s + proxy: []map[string]any{ + {"name": "p", "type": "socks5", "server": "127.0.0.1", "port": 1080, "dialer-proxy": "relay"}, + {"name": "q", "type": "socks5", "server": "127.0.0.1", "port": 1080, "dialer-proxy": "relay"}, + {"name": "relay", "type": "socks5", "server": "127.0.0.1", "port": 1080}, + {"name": "r", "type": "socks5", "server": "127.0.0.1", "port": 1080, "dialer-proxy": "s"}, + {"name": "s", "type": "socks5", "server": "127.0.0.1", "port": 1080, "dialer-proxy": "r"}, + {"name": "t", "type": "socks5", "server": "127.0.0.1", "port": 1080, "dialer-proxy": "s"}, + }, + errs: []string{ + "proxy [r] has circular dialer-proxy dependency", + "proxy [s] has circular dialer-proxy dependency", + "proxy [t] has circular dialer-proxy dependency", + }, }, } for _, testCase := range testCases { t.Run(testCase.testName, func(t *testing.T) { config := RawConfig{Proxy: testCase.proxy} - _, _, err := parseProxies(&config) - if testCase.errContains == "" { - assert.NoError(t, err, testCase.testName) - } else { - assert.ErrorContains(t, err, testCase.errContains, testCase.testName) + // repeat so that different map iteration orders are covered + for i := 0; i < 20; i++ { + _, _, err := parseProxies(&config) + if testCase.errs == nil { + assert.NoError(t, err) + } else if assert.Error(t, err) { + assert.Contains(t, testCase.errs, err.Error()) + } } }) }