v2ray-core/transport/internet/kcp/config_json.go

62 lines
1.4 KiB
Go
Raw Normal View History

2016-06-14 20:54:08 +00:00
// +build json
package kcp
import (
"encoding/json"
2016-06-18 15:14:39 +00:00
2016-06-20 14:10:47 +00:00
"github.com/v2ray/v2ray-core/common"
2016-06-18 15:14:39 +00:00
"github.com/v2ray/v2ray-core/common/log"
2016-06-14 20:54:08 +00:00
)
func (this *Config) UnmarshalJSON(data []byte) error {
type JSONConfig struct {
2016-06-25 19:35:18 +00:00
Mtu *uint32 `json:"mtu"`
Tti *uint32 `json:"tti"`
UpCap *uint32 `json:"uplinkCapacity"`
DownCap *uint32 `json:"downlinkCapacity"`
Congestion *bool `json:"congestion"`
2016-06-14 21:25:06 +00:00
}
jsonConfig := new(JSONConfig)
2016-06-14 20:54:08 +00:00
if err := json.Unmarshal(data, &jsonConfig); err != nil {
return err
}
2016-06-14 21:25:06 +00:00
if jsonConfig.Mtu != nil {
2016-06-18 15:14:39 +00:00
mtu := *jsonConfig.Mtu
if mtu < 576 || mtu > 1460 {
log.Error("KCP|Config: Invalid MTU size: ", mtu)
2016-06-20 14:10:47 +00:00
return common.ErrBadConfiguration
2016-06-18 15:14:39 +00:00
}
2016-06-20 14:10:47 +00:00
this.Mtu = mtu
}
if jsonConfig.Tti != nil {
tti := *jsonConfig.Tti
if tti < 10 || tti > 100 {
log.Error("KCP|Config: Invalid TTI: ", tti)
return common.ErrBadConfiguration
}
this.Tti = tti
}
if jsonConfig.UpCap != nil {
upCap := *jsonConfig.UpCap
2016-06-25 19:35:18 +00:00
if upCap == 0 {
2016-06-20 14:10:47 +00:00
log.Error("KCP|Config: Invalid uplink capacity: ", upCap)
return common.ErrBadConfiguration
}
this.UplinkCapacity = upCap
}
if jsonConfig.DownCap != nil {
downCap := *jsonConfig.DownCap
2016-06-25 19:35:18 +00:00
if downCap == 0 {
2016-06-20 14:10:47 +00:00
log.Error("KCP|Config: Invalid downlink capacity: ", downCap)
return common.ErrBadConfiguration
}
this.DownlinkCapacity = downCap
}
if jsonConfig.Congestion != nil {
this.Congestion = *jsonConfig.Congestion
2016-06-14 21:25:06 +00:00
}
2016-06-14 20:54:08 +00:00
return nil
}