bandwidth_limit_mode as string

This commit is contained in:
Craig O'Donnell 2023-02-02 22:41:43 -05:00
parent 5efa70ae9d
commit 7ec3149a5b
5 changed files with 15 additions and 35 deletions

View File

@ -717,7 +717,7 @@ type = tcp
local_port = 22
remote_port = 6000
bandwidth_limit = 1MB
bandwidth_limit_mode = server
bandwidth_limit_mode = client
```
Set `bandwidth_limit` in each proxy's configure to enable this feature. Supported units are `MB` and `KB`.

View File

@ -155,7 +155,7 @@ local_port = 22
# limit bandwidth for this proxy, unit is KB and MB
bandwidth_limit = 1MB
# where to limit bandwidth, can be 'client' or 'server', default is 'client'
bandwidth_limit_mode = server
bandwidth_limit_mode = client
# true or false, if true, messages between frps and frpc will be encrypted, default is false
use_encryption = false
# if true, message will be compressed

View File

@ -110,6 +110,8 @@ Create new proxy
"proxy_type": <string>,
"use_encryption": <bool>,
"use_compression": <bool>,
"bandwidth_limit": <string>,
"bandwidth_limit_mode": <string>,
"group": <string>,
"group_key": <string>,

View File

@ -144,7 +144,7 @@ type BaseProxyConf struct {
// BandwidthLimitMode specifies whether to limit the bandwidth on the
// client or server side. Valid values include "client" and "server".
// By default, this value is "client".
BandwidthLimitMode BandwidthLimitMode `ini:"bandwidth_limit_mode" json:"bandwidth_limit_mode"`
BandwidthLimitMode string `ini:"bandwidth_limit_mode" json:"bandwidth_limit_mode"`
// meta info for each proxy
Metas map[string]string `ini:"-" json:"metas"`
@ -323,6 +323,7 @@ func defaultBaseProxyConf(proxyType string) BaseProxyConf {
LocalSvrConf: LocalSvrConf{
LocalIP: "127.0.0.1",
},
BandwidthLimitMode: BandwidthLimitModeClient,
}
}
@ -370,15 +371,6 @@ func (cfg *BaseProxyConf) decorate(prefix string, name string, section *ini.Sect
}
}
if bandwidthMode, err := section.GetKey("bandwidth_limit_mode"); err == nil {
cfg.BandwidthLimitMode, err = NewBandwidthLimitMode(bandwidthMode.String())
if err != nil {
return err
}
} else {
cfg.BandwidthLimitMode = BandwidthLimitModeClient
}
// plugin_xxx
cfg.LocalSvrConf.PluginParams = GetMapByPrefix(section.KeysHash(), "plugin_")
@ -404,7 +396,7 @@ func (cfg *BaseProxyConf) marshalToMsg(pMsg *msg.NewProxy) {
pMsg.UseEncryption = cfg.UseEncryption
pMsg.UseCompression = cfg.UseCompression
pMsg.BandwidthLimit = cfg.BandwidthLimit.String()
pMsg.BandwidthLimitMode = cfg.BandwidthLimitMode.String()
pMsg.BandwidthLimitMode = cfg.BandwidthLimitMode
pMsg.Group = cfg.Group
pMsg.GroupKey = cfg.GroupKey
pMsg.Metas = cfg.Metas
@ -416,7 +408,7 @@ func (cfg *BaseProxyConf) unmarshalFromMsg(pMsg *msg.NewProxy) {
cfg.UseEncryption = pMsg.UseEncryption
cfg.UseCompression = pMsg.UseCompression
cfg.BandwidthLimit, _ = NewBandwidthQuantity(pMsg.BandwidthLimit)
cfg.BandwidthLimitMode, _ = NewBandwidthLimitMode(pMsg.BandwidthLimitMode)
cfg.BandwidthLimitMode = pMsg.BandwidthLimitMode
cfg.Group = pMsg.Group
cfg.GroupKey = pMsg.GroupKey
cfg.Metas = pMsg.Metas
@ -429,6 +421,10 @@ func (cfg *BaseProxyConf) checkForCli() (err error) {
}
}
if cfg.BandwidthLimitMode != "client" && cfg.BandwidthLimitMode != "server" {
return fmt.Errorf("bandwidth_limit_mode should be client or server")
}
if err = cfg.LocalSvrConf.checkForCli(); err != nil {
return
}

View File

@ -24,6 +24,9 @@ import (
const (
MB = 1024 * 1024
KB = 1024
BandwidthLimitModeClient = "client"
BandwidthLimitModeServer = "server"
)
type BandwidthQuantity struct {
@ -120,24 +123,3 @@ func (q *BandwidthQuantity) MarshalJSON() ([]byte, error) {
func (q *BandwidthQuantity) Bytes() int64 {
return q.i
}
type BandwidthLimitMode string
const (
BandwidthLimitModeClient BandwidthLimitMode = "client"
BandwidthLimitModeServer BandwidthLimitMode = "server"
)
// Create a new BandwidthLimitMode from a string. Default to "client".
func NewBandwidthLimitMode(s string) (BandwidthLimitMode, error) {
switch BandwidthLimitMode(s) {
case BandwidthLimitModeClient, BandwidthLimitModeServer:
return BandwidthLimitMode(s), nil
default:
return BandwidthLimitModeClient, errors.New("invalid bandwidth_limit_mode")
}
}
func (m BandwidthLimitMode) String() string {
return string(m)
}