1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-02-20 23:47:21 -05:00
v2fly/proxy/shadowsocks/config_json.go

67 lines
1.5 KiB
Go
Raw Normal View History

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
"github.com/v2ray/v2ray-core/common/log"
2016-02-03 12:18:28 +01:00
"github.com/v2ray/v2ray-core/common/protocol"
2016-01-27 15:57:53 +01:00
"github.com/v2ray/v2ray-core/proxy/internal"
2016-01-27 12:46:40 +01:00
)
func (this *Config) UnmarshalJSON(data []byte) error {
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
this.UDP = jsonConfig.UDP
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":
this.Cipher = &AesCfb{
KeyBytes: 32,
}
case "aes-128-cfb":
this.Cipher = &AesCfb{
2016-01-28 12:33:58 +01:00
KeyBytes: 16,
2016-01-27 15:57:53 +01:00
}
2016-02-23 18:16:13 +01:00
case "chacha20":
this.Cipher = &ChaCha20{
IVBytes: 8,
}
case "chacha20-ietf":
this.Cipher = &ChaCha20{
IVBytes: 12,
}
2016-01-27 15:57:53 +01:00
default:
log.Error("Shadowsocks: Unknown cipher method: ", jsonConfig.Cipher)
2016-06-27 08:53:35 +02:00
return internal.ErrBadConfiguration
2016-01-27 15:57:53 +01:00
}
2016-01-28 12:33:58 +01:00
if len(jsonConfig.Password) == 0 {
log.Error("Shadowsocks: Password is not specified.")
2016-06-27 08:53:35 +02:00
return internal.ErrBadConfiguration
2016-01-28 12:33:58 +01:00
}
2016-05-24 22:41:51 +02:00
this.Key = PasswordToCipherKey(jsonConfig.Password, this.Cipher.KeySize())
2016-01-28 12:33:58 +01:00
2016-02-03 12:18:28 +01:00
this.Level = protocol.UserLevel(jsonConfig.Level)
2016-02-28 14:50:30 +01:00
this.Email = jsonConfig.Email
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-06-10 22:26:39 +02:00
internal.RegisterInboundConfig("shadowsocks", func() interface{} { return new(Config) })
2016-01-28 12:33:58 +01:00
}