From cd769ea498c08d5742b444fe6451b4c22899a317 Mon Sep 17 00:00:00 2001
From: zhangzengfei <zhangzengfei@smartai.com>
Date: 星期一, 08 十一月 2021 15:45:10 +0800
Subject: [PATCH] 授权文件添加通道数
---
licence.go | 99 +++++++++++++++++++++++++++++++++++++++----------
1 files changed, 79 insertions(+), 20 deletions(-)
diff --git a/licence.go b/licence.go
index 31d5799..f92d706 100644
--- a/licence.go
+++ b/licence.go
@@ -1,13 +1,20 @@
package licence
import (
- "encoding/base64"
"encoding/json"
+ "fmt"
"io/ioutil"
"os"
"time"
+ "errors"
)
+/*
+ 1.0.0 閲囬泦鏈哄櫒鐮�, 鎺堟潈鏃ユ湡 鐒跺悗 AES 鍔犲瘑, 鐢熸垚Licence, 绉橀挜浼氭毚闇茬粰瀹㈡埛绔�
+ 1.0.1 淇敼涓篟SA 闈炲绉板姞瀵�, 鍏挜寮�鏀�. 鍔犲瘑鍐呭涓簕娉ㄥ唽鐮亄鏈哄櫒鐮�+鍏徃+閭+鎵嬫満}+杩囨湡鏃堕棿+鎺堟潈鏃堕棿}
+ 1.0.2 淇敼Licence鏂囦欢鍐呭鏈�缁堜负AES鍔犲瘑鍚庣殑鍐呭, 鐒跺悗鍐嶇敱鍏挜瑙e瘑. 涓婁釜鐗堟湰鐨刡ug, 浠呬娇鐢ㄩ潪瀵圭О鍔犲瘑, 鐢ㄦ埛鍙互鏇挎崲鍏挜
+ 1.0.3 鎺堟潈鏂囦欢娣诲姞閫氶亾鏁伴噺, 澧炲姞璇诲彇閫氶亾鏁伴噺鎺ュ彛
+*/
type RegisterCode struct {
MachineCode string
Company string
@@ -18,6 +25,7 @@
type Licence struct {
RegCode RegisterCode
+ Channel int64
Expires int64
Timestamp int64
}
@@ -28,7 +36,7 @@
ValidationErrorExpired // Signature expired
ValidationErrorMalformed
- Version = "1.0.1"
+ Version = "1.0.3"
aesKey = "www.aiotlink.com"
)
@@ -46,7 +54,7 @@
return AESDecodeStr(regCode, aesKey)
}
-func GenerateLicence(regCode, timeOut string, privateKey []byte) (string, error) {
+func GenerateLicence(regCode, timeOut, privateKeyPath string, channel int64) (string, error) {
timeLayout := "2006-01-02 15:04:05" //杞寲鎵�闇�妯℃澘
loc, _ := time.LoadLocation("Local") //鑾峰彇鏃跺尯
tmp, _ := time.ParseInLocation(timeLayout, timeOut, loc)
@@ -59,9 +67,22 @@
return "", err
}
- licence := Licence{registerCode, exp, now}
+ licence := Licence{registerCode, channel,exp, now}
json, err := json.Marshal(licence)
if err != nil {
+ return "", err
+ }
+
+ fd, err := os.Open(privateKeyPath)
+ if err != nil {
+ fmt.Println(err)
+ return "", err
+ }
+
+ defer fd.Close()
+ privateKey, err := ioutil.ReadAll(fd)
+ if err != nil {
+ fmt.Println(err)
return "", err
}
@@ -75,36 +96,34 @@
return "", err
}
- licenceText := base64.StdEncoding.EncodeToString(licenceHex)
-
- return licenceText, nil
+ return AESEncodeStr(licenceHex, aesKey), nil
}
-func DecryptLicence(licencePath, publicKeyPath string) ([]byte, error) {
- var publicKey, licenceCode []byte
- var fdLic, fdPub *os.File
- var err error
-
+func DecryptLicenceFile(licencePath, publicKeyPath string) ([]byte, error) {
// 璇诲彇Licence File
- fdLic, err = os.Open(licencePath)
+ fdLic, err := os.Open(licencePath)
if err != nil {
return nil, err
}
defer fdLic.Close()
- licenceCode, err = ioutil.ReadAll(fdLic)
+ licenceCode, err := ioutil.ReadAll(fdLic)
if err != nil {
return nil, err
}
+ return DecryptLicence(string(licenceCode), publicKeyPath)
+}
+
+func DecryptLicence(licenceCode, publicKeyPath string) ([]byte, error) {
// 璇诲彇鍏挜
- fdPub, err = os.Open(publicKeyPath)
+ fdPub, err := os.Open(publicKeyPath)
if err != nil {
return nil, err
}
defer fdPub.Close()
- publicKey, err = ioutil.ReadAll(fdPub)
+ publicKey, err := ioutil.ReadAll(fdPub)
if err != nil {
return nil, err
}
@@ -114,25 +133,65 @@
return nil, err
}
- licenceHex, _ := base64.StdEncoding.DecodeString(string(licenceCode))
+ licenceHex := AESDecodeStr(licenceCode, aesKey)
return RSA.PubKeyDECRYPT(licenceHex)
}
-func VerifyLicence(licencePath, publicKeyPath string) uint32 {
- licenceText, err := DecryptLicence(licencePath, publicKeyPath)
+func VerifyLicenceFile(licencePath, publicKeyPath string) uint32 {
+ licenceText, err := DecryptLicenceFile(licencePath, publicKeyPath)
+ if err != nil {
+ return ValidationErrorDecrypt
+ }
+ return _verifyLicence(licenceText)
+}
+
+func VerifyLicence(licenceCode, publicKeyPath string) uint32 {
+ licenceText, err := DecryptLicence(licenceCode, publicKeyPath)
if err != nil {
return ValidationErrorDecrypt
}
+ return _verifyLicence(licenceText)
+}
+
+func ReadGrantChannels(licencePath, publicKeyPath string) (int64, error) {
+ licenceText, err := DecryptLicenceFile(licencePath, publicKeyPath)
+ if err != nil {
+ return 0 ,err
+ }
+
var licence Licence
+ if err := json.Unmarshal(licenceText, &licence); err != nil {
+ return 0, err
+ }
+
+ if _verifyLicence(licenceText) != 0 {
+ return 0, errors.New("Invalid licence.")
+ }
+
+ return licence.Channel, nil
+}
+
+func _verifyLicence(licenceText []byte) uint32 {
+ var licence Licence
+ var now = time.Now().Unix()
if err := json.Unmarshal(licenceText, &licence); err != nil {
return ValidationErrorMalformed
}
+ // 鍒ゆ柇鏄惁鍙互璇曠敤
+ if licence.RegCode.MachineCode == "FFFFFFFF" {
+ osInstallTime := GetOSInstallationDate()
+ if now - osInstallTime > 60 * 60 * 24 * 30 {
+ return ValidationErrorExpired
+ }
+
+ return 0
+ }
+
// 鍒ゆ柇杩囨湡
- now := time.Now().Unix()
if now > licence.Expires {
return ValidationErrorExpired
}
--
Gitblit v1.8.0