Fixed: throw error when shadowsocks cipher type wrong

This commit is contained in:
Dreamacro 2018-06-16 15:41:26 +08:00
parent c35b2fd958
commit 961250f998
2 changed files with 36 additions and 28 deletions

View File

@ -34,31 +34,32 @@ func (ss *ShadowsocksAdapter) Conn() net.Conn {
} }
type ShadowSocks struct { type ShadowSocks struct {
server string server string
cipher string cipher core.Cipher
password string
} }
func (ss *ShadowSocks) Generator(addr *C.Addr) (adapter C.ProxyAdapter, err error) { func (ss *ShadowSocks) Generator(addr *C.Addr) (adapter C.ProxyAdapter, err error) {
var key []byte
ciph, _ := core.PickCipher(ss.cipher, key, ss.password)
c, err := net.Dial("tcp", ss.server) c, err := net.Dial("tcp", ss.server)
if err != nil { if err != nil {
return nil, fmt.Errorf("%s connect error", ss.server) return nil, fmt.Errorf("%s connect error", ss.server)
} }
c.(*net.TCPConn).SetKeepAlive(true) c.(*net.TCPConn).SetKeepAlive(true)
c = ciph.StreamConn(c) c = ss.cipher.StreamConn(c)
_, err = c.Write(serializesSocksAddr(addr)) _, err = c.Write(serializesSocksAddr(addr))
return &ShadowsocksAdapter{conn: c}, err return &ShadowsocksAdapter{conn: c}, err
} }
func NewShadowSocks(ssURL string) *ShadowSocks { func NewShadowSocks(ssURL string) (*ShadowSocks, error) {
var key []byte
server, cipher, password, _ := parseURL(ssURL) server, cipher, password, _ := parseURL(ssURL)
return &ShadowSocks{ ciph, err := core.PickCipher(cipher, key, password)
server: server, if err != nil {
cipher: cipher, return nil, fmt.Errorf("ss %s initialize error: %s", server, err.Error())
password: password,
} }
return &ShadowSocks{
server: server,
cipher: ciph,
}, nil
} }
func parseURL(s string) (addr, cipher, password string, err error) { func parseURL(s string) (addr, cipher, password string, err error) {

View File

@ -32,23 +32,20 @@ func (t *Tunnel) Add(req C.ServerAdapter) {
} }
func (t *Tunnel) UpdateConfig() (err error) { func (t *Tunnel) UpdateConfig() (err error) {
t.configLock.Lock()
defer t.configLock.Unlock()
cfg, err := C.GetConfig() cfg, err := C.GetConfig()
if err != nil { if err != nil {
return return
} }
// clear proxys and rules // clear proxys and rules
t.proxys = make(map[string]C.Proxy) proxys := make(map[string]C.Proxy)
t.rules = []C.Rule{} rules := []C.Rule{}
proxys := cfg.Section("Proxy") proxysConfig := cfg.Section("Proxy")
rules := cfg.Section("Rule") rulesConfig := cfg.Section("Rule")
// parse proxy // parse proxy
for _, key := range proxys.Keys() { for _, key := range proxysConfig.Keys() {
proxy := strings.Split(key.Value(), ",") proxy := strings.Split(key.Value(), ",")
if len(proxy) == 0 { if len(proxy) == 0 {
continue continue
@ -61,16 +58,20 @@ func (t *Tunnel) UpdateConfig() (err error) {
continue continue
} }
ssURL := fmt.Sprintf("ss://%s:%s@%s:%s", proxy[3], proxy[4], proxy[1], proxy[2]) ssURL := fmt.Sprintf("ss://%s:%s@%s:%s", proxy[3], proxy[4], proxy[1], proxy[2])
t.proxys[key.Name()] = adapters.NewShadowSocks(ssURL) ss, err := adapters.NewShadowSocks(ssURL)
if err != nil {
return err
}
proxys[key.Name()] = ss
} }
} }
// init proxy // init proxy
t.proxys["DIRECT"] = adapters.NewDirect() proxys["DIRECT"] = adapters.NewDirect()
t.proxys["REJECT"] = adapters.NewReject() proxys["REJECT"] = adapters.NewReject()
// parse rules // parse rules
for _, key := range rules.Keys() { for _, key := range rulesConfig.Keys() {
rule := strings.Split(key.Name(), ",") rule := strings.Split(key.Name(), ",")
if len(rule) < 3 { if len(rule) < 3 {
continue continue
@ -78,18 +79,24 @@ func (t *Tunnel) UpdateConfig() (err error) {
rule = trimArr(rule) rule = trimArr(rule)
switch rule[0] { switch rule[0] {
case "DOMAIN-SUFFIX": case "DOMAIN-SUFFIX":
t.rules = append(t.rules, R.NewDomainSuffix(rule[1], rule[2])) rules = append(rules, R.NewDomainSuffix(rule[1], rule[2]))
case "DOMAIN-KEYWORD": case "DOMAIN-KEYWORD":
t.rules = append(t.rules, R.NewDomainKeyword(rule[1], rule[2])) rules = append(rules, R.NewDomainKeyword(rule[1], rule[2]))
case "GEOIP": case "GEOIP":
t.rules = append(t.rules, R.NewGEOIP(rule[1], rule[2])) rules = append(rules, R.NewGEOIP(rule[1], rule[2]))
case "IP-CIDR", "IP-CIDR6": case "IP-CIDR", "IP-CIDR6":
t.rules = append(t.rules, R.NewIPCIDR(rule[1], rule[2])) rules = append(rules, R.NewIPCIDR(rule[1], rule[2]))
case "FINAL": case "FINAL":
t.rules = append(t.rules, R.NewFinal(rule[2])) rules = append(rules, R.NewFinal(rule[2]))
} }
} }
t.configLock.Lock()
defer t.configLock.Unlock()
t.proxys = proxys
t.rules = rules
return nil return nil
} }