server: 平台抽象层+bilibili、服务端加密凭据库、角色中间件;web: 精简页面/路由、macOS 客户端(Swift)与多份方案文档;移除误入库的编译产物
This commit is contained in:
@@ -2,11 +2,14 @@ package pairing
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/ed25519"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -69,11 +72,27 @@ func AutoPair(server, email, password, deviceName, pubKeyB64 string) (uint64, er
|
||||
|
||||
// PairWithCode 凭配对码注册设备,返回服务器分配的设备 ID
|
||||
func PairWithCode(server, pairCode, deviceName, osName, version, pubKeyB64 string) (uint64, error) {
|
||||
pairCode = strings.ToUpper(strings.TrimSpace(pairCode))
|
||||
deviceName = strings.TrimSpace(deviceName)
|
||||
if deviceName == "" {
|
||||
deviceName = "macOS Agent"
|
||||
}
|
||||
pubKeyB64 = strings.TrimSpace(pubKeyB64)
|
||||
if pubKeyB64 == "" {
|
||||
return 0, fmt.Errorf("本机设备公钥为空,请重启客户端后重试")
|
||||
}
|
||||
pubKey, keyErr := base64.StdEncoding.DecodeString(pubKeyB64)
|
||||
if keyErr != nil || len(pubKey) != ed25519.PublicKeySize {
|
||||
return 0, fmt.Errorf("本机设备公钥格式错误,请重启客户端后重试")
|
||||
}
|
||||
code, raw, err := httpJSON("POST", server+"/api/v1/agent/pair", "", ginH{
|
||||
"code": pairCode, "deviceName": deviceName, "os": osName, "version": version, "publicKey": pubKeyB64,
|
||||
})
|
||||
if err != nil || code != 200 {
|
||||
return 0, fmt.Errorf("pair failed: %s", string(raw))
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("配对请求失败:%w", err)
|
||||
}
|
||||
if code != http.StatusOK {
|
||||
return 0, fmt.Errorf("%s", responseMessage(raw, "配对失败"))
|
||||
}
|
||||
id, _ := strconv.ParseUint(field(raw, "deviceId"), 10, 64)
|
||||
if id == 0 {
|
||||
@@ -81,3 +100,17 @@ func PairWithCode(server, pairCode, deviceName, osName, version, pubKeyB64 strin
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func responseMessage(raw []byte, fallback string) string {
|
||||
var envelope struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &envelope); err == nil && strings.TrimSpace(envelope.Message) != "" {
|
||||
return envelope.Message
|
||||
}
|
||||
if text := strings.TrimSpace(string(raw)); text != "" {
|
||||
return text
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user