v2ray-core/proxy/shadowsocks/config_json.go

70 lines
1.7 KiB
Go
Raw Normal View History

2016-01-27 11:46:40 +00:00
// +build json
package shadowsocks
import (
"encoding/json"
2016-06-11 20:52:37 +00:00
"errors"
2016-05-24 20:41:51 +00:00
"strings"
2016-01-27 14:57:53 +00:00
2016-08-20 18:55:45 +00:00
"v2ray.com/core/common"
"v2ray.com/core/common/log"
"v2ray.com/core/common/protocol"
"v2ray.com/core/proxy/registry"
2016-09-17 22:41:21 +00:00
"github.com/golang/protobuf/ptypes"
2016-01-27 11:46:40 +00:00
)
func (this *Config) UnmarshalJSON(data []byte) error {
type JsonConfig struct {
2016-05-24 20:41:51 +00:00
Cipher string `json:"method"`
Password string `json:"password"`
UDP bool `json:"udp"`
Level byte `json:"level"`
Email string `json:"email"`
2016-01-27 11:46:40 +00:00
}
jsonConfig := new(JsonConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
2016-06-11 20:52:37 +00:00
return errors.New("Shadowsocks: Failed to parse config: " + err.Error())
2016-01-27 11:46:40 +00:00
}
2016-01-28 11:33:58 +00:00
2016-09-17 22:41:21 +00:00
this.UdpEnabled = jsonConfig.UDP
2016-05-24 20:41:51 +00:00
jsonConfig.Cipher = strings.ToLower(jsonConfig.Cipher)
switch jsonConfig.Cipher {
2016-01-27 14:57:53 +00:00
case "aes-256-cfb":
2016-09-17 22:41:21 +00:00
this.Cipher = Config_AES_256_CFB
2016-01-27 14:57:53 +00:00
case "aes-128-cfb":
2016-09-17 22:41:21 +00:00
this.Cipher = Config_AES_128_CFB
2016-02-23 17:16:13 +00:00
case "chacha20":
2016-09-17 22:41:21 +00:00
this.Cipher = Config_CHACHA20
2016-02-23 17:16:13 +00:00
case "chacha20-ietf":
2016-09-17 22:41:21 +00:00
this.Cipher = Config_CHACHA20_IEFT
2016-01-27 14:57:53 +00:00
default:
log.Error("Shadowsocks: Unknown cipher method: ", jsonConfig.Cipher)
2016-08-18 06:21:20 +00:00
return common.ErrBadConfiguration
2016-01-27 14:57:53 +00:00
}
2016-01-28 11:33:58 +00:00
if len(jsonConfig.Password) == 0 {
log.Error("Shadowsocks: Password is not specified.")
2016-08-18 06:21:20 +00:00
return common.ErrBadConfiguration
2016-01-28 11:33:58 +00:00
}
2016-09-17 22:41:21 +00:00
account, err := ptypes.MarshalAny(&Account{
Password: jsonConfig.Password,
})
if err != nil {
log.Error("Shadowsocks: Failed to create account: ", err)
return common.ErrBadConfiguration
}
this.User = &protocol.User{
Email: jsonConfig.Email,
Level: uint32(jsonConfig.Level),
Account: account,
}
2016-02-03 11:18:28 +00:00
2016-01-27 14:57:53 +00:00
return nil
2016-01-27 11:46:40 +00:00
}
2016-01-28 11:33:58 +00:00
func init() {
registry.RegisterInboundConfig("shadowsocks", func() interface{} { return new(Config) })
2016-01-28 11:33:58 +00:00
}