1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-23 02:26:49 -05:00
v2fly/proxy/shadowsocks/config_json.go

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