网关实现自定义接口安全检查与边缘节点连接权限验证

This commit is contained in:
2025-05-15 15:56:20 +08:00
parent b29882f0a7
commit d65fe4db6f
25 changed files with 353 additions and 703 deletions

View File

@@ -1,91 +1,88 @@
package report
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"log/slog"
"net/http"
"proxy-server/client/core"
"proxy-server/server/pkg/env"
"proxy-server/server/app"
"proxy-server/server/core"
"proxy-server/server/env"
"strings"
"time"
)
func Online(ctx context.Context, name string) (id int32, err error) {
func Online(name string) (err error) {
var resp string
resp, err = repeat(ctx, env.EndpointOnline, map[string]any{
resp, err = call(env.EndpointOnline, map[string]any{
"name": name,
"version": core.Version,
})
if err != nil {
return 0, err
return err
}
var body struct {
Id int32 `json:"id"`
Id int32 `json:"id"`
Secret string `json:"secret"`
}
err = json.Unmarshal([]byte(resp), &body)
if err != nil {
return 0, err
return err
}
if body.Id == 0 {
return 0, errors.New("服务注册返回 ID 有误")
} else {
return body.Id, nil
}
app.Id = body.Id
app.PlatformSecret = body.Secret
return nil
}
func Offline(ctx context.Context, name string) (err error) {
_, err = repeat(ctx, env.EndpointOffline, map[string]any{
func Offline(name string) (err error) {
_, err = call(env.EndpointOffline, map[string]any{
"name": name,
"version": core.Version,
})
return err
}
func Assigned(ctx context.Context, id int32, edgeId int32, port uint16) (err error) {
_, err = repeat(ctx, env.EndpointAssigned, map[string]any{
"proxy": id,
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 repeat(ctx context.Context, endpoint string, body any) (string, error) {
func call(endpoint string, body any) (string, error) {
bodyStr, err := json.Marshal(body)
if err != nil {
panic(err)
}
for {
req, err := http.NewRequest("POST", endpoint, strings.NewReader(string(bodyStr)))
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Basic "+base64.RawURLEncoding.EncodeToString([]byte("proxy:proxy")))
resp, err := http.DefaultClient.Do(req)
if resp != nil && resp.StatusCode == http.StatusOK {
var body, err = io.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(body), nil
}
select {
case <-ctx.Done():
return "", ctx.Err()
default:
}
slog.Warn("服务调用失败,五秒后重试", "err", err)
time.Sleep(5 * time.Second)
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
}