-
Notifications
You must be signed in to change notification settings - Fork 0
/
license.go
216 lines (193 loc) · 6.12 KB
/
license.go
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
207
208
209
210
211
212
213
214
215
216
package licenseSDK
import (
"fmt"
"github.com/ensaas/license-sdk/checker"
"github.com/ensaas/license-sdk/encryptor"
"github.com/ensaas/license-sdk/liv"
"github.com/ensaas/license-sdk/models"
"github.com/ensaas/license-sdk/retrieve"
"github.com/ensaas/license-sdk/store"
"github.com/ensaas/license-sdk/store/postgre"
"github.com/robfig/cron"
"github.com/sirupsen/logrus"
"log"
)
const (
version = "1.0.1"
checkLicenseCron = "0 0 0,8,16 * * ?"
)
var appParam *appParams
// License is app license used be validate
type License struct {
Pn string
Authcode string
ExpireTimestamp int64
Number int
}
// AppParams is parameter of app and which are fixed
type appParams struct {
ServiceName string
LicenseId string
ActiveInfo string
}
// LicenseManager is for manager app license lifecycle
type LicenseManager interface {
// IsLegalLicense check license is legal
IsLegalLicense(pn string) (bool, error)
// GetStatus get current license check status by pn
GetAvailableDays(pn string) (int, error)
//InitAppParams initialize app parameters which are fixed value
InitAppParams(serviceName, licenseId, activeInfo string)
// StartValidate is validate license status
StartValidate(licenses []*License) error
// Version print sdk version
Version() string
// LivVersion
LivVersion() (string, error)
}
type licenseManager struct {
checker checker.Checker
retriever retrieve.Retriever
licenses []*License
}
func (lic *licenseManager) InitAppParams(serviceName, licenseId, activeInfo string) {
appParam = &appParams{
ServiceName: serviceName,
LicenseId: licenseId,
ActiveInfo: activeInfo,
}
}
// IsLegalLicense
func (lic *licenseManager) IsLegalLicense(pn string) (bool, error) {
availableDays, err := lic.checker.GetAvailableDays(pn)
if err != nil {
log.Printf("get available days failed:%s", err.Error())
return false, err
}
return availableDays > 0, nil
}
// GetStatus is get check license status,data in db format is serviceName:failedTimes
func (lic *licenseManager) GetAvailableDays(pn string) (int, error) {
return lic.checker.GetAvailableDays(pn)
}
// StartValidate start validate license jobs
func (lic *licenseManager) StartValidate(licenses []*License) (err error) {
lic.licenses = licenses
// when app start check license
if err = lic.licenseCheck(licenses, true); err != nil {
return
}
// check license in fixed time
cronObj := cron.New()
if err := cronObj.AddJob(checkLicenseCron, lic); err != nil {
log.Printf("license start check license job failed:%s", err.Error())
return err
}
cronObj.Start()
return
}
// CheckLicenseJob
func (lic *licenseManager) Run() {
if err := lic.licenseCheck(lic.licenses, false); err != nil {
logrus.Errorf("[license check]- check license failed:[%v]", err)
}
}
// licenseCheck is check license
func (lic *licenseManager) licenseCheck(licenses []*License, isAppBootCheck bool) error {
// if all license from app check failed
isAllFailed := true
for _, license := range licenses {
commonLic := &models.License{
LicenseID: appParam.LicenseId,
Pn: license.Pn,
ActiveInfo: appParam.ActiveInfo,
Authcode: license.Authcode,
Number: license.Number,
ExpireTimestamp: license.ExpireTimestamp,
}
isValid, err := lic.checker.ValidateLicense(commonLic)
if err != nil {
return err
}
// record license status
if err := lic.checker.RecordLicenseStatus(license.Pn, isValid, isAppBootCheck); err != nil {
logrus.Warnf("record license status failed:[%v]", err)
}
if isValid {
isAllFailed = false
}
}
// if not all license check failed
if !isAllFailed {
return nil
}
// get license from api by service name
apiLicenses, err := lic.retriever.LicenseWithActiveInfoBy(appParam.LicenseId, appParam.ServiceName)
if err != nil {
logrus.Warningf("get license from license API failed:[%v]", err)
}
for _, license := range apiLicenses {
commonLic := &models.License{
LicenseID: appParam.LicenseId,
Pn: license.Pn,
ActiveInfo: appParam.ActiveInfo,
Authcode: license.AuthCode,
Number: license.Number,
ExpireTimestamp: license.ExpireTimestamp,
}
isValid, err := lic.checker.ValidateLicense(commonLic)
if err != nil {
return err
}
// record license status
if err := lic.checker.RecordLicenseStatus(license.Pn, isValid, isAppBootCheck); err != nil {
logrus.Warnf("record license status failed:[%v]", err)
}
}
return nil
}
func (lic *licenseManager) Version() string {
return version
}
func (lic *licenseManager) LivVersion() (string, error) {
version, err := liv.New().GetVersion()
if err != nil {
return "", fmt.Errorf("get liv version failed:[%v]", err)
}
return version, nil
}
// NewLicenseManager create a license manager with store,encryptor,licenseUrl
func New(store store.Store, encryptor encryptor.Encryptor, licenseUrl string) LicenseManager {
return &licenseManager{
checker: checker.New(store, encryptor),
retriever: retrieve.NewRetriever(licenseUrl),
}
}
// NewWithDefaultEncryptor create a license manager with default encryptor
func NewWithDefaultEncryptor(store store.Store, salt, licenseUrl string) (LicenseManager, error) {
etor, err := encryptor.New(salt)
if err != nil {
return nil, fmt.Errorf("init encryptor failed:%v", err)
}
return New(store, etor, licenseUrl), nil
}
// NewWithDefaultStore create a license manager with default store
func NewWithDefaultStore(encryptor encryptor.Encryptor, storeParams map[string]interface{}, licenseUrl string) (LicenseManager, error) {
pg, err := postgre.New(storeParams)
if err != nil {
return nil, fmt.Errorf("create postgre store failed:[%v]", err)
}
return New(pg, encryptor, licenseUrl), nil
}
// NewLicenseManagerWithDefault create license manager with default component
func NewWithDefault(storeParams map[string]interface{}, aesSalt, licenseUrl string) (LicenseManager, error) {
pg, err := postgre.New(storeParams)
if err != nil {
return nil, fmt.Errorf("create postgre store failed:[%v]", err)
}
etor, err := encryptor.New(aesSalt)
if err != nil {
return nil, fmt.Errorf("init encryptor failed:%v", err)
}
return New(pg, etor, licenseUrl), nil
}