This repository was archived by the owner on Jan 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathlicense-robot.js
More file actions
167 lines (150 loc) · 6.03 KB
/
license-robot.js
File metadata and controls
167 lines (150 loc) · 6.03 KB
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
165
166
167
const puppeteer = require('puppeteer');
const otplib = require('otplib');
const fs = require('fs');
module.exports = { getPersonalLicense };
let RETRY_COUNT = 2;
async function getPersonalLicense(licenseRequestFile, username, password, authenticatorKey, headless = true) {
const licenseRequestData = fs.readFileSync(licenseRequestFile, 'utf8');
console.log("License robot. Start");
const licenseData = await retry(() => browser_getPersonalLicense(licenseRequestData, username, password, authenticatorKey, headless), RETRY_COUNT);
console.log("License robot. Finish");
return licenseData;
}
async function browser_getPersonalLicense(licenseRequestData, username, password, authenticatorKey, headless) {
const browser = await puppeteer.launch({ headless: headless });
try {
const page = await browser.newPage();
await page.setDefaultNavigationTimeout(120 * 1000);
await page.goto('https://license.unity3d.com/manual');
await licensePage_login(page, username, password, authenticatorKey);
try {
await licensePage_attachFileData(page, licenseRequestData);
} catch {
// https://forum.unity.com/threads/i-cant-create-a-unity-license.1001648/
await licensePage_attachFileData(page, convertToUtf16(licenseRequestData));
}
await licensePage_selectType(page);
const licenseData = await licensePage_downloadLicense(page);
return licenseData;
} finally {
await browser.close();
}
}
/**
* @param {import("puppeteer").Page} page
*/
async function licensePage_login(page, username, password, authenticatorKey) {
console.log("License robot. Login...");
await page.waitForSelector("#conversations_create_session_form_email");
await page.evaluate((x) => { document.getElementById("conversations_create_session_form_email").value = x; }, username);
await page.evaluate((x) => { document.getElementById("conversations_create_session_form_password").value = x; }, password);
await Promise.all([
page.click('input[name=commit]'),
page.waitForNavigation({ waitUntil: 'networkidle0' })
]);
const verifyCodeInputPromise = page.$('#conversations_tfa_required_form_verify_code');
const licenseFileInputPromise = page.$('#licenseFile');
const verifyCodeInput = await verifyCodeInputPromise.catch(() => null);
const licenseFileInput = await licenseFileInputPromise.catch(() => null);
if (verifyCodeInput) {
console.log("License robot. Passing two-factor authentication...");
if (!authenticatorKey) {
throw new Error('account verification requested but authenticator key is not provided');
}
const otpTimeRemaining = otplib.authenticator.timeRemaining();
if (otpTimeRemaining < 5) {
await page.waitForTimeout((otpTimeRemaining + 2) * 1000); // wait for new code
}
const otpCode = otplib.authenticator.generate(authenticatorKey.replace(/ /g, ''));
await verifyCodeInput.type(otpCode);
await Promise.all([
page.click('input[name="conversations_tfa_required_form[submit_verify_code]"]'),
page.waitForNavigation({ waitUntil: 'networkidle0' })
]);
}
if (licenseFileInput) {
console.log("License robot. Skip two-factor authentication");
}
}
/**
* @param {import("puppeteer").Page} page
*/
async function licensePage_attachFileData(page, licenseRequestData) {
console.log("License robot. Attach license request file...");
await page.waitForTimeout(5000);
await page.setRequestInterception(true);
page.once("request", interceptedRequest => {
interceptedRequest.continue({
method: "POST",
postData: licenseRequestData,
headers: { "Content-Type": "text/xml" }
});
});
const response = await page.goto('https://license.unity3d.com/genesis/activation/create-transaction');
if (!response.ok()) {
console.log(await response.text());
throw new Error(response.statusText());
}
}
/**
* @param {import("puppeteer").Page} page
*/
async function licensePage_selectType(page) {
console.log("License robot. Select license type...");
await page.waitForTimeout(5000);
page.once("request", interceptedRequest => {
interceptedRequest.continue({
method: "PUT",
postData: JSON.stringify({ transaction: { serial: { type: "personal" } } }),
headers: { "Content-Type": "application/json" }
});
});
const response = await page.goto('https://license.unity3d.com/genesis/activation/update-transaction');
if (!response.ok()) {
console.log(await response.text());
throw new Error(response.statusText());
}
}
/**
* @param {import("puppeteer").Page} page
*/
async function licensePage_downloadLicense(page) {
console.log("License robot. Download license file...");
await page.waitForTimeout(5000);
page.once("request", interceptedRequest => {
interceptedRequest.continue({
method: "POST",
postData: JSON.stringify({}),
headers: { "Content-Type": "application/json" }
});
});
const response = await page.goto('https://license.unity3d.com/genesis/activation/download-license');
if (response.ok()) {
const json = await response.json();
return json['xml'];
} else {
console.log(await response.text());
throw new Error(response.statusText());
}
}
async function retry(func, retryCount) {
while (true) {
try {
return await func();
} catch (error) {
if (retryCount > 0) {
retryCount--;
console.error(error);
}
else throw error;
}
}
}
function convertToUtf16(str) {
var buf = new ArrayBuffer(str.length * 2);
var bufView = new Uint16Array(buf);
for (var i = 0, strLen = str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return String.fromCharCode.apply(null, new Uint8Array(buf));
}