ppx-inject 0.3.9 → 0.3.12
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/lib/cli.js +103 -103
- package/lib/cli.js.map +1 -1
- package/lib/profile.js +80 -80
- package/lib/split-string-according-to-length-and-delimiter.js +19 -19
- package/lib/statistics-file.js +34 -34
- package/package.json +15 -15
- package/CHANGELOG.md +0 -55
package/lib/cli.js
CHANGED
|
@@ -1,104 +1,104 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
"use strict";
|
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
const commander_1 = require("commander");
|
|
5
|
-
const profile_1 = require("./profile");
|
|
6
|
-
const fs_1 = require("fs");
|
|
7
|
-
const extra_filesystem_1 = require("extra-filesystem");
|
|
8
|
-
const statistics_file_1 = require("./statistics-file");
|
|
9
|
-
const internet_number_1 = require("internet-number");
|
|
10
|
-
const path = require("path");
|
|
11
|
-
commander_1.program
|
|
12
|
-
.name(require('../package.json').name)
|
|
13
|
-
.version(require('../package.json').version)
|
|
14
|
-
.description(require('../package.json').description)
|
|
15
|
-
.
|
|
16
|
-
.arguments('<profile>')
|
|
17
|
-
.action(async (profile) => {
|
|
18
|
-
const opts = commander_1.program.opts();
|
|
19
|
-
const cc = opts.cc;
|
|
20
|
-
await inject(cc, profile);
|
|
21
|
-
})
|
|
22
|
-
.parse();
|
|
23
|
-
async function inject(cc, profileFilename) {
|
|
24
|
-
if (await isDataFilesExisted()) {
|
|
25
|
-
console.info('Checking for updates...');
|
|
26
|
-
const checksum = await (0, internet_number_1.fetchLatestChecksum)(internet_number_1.Domain.APNIC, internet_number_1.Registry.APNIC);
|
|
27
|
-
if (await getExistedChecksum() !== checksum) {
|
|
28
|
-
console.info('Cleaning expired files...');
|
|
29
|
-
await cleanExpiredFiles();
|
|
30
|
-
await saveChecksum(checksum);
|
|
31
|
-
console.info('Downloading the latest statistics file...');
|
|
32
|
-
await downloadStatisticsFile();
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
else {
|
|
36
|
-
await ensureDataPath();
|
|
37
|
-
console.info('Downloading the latest checksum file...');
|
|
38
|
-
await downloadChecksum();
|
|
39
|
-
console.info('Downloading the latest statistics file...');
|
|
40
|
-
await downloadStatisticsFile();
|
|
41
|
-
}
|
|
42
|
-
console.info('Updating the profile...');
|
|
43
|
-
const ipRanges = await (0, statistics_file_1.parseAddressRangesFromStatisticsFile)(getStatisticsPath(), cc);
|
|
44
|
-
const targets = (0, profile_1.createTargetsFromAddressRanges)(ipRanges);
|
|
45
|
-
const profile = await (0, profile_1.readProfileFile)(profileFilename);
|
|
46
|
-
const newRuleList = (0, profile_1.mergeRuleList)((0, profile_1.getRuleList)(profile), (0, profile_1.createDirectRules)(targets));
|
|
47
|
-
const newProfile = (0, profile_1.updateProfile)(profile, newRuleList);
|
|
48
|
-
await (0, profile_1.writeProfileFile)(profileFilename, newProfile);
|
|
49
|
-
console.info('Done.\n');
|
|
50
|
-
console.info('Please run the command to load the new profile file:');
|
|
51
|
-
console.info(`Proxifier ${path.resolve(profileFilename)} silent-load`);
|
|
52
|
-
}
|
|
53
|
-
async function ensureDataPath() {
|
|
54
|
-
return await (0, extra_filesystem_1.ensureDir)(getDataPath());
|
|
55
|
-
}
|
|
56
|
-
function getStatisticsPath() {
|
|
57
|
-
return getDataPath('statistics');
|
|
58
|
-
}
|
|
59
|
-
function getChecksumPath() {
|
|
60
|
-
return getDataPath('checksum');
|
|
61
|
-
}
|
|
62
|
-
async function isDataFilesExisted() {
|
|
63
|
-
return await (0, extra_filesystem_1.pathExists)(getDataPath())
|
|
64
|
-
&& await (0, extra_filesystem_1.pathExists)(getChecksumPath())
|
|
65
|
-
&& await (0, extra_filesystem_1.pathExists)(getStatisticsPath());
|
|
66
|
-
}
|
|
67
|
-
async function cleanExpiredFiles() {
|
|
68
|
-
await fs_1.promises.rm(getChecksumPath());
|
|
69
|
-
await fs_1.promises.rm(getStatisticsPath());
|
|
70
|
-
}
|
|
71
|
-
async function getExistedChecksum() {
|
|
72
|
-
return await fs_1.promises.readFile(getChecksumPath(), { encoding: 'utf8' });
|
|
73
|
-
}
|
|
74
|
-
async function saveChecksum(checksum) {
|
|
75
|
-
await fs_1.promises.writeFile(getChecksumPath(), checksum);
|
|
76
|
-
}
|
|
77
|
-
async function downloadChecksum() {
|
|
78
|
-
const checksum = await (0, internet_number_1.fetchLatestChecksum)(internet_number_1.Domain.APNIC, internet_number_1.Registry.APNIC);
|
|
79
|
-
await saveChecksum(checksum);
|
|
80
|
-
}
|
|
81
|
-
async function downloadStatisticsFile() {
|
|
82
|
-
const rs = await (0, internet_number_1.fetchLatestStatisticsFile)(internet_number_1.Domain.APNIC, internet_number_1.Registry.APNIC);
|
|
83
|
-
const tempFilename = getStatisticsPath() + '.downloading';
|
|
84
|
-
const ws = (0, fs_1.createWriteStream)(tempFilename);
|
|
85
|
-
await pipePromise(rs, ws);
|
|
86
|
-
await (0, extra_filesystem_1.move)(tempFilename, getStatisticsPath());
|
|
87
|
-
}
|
|
88
|
-
function getDataPath(file) {
|
|
89
|
-
const dataDir = path.resolve(__dirname, '../data');
|
|
90
|
-
if (file) {
|
|
91
|
-
return path.join(dataDir, file);
|
|
92
|
-
}
|
|
93
|
-
else {
|
|
94
|
-
return dataDir;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
function pipePromise(readStream, writeStream) {
|
|
98
|
-
return new Promise((resolve, reject) => {
|
|
99
|
-
const stream = readStream.pipe(writeStream);
|
|
100
|
-
stream.once('finish', resolve);
|
|
101
|
-
stream.once('error', reject);
|
|
102
|
-
});
|
|
103
|
-
}
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
const commander_1 = require("commander");
|
|
5
|
+
const profile_1 = require("./profile");
|
|
6
|
+
const fs_1 = require("fs");
|
|
7
|
+
const extra_filesystem_1 = require("extra-filesystem");
|
|
8
|
+
const statistics_file_1 = require("./statistics-file");
|
|
9
|
+
const internet_number_1 = require("internet-number");
|
|
10
|
+
const path = require("path");
|
|
11
|
+
commander_1.program
|
|
12
|
+
.name(require('../package.json').name)
|
|
13
|
+
.version(require('../package.json').version)
|
|
14
|
+
.description(require('../package.json').description)
|
|
15
|
+
.requiredOption('--cc <cc...>', 'ISO 3166 2-letter code of the organization to which the allocation or assignment was made.')
|
|
16
|
+
.arguments('<profile>')
|
|
17
|
+
.action(async (profile) => {
|
|
18
|
+
const opts = commander_1.program.opts();
|
|
19
|
+
const cc = opts.cc;
|
|
20
|
+
await inject(cc, profile);
|
|
21
|
+
})
|
|
22
|
+
.parse();
|
|
23
|
+
async function inject(cc, profileFilename) {
|
|
24
|
+
if (await isDataFilesExisted()) {
|
|
25
|
+
console.info('Checking for updates...');
|
|
26
|
+
const checksum = await (0, internet_number_1.fetchLatestChecksum)(internet_number_1.Domain.APNIC, internet_number_1.Registry.APNIC);
|
|
27
|
+
if (await getExistedChecksum() !== checksum) {
|
|
28
|
+
console.info('Cleaning expired files...');
|
|
29
|
+
await cleanExpiredFiles();
|
|
30
|
+
await saveChecksum(checksum);
|
|
31
|
+
console.info('Downloading the latest statistics file...');
|
|
32
|
+
await downloadStatisticsFile();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
await ensureDataPath();
|
|
37
|
+
console.info('Downloading the latest checksum file...');
|
|
38
|
+
await downloadChecksum();
|
|
39
|
+
console.info('Downloading the latest statistics file...');
|
|
40
|
+
await downloadStatisticsFile();
|
|
41
|
+
}
|
|
42
|
+
console.info('Updating the profile...');
|
|
43
|
+
const ipRanges = await (0, statistics_file_1.parseAddressRangesFromStatisticsFile)(getStatisticsPath(), cc);
|
|
44
|
+
const targets = (0, profile_1.createTargetsFromAddressRanges)(ipRanges);
|
|
45
|
+
const profile = await (0, profile_1.readProfileFile)(profileFilename);
|
|
46
|
+
const newRuleList = (0, profile_1.mergeRuleList)((0, profile_1.getRuleList)(profile), (0, profile_1.createDirectRules)(targets));
|
|
47
|
+
const newProfile = (0, profile_1.updateProfile)(profile, newRuleList);
|
|
48
|
+
await (0, profile_1.writeProfileFile)(profileFilename, newProfile);
|
|
49
|
+
console.info('Done.\n');
|
|
50
|
+
console.info('Please run the command to load the new profile file:');
|
|
51
|
+
console.info(`Proxifier ${path.resolve(profileFilename)} silent-load`);
|
|
52
|
+
}
|
|
53
|
+
async function ensureDataPath() {
|
|
54
|
+
return await (0, extra_filesystem_1.ensureDir)(getDataPath());
|
|
55
|
+
}
|
|
56
|
+
function getStatisticsPath() {
|
|
57
|
+
return getDataPath('statistics');
|
|
58
|
+
}
|
|
59
|
+
function getChecksumPath() {
|
|
60
|
+
return getDataPath('checksum');
|
|
61
|
+
}
|
|
62
|
+
async function isDataFilesExisted() {
|
|
63
|
+
return await (0, extra_filesystem_1.pathExists)(getDataPath())
|
|
64
|
+
&& await (0, extra_filesystem_1.pathExists)(getChecksumPath())
|
|
65
|
+
&& await (0, extra_filesystem_1.pathExists)(getStatisticsPath());
|
|
66
|
+
}
|
|
67
|
+
async function cleanExpiredFiles() {
|
|
68
|
+
await fs_1.promises.rm(getChecksumPath());
|
|
69
|
+
await fs_1.promises.rm(getStatisticsPath());
|
|
70
|
+
}
|
|
71
|
+
async function getExistedChecksum() {
|
|
72
|
+
return await fs_1.promises.readFile(getChecksumPath(), { encoding: 'utf8' });
|
|
73
|
+
}
|
|
74
|
+
async function saveChecksum(checksum) {
|
|
75
|
+
await fs_1.promises.writeFile(getChecksumPath(), checksum);
|
|
76
|
+
}
|
|
77
|
+
async function downloadChecksum() {
|
|
78
|
+
const checksum = await (0, internet_number_1.fetchLatestChecksum)(internet_number_1.Domain.APNIC, internet_number_1.Registry.APNIC);
|
|
79
|
+
await saveChecksum(checksum);
|
|
80
|
+
}
|
|
81
|
+
async function downloadStatisticsFile() {
|
|
82
|
+
const rs = await (0, internet_number_1.fetchLatestStatisticsFile)(internet_number_1.Domain.APNIC, internet_number_1.Registry.APNIC);
|
|
83
|
+
const tempFilename = getStatisticsPath() + '.downloading';
|
|
84
|
+
const ws = (0, fs_1.createWriteStream)(tempFilename);
|
|
85
|
+
await pipePromise(rs, ws);
|
|
86
|
+
await (0, extra_filesystem_1.move)(tempFilename, getStatisticsPath());
|
|
87
|
+
}
|
|
88
|
+
function getDataPath(file) {
|
|
89
|
+
const dataDir = path.resolve(__dirname, '../data');
|
|
90
|
+
if (file) {
|
|
91
|
+
return path.join(dataDir, file);
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
return dataDir;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
function pipePromise(readStream, writeStream) {
|
|
98
|
+
return new Promise((resolve, reject) => {
|
|
99
|
+
const stream = readStream.pipe(writeStream);
|
|
100
|
+
stream.once('finish', resolve);
|
|
101
|
+
stream.once('error', reject);
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
104
|
//# sourceMappingURL=cli.js.map
|
package/lib/cli.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";;;AACA,yCAAmC;AACnC,uCAQkB;AAClB,2BAAsD;AACtD,uDAA8D;AAC9D,uDAAwE;AACxE,qDAKwB;AACxB,6BAA4B;AAE5B,mBAAO;KACJ,IAAI,CAAC,OAAO,CAAC,iBAAiB,CAAC,CAAC,IAAI,CAAC;KACrC,OAAO,CAAC,OAAO,CAAC,iBAAiB,CAAC,CAAC,OAAO,CAAC;KAC3C,WAAW,CAAC,OAAO,CAAC,iBAAiB,CAAC,CAAC,WAAW,CAAC;KACnD,
|
|
1
|
+
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";;;AACA,yCAAmC;AACnC,uCAQkB;AAClB,2BAAsD;AACtD,uDAA8D;AAC9D,uDAAwE;AACxE,qDAKwB;AACxB,6BAA4B;AAE5B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SAAS,WAAW,CAClB,UAAiC,EACjC,WAAkC;IAElC,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACrC,MAAM,MAAM,GAAG,UAAU,CAAC,IAAI,CAAC,WAAW,CAAC,CAAA;QAC3C,MAAM,CAAC,IAAI,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAA;QAC9B,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,MAAM,CAAC,CAAA;IAC9B,CAAC,CAAC,CAAA;AACJ,CAAC"}
|
package/lib/profile.js
CHANGED
|
@@ -1,81 +1,81 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createDirectRules = exports.mergeRuleList = exports.getRuleList = exports.createTargetsFromAddressRanges = exports.updateProfile = exports.writeProfileFile = exports.readProfileFile = void 0;
|
|
4
|
-
const split_string_according_to_length_and_delimiter_1 = require("./split-string-according-to-length-and-delimiter");
|
|
5
|
-
const fs_1 = require("fs");
|
|
6
|
-
const xml2js = require("xml2js");
|
|
7
|
-
const nanoid_1 = require("nanoid");
|
|
8
|
-
const immer_1 = require("immer");
|
|
9
|
-
async function readProfileFile(filename) {
|
|
10
|
-
const text = await fs_1.promises.readFile(filename, { encoding: 'utf8' });
|
|
11
|
-
return await xml2js.parseStringPromise(text);
|
|
12
|
-
}
|
|
13
|
-
exports.readProfileFile = readProfileFile;
|
|
14
|
-
async function writeProfileFile(filename, profile) {
|
|
15
|
-
const xml = buildProfileXml(profile);
|
|
16
|
-
await fs_1.promises.writeFile(filename, xml, { encoding: 'utf8' });
|
|
17
|
-
}
|
|
18
|
-
exports.writeProfileFile = writeProfileFile;
|
|
19
|
-
function updateProfile(profile, newRuleList) {
|
|
20
|
-
const newProfile = replaceRuleList(profile, newRuleList);
|
|
21
|
-
return newProfile;
|
|
22
|
-
}
|
|
23
|
-
exports.updateProfile = updateProfile;
|
|
24
|
-
function createTargetsFromAddressRanges(ranges) {
|
|
25
|
-
return ranges
|
|
26
|
-
.map(x => x.toString())
|
|
27
|
-
.join(';');
|
|
28
|
-
}
|
|
29
|
-
exports.createTargetsFromAddressRanges = createTargetsFromAddressRanges;
|
|
30
|
-
function getRuleList(profile) {
|
|
31
|
-
return profile.ProxifierProfile.RuleList[0].Rule;
|
|
32
|
-
}
|
|
33
|
-
exports.getRuleList = getRuleList;
|
|
34
|
-
function mergeRuleList(oldRuleList, newRuleList) {
|
|
35
|
-
const defaultRule = last(oldRuleList);
|
|
36
|
-
return oldRuleList
|
|
37
|
-
.filter(isntProgramCreated)
|
|
38
|
-
.filter(isnt(defaultRule))
|
|
39
|
-
.concat(newRuleList)
|
|
40
|
-
.concat([defaultRule]);
|
|
41
|
-
function isntProgramCreated(x) {
|
|
42
|
-
return !x.Name[0].includes(getProgramCreatedFlag());
|
|
43
|
-
}
|
|
44
|
-
function isnt(val) {
|
|
45
|
-
return (x) => x !== val;
|
|
46
|
-
}
|
|
47
|
-
function last(xs) {
|
|
48
|
-
return xs[xs.length - 1];
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
exports.mergeRuleList = mergeRuleList;
|
|
52
|
-
function createDirectRules(targets) {
|
|
53
|
-
const LIMIT_PER_RULE = 32767;
|
|
54
|
-
const group = (0, split_string_according_to_length_and_delimiter_1.splitStringAccordingToLengthAndDelimiter)(targets, LIMIT_PER_RULE, ';');
|
|
55
|
-
return group.map(rule => createDirectRule('directips', rule));
|
|
56
|
-
function createDirectRule(prefix, target) {
|
|
57
|
-
return {
|
|
58
|
-
$: { enabled: 'true' },
|
|
59
|
-
Name: [prefix + '-' + (0, nanoid_1.nanoid)() + ' ' + getProgramCreatedFlag()],
|
|
60
|
-
Targets: [target],
|
|
61
|
-
Action: [{
|
|
62
|
-
$: { type: 'Direct' }
|
|
63
|
-
}]
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
exports.createDirectRules = createDirectRules;
|
|
68
|
-
function replaceRuleList(profile, ruleList) {
|
|
69
|
-
return (0, immer_1.default)(profile, (profile => {
|
|
70
|
-
profile.ProxifierProfile.RuleList[0].Rule = ruleList;
|
|
71
|
-
}));
|
|
72
|
-
}
|
|
73
|
-
function getProgramCreatedFlag() {
|
|
74
|
-
return '[program-created]';
|
|
75
|
-
}
|
|
76
|
-
function buildProfileXml(profile) {
|
|
77
|
-
const builder = new xml2js.Builder();
|
|
78
|
-
const xml = builder.buildObject(profile);
|
|
79
|
-
return xml;
|
|
80
|
-
}
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createDirectRules = exports.mergeRuleList = exports.getRuleList = exports.createTargetsFromAddressRanges = exports.updateProfile = exports.writeProfileFile = exports.readProfileFile = void 0;
|
|
4
|
+
const split_string_according_to_length_and_delimiter_1 = require("./split-string-according-to-length-and-delimiter");
|
|
5
|
+
const fs_1 = require("fs");
|
|
6
|
+
const xml2js = require("xml2js");
|
|
7
|
+
const nanoid_1 = require("nanoid");
|
|
8
|
+
const immer_1 = require("immer");
|
|
9
|
+
async function readProfileFile(filename) {
|
|
10
|
+
const text = await fs_1.promises.readFile(filename, { encoding: 'utf8' });
|
|
11
|
+
return await xml2js.parseStringPromise(text);
|
|
12
|
+
}
|
|
13
|
+
exports.readProfileFile = readProfileFile;
|
|
14
|
+
async function writeProfileFile(filename, profile) {
|
|
15
|
+
const xml = buildProfileXml(profile);
|
|
16
|
+
await fs_1.promises.writeFile(filename, xml, { encoding: 'utf8' });
|
|
17
|
+
}
|
|
18
|
+
exports.writeProfileFile = writeProfileFile;
|
|
19
|
+
function updateProfile(profile, newRuleList) {
|
|
20
|
+
const newProfile = replaceRuleList(profile, newRuleList);
|
|
21
|
+
return newProfile;
|
|
22
|
+
}
|
|
23
|
+
exports.updateProfile = updateProfile;
|
|
24
|
+
function createTargetsFromAddressRanges(ranges) {
|
|
25
|
+
return ranges
|
|
26
|
+
.map(x => x.toString())
|
|
27
|
+
.join(';');
|
|
28
|
+
}
|
|
29
|
+
exports.createTargetsFromAddressRanges = createTargetsFromAddressRanges;
|
|
30
|
+
function getRuleList(profile) {
|
|
31
|
+
return profile.ProxifierProfile.RuleList[0].Rule;
|
|
32
|
+
}
|
|
33
|
+
exports.getRuleList = getRuleList;
|
|
34
|
+
function mergeRuleList(oldRuleList, newRuleList) {
|
|
35
|
+
const defaultRule = last(oldRuleList);
|
|
36
|
+
return oldRuleList
|
|
37
|
+
.filter(isntProgramCreated)
|
|
38
|
+
.filter(isnt(defaultRule))
|
|
39
|
+
.concat(newRuleList)
|
|
40
|
+
.concat([defaultRule]);
|
|
41
|
+
function isntProgramCreated(x) {
|
|
42
|
+
return !x.Name[0].includes(getProgramCreatedFlag());
|
|
43
|
+
}
|
|
44
|
+
function isnt(val) {
|
|
45
|
+
return (x) => x !== val;
|
|
46
|
+
}
|
|
47
|
+
function last(xs) {
|
|
48
|
+
return xs[xs.length - 1];
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
exports.mergeRuleList = mergeRuleList;
|
|
52
|
+
function createDirectRules(targets) {
|
|
53
|
+
const LIMIT_PER_RULE = 32767;
|
|
54
|
+
const group = (0, split_string_according_to_length_and_delimiter_1.splitStringAccordingToLengthAndDelimiter)(targets, LIMIT_PER_RULE, ';');
|
|
55
|
+
return group.map(rule => createDirectRule('directips', rule));
|
|
56
|
+
function createDirectRule(prefix, target) {
|
|
57
|
+
return {
|
|
58
|
+
$: { enabled: 'true' },
|
|
59
|
+
Name: [prefix + '-' + (0, nanoid_1.nanoid)() + ' ' + getProgramCreatedFlag()],
|
|
60
|
+
Targets: [target],
|
|
61
|
+
Action: [{
|
|
62
|
+
$: { type: 'Direct' }
|
|
63
|
+
}]
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
exports.createDirectRules = createDirectRules;
|
|
68
|
+
function replaceRuleList(profile, ruleList) {
|
|
69
|
+
return (0, immer_1.default)(profile, (profile => {
|
|
70
|
+
profile.ProxifierProfile.RuleList[0].Rule = ruleList;
|
|
71
|
+
}));
|
|
72
|
+
}
|
|
73
|
+
function getProgramCreatedFlag() {
|
|
74
|
+
return '[program-created]';
|
|
75
|
+
}
|
|
76
|
+
function buildProfileXml(profile) {
|
|
77
|
+
const builder = new xml2js.Builder();
|
|
78
|
+
const xml = builder.buildObject(profile);
|
|
79
|
+
return xml;
|
|
80
|
+
}
|
|
81
81
|
//# sourceMappingURL=profile.js.map
|
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.splitStringAccordingToLengthAndDelimiter = void 0;
|
|
4
|
-
const structures_1 = require("@blackglory/structures");
|
|
5
|
-
function splitStringAccordingToLengthAndDelimiter(str, sliceMaximumLength, delimiter) {
|
|
6
|
-
return (0, structures_1.convertConsToArray)(slice(str));
|
|
7
|
-
function slice(str) {
|
|
8
|
-
if (str.length > sliceMaximumLength) {
|
|
9
|
-
const endIndex = str.lastIndexOf(delimiter, sliceMaximumLength - 1);
|
|
10
|
-
const value = str.slice(0, endIndex);
|
|
11
|
-
const next = slice(str.slice(endIndex + delimiter.length));
|
|
12
|
-
return [value, next];
|
|
13
|
-
}
|
|
14
|
-
else {
|
|
15
|
-
return [str, null];
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
exports.splitStringAccordingToLengthAndDelimiter = splitStringAccordingToLengthAndDelimiter;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.splitStringAccordingToLengthAndDelimiter = void 0;
|
|
4
|
+
const structures_1 = require("@blackglory/structures");
|
|
5
|
+
function splitStringAccordingToLengthAndDelimiter(str, sliceMaximumLength, delimiter) {
|
|
6
|
+
return (0, structures_1.convertConsToArray)(slice(str));
|
|
7
|
+
function slice(str) {
|
|
8
|
+
if (str.length > sliceMaximumLength) {
|
|
9
|
+
const endIndex = str.lastIndexOf(delimiter, sliceMaximumLength - 1);
|
|
10
|
+
const value = str.slice(0, endIndex);
|
|
11
|
+
const next = slice(str.slice(endIndex + delimiter.length));
|
|
12
|
+
return [value, next];
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
return [str, null];
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
exports.splitStringAccordingToLengthAndDelimiter = splitStringAccordingToLengthAndDelimiter;
|
|
20
20
|
//# sourceMappingURL=split-string-according-to-length-and-delimiter.js.map
|
package/lib/statistics-file.js
CHANGED
|
@@ -1,35 +1,35 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.convertRecordsToRanges = exports.parseAddressRangesFromStatisticsFile = void 0;
|
|
4
|
-
const address_range_1 = require("address-range");
|
|
5
|
-
const internet_number_1 = require("internet-number");
|
|
6
|
-
const chaining_1 = require("iterable-operator/lib/es2018/style/chaining");
|
|
7
|
-
async function parseAddressRangesFromStatisticsFile(filename, cc) {
|
|
8
|
-
const records = await new chaining_1.AsyncIterableOperator((0, internet_number_1.parseStatisticsFile)(filename))
|
|
9
|
-
.filterAsync(internet_number_1.isRecord)
|
|
10
|
-
.filterAsync(record => cc.includes(record.cc))
|
|
11
|
-
.toArrayAsync();
|
|
12
|
-
return convertRecordsToRanges(records);
|
|
13
|
-
}
|
|
14
|
-
exports.parseAddressRangesFromStatisticsFile = parseAddressRangesFromStatisticsFile;
|
|
15
|
-
function convertRecordsToRanges(records) {
|
|
16
|
-
const ipv4Ranges = records
|
|
17
|
-
.filter(x => x.type === 'ipv4')
|
|
18
|
-
.map(x => {
|
|
19
|
-
const startAddress = x.start;
|
|
20
|
-
const hosts = Number.parseInt(x.value, 10);
|
|
21
|
-
return address_range_1.IPv4AddressRange.from(startAddress, hosts);
|
|
22
|
-
});
|
|
23
|
-
const ipv6Ranges = records
|
|
24
|
-
.filter(x => x.type === 'ipv6')
|
|
25
|
-
.map(x => {
|
|
26
|
-
const startAddress = x.start;
|
|
27
|
-
const cidr = Number.parseInt(x.value, 10);
|
|
28
|
-
return address_range_1.IPv6AddressRange.from(startAddress, cidr);
|
|
29
|
-
});
|
|
30
|
-
const cleanIPv4Ranges = (0, address_range_1.compress)(ipv4Ranges, address_range_1.IPv4AddressRange);
|
|
31
|
-
const cleanIPv6Ranges = (0, address_range_1.compress)(ipv6Ranges, address_range_1.IPv6AddressRange);
|
|
32
|
-
return [...cleanIPv4Ranges, ...cleanIPv6Ranges];
|
|
33
|
-
}
|
|
34
|
-
exports.convertRecordsToRanges = convertRecordsToRanges;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.convertRecordsToRanges = exports.parseAddressRangesFromStatisticsFile = void 0;
|
|
4
|
+
const address_range_1 = require("address-range");
|
|
5
|
+
const internet_number_1 = require("internet-number");
|
|
6
|
+
const chaining_1 = require("iterable-operator/lib/es2018/style/chaining");
|
|
7
|
+
async function parseAddressRangesFromStatisticsFile(filename, cc) {
|
|
8
|
+
const records = await new chaining_1.AsyncIterableOperator((0, internet_number_1.parseStatisticsFile)(filename))
|
|
9
|
+
.filterAsync(internet_number_1.isRecord)
|
|
10
|
+
.filterAsync(record => cc.includes(record.cc))
|
|
11
|
+
.toArrayAsync();
|
|
12
|
+
return convertRecordsToRanges(records);
|
|
13
|
+
}
|
|
14
|
+
exports.parseAddressRangesFromStatisticsFile = parseAddressRangesFromStatisticsFile;
|
|
15
|
+
function convertRecordsToRanges(records) {
|
|
16
|
+
const ipv4Ranges = records
|
|
17
|
+
.filter(x => x.type === 'ipv4')
|
|
18
|
+
.map(x => {
|
|
19
|
+
const startAddress = x.start;
|
|
20
|
+
const hosts = Number.parseInt(x.value, 10);
|
|
21
|
+
return address_range_1.IPv4AddressRange.from(startAddress, hosts);
|
|
22
|
+
});
|
|
23
|
+
const ipv6Ranges = records
|
|
24
|
+
.filter(x => x.type === 'ipv6')
|
|
25
|
+
.map(x => {
|
|
26
|
+
const startAddress = x.start;
|
|
27
|
+
const cidr = Number.parseInt(x.value, 10);
|
|
28
|
+
return address_range_1.IPv6AddressRange.from(startAddress, cidr);
|
|
29
|
+
});
|
|
30
|
+
const cleanIPv4Ranges = (0, address_range_1.compress)(ipv4Ranges, address_range_1.IPv4AddressRange);
|
|
31
|
+
const cleanIPv6Ranges = (0, address_range_1.compress)(ipv6Ranges, address_range_1.IPv6AddressRange);
|
|
32
|
+
return [...cleanIPv4Ranges, ...cleanIPv6Ranges];
|
|
33
|
+
}
|
|
34
|
+
exports.convertRecordsToRanges = convertRecordsToRanges;
|
|
35
35
|
//# sourceMappingURL=statistics-file.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ppx-inject",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.12",
|
|
4
4
|
"description": "The CLI program that inject Direct access rules into Proxifier's profile.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"profixier"
|
|
@@ -36,34 +36,34 @@
|
|
|
36
36
|
"node": ">= 14"
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@blackglory/structures": "^0.2
|
|
39
|
+
"@blackglory/structures": "^0.3.2",
|
|
40
40
|
"address-range": "^0.2.9",
|
|
41
41
|
"commander": "^8.3.0",
|
|
42
|
-
"extra-filesystem": "^0.
|
|
42
|
+
"extra-filesystem": "^0.4.1",
|
|
43
43
|
"immer": "^9.0.7",
|
|
44
|
-
"internet-number": "^
|
|
45
|
-
"iterable-operator": "^0.14.
|
|
44
|
+
"internet-number": "^3.0.1",
|
|
45
|
+
"iterable-operator": "^0.14.5",
|
|
46
46
|
"nanoid": "^3.1.30",
|
|
47
47
|
"xml2js": "^0.4.23"
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
|
-
"@commitlint/cli": "^
|
|
51
|
-
"@commitlint/config-conventional": "^
|
|
52
|
-
"@types/jest": "^27.0
|
|
50
|
+
"@commitlint/cli": "^16.0.1",
|
|
51
|
+
"@commitlint/config-conventional": "^16.0.0",
|
|
52
|
+
"@types/jest": "^27.4.0",
|
|
53
53
|
"@types/jsbn": "^1.2.29",
|
|
54
|
-
"@types/node": "
|
|
54
|
+
"@types/node": "14",
|
|
55
55
|
"@types/xml2js": "^0.4.9",
|
|
56
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
|
57
|
-
"@typescript-eslint/parser": "^5.
|
|
58
|
-
"eslint": "^8.
|
|
56
|
+
"@typescript-eslint/eslint-plugin": "^5.9.0",
|
|
57
|
+
"@typescript-eslint/parser": "^5.9.0",
|
|
58
|
+
"eslint": "^8.6.0",
|
|
59
59
|
"husky": "4",
|
|
60
|
-
"jest": "^27.4.
|
|
60
|
+
"jest": "^27.4.7",
|
|
61
61
|
"npm-run-all": "^4.1.5",
|
|
62
62
|
"rimraf": "^3.0.2",
|
|
63
63
|
"standard-version": "^9.3.2",
|
|
64
|
-
"ts-jest": "^27.1.
|
|
64
|
+
"ts-jest": "^27.1.2",
|
|
65
65
|
"ts-node": "^10.4.0",
|
|
66
66
|
"tscpaths": "^0.0.9",
|
|
67
|
-
"typescript": "^4.5.
|
|
67
|
+
"typescript": "^4.5.4"
|
|
68
68
|
}
|
|
69
69
|
}
|
package/CHANGELOG.md
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
# Changelog
|
|
2
|
-
|
|
3
|
-
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
|
-
|
|
5
|
-
### [0.3.9](https://github.com/BlackGlory/ppx-inject/compare/v0.3.8...v0.3.9) (2021-12-12)
|
|
6
|
-
|
|
7
|
-
### [0.3.8](https://github.com/BlackGlory/ppx-inject/compare/v0.3.7...v0.3.8) (2021-10-14)
|
|
8
|
-
|
|
9
|
-
### [0.3.7](https://github.com/BlackGlory/ppx-inject/compare/v0.3.6...v0.3.7) (2021-05-16)
|
|
10
|
-
|
|
11
|
-
### [0.3.6](https://github.com/BlackGlory/ppx-inject/compare/v0.3.5...v0.3.6) (2021-05-16)
|
|
12
|
-
|
|
13
|
-
### [0.3.5](https://github.com/BlackGlory/ppx-inject/compare/v0.3.4...v0.3.5) (2021-05-14)
|
|
14
|
-
|
|
15
|
-
### [0.3.4](https://github.com/BlackGlory/ppx-inject/compare/v0.3.3...v0.3.4) (2021-05-08)
|
|
16
|
-
|
|
17
|
-
### [0.3.3](https://github.com/BlackGlory/ppx-inject/compare/v0.3.2...v0.3.3) (2021-05-08)
|
|
18
|
-
|
|
19
|
-
### [0.3.2](https://github.com/BlackGlory/ppx-inject/compare/v0.3.1...v0.3.2) (2021-04-11)
|
|
20
|
-
|
|
21
|
-
### [0.3.1](https://github.com/BlackGlory/ppx-inject/compare/v0.3.0...v0.3.1) (2021-04-10)
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
### Bug Fixes
|
|
25
|
-
|
|
26
|
-
* dependencies ([0483604](https://github.com/BlackGlory/ppx-inject/commit/048360434985276d545b9a3d6d72b3dab1857e50))
|
|
27
|
-
* dependencies ([2f0ee5b](https://github.com/BlackGlory/ppx-inject/commit/2f0ee5b4541c4355f754dfd48cda4fafb62eb255))
|
|
28
|
-
|
|
29
|
-
## [0.3.0](https://github.com/BlackGlory/ppx-inject/compare/v0.2.8...v0.3.0) (2021-04-10)
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
### ⚠ BREAKING CHANGES
|
|
33
|
-
|
|
34
|
-
* CLI parameters changed
|
|
35
|
-
|
|
36
|
-
### Features
|
|
37
|
-
|
|
38
|
-
* add the parameter cc ([2bdee8a](https://github.com/BlackGlory/ppx-inject/commit/2bdee8a37073ed3d1c6c9f2078bdf9aa37bdd6d7))
|
|
39
|
-
|
|
40
|
-
### [0.2.8](https://github.com/BlackGlory/ppx-inject/compare/v0.2.7...v0.2.8) (2021-02-28)
|
|
41
|
-
|
|
42
|
-
### [0.2.7](https://github.com/BlackGlory/ppx-inject/compare/v0.2.6...v0.2.7) (2021-02-25)
|
|
43
|
-
|
|
44
|
-
### [0.2.6](https://github.com/BlackGlory/ppx-inject/compare/v0.2.5...v0.2.6) (2021-02-04)
|
|
45
|
-
|
|
46
|
-
### [0.2.5](https://github.com/BlackGlory/ppx-inject/compare/v0.2.4...v0.2.5) (2021-02-04)
|
|
47
|
-
|
|
48
|
-
### [0.2.4](https://github.com/BlackGlory/ppx-inject/compare/v0.2.3...v0.2.4) (2020-12-19)
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
### Bug Fixes
|
|
52
|
-
|
|
53
|
-
* build ([e97be4b](https://github.com/BlackGlory/ppx-inject/commit/e97be4b57f256d8fb9932b8629adc8acf17f7489))
|
|
54
|
-
|
|
55
|
-
### [0.2.3](https://github.com/BlackGlory/ppx-inject/compare/v0.2.2...v0.2.3) (2020-12-19)
|