建立仓库

This commit is contained in:
2025-08-05 10:51:35 +08:00
commit 4bbc05fe1f
36 changed files with 1946 additions and 0 deletions

45
clients/jd/client-auth.go Normal file
View File

@@ -0,0 +1,45 @@
package jd
import (
"encoding/json"
"errors"
"os"
)
func ClientAuth() (*ClientAuthResp, error) {
var username = os.Getenv("JD_USERNAME")
if username == "" {
return nil, errors.New("JD_USERNAME 环境变量未设置")
}
var password = os.Getenv("JD_PASSWORD")
if password == "" {
return nil, errors.New("JD_PASSWORD 环境变量未设置")
}
path := "/client/auth"
resp, err := Post(path, map[string]any{
"username": username,
"password": password,
}, false)
if err != nil {
return nil, err
}
var respData = new(ClientAuthResp)
if err := json.NewDecoder(resp).Decode(respData); err != nil {
panic("响应解析失败: " + err.Error())
}
if respData.Code != 0 {
panic("响应失败: " + respData.Meta)
}
return respData, nil
}
type ClientAuthResp struct {
Code int `json:"code"`
Meta string `json:"meta"`
Data string
}

77
clients/jd/common.go Normal file
View File

@@ -0,0 +1,77 @@
package jd
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"github.com/redis/go-redis/v9"
"io"
"net/http"
"time"
"zzman/clients"
)
const base = "https://smart.jdbox.xyz:58001"
func Post(path string, data any, auth ...bool) (io.ReadCloser, error) {
reqBytes, err := json.Marshal(data)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", base+path, bytes.NewReader(reqBytes))
if err != nil {
return nil, err
}
defer req.Body.Close()
req.Header.Set("Content-Type", "application/json")
if len(auth) == 0 || auth[0] {
token, err := Token()
if err != nil {
return nil, fmt.Errorf("获取令牌失败: %w", err)
}
req.Header.Set("X-Token", token)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s 响应失败: %s", path, resp.Status)
}
return resp.Body, nil
}
func Token() (string, error) {
// 尝试从 Redis 获取缓存的 token
token, err := clients.Redis.Get(context.Background(), "token").Result()
if err != nil && !errors.Is(err, redis.Nil) {
return "", fmt.Errorf("获取 token 失败: %w", err)
}
if token != "" {
return token, nil
}
// 如果缓存中没有 token则进行认证请求
resp, err := ClientAuth()
if err != nil {
return "", fmt.Errorf("认证请求失败: %w", err)
}
// 返回请求的 token
token = resp.Data
if err := clients.Redis.Set(context.Background(), "token", token, 6*24*time.Hour).Err(); err != nil {
return "", fmt.Errorf("缓存 token 失败: %w", err)
}
return token, nil
}

52
clients/jd/edge-device.go Normal file
View File

@@ -0,0 +1,52 @@
package jd
import (
"encoding/json"
"fmt"
)
func EdgeDevice(req EdgeDeviceReq) (*EdgeDeviceData, error) {
path := "/edge/device"
resp, err := Post(path, req)
if err != nil {
return nil, err
}
var respData EdgeDeviceResp
if err := json.NewDecoder(resp).Decode(&respData); err != nil {
return nil, fmt.Errorf("%s 响应解析失败: %v", path, err)
}
if respData.Code != 0 {
return nil, fmt.Errorf("%s 响应失败: %s", path, respData.Meta)
}
return &respData.Data, nil
}
type EdgeDeviceReq struct {
Geo string `json:"geo"`
Offset int `json:"offset"`
Num int `json:"num"`
}
type EdgeDeviceResp struct {
Code int `json:"code"`
Meta string `json:"meta"`
Data EdgeDeviceData `json:"data"`
}
type EdgeDeviceData struct {
Count int `json:"count"`
Edges []EdgeDeviceEdge `json:"edges"`
}
type EdgeDeviceEdge struct {
Macaddr string `json:"macaddr"`
Public string `json:"public"`
Isp string `json:"isp"`
Single int `json:"single"`
Sole bool `json:"sole"`
Arch int `json:"arch"`
Online int `json:"online"`
}

View File

@@ -0,0 +1,61 @@
package jd
import "fmt"
type EdgeInfo struct {
Mac string
City string
}
func GatewayConfigSet(version int, macaddr string, edges []EdgeInfo) error {
if version < 0 {
return fmt.Errorf("版本号不能小于 0")
}
if macaddr == "" {
return fmt.Errorf("macaddr 不能为空")
}
if len(edges) > 250 {
return fmt.Errorf("边缘节点数量不能超过 250")
}
rules := make([]GateConfigSetReqRule, len(edges))
for i, edge := range edges {
rules[i] = GateConfigSetReqRule{
Enable: true,
Edge: []string{edge.Mac},
Network: []string{fmt.Sprintf("172.30.168.%d", i+2)},
Cityhash: edge.City, // 每个 edge 的城市应当相同
}
}
req := GatewayConfigSetReq{
Macaddr: macaddr,
Config: GateConfigSetReqConfig{
Id: version,
Rules: rules,
},
}
if _, err := Post("/gateway/config/set", req); err != nil {
return fmt.Errorf("设置网关配置失败: %v", err)
}
return nil
}
type GatewayConfigSetReq struct {
Macaddr string `json:"macaddr"`
Config GateConfigSetReqConfig `json:"config"`
}
type GateConfigSetReqConfig struct {
Id int `json:"id"`
Rules []GateConfigSetReqRule `json:"rules"`
}
type GateConfigSetReqRule struct {
Enable bool `json:"enable"`
Edge []string `json:"edge"`
Network []string `json:"network"`
Cityhash string `json:"cityhash"`
}

31
clients/redis.go Normal file
View File

@@ -0,0 +1,31 @@
package clients
import (
redis "github.com/redis/go-redis/v9"
"log/slog"
"os"
)
var Redis *redis.Client
func InitRedis() {
host := os.Getenv("REDIS_HOST")
if host == "" {
slog.Debug("REDIS_HOST 环境变量未设置,使用默认值 localhost")
host = "localhost"
}
port := os.Getenv("REDIS_PORT")
if port == "" {
slog.Debug("REDIS_PORT 环境变量未设置,使用默认值 6379")
port = "6379"
}
password := os.Getenv("REDIS_PASSWORD")
if password == "" {
slog.Debug("REDIS_PASSWORD 环境变量未设置,使用默认值空字符串")
}
Redis = redis.NewClient(&redis.Options{
Addr: host + ":" + port,
Password: password,
})
}