2016-06-14 16:54:08 -04:00
|
|
|
// +build json
|
|
|
|
|
|
|
|
package kcp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2016-06-18 11:14:39 -04:00
|
|
|
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/common"
|
|
|
|
"v2ray.com/core/common/log"
|
|
|
|
"v2ray.com/core/transport/internet"
|
2016-06-14 16:54:08 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func (this *Config) UnmarshalJSON(data []byte) error {
|
|
|
|
type JSONConfig struct {
|
2016-08-06 15:59:22 -04:00
|
|
|
Mtu *uint32 `json:"mtu"`
|
|
|
|
Tti *uint32 `json:"tti"`
|
|
|
|
UpCap *uint32 `json:"uplinkCapacity"`
|
|
|
|
DownCap *uint32 `json:"downlinkCapacity"`
|
|
|
|
Congestion *bool `json:"congestion"`
|
|
|
|
ReadBufferSize *uint32 `json:"readBufferSize"`
|
|
|
|
WriteBufferSize *uint32 `json:"writeBufferSize"`
|
|
|
|
HeaderConfig json.RawMessage `json:"header"`
|
2016-06-14 17:25:06 -04:00
|
|
|
}
|
|
|
|
jsonConfig := new(JSONConfig)
|
2016-06-14 16:54:08 -04:00
|
|
|
if err := json.Unmarshal(data, &jsonConfig); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-14 17:25:06 -04:00
|
|
|
if jsonConfig.Mtu != nil {
|
2016-06-18 11:14:39 -04:00
|
|
|
mtu := *jsonConfig.Mtu
|
|
|
|
if mtu < 576 || mtu > 1460 {
|
|
|
|
log.Error("KCP|Config: Invalid MTU size: ", mtu)
|
2016-06-20 10:10:47 -04:00
|
|
|
return common.ErrBadConfiguration
|
2016-06-18 11:14:39 -04:00
|
|
|
}
|
2016-09-21 15:08:05 -04:00
|
|
|
this.Mtu = &MTU{Value: *jsonConfig.Mtu}
|
2016-06-20 10:10:47 -04:00
|
|
|
}
|
|
|
|
if jsonConfig.Tti != nil {
|
|
|
|
tti := *jsonConfig.Tti
|
|
|
|
if tti < 10 || tti > 100 {
|
|
|
|
log.Error("KCP|Config: Invalid TTI: ", tti)
|
|
|
|
return common.ErrBadConfiguration
|
|
|
|
}
|
2016-09-21 15:08:05 -04:00
|
|
|
this.Tti = &TTI{Value: *jsonConfig.Tti}
|
2016-06-20 10:10:47 -04:00
|
|
|
}
|
|
|
|
if jsonConfig.UpCap != nil {
|
2016-09-21 15:08:05 -04:00
|
|
|
this.UplinkCapacity = &UplinkCapacity{Value: *jsonConfig.UpCap}
|
2016-06-20 10:10:47 -04:00
|
|
|
}
|
|
|
|
if jsonConfig.DownCap != nil {
|
2016-09-21 15:08:05 -04:00
|
|
|
this.DownlinkCapacity = &DownlinkCapacity{Value: *jsonConfig.DownCap}
|
2016-06-20 10:10:47 -04:00
|
|
|
}
|
|
|
|
if jsonConfig.Congestion != nil {
|
|
|
|
this.Congestion = *jsonConfig.Congestion
|
2016-06-14 17:25:06 -04:00
|
|
|
}
|
2016-07-15 15:27:23 -04:00
|
|
|
if jsonConfig.ReadBufferSize != nil {
|
|
|
|
size := *jsonConfig.ReadBufferSize
|
|
|
|
if size > 0 {
|
2016-09-21 15:08:05 -04:00
|
|
|
this.ReadBuffer = &ReadBuffer{Size: size * 1024 * 1024}
|
2016-07-15 15:27:23 -04:00
|
|
|
} else {
|
2016-09-21 15:08:05 -04:00
|
|
|
this.ReadBuffer = &ReadBuffer{Size: 512 * 1024}
|
2016-07-15 15:27:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if jsonConfig.WriteBufferSize != nil {
|
|
|
|
size := *jsonConfig.WriteBufferSize
|
|
|
|
if size > 0 {
|
2016-09-21 15:08:05 -04:00
|
|
|
this.WriteBuffer = &WriteBuffer{Size: size * 1024 * 1024}
|
2016-07-15 15:27:23 -04:00
|
|
|
} else {
|
2016-09-21 15:08:05 -04:00
|
|
|
this.WriteBuffer = &WriteBuffer{Size: 512 * 1024}
|
2016-07-15 15:27:23 -04:00
|
|
|
}
|
|
|
|
}
|
2016-08-06 15:59:22 -04:00
|
|
|
if len(jsonConfig.HeaderConfig) > 0 {
|
2016-08-06 16:52:02 -04:00
|
|
|
name, config, err := internet.CreateAuthenticatorConfig(jsonConfig.HeaderConfig)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("KCP|Config: Failed to parse header config: ", err)
|
|
|
|
return err
|
|
|
|
}
|
2016-09-21 08:39:07 -04:00
|
|
|
authConfig, err := internet.NewAuthenticatorConfig(name, config)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("KCP:Config: Failed to create header config: ", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
this.HeaderConfig = authConfig
|
2016-08-06 15:59:22 -04:00
|
|
|
}
|
2016-06-14 17:25:06 -04:00
|
|
|
|
2016-06-14 16:54:08 -04:00
|
|
|
return nil
|
|
|
|
}
|