forked from rchunping/TcpRoute2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupstream_dialclients_test.go
89 lines (70 loc) · 1.9 KB
/
upstream_dialclients_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"testing"
"os"
"github.com/koding/multiconfig"
"time"
)
func TestUpStreamDialClient(t *testing.T) {
f, err := os.Create("Blacklist.txt")
if _, err := f.Write([]byte(" \r\n #000 \r\n xxx.com ")); err != nil {
t.Fatal(err)
}
defer os.Remove("Blacklist.txt")
f, err = os.Create("Whitelist-config.toml")
if _, err := f.Write([]byte(`
BasePath="."
[[UpStreams]]
Name="direct"
ProxyUrl="direct://0.0.0.0:0000/"
DnsResolve=true
Credit=0
Sleep=0
[[UpStreams.Whitelist]]
Path="https://raw.githubusercontent.com/renzhn/MEOW/af19d0d3e22d485254bf50cfce4e5ed12b0e445b/doc/sample-config/direct"
UpdateInterval="24h"
Type="Suffix"
[[UpStreams.Blacklist]]
Path="Blacklist.txt"
UpdateInterval="24h"
Type="Suffix"
[[UpStreams]]
Name="http"
ProxyUrl="http://123.123.123.123:8088"
DnsResolve=false
Credit=0
Sleep=80
[[UpStreams.Whitelist]]
Path="https://raw.githubusercontent.com/renzhn/MEOW/af19d0d3e22d485254bf50cfce4e5ed12b0e445b/doc/sample-config/proxy"
UpdateInterval="24h"
Type="Suffix"
`)); err != nil {
t.Fatal(err)
}
f.Close()
defer os.Remove("Whitelist-config.txt")
config := ConfigDialClients{}
m := multiconfig.TOMLLoader{Path:"Whitelist-config.toml", } // supports TOML and JSON
err = m.Load(&config)
if err != nil {
t.Fatal(err)
}
clients, err := NewDialClients(&config)
if err != nil {
t.Fatal(err)
}
// 等待白名单、黑名单载入。
time.Sleep(5 * time.Second)
// 测试白名单效果
if dialclients, edit := clients.Get("www.163.com"); edit != true || len(dialclients) != 1 || dialclients[0].name != "direct" {
t.Error(clients)
}
// 测试黑名单效果
if dialclients, edit := clients.Get("www.xxx.com"); edit != true || len(dialclients) != 1 || dialclients[0].name != "http" {
t.Error(clients)
}
// 测试普通域名效果
if dialclients, edit := clients.Get("www.9999999.com"); len(dialclients) != 2 || edit != false {
t.Error(clients)
}
}