zhangzengfei
2023-11-28 3a706d3378aa3626501370352963883fd2783558
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
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
}