1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-10-01 16:26:02 -04:00
v2fly/proxy/shadowsocks/config_json.go

72 lines
1.7 KiB
Go
Raw Normal View History

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