-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.js
152 lines (152 loc) · 5.31 KB
/
util.js
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
/// <reference path='../typings/index.d.ts' />
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator.throw(value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments)).next());
});
};
const dns = require("dns");
const fs = require("fs");
const hostile = require('hostile');
const child_process = require('child_process');
const path = require('path');
const md5file = require('md5-file');
class Util {
static unique(arr) {
return arr.filter(function (item, pos) {
return arr.indexOf(item) == pos;
});
}
static _resolve(domain, server) {
return new Promise((resolve, reject) => {
let dservers = dns.getServers();
dns.setServers([server]);
dns.resolve4(domain, (err, add) => {
if (err)
console.log(err);
setTimeout(() => {
dns.setServers(dservers);
if (err) {
resolve([]);
}
else {
resolve(add);
}
});
});
});
}
static resolve(domain, servers) {
return __awaiter(this, void 0, void 0, function* () {
let res = [];
for (let i = 0; i < servers.length; i++) {
let tmp = yield this._resolve(domain, servers[i]);
console.log(tmp);
tmp.forEach(e => {
res.push(e);
});
}
return this.unique(res);
});
}
static _wget(domain, ip, types) {
return new Promise((resolve, reject) => {
hostile.set(ip, domain, (err) => {
if (err) {
resolve({
error: err,
ip: ip,
domain: domain
});
}
else {
let error = "";
let cmd = 'wget -m -nd -A ' + types.join(',') + ' -P ./tmp/' + domain + '@' + ip + ' ' + domain;
console.log(cmd);
child_process.exec(cmd, { timeout: 30000 }, (err, stdout, stderr) => {
hostile.remove(ip, domain, (error) => {
resolve({
error: error,
ip: ip,
domain: domain
});
});
});
}
});
});
}
static wget(domain, ip, types) {
return __awaiter(this, void 0, void 0, function* () {
for (var i = 0; i < ip.length; i++) {
yield this._wget(domain, ip[i], types);
}
});
}
static dircmp(dir1, dir2) {
let res = [];
if (dir1 == dir2)
return res;
let files = fs.readdirSync(dir1);
let sep = path.sep;
files.forEach(file => {
let name1 = [dir1, file].join(sep);
let name2 = [dir2, file].join(sep);
let stats = fs.statSync(name1);
if (stats.isFile()) {
try {
if (fs.statSync(name2).isFile()) {
if (md5file.sync(name1) != md5file.sync(name2)) {
res.push({
host: name1,
cdn: name2,
reason: 'MD5 Fail'
});
}
}
else {
res.push({
host: name1,
cdn: name2,
reason: "Can't find file"
});
}
}
catch (error) {
res.push({
host: name1,
cdn: name2,
reason: "Can't find file"
});
}
}
else if (stats.isDirectory()) {
try {
if (fs.statSync(name2).isDirectory()) {
let ret = this.dircmp(name1, name2);
ret.forEach(e => res.push(e));
}
else {
res.push({
host: name1,
cdn: name2,
reason: "Can't find directory"
});
}
}
catch (error) {
res.push({
host: name1,
cdn: name2,
reason: "Can't find directory"
});
}
}
});
return res;
}
}
exports.Util = Util;