2016-01-27 06:46:40 -05:00
|
|
|
// +build json
|
|
|
|
|
|
|
|
package shadowsocks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
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/common/serial"
|
|
|
|
"github.com/v2ray/v2ray-core/proxy/internal"
|
2016-01-28 06:33:58 -05:00
|
|
|
"github.com/v2ray/v2ray-core/proxy/internal/config"
|
2016-01-27 06:46:40 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func (this *Config) UnmarshalJSON(data []byte) error {
|
|
|
|
type JsonConfig struct {
|
2016-01-27 09:57:53 -05:00
|
|
|
Cipher serial.StringLiteral `json:"method"`
|
|
|
|
Password serial.StringLiteral `json:"password"`
|
2016-01-27 16:11:31 -05:00
|
|
|
UDP bool `json:"udp"`
|
2016-02-03 06:18:28 -05:00
|
|
|
Level byte `json:"level"`
|
2016-01-27 06:46:40 -05:00
|
|
|
}
|
|
|
|
jsonConfig := new(JsonConfig)
|
|
|
|
if err := json.Unmarshal(data, jsonConfig); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-28 06:33:58 -05:00
|
|
|
|
2016-01-27 16:11:31 -05:00
|
|
|
this.UDP = jsonConfig.UDP
|
2016-01-27 09:57:53 -05:00
|
|
|
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{
|
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
|
|
|
|
}
|
|
|
|
this.Key = PasswordToCipherKey(jsonConfig.Password.String(), this.Cipher.KeySize())
|
|
|
|
|
2016-02-03 06:18:28 -05:00
|
|
|
this.Level = protocol.UserLevel(jsonConfig.Level)
|
|
|
|
|
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() {
|
|
|
|
config.RegisterInboundConfig("shadowsocks", func(data []byte) (interface{}, error) {
|
|
|
|
rawConfig := new(Config)
|
|
|
|
err := json.Unmarshal(data, rawConfig)
|
|
|
|
return rawConfig, err
|
|
|
|
})
|
|
|
|
}
|