/
license.go
164 lines (148 loc) · 4.78 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
// RAINBOND, Application Management Platform
// Copyright (C) 2014-2017 Goodrain Co., Ltd.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. For any non-GPL usage of Rainbond,
// one or multiple Commercial Licenses authorized by Goodrain Co., Ltd.
// must be obtained first.
// 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 General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package license
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"plugin"
"strconv"
"github.com/Sirupsen/logrus"
)
var enterprise = "false"
// LicInfo license information
type LicInfo struct {
LicKey string `json:"license_key"`
Code string `json:"code"`
Company string `json:"company"`
Node int64 `json:"node"`
CPU int64 `json:"cpu"`
Memory int64 `json:"memory"`
Tenant int64 `json:"tenant"`
EndTime string `json:"end_time"`
StartTime string `json:"start_time"`
DataCenter int64 `json:"data_center"`
ModuleList []string `json:"module_list"`
}
func isEnterprise() bool {
res, err := strconv.ParseBool(enterprise)
if err != nil {
logrus.Warningf("enterprise: %s; error parsing 'string' to 'bool': %v", enterprise, err)
}
return res
}
func readFromFile(lfile string) (string, error) {
_, err := os.Stat(lfile)
if err != nil {
logrus.Errorf("license file is incorrect: %v", err)
return "", err
}
bytes, err := ioutil.ReadFile(lfile)
if err != nil {
logrus.Errorf("license file: %s; error reading license file: %v", lfile, err)
return "", err
}
return string(bytes), nil
}
// VerifyTime verifies the time in the license.
func VerifyTime(licPath, licSoPath string) bool {
if !isEnterprise() {
return true
}
lic, err := readFromFile(licPath)
if err != nil {
logrus.Errorf("failed to read license from file: %v", err)
return false
}
p, err := plugin.Open(licSoPath)
if err != nil {
logrus.Errorf("license.so path: %s; error opening license.so: %v", licSoPath, err)
return false
}
f, err := p.Lookup("VerifyTime")
if err != nil {
logrus.Errorf("method 'VerifyTime'; error looking up func: %v", err)
return false
}
return f.(func(string) bool)(lic)
}
// VerifyNodes verifies the number of the nodes in the license.
func VerifyNodes(licPath, licSoPath string, nodeNums int) bool {
if !isEnterprise() {
return true
}
lic, err := readFromFile(licPath)
if err != nil {
logrus.Errorf("failed to read license from file: %v", err)
return false
}
p, err := plugin.Open(licSoPath)
if err != nil {
logrus.Errorf("license.so path: %s; error opening license.so: %v", licSoPath, err)
return false
}
f, err := p.Lookup("VerifyNodes")
if err != nil {
logrus.Errorf("method 'VerifyNodes'; error looking up func: %v", err)
return false
}
return f.(func(string, int) bool)(lic, nodeNums)
}
// GetLicInfo -
func GetLicInfo(licPath, licSoPath string) (*LicInfo, error) {
if !isEnterprise() {
return nil, nil
}
lic, err := readFromFile(licPath)
if err != nil {
logrus.Errorf("failed to read license from file: %v", err)
return nil, fmt.Errorf("failed to read license from file: %v", err)
}
p, err := plugin.Open(licSoPath)
if err != nil {
logrus.Errorf("license.so path: %s; error opening license.so: %v", licSoPath, err)
return nil, fmt.Errorf("license.so path: %s; error opening license.so: %v", licSoPath, err)
}
f, err := p.Lookup("Decrypt")
if err != nil {
logrus.Errorf("method 'Decrypt'; error looking up func: %v", err)
return nil, fmt.Errorf("method 'Decrypt'; error looking up func: %v", err)
}
bytes, err := f.(func(string) ([]byte, error))(lic)
var licInfo LicInfo
if err := json.Unmarshal(bytes, &licInfo); err != nil {
logrus.Errorf("error unmarshalling license: %v", err)
return nil, fmt.Errorf("error unmarshalling license: %v", err)
}
return &licInfo, nil
}
// GenLicKey -
func GenLicKey(licSoPath string) (string, error) {
if !isEnterprise() {
return "", nil
}
p, err := plugin.Open(licSoPath)
if err != nil {
logrus.Errorf("license.so path: %s; error opening license.so: %v", licSoPath, err)
return "", fmt.Errorf("license.so path: %s; error opening license.so: %v", licSoPath, err)
}
f, err := p.Lookup("GenLicKey")
if err != nil {
logrus.Errorf("method 'GenLicKey'; error looking up func: %v", err)
return "", fmt.Errorf("method 'GenLicKey'; error looking up func: %v", err)
}
return f.(func() (string, error))()
}