65 Star 395 Fork 128

admpub/nging

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
official.go 4.33 KB
一键复制 编辑 原始数据 按行查看 历史
admpub 提交于 2019-05-10 23:35 . v2.0.0
/*
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)
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/admpub/nging.git
git@gitee.com:admpub/nging.git
admpub
nging
nging
v2.1.0

搜索帮助