forked from goodrain/rainbond
-
Notifications
You must be signed in to change notification settings - Fork 0
/
license_pack.go
146 lines (130 loc) · 3.88 KB
/
license_pack.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
// Copyright (C) 2014-2018 Goodrain Co., Ltd.
// RAINBOND, Application Management Platform
// 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 handler
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"os"
"time"
"github.com/golang/glog"
)
//Info license 信息
type Info struct {
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"`
}
var key = []byte("qa123zxswe3532crfvtg123bnhymjuki")
//decrypt 解密算法
func decrypt(key []byte, encrypted string) ([]byte, error) {
return []byte{}, nil
}
//ReadLicenseFromFile 从文件获取license
func ReadLicenseFromFile(licenseFile string) (Info, error) {
info := Info{}
//step1 read license file
_, err := os.Stat(licenseFile)
if err != nil {
return info, err
}
infoBody, err := ioutil.ReadFile(licenseFile)
if err != nil {
return info, errors.New("LICENSE文件不可读")
}
//step2 decryption info
infoData, err := decrypt(key, string(infoBody))
if err != nil {
return info, errors.New("LICENSE解密发生错误。")
}
err = json.Unmarshal(infoData, &info)
if err != nil {
return info, errors.New("解码LICENSE文件发生错误")
}
return info, nil
}
//ReadLicenseFromConsole 从控制台api获取license
func ReadLicenseFromConsole(token string, defaultLicense string) (Info, error) {
var info Info
req, err := http.NewRequest("GET", "http://console.goodrain.me/api/license", nil)
if err != nil {
return info, err
}
req.Header.Add("Content-Type", "application/json")
if token != "" {
req.Header.Add("Authorization", "Token "+token)
}
http.DefaultClient.Timeout = time.Second * 5
res, err := http.DefaultClient.Do(req)
if err != nil {
glog.Info("控制台读取授权失败,使用默认授权。")
return ReadLicenseFromFile(defaultLicense)
}
if res != nil {
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return info, err
}
apiInfo := struct {
Body struct {
Bean string `json:"bean"`
}
}{}
err = json.Unmarshal(body, &apiInfo)
if err != nil {
return info, err
}
//step2 decryption info
infoData, err := decrypt(key, apiInfo.Body.Bean)
if err != nil {
return info, errors.New("LICENSE解密发生错误。")
}
err = json.Unmarshal(infoData, &info)
if err != nil {
return info, errors.New("解码LICENSE文件发生错误")
}
return info, nil
}
return info, errors.New("res body is nil")
}
//BasePack base pack
func BasePack(text []byte) (string, error) {
token := ""
encodeStr := base64.StdEncoding.EncodeToString(text)
begin := 0
if len([]byte(encodeStr)) > 40 {
begin = randInt(0, (len([]byte(encodeStr)) - 40))
} else {
return token, fmt.Errorf("error license")
}
token = string([]byte(encodeStr)[begin:(begin + 40)])
return token, nil
}
func randInt(min int, max int) int {
rand.Seed(time.Now().UTC().UnixNano())
return min + rand.Intn(max-min)
}