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