Fix: typo

This commit is contained in:
Dreamacro 2018-07-18 21:50:16 +08:00
parent d55e1b664b
commit d540a0d29f
7 changed files with 63 additions and 63 deletions

View file

@ -78,7 +78,7 @@ Proxy2 = ss, server2, port, AEAD_CHACHA20_POLY1305, password
[Proxy Group] [Proxy Group]
# url-test select which proxy will be used by benchmarking speed to a URL. # url-test select which proxy will be used by benchmarking speed to a URL.
# name = url-test, [proxys], url, interval(second) # name = url-test, [proxies], url, interval(second)
Proxy = url-test, Proxy1, Proxy2, http://www.google.com/generate_204, 300 Proxy = url-test, Proxy1, Proxy2, http://www.google.com/generate_204, 300
[Rule] [Rule]

View file

@ -9,7 +9,7 @@ import (
type Selector struct { type Selector struct {
name string name string
selected C.Proxy selected C.Proxy
proxys map[string]C.Proxy proxies map[string]C.Proxy
} }
func (s *Selector) Name() string { func (s *Selector) Name() string {
@ -30,14 +30,14 @@ func (s *Selector) Now() string {
func (s *Selector) All() []string { func (s *Selector) All() []string {
var all []string var all []string
for k := range s.proxys { for k := range s.proxies {
all = append(all, k) all = append(all, k)
} }
return all return all
} }
func (s *Selector) Set(name string) error { func (s *Selector) Set(name string) error {
proxy, exist := s.proxys[name] proxy, exist := s.proxies[name]
if !exist { if !exist {
return errors.New("Proxy does not exist") return errors.New("Proxy does not exist")
} }
@ -45,21 +45,21 @@ func (s *Selector) Set(name string) error {
return nil return nil
} }
func NewSelector(name string, proxys map[string]C.Proxy) (*Selector, error) { func NewSelector(name string, proxies map[string]C.Proxy) (*Selector, error) {
if len(proxys) == 0 { if len(proxies) == 0 {
return nil, errors.New("Provide at least one proxy") return nil, errors.New("Provide at least one proxy")
} }
mapping := make(map[string]C.Proxy) mapping := make(map[string]C.Proxy)
var init string var init string
for k, v := range proxys { for k, v := range proxies {
mapping[k] = v mapping[k] = v
init = k init = k
} }
s := &Selector{ s := &Selector{
name: name, name: name,
proxys: mapping, proxies: mapping,
selected: proxys[init], selected: proxies[init],
} }
return s, nil return s, nil
} }

View file

@ -12,14 +12,14 @@ import (
) )
type URLTest struct { type URLTest struct {
name string name string
proxys []C.Proxy proxies []C.Proxy
url *url.URL url *url.URL
rawURL string rawURL string
addr *C.Addr addr *C.Addr
fast C.Proxy fast C.Proxy
delay time.Duration delay time.Duration
done chan struct{} done chan struct{}
} }
func (u *URLTest) Name() string { func (u *URLTest) Name() string {
@ -58,12 +58,12 @@ Loop:
func (u *URLTest) speedTest() { func (u *URLTest) speedTest() {
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
wg.Add(len(u.proxys)) wg.Add(len(u.proxies))
c := make(chan interface{}) c := make(chan interface{})
fast := selectFast(c) fast := selectFast(c)
timer := time.NewTimer(u.delay) timer := time.NewTimer(u.delay)
for _, p := range u.proxys { for _, p := range u.proxies {
go func(p C.Proxy) { go func(p C.Proxy) {
err := getUrl(p, u.addr, u.rawURL) err := getUrl(p, u.addr, u.rawURL)
if err == nil { if err == nil {
@ -129,7 +129,7 @@ func selectFast(in chan interface{}) chan interface{} {
return out return out
} }
func NewURLTest(name string, proxys []C.Proxy, rawURL string, delay time.Duration) (*URLTest, error) { func NewURLTest(name string, proxies []C.Proxy, rawURL string, delay time.Duration) (*URLTest, error) {
u, err := url.Parse(rawURL) u, err := url.Parse(rawURL)
if err != nil { if err != nil {
return nil, err return nil, err
@ -154,14 +154,14 @@ func NewURLTest(name string, proxys []C.Proxy, rawURL string, delay time.Duratio
} }
urlTest := &URLTest{ urlTest := &URLTest{
name: name, name: name,
proxys: proxys[:], proxies: proxies[:],
rawURL: rawURL, rawURL: rawURL,
url: u, url: u,
addr: addr, addr: addr,
fast: proxys[0], fast: proxies[0],
delay: delay, delay: delay,
done: make(chan struct{}), done: make(chan struct{}),
} }
go urlTest.loop() go urlTest.loop()
return urlTest, nil return urlTest, nil

View file

@ -13,7 +13,7 @@ import (
func proxyRouter() http.Handler { func proxyRouter() http.Handler {
r := chi.NewRouter() r := chi.NewRouter()
r.Get("/", getProxys) r.Get("/", getProxies)
r.Get("/{name}", getProxy) r.Get("/{name}", getProxy)
r.Put("/{name}", updateProxy) r.Put("/{name}", updateProxy)
return r return r
@ -56,23 +56,23 @@ func transformProxy(proxy C.Proxy) interface{} {
} }
} }
type GetProxysResponse struct { type GetProxiesResponse struct {
Proxys map[string]interface{} `json:"proxys"` Proxies map[string]interface{} `json:"proxies"`
} }
func getProxys(w http.ResponseWriter, r *http.Request) { func getProxies(w http.ResponseWriter, r *http.Request) {
_, rawProxys := tunnel.Config() _, rawProxies := tunnel.Config()
proxys := make(map[string]interface{}) proxies := make(map[string]interface{})
for name, proxy := range rawProxys { for name, proxy := range rawProxies {
proxys[name] = transformProxy(proxy) proxies[name] = transformProxy(proxy)
} }
render.JSON(w, r, GetProxysResponse{Proxys: proxys}) render.JSON(w, r, GetProxiesResponse{Proxies: proxies})
} }
func getProxy(w http.ResponseWriter, r *http.Request) { func getProxy(w http.ResponseWriter, r *http.Request) {
name := chi.URLParam(r, "name") name := chi.URLParam(r, "name")
_, proxys := tunnel.Config() _, proxies := tunnel.Config()
proxy, exist := proxys[name] proxy, exist := proxies[name]
if !exist { if !exist {
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
render.JSON(w, r, Error{ render.JSON(w, r, Error{
@ -98,8 +98,8 @@ func updateProxy(w http.ResponseWriter, r *http.Request) {
} }
name := chi.URLParam(r, "name") name := chi.URLParam(r, "name")
_, proxys := tunnel.Config() _, proxies := tunnel.Config()
proxy, exist := proxys[name] proxy, exist := proxies[name]
if !exist { if !exist {
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
render.JSON(w, r, Error{ render.JSON(w, r, Error{

View file

@ -23,7 +23,7 @@ func NewHub(addr string) {
r.Get("/traffic", traffic) r.Get("/traffic", traffic)
r.Get("/logs", getLogs) r.Get("/logs", getLogs)
r.Mount("/configs", configRouter()) r.Mount("/configs", configRouter())
r.Mount("/proxys", proxyRouter()) r.Mount("/proxies", proxyRouter())
r.Mount("/rules", ruleRouter()) r.Mount("/rules", ruleRouter())
err := http.ListenAndServe(addr, r) err := http.ListenAndServe(addr, r)

View file

@ -26,7 +26,7 @@ type Listener struct {
socksSignal *C.ProxySignal socksSignal *C.ProxySignal
} }
// Info returns the proxys's current configuration // Info returns the proxies's current configuration
func (l *Listener) Info() (info C.General) { func (l *Listener) Info() (info C.General) {
return C.General{ return C.General{
Port: &l.httpPort, Port: &l.httpPort,

View file

@ -23,7 +23,7 @@ var (
type Tunnel struct { type Tunnel struct {
queue *channels.InfiniteChannel queue *channels.InfiniteChannel
rules []C.Rule rules []C.Rule
proxys map[string]C.Proxy proxies map[string]C.Proxy
observable *observable.Observable observable *observable.Observable
logCh chan interface{} logCh chan interface{}
configLock *sync.RWMutex configLock *sync.RWMutex
@ -41,7 +41,7 @@ func (t *Tunnel) Traffic() *C.Traffic {
} }
func (t *Tunnel) Config() ([]C.Rule, map[string]C.Proxy) { func (t *Tunnel) Config() ([]C.Rule, map[string]C.Proxy) {
return t.rules, t.proxys return t.rules, t.proxies
} }
func (t *Tunnel) Log() *observable.Observable { func (t *Tunnel) Log() *observable.Observable {
@ -62,16 +62,16 @@ func (t *Tunnel) UpdateConfig() (err error) {
return return
} }
// empty proxys and rules // empty proxies and rules
proxys := make(map[string]C.Proxy) proxies := make(map[string]C.Proxy)
rules := []C.Rule{} rules := []C.Rule{}
proxysConfig := cfg.Section("Proxy") proxiesConfig := cfg.Section("Proxy")
rulesConfig := cfg.Section("Rule") rulesConfig := cfg.Section("Rule")
groupsConfig := cfg.Section("Proxy Group") groupsConfig := cfg.Section("Proxy Group")
// parse proxy // parse proxy
for _, key := range proxysConfig.Keys() { for _, key := range proxiesConfig.Keys() {
proxy := key.Strings(",") proxy := key.Strings(",")
if len(proxy) == 0 { if len(proxy) == 0 {
continue continue
@ -87,7 +87,7 @@ func (t *Tunnel) UpdateConfig() (err error) {
if err != nil { if err != nil {
return err return err
} }
proxys[key.Name()] = ss proxies[key.Name()] = ss
} }
} }
@ -126,7 +126,7 @@ func (t *Tunnel) UpdateConfig() (err error) {
url := rule[len(rule)-2] url := rule[len(rule)-2]
var ps []C.Proxy var ps []C.Proxy
for _, name := range proxyNames { for _, name := range proxyNames {
if p, ok := proxys[name]; ok { if p, ok := proxies[name]; ok {
ps = append(ps, p) ps = append(ps, p)
} }
} }
@ -135,7 +135,7 @@ func (t *Tunnel) UpdateConfig() (err error) {
if err != nil { if err != nil {
return fmt.Errorf("Config error: %s", err.Error()) return fmt.Errorf("Config error: %s", err.Error())
} }
proxys[key.Name()] = adapter proxies[key.Name()] = adapter
case "select": case "select":
if len(rule) < 3 { if len(rule) < 3 {
return fmt.Errorf("Selector need more than 3 param") return fmt.Errorf("Selector need more than 3 param")
@ -143,7 +143,7 @@ func (t *Tunnel) UpdateConfig() (err error) {
proxyNames := rule[1:] proxyNames := rule[1:]
selectProxy := make(map[string]C.Proxy) selectProxy := make(map[string]C.Proxy)
for _, name := range proxyNames { for _, name := range proxyNames {
proxy, exist := proxys[name] proxy, exist := proxies[name]
if !exist { if !exist {
return fmt.Errorf("Proxy %s not exist", name) return fmt.Errorf("Proxy %s not exist", name)
} }
@ -153,31 +153,31 @@ func (t *Tunnel) UpdateConfig() (err error) {
if err != nil { if err != nil {
return fmt.Errorf("Selector create error: %s", err.Error()) return fmt.Errorf("Selector create error: %s", err.Error())
} }
proxys[key.Name()] = selector proxies[key.Name()] = selector
} }
} }
// init proxy // init proxy
proxys["DIRECT"] = adapters.NewDirect(t.traffic) proxies["DIRECT"] = adapters.NewDirect(t.traffic)
proxys["REJECT"] = adapters.NewReject() proxies["REJECT"] = adapters.NewReject()
t.configLock.Lock() t.configLock.Lock()
defer t.configLock.Unlock() defer t.configLock.Unlock()
// stop url-test // stop url-test
for _, elm := range t.proxys { for _, elm := range t.proxies {
urlTest, ok := elm.(*adapters.URLTest) urlTest, ok := elm.(*adapters.URLTest)
if ok { if ok {
urlTest.Close() urlTest.Close()
} }
} }
s, err := adapters.NewSelector("Proxy", proxys) s, err := adapters.NewSelector("Proxy", proxies)
if err != nil { if err != nil {
return err return err
} }
t.proxys = proxys t.proxies = proxies
t.rules = rules t.rules = rules
t.selector = s t.selector = s
@ -200,7 +200,7 @@ func (t *Tunnel) handleConn(localConn C.ServerAdapter) {
var proxy C.Proxy var proxy C.Proxy
switch t.mode { switch t.mode {
case Direct: case Direct:
proxy = t.proxys["DIRECT"] proxy = t.proxies["DIRECT"]
case Global: case Global:
proxy = t.selector proxy = t.selector
// Rule // Rule
@ -223,7 +223,7 @@ func (t *Tunnel) match(addr *C.Addr) C.Proxy {
for _, rule := range t.rules { for _, rule := range t.rules {
if rule.IsMatch(addr) { if rule.IsMatch(addr) {
a, ok := t.proxys[rule.Adapter()] a, ok := t.proxies[rule.Adapter()]
if !ok { if !ok {
continue continue
} }
@ -232,14 +232,14 @@ func (t *Tunnel) match(addr *C.Addr) C.Proxy {
} }
} }
t.logCh <- newLog(INFO, "%v doesn't match any rule using DIRECT", addr.String()) t.logCh <- newLog(INFO, "%v doesn't match any rule using DIRECT", addr.String())
return t.proxys["DIRECT"] return t.proxies["DIRECT"]
} }
func newTunnel() *Tunnel { func newTunnel() *Tunnel {
logCh := make(chan interface{}) logCh := make(chan interface{})
tunnel := &Tunnel{ tunnel := &Tunnel{
queue: channels.NewInfiniteChannel(), queue: channels.NewInfiniteChannel(),
proxys: make(map[string]C.Proxy), proxies: make(map[string]C.Proxy),
observable: observable.NewObservable(logCh), observable: observable.NewObservable(logCh),
logCh: logCh, logCh: logCh,
configLock: &sync.RWMutex{}, configLock: &sync.RWMutex{},