diff --git a/README.md b/README.md index 86ba3020..5101fb7a 100644 --- a/README.md +++ b/README.md @@ -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`. diff --git a/conf/frpc_full.ini b/conf/frpc_full.ini index 99ac0780..29f6bcab 100644 --- a/conf/frpc_full.ini +++ b/conf/frpc_full.ini @@ -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 diff --git a/doc/server_plugin.md b/doc/server_plugin.md index d73d2439..f4e377f4 100644 --- a/doc/server_plugin.md +++ b/doc/server_plugin.md @@ -110,6 +110,8 @@ Create new proxy "proxy_type": , "use_encryption": , "use_compression": , + "bandwidth_limit": , + "bandwidth_limit_mode": , "group": , "group_key": , diff --git a/pkg/config/proxy.go b/pkg/config/proxy.go index 359eda81..f57cbfb6 100644 --- a/pkg/config/proxy.go +++ b/pkg/config/proxy.go @@ -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 } diff --git a/pkg/config/types.go b/pkg/config/types.go index 93ef9302..7aefee12 100644 --- a/pkg/config/types.go +++ b/pkg/config/types.go @@ -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) -}