zhangzengfei
2023-09-04 e8e536d1cb52d2126c8c7ce2ba1c7a76f7208678
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
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"`         //摄像机数量
}