v2ray-core/proxy/shadowsocks/config_json.go

49 lines
1.2 KiB
Go
Raw Normal View History

2016-01-27 12:46:40 +01:00
// +build json
package shadowsocks
import (
"encoding/json"
2016-01-27 15:57:53 +01:00
"github.com/v2ray/v2ray-core/common/log"
"github.com/v2ray/v2ray-core/common/serial"
"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-01-27 15:57:53 +01:00
Cipher serial.StringLiteral `json:"method"`
Password serial.StringLiteral `json:"password"`
UDP bool `json:"udp"`
2016-01-27 12:46:40 +01:00
}
jsonConfig := new(JsonConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
return err
}
2016-01-27 15:57:53 +01:00
if len(jsonConfig.Password) == 0 {
log.Error("Shadowsocks: Password is not specified.")
return internal.ErrorBadConfiguration
}
this.UDP = jsonConfig.UDP
2016-01-27 15:57:53 +01:00
this.Password = jsonConfig.Password.String()
if this.Cipher == nil {
log.Error("Shadowsocks: Cipher method is not specified.")
return internal.ErrorBadConfiguration
}
jsonConfig.Cipher = jsonConfig.Cipher.ToLower()
switch jsonConfig.Cipher.String() {
case "aes-256-cfb":
this.Cipher = &AesCfb{
KeyBytes: 32,
}
case "aes-128-cfb":
this.Cipher = &AesCfb{
KeyBytes: 32,
}
default:
log.Error("Shadowsocks: Unknown cipher method: ", jsonConfig.Cipher)
return internal.ErrorBadConfiguration
}
return nil
2016-01-27 12:46:40 +01:00
}