-
-
Notifications
You must be signed in to change notification settings - Fork 201
/
license.go
348 lines (305 loc) · 7.89 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
Nging is a toolbox for webmasters
Copyright (C) 2018-present Wenhui Shen <swh@admpub.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package license
import (
"errors"
"fmt"
"html/template"
"net/url"
"path/filepath"
"strings"
"sync"
"time"
"github.com/admpub/license_gen/lib"
"github.com/admpub/once"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/webx-top/com"
"github.com/webx-top/echo"
"golang.org/x/net/publicsuffix"
"github.com/admpub/nging/v4/application/library/config"
)
type Mode int
const (
ModeMachineID Mode = iota
ModeDomain
)
var (
trackerURL = `https://www.webx.top/product/script/nging/tracker.js`
productURL = `https://www.webx.top/product/detail/nging`
licenseURL = `https://www.webx.top/product/license/nging`
versionURL = `https://www.webx.top/product/version/nging`
licenseMode = ModeMachineID
licenseData *lib.LicenseData // 拥有的授权数据
licenseFileName = `license.key`
licenseFile = filepath.Join(echo.Wd(), licenseFileName)
licenseError = lib.UnlicensedVersion
licenseModTime time.Time
emptyLicense = lib.LicenseData{}
downloadOnce once.Once
downloadError error
downloadTime time.Time
lock4err sync.RWMutex
lock4data sync.RWMutex
onSetLicenseHooks []func(*lib.LicenseData)
// ErrLicenseNotFound 授权证书不存在
ErrLicenseNotFound = errors.New(`License does not exist`)
// SkipLicenseCheck 跳过授权检测
SkipLicenseCheck = true
// - 需要验证的数据
version string //1.2.3-beta
packageName string //free
machineID string
domain string
)
func OnSetLicense(fn func(*lib.LicenseData)) {
if fn == nil {
return
}
onSetLicenseHooks = append(onSetLicenseHooks, fn)
}
func FireSetLicense(data *lib.LicenseData) {
for _, fn := range onSetLicenseHooks {
fn(data)
}
}
type ServerURL struct {
Tracker string //用于统计分析的js地址
Product string //该产品的详情介绍页面网址
License string //许可证验证和许可证下载API网址
Version string //该产品最新版本信息API网址
LicenseFileName string //许可证文件名称
}
func (s *ServerURL) Apply() {
if len(s.Tracker) > 0 {
trackerURL = s.Tracker
}
if len(s.Product) > 0 {
productURL = s.Product
}
if len(s.License) > 0 {
licenseURL = s.License
}
if len(s.Version) > 0 {
versionURL = s.Version
}
if len(s.LicenseFileName) > 0 {
licenseFileName = s.LicenseFileName
licenseFile = filepath.Join(echo.Wd(), licenseFileName)
}
}
func SetServerURL(s *ServerURL) {
if s != nil {
s.Apply()
}
}
func SetProductName(name string, domains ...string) {
domain := `www.webx.top`
if len(domains) > 0 && len(domains[0]) > 0 {
domain = domains[0]
}
trackerURL = `https://` + domain + `/product/script/` + name + `/tracker.js`
productURL = `https://` + domain + `/product/detail/` + name
licenseURL = `https://` + domain + `/product/license/` + name
versionURL = `https://` + domain + `/product/version/` + name
}
func SetProductDomain(domain string) {
trackerURL = `https://` + domain + `/script/tracker.js`
productURL = `https://` + domain + `/`
licenseURL = `https://` + domain + `/license`
versionURL = `https://` + domain + `/version`
}
func SetVersion(ver string) {
version = ver
}
func SetPackage(pkg string) {
packageName = pkg
}
func Version() string {
return version
}
func Package() string {
return packageName
}
func ProductURL() string {
return productURL
}
func Domain() string {
return domain
}
func SetDomain(_domain string) {
if licenseMode != ModeDomain {
licenseMode = ModeDomain
}
domain = _domain
}
func FullDomain() string {
rootDomain := License().Info.Domain
if len(rootDomain) == 0 {
return rootDomain
}
rootDomain = strings.Trim(rootDomain, `.`)
realDomain, _ := publicsuffix.EffectiveTLDPlusOne(rootDomain)
if rootDomain == realDomain {
return `www.` + realDomain
}
return rootDomain
}
func EqDomain(fullDomain string, rootDomain string) bool {
return lib.CheckDomain(fullDomain, rootDomain)
}
func LicenseMode() Mode {
return licenseMode
}
func DownloadTime() time.Time {
return downloadTime
}
func ProductDetailURL() (url string) {
url = ProductURL() + `?version=` + Version()
switch licenseMode {
case ModeMachineID:
mid, err := MachineID()
if err != nil {
panic(err)
}
url += `&machineID=` + mid
case ModeDomain:
if len(Domain()) > 0 {
url += `&domain=` + Domain()
}
default:
panic(fmt.Sprintf(`unsupported license mode: %d`, licenseMode))
}
return
}
func TrackerURL() string {
if trackerURL == `#` {
return ``
}
return trackerURL + `?version=` + Version() + `&package=` + Package() + `&os=` + config.Version.BuildOS + `&arch=` + config.Version.BuildArch
}
func TrackerHTML() template.HTML {
_trackerURL := TrackerURL()
if len(_trackerURL) == 0 {
return template.HTML(``)
}
return template.HTML(`<script type="text/javascript" async src="` + _trackerURL + `"></script>`)
}
func FilePath() string {
return licenseFile
}
func FileName() string {
return licenseFileName
}
func Error() error {
lock4err.RLock()
defer lock4err.RUnlock()
return licenseError
}
func SetError(err error) {
lock4err.Lock()
licenseError = err
lock4err.Unlock()
}
func License() lib.LicenseData {
lock4data.RLock()
defer lock4data.RUnlock()
if licenseData == nil {
return emptyLicense
}
return *licenseData
}
func SetLicense(data *lib.LicenseData) {
FireSetLicense(data)
lock4data.Lock()
licenseData = data
lock4data.Unlock()
switch licenseMode {
case ModeDomain:
if len(Domain()) == 0 {
SetDomain(data.Info.Domain)
}
case ModeMachineID:
}
}
var (
MachineIDEncode = func(v string) string {
return com.MakePassword(v, `coscms`, 3, 8, 19)
}
LicenseDecode = func(b []byte) ([]byte, string) {
return b, GetOrLoadPublicKey()
}
)
// MachineID 生成当前机器的机器码
func MachineID() (string, error) {
if len(machineID) > 0 {
return machineID, nil
}
addrs, err := lib.MACAddresses(false)
if err != nil {
return ``, err
}
if len(addrs) < 1 {
return ``, lib.ErrorMachineID
}
cpuInfo, err := cpu.Info()
if err != nil {
return ``, err
}
var cpuID string
if len(cpuInfo) > 0 {
cpuID = cpuInfo[0].PhysicalID
if len(cpuID) == 0 {
cpuID = com.Md5(com.Dump(cpuInfo, false))
}
}
machineID = MachineIDEncode(lib.Hash(addrs[0]) + `#` + cpuID)
return machineID, err
}
// FullLicenseURL 包含完整参数的授权网址
func FullLicenseURL(ctx echo.Context) string {
return licenseURL + `?` + URLValues(ctx).Encode()
}
// URLValues 组装网址参数
func URLValues(ctx echo.Context) url.Values {
v := url.Values{}
v.Set(`os`, config.Version.BuildOS)
v.Set(`arch`, config.Version.BuildArch)
v.Set(`sn`, License().Info.LicenseID)
v.Set(`version`, Version())
v.Set(`package`, Package())
if ctx != nil {
v.Set(`source`, ctx.RequestURI())
}
switch licenseMode {
case ModeMachineID:
if len(machineID) == 0 {
var err error
machineID, err = MachineID()
if err != nil {
panic(fmt.Errorf(`failed to get machineID: %v`, err))
}
}
v.Set(`machineID`, machineID)
case ModeDomain:
if len(Domain()) == 0 {
panic(`license domain is required`)
}
v.Set(`domain`, Domain())
default:
panic(fmt.Sprintf(`unsupported license mode: %d`, licenseMode))
}
v.Set(`time`, time.Now().Format(`20060102-150405`))
return v
}