-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
196 lines (178 loc) · 4.66 KB
/
utils.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
const os = require('os');
const axios = require('axios')
const md5 = require('md5-node');
const fs = require('fs')
const qs = require("qs")
Date.prototype.format = function (fmt) {
let ret;
const opt = {
"Y+": this.getFullYear().toString(),
"m+": (this.getMonth() + 1).toString(),
"d+": this.getDate().toString(),
"H+": this.getHours().toString(),
"M+": this.getMinutes().toString(),
"S+": this.getSeconds().toString()
};
for (let k in opt) {
ret = new RegExp("(" + k + ")").exec(fmt);
if (ret) {
fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
};
};
return fmt;
}
axios.interceptors.request.use(function (config) {
const headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'DNT': 1,
'Upgrade-Insecure-Requests': 1,
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
'Connection': 'keep-alive'
}
config.headers = { ...config.headers, ...headers }
return config;
}, function (error) {
return Promise.reject(error);
});
// retry configure
axios.interceptors.response.use(e => e, async function(err){
let config = err.config
config = {
...config,
retry: config.retry || 0,
timeout: config.timeout || 3000,
__retry_counter: config.__retry_counter || 0
}
if (config.__retry_counter >= config.retry){
return Promise.reject(new Error("Timeout exceeded"))
}
config.__retry_counter++;
// retry output
console.log('timeout >>> ' + config.__retry_counter);
await sleep(config.timeout)
// call again
return axios(config)
})
// axios.defaults.timeout = 3000
// update in Apri. 24
async function getRedirectUrl(url){
// const url = "http://www.baidu.com"
if (url.indexOf("http") === -1){
url = "http://" + url
}
const exp = /((\d+\.){3}\d+)+/g
let ret = null
ret = await axios.get(url, { retry: 3, timeout: 10000 })
const host = ret.request.socket._host
const { path } = ret.request
const ip = path.match(exp)
if (url !== `http://${host}`){
let res = { host, path }
if (ip){
res = {
...res,
nasip: ip[0],
wlanip: ip[1]
}
}
return res
} else {
return null
}
}
// return the md5 value calc by array element's merge
function getHash(array){
const secret = "Eshore!@#";
if (!(array instanceof Array)){
return null
}
array.push(secret)
str = array.join("")
return md5(str).toUpperCase()
}
function responseToJSON(data){
return JSON.parse(decodeURIComponent(Buffer.from(data, "base64").toString("binary")))
}
function constructLoginForm(username, password, address){
const form = {
userName: username,
userPwd: Buffer.from(password, "binary").toString("base64"),
userDynamicPwd: "",
userDynamicPwdd: "",
serviceType: "",
userurl: "",
userip: "",
basip: "",
language: "Chinese",
usermac: "null",
wlannasid: "",
wlanssid: "",
entrance: "null",
loginVerifyCode: "",
userDynamicPwddd: "",
customPageId: 100,
pwdMode: 0,
portalProxyIP: address,
portalProxyPort: 50200,
dcPwdNeedEncrypt: 1,
assignIpType: 0,
appRootUrl: `http://${address}:8080/portal/`,
manualUrl: ""
}
return qs.stringify(form)
}
// return the default network info
function getNetworkInfo() {
var interfaces = os.networkInterfaces();
for (var devName in interfaces) {
var iface = interfaces[devName];
for (var i = 0; i < iface.length; i++) {
var alias = iface[i];
if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
return alias
}
}
}
return null
}
function sleep(ms){
return new Promise(resolve => setTimeout(resolve,ms))
}
class Config{
constructor(path){
const exp = /\w+\.json/
if (typeof path !== "string" || !exp.test(path)){
throw new Error("illegal path")
}
if(fs.existsSync(path)){
this.configObj = require(path)
} else {
this.configObj = {}
}
this.path = path
}
get(){
return this.configObj
}
// load / reload
load(){
this.configObj = require(this.path)
return this.configObj
}
write(){
const str = JSON.stringify(this.configObj)
fs.writeFileSync(this.path, str)
}
}
module.exports = {
getNetworkInfo,
getRedirectUrl,
getHash,
responseToJSON,
constructLoginForm,
Config,
sleep
}