saaslic 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +29 -0
- package/index.js +73 -0
- package/package.json +17 -0
package/README.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# saaslic
|
|
2
|
+
|
|
3
|
+
SaaSlic Node.js SDK - 3 行代码接入软件授权验证。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
npm install saaslic
|
|
8
|
+
|
|
9
|
+
## 使用
|
|
10
|
+
|
|
11
|
+
const { LicenseKit } = require("saaslic");
|
|
12
|
+
|
|
13
|
+
const lk = new LicenseKit("YOUR_PROJECT_CODE");
|
|
14
|
+
|
|
15
|
+
const result = await lk.verify("LIC-XXXXXXXX-XXXX");
|
|
16
|
+
if (result.ok) {
|
|
17
|
+
console.log("验证成功:", result.license.project_name);
|
|
18
|
+
} else {
|
|
19
|
+
console.log("验证失败:", result.message);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// 简洁写法
|
|
23
|
+
if (await lk.check("LIC-XXXXXXXX-XXXX")) {
|
|
24
|
+
console.log("授权有效");
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
## 文档
|
|
28
|
+
|
|
29
|
+
https://saaslic.com/docs
|
package/index.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
const https = require("https");
|
|
2
|
+
const http = require("http");
|
|
3
|
+
const os = require("os");
|
|
4
|
+
const crypto = require("crypto");
|
|
5
|
+
|
|
6
|
+
function getDeviceId() {
|
|
7
|
+
const raw = [
|
|
8
|
+
os.hostname(),
|
|
9
|
+
os.platform(),
|
|
10
|
+
os.arch(),
|
|
11
|
+
os.cpus()[0]?.model || "",
|
|
12
|
+
].join("|");
|
|
13
|
+
return crypto.createHash("sha256").update(raw).digest("hex").slice(0, 32);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function httpPost(url, data) {
|
|
17
|
+
return new Promise((resolve, reject) => {
|
|
18
|
+
const body = JSON.stringify(data);
|
|
19
|
+
const parsed = new URL(url);
|
|
20
|
+
const mod = parsed.protocol === "https:" ? https : http;
|
|
21
|
+
const options = {
|
|
22
|
+
hostname: parsed.hostname,
|
|
23
|
+
port: parsed.port || (parsed.protocol === "https:" ? 443 : 80),
|
|
24
|
+
path: parsed.pathname,
|
|
25
|
+
method: "POST",
|
|
26
|
+
headers: {
|
|
27
|
+
"Content-Type": "application/json",
|
|
28
|
+
"Content-Length": Buffer.byteLength(body),
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
const req = mod.request(options, (res) => {
|
|
32
|
+
let raw = "";
|
|
33
|
+
res.on("data", (chunk) => (raw += chunk));
|
|
34
|
+
res.on("end", () => {
|
|
35
|
+
try { resolve(JSON.parse(raw)); }
|
|
36
|
+
catch (e) { reject(new Error("Invalid JSON response")); }
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
req.on("error", reject);
|
|
40
|
+
req.setTimeout(5000, () => { req.destroy(); reject(new Error("请求超时")); });
|
|
41
|
+
req.write(body);
|
|
42
|
+
req.end();
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
class LicenseKit {
|
|
47
|
+
constructor(projectCode, options = {}) {
|
|
48
|
+
this.projectCode = projectCode;
|
|
49
|
+
this.baseUrl = options.baseUrl || "https://api.saaslic.com";
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async verify(licenseKey, options = {}) {
|
|
53
|
+
const payload = {
|
|
54
|
+
license_key: licenseKey,
|
|
55
|
+
project_code: this.projectCode,
|
|
56
|
+
device_id: getDeviceId(),
|
|
57
|
+
device_name: options.deviceName || os.hostname(),
|
|
58
|
+
platform: options.platform || os.platform(),
|
|
59
|
+
};
|
|
60
|
+
try {
|
|
61
|
+
return await httpPost(`${this.baseUrl}/api/license/verify`, payload);
|
|
62
|
+
} catch (e) {
|
|
63
|
+
return { ok: false, message: e.message };
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async check(licenseKey, options = {}) {
|
|
68
|
+
const result = await this.verify(licenseKey, options);
|
|
69
|
+
return result.ok === true;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
module.exports = { LicenseKit };
|
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "saaslic",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "SaaSlic Node.js SDK - Software license verification made simple",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "node test.js"
|
|
8
|
+
},
|
|
9
|
+
"keywords": ["license", "activation", "software", "protection", "saaslic"],
|
|
10
|
+
"author": "LicenseKit <hi@saaslic.com>",
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"homepage": "https://saaslic.com",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "https://github.com/licensekit/saaslic-node"
|
|
16
|
+
}
|
|
17
|
+
}
|