重命名包 client 为 edge;重命名包 server 为 gateway
This commit is contained in:
88
gateway/report/report.go
Normal file
88
gateway/report/report.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package report
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"proxy-server/gateway/app"
|
||||
"proxy-server/gateway/core"
|
||||
"proxy-server/gateway/env"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Online(name string) (err error) {
|
||||
var resp string
|
||||
resp, err = call(env.EndpointOnline, map[string]any{
|
||||
"name": name,
|
||||
"version": core.Version,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var body struct {
|
||||
Id int32 `json:"id"`
|
||||
Secret string `json:"secret"`
|
||||
}
|
||||
err = json.Unmarshal([]byte(resp), &body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
app.Id = body.Id
|
||||
app.PlatformSecret = body.Secret
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func Offline(name string) (err error) {
|
||||
_, err = call(env.EndpointOffline, map[string]any{
|
||||
"name": name,
|
||||
"version": core.Version,
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func Assigned(edgeId int32, port uint16) (err error) {
|
||||
_, err = call(env.EndpointAssigned, map[string]any{
|
||||
"proxy": app.Id,
|
||||
"edge": edgeId,
|
||||
"port": port,
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func call(endpoint string, body any) (string, error) {
|
||||
bodyStr, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", endpoint, strings.NewReader(string(bodyStr)))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
var auth = base64.RawURLEncoding.EncodeToString([]byte(env.ClientId + ":" + env.ClientSecret))
|
||||
var basic = fmt.Sprintf("Basic %s", auth)
|
||||
req.Header.Set("Authorization", basic)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return "", fmt.Errorf("请求失败,状态码:%d", resp.StatusCode)
|
||||
}
|
||||
|
||||
respBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(respBody), nil
|
||||
}
|
||||
Reference in New Issue
Block a user