validate(){returnnewPromise((resolve,reject)=>{try{// Local check
varfile=this.findLicense()if(!file){reject('License key not found')}else{vardata=fs.readFileSync(file,'utf8')licenseInfo=JSON.parse(data)varbase=SK+licenseInfo.name+SK+licenseInfo.product+'-'+licenseInfo.licenseType+SK+licenseInfo.quantity+SK+licenseInfo.timestamp+SKvar_key=crypto.createHash('sha1').update(base).digest('hex').toUpperCase()if(_key!==licenseInfo.licenseKey){reject('Invalid license key')}else{// Server check
$.post(app.config.validation_url,{licenseKey:licenseInfo.licenseKey}).done(data=>{resolve(data)}).fail(err=>{if(err&&err.status===499){/* License key not exists */reject(err)}else{// If server is not available, assume that license key is valid
resolve(licenseInfo)}})}}}catch(err){reject(err)}})}
可以看到验证分两部分:先本地验证再进行网络验证,网络验证成功后返回许可证信息。
本地验证
我们一步步来,先处理本地验证:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Local check
varfile=this.findLicense()if(!file){reject('License key not found')}else{vardata=fs.readFileSync(file,'utf8')licenseInfo=JSON.parse(data)varbase=SK+licenseInfo.name+SK+licenseInfo.product+'-'+licenseInfo.licenseType+SK+licenseInfo.quantity+SK+licenseInfo.timestamp+SKvar_key=crypto.createHash('sha1').update(base).digest('hex').toUpperCase()if(_key!==licenseInfo.licenseKey){reject('Invalid license key')}.....}
// Server check
$.post(app.config.validation_url,{licenseKey:licenseInfo.licenseKey}).done(data=>{resolve(data)}).fail(err=>{if(err&&err.status===499){/* License key not exists */reject(err)}else{// If server is not available, assume that license key is valid
resolve(licenseInfo)}})
又是一个 POST 请求!又是返回data!认真看了一下还是与上一步输入注册码同样的网络请求!好了,多的我就不说了👀,大家应该知道怎么办,可以同上一步一样模拟data的内容,然后resolve(data)返回即可。
但是,这里我选择直接返回licenseInfo,为什么呢?
大家看一下本地验证,licenseInfo的内容是从文件license.key中转换而来,文件license.key的内容是上一步的 POST 请求而来,嗯….不用我多说了吧!
整理代码后如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
validate(){returnnewPromise((resolve,reject)=>{try{// Local check
varfile=this.findLicense()if(!file){reject('License key not found')}else{vardata=fs.readFileSync(file,'utf8')licenseInfo=JSON.parse(data)resolve(licenseInfo)}}catch(err){reject(err)}})}
......var$license=$dlg.find('.license')var$licenseType=$dlg.find('.licenseType')var$quantity=$dlg.find('.quantity')......$license.html('Licensed to '+info.name)$licenseType.html(licenseTypeName+' License')$quantity.html(info.quantity+' User(s)')......
......var$license=$dlg.find('.license')var$licenseType=$dlg.find('.licenseType')var$quantity=$dlg.find('.quantity')var$crackedAuthor=$dlg.find('.crackedAuthor')......$license.html('Licensed to '+info.name)$licenseType.html(licenseTypeName+' License')$quantity.html(info.quantity+' User(s)')$crackedAuthor.html('Cracked by '+info.crackedAuthor)......