Commit 5db8673f authored by Lei Li's avatar Lei Li
Browse files

feat: 设置序列号

parent 5cb2c77c
...@@ -139,17 +139,21 @@ func genVersion() string { ...@@ -139,17 +139,21 @@ func genVersion() string {
func initSvcCfg() error { func initSvcCfg() error {
// 异常情况处理 // 异常情况处理
if _, err := os.Stat(global.SelfOperatedAndroid); os.IsExist(err) { if info, err := os.Stat(global.SelfOperatedAndroid); err == nil && info != nil && !info.IsDir() {
l.Infof("the host is a self operated Android device") l.Infof("the host is a self operated Android device")
for { for {
time.Sleep(24 * time.Hour) time.Sleep(24 * time.Hour)
} }
} }
// TODO: 序列号设置 // 序列号设置
if option.Opt.SerialNumber != "" { if option.Opt.SerialNumber != "" {
global.DeviceSerialNumber = option.Opt.SerialNumber global.DeviceSerialNumber = option.Opt.SerialNumber
} else { } else {
sn, err := global.GetDeviceID()
if err != nil {
return err
}
global.DeviceSerialNumber = sn
} }
global.SubscribePrefixInfo = global.DeviceSerialNumber + "/publish/" global.SubscribePrefixInfo = global.DeviceSerialNumber + "/publish/"
......
package global package global
import (
"crypto/hmac"
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"fmt"
"os"
"path"
"path/filepath"
"strings"
"agent/cmd/agent/config"
"agent/cmd/agent/option"
"linkfog.com/public/lib/l"
"github.com/gofrs/uuid"
)
const ( const (
DeviceIDFile = ".deviceID" DeviceIDFile = ".deviceID"
DeviceRegisterFIle = ".reg2lw.lock" DeviceRegisterFIle = ".reg2lw.lock"
...@@ -21,5 +39,49 @@ const ( ...@@ -21,5 +39,49 @@ const (
Dcs9942URL = "https://newdcs.bkdomain.cn:9992" Dcs9942URL = "https://newdcs.bkdomain.cn:9992"
DcsUploadFlowDeviceAPI = "/api/v4/sds/uploadFlowDevice" DcsUploadFlowDeviceAPI = "/api/v4/sds/uploadFlowDevice"
HmacKey = "4N][zmn;bPW!a>F~iU+[" HmacKey = "4N][zmn;bPW!a>F~iU+["
HmacKey2 = "semigroupal"
) )
func GetDeviceID() (string, error) {
sn := ""
if _, err := os.Stat(path.Join(config.WorkDir, DeviceIDFile)); os.IsNotExist(err) {
l.Warnf("the device has been registered and needs to be re registered")
sn, err = produceDeviceID()
if err != nil {
return "", err
}
} else {
content, err := os.ReadFile(path.Join(config.WorkDir, DeviceIDFile))
if err != nil {
return "", err
}
sn = strings.TrimSpace(string(content))
}
return sn, nil
}
func produceDeviceID() (string, error) {
if option.Opt.Vendor == "" {
return "", fmt.Errorf("first registration requires setting up a vendor")
}
//create sn
uuidV4, _ := uuid.NewV4()
uuid32 := uuidV4.String()
shaSum256 := fmt.Sprintf("%x", sha256.Sum256([]byte(uuid32)))
shaSum256 = strings.ToUpper(shaSum256[:12])
mac := hmac.New(sha512.New, []byte(HmacKey2))
mac.Write([]byte("32_" + option.Opt.Vendor + "_" + shaSum256))
last4 := hex.EncodeToString(mac.Sum(nil))[:4]
tmp := "32_" + option.Opt.Vendor + "_" + shaSum256 + last4 + "-DISKSN"
snByte := []byte(strings.ToUpper(tmp))
//generate sn file
err := os.WriteFile(filepath.Join(config.WorkDir, DeviceIDFile), snByte, 0666)
if err != nil {
return "", fmt.Errorf("failed to write device id file, err:%v", err)
}
return string(snByte), nil
}
...@@ -38,6 +38,7 @@ type options struct { ...@@ -38,6 +38,7 @@ type options struct {
MonitorDefaultBackupFileName string MonitorDefaultBackupFileName string
SerialNumber string SerialNumber string
Vendor string
// 调试信息 // 调试信息
MQTTBrokerURL string MQTTBrokerURL string
...@@ -66,7 +67,8 @@ const ( ...@@ -66,7 +67,8 @@ const (
ArgMonitorDefaultMaxRetryTimes = "monitor-default-max-retry-times" ArgMonitorDefaultMaxRetryTimes = "monitor-default-max-retry-times"
ArgMonitorDefaultBackupFileName = "monitor-default-backup-file-name" ArgMonitorDefaultBackupFileName = "monitor-default-backup-file-name"
ArgSerialNumber = "SN" ArgSerialNumber = "sn"
ArgVendor = "vendor"
) )
func flagSet() { func flagSet() {
...@@ -96,6 +98,7 @@ func flagSet() { ...@@ -96,6 +98,7 @@ func flagSet() {
flag.StringVar(&Opt.MonitorDefaultBackupFileName, ArgMonitorDefaultBackupFileName, "agent.bak", "agent file backup") flag.StringVar(&Opt.MonitorDefaultBackupFileName, ArgMonitorDefaultBackupFileName, "agent.bak", "agent file backup")
flag.StringVar(&Opt.SerialNumber, ArgSerialNumber, "", "serial number") flag.StringVar(&Opt.SerialNumber, ArgSerialNumber, "", "serial number")
flag.StringVar(&Opt.Vendor, ArgVendor, "", "vendor")
} }
func Parse() { func Parse() {
......
...@@ -9,6 +9,7 @@ replace linkfog.com/pluginx => web.lueluesay.top/git/lil/pluginx v0.0.0-20241017 ...@@ -9,6 +9,7 @@ replace linkfog.com/pluginx => web.lueluesay.top/git/lil/pluginx v0.0.0-20241017
require ( require (
github.com/beevik/ntp v1.4.3 github.com/beevik/ntp v1.4.3
github.com/eclipse/paho.mqtt.golang v1.5.0 github.com/eclipse/paho.mqtt.golang v1.5.0
github.com/gofrs/uuid v4.4.0+incompatible
github.com/judwhite/go-svc v1.2.1 github.com/judwhite/go-svc v1.2.1
github.com/olekukonko/tablewriter v0.0.5 github.com/olekukonko/tablewriter v0.0.5
github.com/prometheus/procfs v0.15.1 github.com/prometheus/procfs v0.15.1
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment