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

View File

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

View File

@ -433,7 +433,7 @@ func (ctl *Control) RegisterProxy(pxyMsg *msg.NewProxy) (remoteAddr string, err
if err != nil { if err != nil {
return return
} }
if g.GlbServerCfg.EnableApi { if g.GlbServerCfg.EnableApi {
nowTime := time.Now().Unix() 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") 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 { if err != nil {
return remoteAddr, err return remoteAddr, err
} }
@ -466,7 +466,7 @@ func (ctl *Control) RegisterProxy(pxyMsg *msg.NewProxy) (remoteAddr string, err
if err != nil { if err != nil {
return remoteAddr, err return remoteAddr, err
} }
err = ctl.pxyManager.Add(pxyMsg.ProxyName, pxy) err = ctl.pxyManager.Add(pxyMsg.ProxyName, pxy)
if err != nil { if err != nil {
return return

View File

@ -46,7 +46,7 @@ import (
"github.com/fatedier/golib/net/mux" "github.com/fatedier/golib/net/mux"
fmux "github.com/hashicorp/yamux" fmux "github.com/hashicorp/yamux"
"github.com/fatedier/frp/extend/api" "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") err = fmt.Errorf("authorization failed")
return return
} }
if g.GlbServerCfg.EnableApi { if g.GlbServerCfg.EnableApi {
nowTime := time.Now().Unix() nowTime := time.Now().Unix()
s, err := api.NewService(g.GlbServerCfg.ApiBaseUrl) s, err := api.NewService(g.GlbServerCfg.ApiBaseUrl)
if err != nil { if err != nil {
return err 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}$`) r := regexp.MustCompile(`^[A-Za-z0-9]{1,32}$`)
mm := r.FindAllStringSubmatch(loginMsg.User, -1) mm := r.FindAllStringSubmatch(loginMsg.User, -1)
if len(mm) < 1 { if len(mm) < 1 {
return fmt.Errorf("invalid username") return fmt.Errorf("invalid username")
} }
// Connect to API server and verify the user. // Connect to API server and verify the user.
valid, err := s.CheckToken(loginMsg.User, loginMsg.PrivilegeKey, nowTime, g.GlbServerCfg.ApiToken) valid, err := s.CheckToken(loginMsg.User, loginMsg.PrivilegeKey, nowTime, g.GlbServerCfg.ApiToken)
if err != nil { if err != nil {
return err return err
} }
if !valid { if !valid {
return fmt.Errorf("authorization failed") return fmt.Errorf("authorization failed")
} }
} }
// If client's RunId is empty, it's a new client, we just create a new controller. // 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. // 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 == "" { if loginMsg.RunId == "" {