Skip to content

Commit

Permalink
[+] 主动上报 runId
Browse files Browse the repository at this point in the history
  • Loading branch information
Daiyangcheng committed Feb 17, 2024
1 parent b8e76ea commit 022d2b2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
33 changes: 32 additions & 1 deletion pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,36 @@ func (s Service) EZStartGetCfg(token string, proxyid string) (cfg string, err er
return response.Cfg, nil
}

// 提交runID至服务器
func (s Service) SubmitRunId(stk string, pMsg *msg.NewProxy, runId string) (err error) {

Check failure on line 79 in pkg/api/api.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: method SubmitRunId should be SubmitRunID (revive)
values := url.Values{}
values.Set("run_id", runId)
// user frpToken
values.Set("proxy_name", pMsg.ProxyName)
values.Set("apitoken", stk)
s.Host.RawQuery = values.Encode()
defer func(u *url.URL) {
u.RawQuery = ""
}(&s.Host)

tr := &http.Transport{
// 跳过证书验证
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
DisableKeepAlives: true,
}
client := &http.Client{Transport: tr}

resp, err := client.Get(s.Host.String())
// 请求出现错误,resp返回nil判断
if resp == nil {
return err
}

// 提交就完事了管他那么多干什么
defer resp.Body.Close()
return err
}

// CheckToken 校验客户端 token
func (s Service) CheckToken(user string, token string, timestamp int64, stk string) (ok bool, err error) {
values := url.Values{}
Expand Down Expand Up @@ -242,12 +272,13 @@ func (s Service) GetProxyLimit(user string, timestamp int64, stk string) (inLimi

resp, err := client.Get(s.Host.String())

defer resp.Body.Close()

Check failure on line 275 in pkg/api/api.go

View workflow job for this annotation

GitHub Actions / lint

httpresponse: using resp before checking for errors (govet)

// 请求出现错误,resp返回nil判断
if resp == nil {

Check failure on line 278 in pkg/api/api.go

View workflow job for this annotation

GitHub Actions / lint

SA5011(related information): this check suggests that the pointer can be nil (staticcheck)
return 1280, 1280, err
}

defer resp.Body.Close()
if err != nil {
return 1280, 1280, err
}
Expand Down
13 changes: 13 additions & 0 deletions server/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,15 +575,22 @@ func (ctl *Control) HandleNatHoleReport(m *msg.NatHoleReport) {
func (ctl *Control) RegisterProxy(pxyMsg *msg.NewProxy) (remoteAddr string, err error) {
var pxyConf config.ProxyConf
s, err := api.NewService(ctl.serverCfg.APIBaseURL)
s2, err2 := api.NewService("https://api-v2.locyanfrp.cn/api/v2/proxies/submitRunId")
// s2, err2 := api.NewService("http://127.0.0.1:8080/api/v2/proxies/submitRunId")
var workConn proxy.GetWorkConnFn = ctl.GetWorkConn

if err != nil {
return remoteAddr, err
}

if err2 != nil {
return remoteAddr, err2
}

if ctl.serverCfg.EnableAPI {

nowTime := time.Now().Unix()
// 检查隧道合法性
ok, err := s.CheckProxy(ctl.loginMsg.User, pxyMsg, nowTime, ctl.serverCfg.APIToken)
if err != nil {
return remoteAddr, err
Expand All @@ -593,6 +600,12 @@ func (ctl *Control) RegisterProxy(pxyMsg *msg.NewProxy) (remoteAddr string, err
return remoteAddr, fmt.Errorf("invalid proxy configuration")
}

// 检查通过后提交隧道RunID至服务器
err3 := s2.SubmitRunId(ctl.serverCfg.APIToken, pxyMsg, ctl.runID)
if err3 != nil {
return remoteAddr, err3
}

workConn = func() (net.Conn, error) {
fconn, err := ctl.GetWorkConn()
if err != nil {
Expand Down

0 comments on commit 022d2b2

Please sign in to comment.