This commit is contained in:
kasuganosoras 2019-09-08 20:49:26 +08:00
parent 6a34c73071
commit fb65a33047
4 changed files with 30 additions and 30 deletions

View File

@ -8,7 +8,7 @@ import (
"net/url"
"strconv"
"github.com/fatedier/frp/extend/limit"
// "github.com/fatedier/frp/extend/limit"
"github.com/fatedier/frp/models/config"
"github.com/fatedier/frp/models/msg"
)
@ -147,11 +147,12 @@ func (s Service) CheckProxy(user string, pMsg *msg.NewProxy, timestamp int64, st
}
// GetProxyLimit 获取隧道限速信息
func (s Service) GetProxyLimit(pxyConf *config.BaseProxyConf) (inLimit, outLimit uint64, err error) {
func (s Service) GetProxyLimit(user string, pxyConf *config.BaseProxyConf, timestamp int64, stk string) (inLimit, outLimit uint64, err error) {
// 这部分就照之前的搬过去了能跑就行x
values := url.Values{}
values.Set("do", "getlimit")
values.Set("user", user)
values.Set("timestamp", fmt.Sprintf("%d", timestamp))
values.Set("frpstoken", stk)
s.Host.RawQuery = values.Encode()
defer func(u *url.URL) {
@ -159,27 +160,27 @@ func (s Service) GetProxyLimit(pxyConf *config.BaseProxyConf) (inLimit, outLimit
}(&s.Host)
resp, err := http.Get(s.Host.String())
if err != nil {
return nil, err
return 0, 0, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
return 0, 0, err
}
er := &ErrHTTPStatus{}
if err = json.Unmarshal(body, er); err != nil {
return nil, err
return 0, 0, err
}
if er.Status != 200 {
return nil, er
return 0, 0, er
}
r = &ResponseGetLimit{}
if err = json.Unmarshal(body, r); err != nil {
return nil, err
response := &ResponseGetLimit{}
if err = json.Unmarshal(body, response); err != nil {
return 0, 0, err
}
return r.MaxIn, r.MaxOut, nil
return response.MaxIn, response.MaxOut, nil
}
func BoolToString(val bool) (str string) {

View File

@ -76,12 +76,11 @@ type ServerCommonConf struct {
MaxPortsPerClient int64 `json:"max_ports_per_client"`
HeartBeatTimeout int64 `json:"heart_beat_timeout"`
UserConnTimeout int64 `json:"user_conn_timeout"`
// API
EnableApi bool `json:"api_enable"`
ApiBaseUrl string `json:"api_baseurl"`
ApiToken string `json:"api_token"`
EnableApi bool `json:"api_enable"`
ApiBaseUrl string `json:"api_baseurl"`
ApiToken string `json:"api_token"`
}
func GetDefaultServerConf() *ServerCommonConf {
@ -317,21 +316,21 @@ func UnmarshalServerConfFromIni(defaultCfg *ServerCommonConf, content string) (c
cfg.HeartBeatTimeout = v
}
}
if tmpStr, ok = conf.Get("common", "api_enable"); ok && tmpStr == "false" {
cfg.EnableApi = false
} else {
cfg.EnableApi = true
}
if tmpStr, ok = conf.Get("common", "api_baseurl"); ok {
cfg.ApiBaseUrl = tmpStr
}
if tmpStr, ok = conf.Get("common", "api_token"); ok {
cfg.ApiToken = tmpStr
}
return
}

View File

@ -433,7 +433,7 @@ func (ctl *Control) RegisterProxy(pxyMsg *msg.NewProxy) (remoteAddr string, err
if err != nil {
return
}
if g.GlbServerCfg.EnableApi {
nowTime := time.Now().Unix()
@ -447,7 +447,7 @@ func (ctl *Control) RegisterProxy(pxyMsg *msg.NewProxy) (remoteAddr string, err
return remoteAddr, fmt.Errorf("invalid proxy configuration")
}
in, out, err := s.GetProxyLimit(pxyConf.GetBaseInfo())
in, out, err := s.GetProxyLimit(ctl.loginMsg.User, pxyConf.GetBaseInfo(), nowTime, g.GlbServerCfg.ApiToken)
if err != nil {
return remoteAddr, err
}
@ -466,7 +466,7 @@ func (ctl *Control) RegisterProxy(pxyMsg *msg.NewProxy) (remoteAddr string, err
if err != nil {
return remoteAddr, err
}
err = ctl.pxyManager.Add(pxyMsg.ProxyName, pxy)
if err != nil {
return

View File

@ -46,7 +46,7 @@ import (
"github.com/fatedier/golib/net/mux"
fmux "github.com/hashicorp/yamux"
"github.com/fatedier/frp/extend/api"
)
@ -370,11 +370,11 @@ func (svr *Service) RegisterControl(ctlConn frpNet.Conn, loginMsg *msg.Login) (e
err = fmt.Errorf("authorization failed")
return
}
if g.GlbServerCfg.EnableApi {
nowTime := time.Now().Unix()
s, err := api.NewService(g.GlbServerCfg.ApiBaseUrl)
if err != nil {
return err
@ -382,23 +382,23 @@ func (svr *Service) RegisterControl(ctlConn frpNet.Conn, loginMsg *msg.Login) (e
r := regexp.MustCompile(`^[A-Za-z0-9]{1,32}$`)
mm := r.FindAllStringSubmatch(loginMsg.User, -1)
if len(mm) < 1 {
return fmt.Errorf("invalid username")
}
// Connect to API server and verify the user.
valid, err := s.CheckToken(loginMsg.User, loginMsg.PrivilegeKey, nowTime, g.GlbServerCfg.ApiToken)
if err != nil {
return err
}
if !valid {
return fmt.Errorf("authorization failed")
}
}
// If client's RunId is empty, it's a new client, we just create a new controller.
// Otherwise, we check if there is one controller has the same run id. If so, we release previous controller and start new one.
if loginMsg.RunId == "" {