package util
|
|
import (
|
"basic.com/valib/licence.git"
|
"basic.com/valib/logger.git"
|
"context"
|
"encoding/json"
|
"errors"
|
"time"
|
)
|
|
//控制授权逻辑
|
//授权文件包含有edition_enc和auth_enc
|
var (
|
SysExpired = false //产品是否过期
|
SysExpireTime = "" //系统过期时间
|
SysEdition = "" //系统版本
|
SysChCount = 0 //通道数量
|
SysCameraCount = 0 //授权摄像机数量
|
Edition_Alpha = "alpha" //内测
|
Edition_Beta = "beta" //公测
|
Edition_Try = "trial" //试用
|
Edition_Test = "test" //测试
|
Edition_Official = "official" //正式版
|
Edition_Custom = "custom" //定制版
|
)
|
|
//判断试用是否已过期
|
func AuthCheck(ctx context.Context) {
|
go func(be *bool) {
|
ticker := time.NewTicker(time.Second * 20)
|
for {
|
select {
|
case <-ctx.Done():
|
return
|
case <-ticker.C:
|
if !isOfficialAuthed() {
|
isoInstallExpired(be)
|
} else {
|
*be = false
|
}
|
default:
|
time.Sleep(10 * time.Second)
|
}
|
}
|
}(&SysExpired)
|
}
|
|
//判断初次安装时使用的授权是否过期
|
func isoInstallExpired(be *bool) (bool, error) {
|
pe, err := GetEdtionSetFromIns()
|
if err != nil {
|
*be = true
|
return false, err
|
}
|
insTime, _ := time.Parse("2006-01-02 15:04:05", pe.InstallTime)
|
if pe.Edition != Edition_Custom { //测试版本,判断授权是否过期
|
if int(time.Now().Sub(insTime).Hours()) > pe.Setting.TrialDays * 24 {
|
*be = true
|
//试用过期,页面登录或者系统首页显示试用已过期
|
logger.Error("SysExpired!!!!")
|
return false, errors.New("产品已过期,请购买授权")
|
}
|
} else {
|
//如果是正式版或者定制版,则没有试用时长的限制
|
}
|
trialDays := 30
|
if pe.OrderAuth !=nil && pe.OrderAuth.OrderId != "" && pe.OrderAuth.ProductId != "" {
|
SysChCount = pe.OrderAuth.ChCount
|
SysCameraCount = pe.OrderAuth.CameraCount
|
trialDays = pe.Setting.TrialDays
|
SysEdition = pe.Edition
|
} else {
|
SysChCount = pe.Setting.ChanCount
|
SysCameraCount = 500
|
trialDays = pe.Setting.TrialDays
|
SysEdition = Edition_Try
|
}
|
*be = false
|
SysExpireTime = insTime.AddDate(0, 0, trialDays).Format("2006-01-02 15:04:05")
|
logger.Info("edition:", SysEdition, " expired:", *be," expireTime:", SysExpireTime, " chCount:", SysChCount)
|
return true, nil
|
}
|
|
type EncryptInstallInfo struct {
|
Edition string
|
Setting *PdtSetting
|
//ChCount int
|
//AuthCount int
|
InstallTime string
|
|
OrderAuth *PackageAuth
|
}
|
func GetEdtionSetFromIns() (*EncryptInstallInfo, error) {
|
insTimeF := "/opt/vasystem/inst_enc"
|
editionFile := "/opt/vasystem/edition_enc"
|
authFile := "/opt/vasystem/auth_enc"
|
if !FileExists(insTimeF) || !FileExists(editionFile) || !FileExists(authFile){
|
//没有授权文件,则表示为试用版,判断试用时间是否结束
|
err:= errors.New("授权文件已丢失,请重新授权")
|
logger.Error(err)
|
return nil, err
|
}
|
ei := &EncryptInstallInfo{}
|
//1.获取安装时间
|
td, err := CallDecFileContent(insTimeF)
|
if err != nil {
|
logger.Error("Read installTime file err:", err)
|
return nil, errors.New("授权文件已丢失,请重新授权")
|
}
|
ei.InstallTime = string(td)
|
|
//2.解析edition_enc(内含版本信息)
|
bEdition, err := CallDecFileContent(editionFile)
|
if err != nil {
|
logger.Error("Read edition file err:", err)
|
return nil, errors.New("授权文件已丢失,请重新授权")
|
}
|
var pe ProductEdition
|
err = json.Unmarshal(bEdition, &pe)
|
if err != nil {
|
logger.Error("Unmarshal productEdition err:", err)
|
return nil, errors.New("解析授权文件失败")
|
}
|
ei.Edition = pe.Edition
|
ei.Setting = &PdtSetting{
|
BasePackId: pe.BasePackId,
|
TrialDays: pe.TrialDays,
|
ChanCount: pe.ChanCount,
|
WorkHours: pe.WorkHours,
|
}
|
|
//3.解析auth_enc(内含授权信息)
|
bAuth, err := CallDecFileContent(authFile)
|
if err != nil {
|
logger.Error("Read auth_enc err:", err)
|
return ei, nil
|
}
|
var auth PackageAuth
|
err = json.Unmarshal(bAuth, &auth)
|
if err != nil {
|
logger.Error("Unmarshal PackageAuth err:", err)
|
return ei, nil
|
}
|
ei.OrderAuth = &auth
|
|
return ei, nil
|
}
|
|
//判断是否已授权
|
func isOfficialAuthed() bool {
|
authStr := GetAuthorization()
|
logger.Info("isOfficialAuthed auth:", authStr)
|
authinfo, err := GetAuthorizationInfo(authStr)
|
logger.Info("authinfo:", authinfo, " err:", err)
|
if err != nil {
|
return false
|
}
|
insTime := authinfo.InstallTime
|
chCount := authinfo.ChCount
|
if authinfo.MachineCode != licence.GetMachineCode() { //
|
logger.Info("current smart-ai bus system was copied")
|
return false
|
}
|
|
expireT := time.Unix(authinfo.ExpirationTime, 0)
|
if time.Now().After(expireT) {
|
logger.Info("sys smart-ai expired!!!")
|
return false
|
}
|
|
SysChCount = chCount
|
SysCameraCount = authinfo.CameraCount //授权的摄像机数量
|
SysEdition = Edition_Official
|
SysExpireTime = expireT.Format("2006-01-02 15:04:05")
|
logger.Info("current sys is official, edition:", SysEdition, " installTime:", insTime," expireTime:", SysExpireTime, " chCount:", SysChCount)
|
|
return true
|
}
|
|
|
type ProductEdition struct {
|
Edition string `json:"edition"`
|
PdtSetting
|
}
|
|
type PdtSetting struct {
|
BasePackId string `json:"basePackId,omitempty"` //基础安装包id
|
TrialDays int `json:"trialDays,omitempty"` //试用天数
|
ChanCount int `json:"chanCount,omitempty"` //通道数量
|
WorkHours int `json:"workHours,omitempty"` //配置设备每天的工作时长,超过此时长视频分析不再生效
|
}
|
|
type PackageAuth struct {
|
OrderId string `json:"orderId"`
|
ProductId string `json:"productId"`
|
DevCount int `json:"devCount"`
|
ProductPrice float32 `json:"productPrice"` //购买商品单价
|
ActivateCode string `json:"activateCode"`
|
ChCount int `json:"chCount"`
|
AuthCount int `json:"authCount"`
|
ServeYear int `json:"serveYear"`
|
Quantity int `json:"quantity"`
|
TargetPlatform string `json:"targetPlatform"` //目标平台
|
CameraCount int `json:"cameraCount"` //摄像机数量
|
}
|