-
-
Notifications
You must be signed in to change notification settings - Fork 201
/
license.go
90 lines (80 loc) · 2.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
/*
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 setup
import (
"errors"
"github.com/admpub/nging/v5/application/handler"
"github.com/admpub/nging/v5/application/library/config"
"github.com/admpub/nging/v5/application/library/license"
"github.com/webx-top/com"
"github.com/webx-top/echo"
"github.com/webx-top/echo/code"
)
func postLicense(c echo.Context) error {
content := c.Formx(`license`).String()
if len(content) == 0 {
return c.NewError(code.DataUnavailable, `授权文件内容不能为空`).SetZone(`license`)
}
b := com.Str2bytes(content)
err := license.VerifyPostLicenseContent(c, b)
if err != nil {
return err
}
err = license.Save(b)
return err
}
// License 获取商业授权
func License(c echo.Context) error {
err := license.Check(c)
if err != nil && c.IsPost() {
err = postLicense(c)
}
if err == nil {
nextURL := c.Form(`next`)
if len(nextURL) == 0 {
nextURL = handler.URLFor(`/`)
}
config.Version.Licensed = true
return c.Redirect(nextURL)
}
errMap := map[string]string{
`Could not read license`: c.T(`读取授权文件失败`),
`Could not read private key`: c.T(`读取私钥失败`),
`Could not read public key`: c.T(`读取公钥失败`),
`Could not read machine number`: c.T(`获取机器码失败`),
`Invalid private key`: c.T(`私钥无效`),
`Invalid public key`: c.T(`公钥无效`),
`Invalid License file`: c.T(`授权文件无效`),
`Unlicensed Version`: c.T(`未授权版本`),
`Invalid MachineID`: c.T(`机器码无效`),
`Invalid LicenseID`: c.T(`授权ID无效`),
`License expired`: c.T(`授权已过期`),
`License does not exist`: c.T(`授权文件不存在`),
}
if errStr, ok := errMap[err.Error()]; ok {
err = errors.New(errStr)
}
//需要重新获取授权文件
if err == license.ErrLicenseNotFound && license.LicenseMode() != license.ModeDomain {
err = license.DownloadOnce(c)
c.Set(`downloaded`, err == nil)
} else {
c.Set(`downloaded`, false)
}
c.Set(`licenseFile`, license.FilePath())
c.Set(`productURL`, license.ProductDetailURL())
c.Set(`fileName`, license.FileName())
return c.Render(`license`, err)
}