package license
|
|
import (
|
"basic.com/valib/licence.git"
|
"basic.com/valib/logger.git"
|
"encoding/json"
|
"github.com/gin-gonic/gin"
|
"github.com/robfig/cron"
|
"io/ioutil"
|
"os"
|
"strings"
|
"sync"
|
"time"
|
"vamicro/config"
|
"vamicro/extend/code"
|
"vamicro/extend/util"
|
)
|
|
var isExpired = true
|
|
var lock sync.RWMutex
|
|
func setExpire(b bool) {
|
lock.Lock()
|
defer lock.Unlock()
|
isExpired = b
|
}
|
|
func getExipre() bool {
|
lock.Lock()
|
defer lock.Unlock()
|
return isExpired
|
}
|
|
func InitExpire() {
|
flag, machineCode := VerifyLicence()
|
logger.Debug("InitExpire flag:",flag, "machineCode:",machineCode)
|
if flag {
|
setExpire(false)
|
} else {
|
setExpire(true)
|
}
|
}
|
|
func Schedule() {
|
c := cron.New()
|
//每小时监测一次是否过期
|
c.AddFunc("0 0 * * * ?", func() {
|
logger.Debug("license Schedule:",time.Now().Format("2006-01-02 15:04:05"))
|
InitExpire()
|
})
|
c.Start()
|
}
|
|
func FilterLicense() gin.HandlerFunc {
|
return func(c *gin.Context) {
|
urlPath := c.Request.URL.Path
|
if strings.Contains(urlPath,"/data/api") && !strings.Contains(urlPath, "/data/api-v/license"){
|
//验证是否已授权
|
if getExipre() {
|
util.ResponseFormat(c,code.LicenseExpired, "未授权或授权已过期")
|
c.Abort()
|
return
|
}
|
c.Next()
|
} else {
|
c.Next()
|
}
|
}
|
}
|
|
const (
|
verify_success = 0
|
verify_fail = 1
|
verify_noauth = 2
|
verify_expire = 3
|
)
|
|
//验证授权
|
func VerifyLicence() (flag bool,code string) {
|
machineCode := licence.GetMachineCode()
|
|
licencePath := config.Server.LicensePath
|
publicKeyPath := config.Server.LPublicKeyPath
|
result := licence.VerifyLicenceFile(licencePath,publicKeyPath)
|
logger.Debug("verify licence result:",result, "licencePath:",licencePath, "publicKeyPath:", publicKeyPath)
|
if result == verify_success {
|
return true,machineCode
|
} else {
|
return false,machineCode
|
}
|
}
|
|
func UpdateLicenseCode(code string) bool {
|
//1.先验证发过来的license是否合法
|
result := licence.VerifyLicence(code, config.Server.LPublicKeyPath)
|
logger.Debug("UpdateLicenseCode result:",result,"code:",code,"lPublicKeyPath:",config.Server.LPublicKeyPath)
|
if result != verify_success {
|
return false
|
}
|
|
//2.更新licence
|
licencePath := config.Server.LicensePath
|
var f *os.File
|
var err error
|
//如果没有licence文件需要先创建
|
if !util.Exists(licencePath) {
|
f,err = os.Create(licencePath)
|
if err != nil {
|
return false
|
}
|
} else {
|
f, err = os.OpenFile(licencePath, os.O_WRONLY|os.O_TRUNC, 0600)
|
if err != nil {
|
return false
|
}
|
}
|
|
defer f.Close()
|
if err !=nil {
|
return false
|
}
|
|
num, err := f.WriteString(code)
|
if err ==nil && num >0 {
|
setExpire(false)
|
return true
|
}
|
return false
|
}
|
|
//获取注册码
|
func GetRegisterCode(company string,email string,phone string) string {
|
return licence.GetRegisterCode(company, email, phone)
|
}
|
|
type ResultLicense struct {
|
licence.Licence
|
LicenseCode string
|
}
|
|
func ShowLicense() (flag bool,result ResultLicense) {
|
licencePath := config.Server.LicensePath
|
publicKeyPath := config.Server.LPublicKeyPath
|
|
b, err := licence.DecryptLicenceFile(licencePath, publicKeyPath)
|
if err ==nil {
|
var lse licence.Licence
|
err = json.Unmarshal(b, &lse)
|
if err ==nil {
|
result.Licence = lse
|
bytes, e := ioutil.ReadFile(licencePath)
|
if e==nil {
|
result.LicenseCode = string(bytes)
|
}
|
if lse.Expires > time.Now().Unix() {
|
return true, result
|
} else {
|
return false, result
|
}
|
}
|
}
|
return false, result
|
}
|