-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
42 lines (39 loc) · 870 Bytes
/
config.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
package proxy
import (
"fmt"
"strings"
)
type Config struct {
Proto string
Addr string
Method string
Password string
}
func ParseConfig(expr string) (*Config, error) {
if expr == "tcp" {
return &Config{Proto: "tcp"}, nil
}
parts := strings.SplitN(expr, "://", 2)
if len(parts) != 2 {
return nil, fmt.Errorf("bad expr %s", expr)
}
conf := &Config{Proto: parts[0]}
if conf.Proto == "socks5" {
conf.Addr = parts[1]
return conf, nil
}
if conf.Proto == "ssocks" {
parts = strings.SplitN(parts[1], "@", 2)
if len(parts) != 2 {
return nil, fmt.Errorf("bad expr %s", expr)
}
conf.Addr = parts[1]
parts = strings.SplitN(parts[0], ":", 2)
if len(parts) != 2 {
return nil, fmt.Errorf("bad expr %s", expr)
}
conf.Method, conf.Password = parts[0], parts[1]
return conf, nil
}
return nil, fmt.Errorf("bad expr %s", expr)
}