-
-
Notifications
You must be signed in to change notification settings - Fork 204
/
official.go
180 lines (163 loc) · 4.33 KB
/
official.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
/*
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 (
"net/http"
"os"
"path"
"path/filepath"
"strings"
"github.com/admpub/errors"
"github.com/admpub/license_gen/lib"
"github.com/admpub/nging/application/library/config"
"github.com/admpub/restful/rest"
"github.com/webx-top/com"
"github.com/webx-top/echo"
)
var (
ErrConnectionNotFound = errors.New(`连接授权服务器失败`)
ErrOfficialDataUnexcepted = errors.New(`官方数据返回异常`)
)
type OfficialData struct {
lib.LicenseData
Timestamp int64
}
type OfficialResp struct {
Code int
Info string
Zone string `json:",omitempty" xml:",omitempty"`
Data *OfficialData `json:",omitempty" xml:",omitempty"`
}
type ValidResp struct {
Code int
Info string
Zone string `json:",omitempty" xml:",omitempty"`
Data Validator `json:",omitempty" xml:",omitempty"`
}
type Validator interface {
Validate() error
}
var NewValidResp = func() *ValidResp {
return &ValidResp{Data: &ValidResult{}}
}
type ValidResult struct {
}
func (v *ValidResult) Validate() error {
return nil
}
func validateFromOfficial(machineID, domain string) error {
response := rest.Get(FullLicenseURL(machineID, domain))
if response == nil {
return ErrConnectionNotFound
}
if response.Err != nil {
return errors.Wrap(response.Err, `Connection to the license server failed`)
}
switch response.StatusCode {
case http.StatusOK:
result := NewValidResp()
err := response.FillUp(result)
if err != nil {
return err
}
if result.Code != 1 {
return errors.New(result.Info)
}
if result.Data == nil {
return ErrOfficialDataUnexcepted
}
return result.Data.Validate()
case http.StatusNotFound:
return ErrConnectionNotFound
default:
return errors.New(response.Status)
}
}
type VersionResp struct {
Code int
Info string
Zone string `json:",omitempty" xml:",omitempty"`
Data *ProductVersion `json:",omitempty" xml:",omitempty"`
}
func latestVersion() error {
response := rest.Get(versionURL)
if response == nil {
return ErrConnectionNotFound
}
if response.Err != nil {
return errors.Wrap(response.Err, `Check for the latest version failed`)
}
switch response.StatusCode {
case http.StatusOK:
result := &VersionResp{}
err := response.FillUp(result)
if err != nil {
return err
}
if result.Code != 1 {
return errors.New(result.Info)
}
if result.Data == nil {
return ErrOfficialDataUnexcepted
}
hasNew := config.Version.IsNew(result.Data.Version, result.Data.Type)
if hasNew {
if result.Data.ForceUpgrade == `Y` {
if len(result.Data.DownloadUrl) > 0 {
//TODO: download
saveTo := filepath.Join(echo.Wd(), `data/cache/nging-new-version`)
err = os.MkdirAll(saveTo, 0777)
if err != nil {
return err
}
saveTo += echo.FilePathSeparator + path.Base(result.Data.DownloadUrl)
err = com.RangeDownload(result.Data.DownloadUrl, saveTo)
if err != nil {
if len(result.Data.DownloadUrlOther) > 0 {
err = com.RangeDownload(result.Data.DownloadUrlOther, saveTo)
}
}
if err != nil {
return err
}
//TODO: verify sign
var signList []string
if len(result.Data.Sign) > 0 {
signList = strings.Split(result.Data.Sign, `,`)
}
if len(signList) > 0 {
fileMd5 := com.Md5file(saveTo)
var matched bool
for _, sign := range signList {
if sign == fileMd5 {
matched = true
break
}
}
if !matched {
return com.ErrMd5Unmatched
}
}
//OK
}
}
}
return nil
case http.StatusNotFound:
return ErrConnectionNotFound
default:
return errors.New(response.Status)
}
}