-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet.js
223 lines (185 loc) · 13.2 KB
/
net.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
class httpMethod {
static _httpRequestCallback(resolve, reject, error, response, data) {
if (error) {
reject(error);
} else {
resolve(Object.assign(response, { data }));
}
}
static get(option = {}) {
return new Promise((resolve, reject) => {
$httpClient.get(option, (error, response, data) => {
this._httpRequestCallback(resolve, reject, error, response, data);
});
});
}
static post(option = {}) {
return new Promise((resolve, reject) => {
$httpClient.post(option, (error, response, data) => {
this._httpRequestCallback(resolve, reject, error, response, data);
});
});
}
}
class loggerUtil {
constructor() {
this.id = randomString();
}
log(message) {
message = `[${this.id}] [ LOG ] ${message}`;
console.log(message);
}
error(message) {
message = `[${this.id}] [ERROR] ${message}`;
console.log(message);
}
}
var logger = new loggerUtil();
function randomString(e = 6) {
var t = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678",
a = t.length,
n = "";
for (i = 0; i < e; i++) n += t.charAt(Math.floor(Math.random() * a));
return n;
}
function getFlagEmoji(countryCode) {
const codePoints = countryCode
.toUpperCase()
.split('')
.map((char) => 127397 + char.charCodeAt());
return String.fromCodePoint(...codePoints);
}
function loadCarrierNames() {
return {
'466-11': '中華電信', '466-92': '中華電信',
'466-01': '遠傳電信', '466-03': '遠傳電信',
'466-97': '台灣大哥大', '466-89': '台灣之星', '466-05': 'GT',
'460-03': '中国电信', '460-05': '中国电信', '460-11': '中国电信',
'460-01': '中国联通', '460-06': '中国联通', '460-09': '中国联通',
'460-00': '中国移动', '460-02': '中国移动', '460-04': '中国移动', '460-07': '中国移动', '460-08': '中国移动',
'460-15': '中国广电', '460-20': '中移铁通',
'454-00': 'CSL', '454-02': 'CSL', '454-10': 'CSL', '454-18': 'CSL',
'454-03': '3', '454-04': '3', '454-05': '3',
'454-06': 'SMC HK', '454-15': 'SMC HK', '454-17': 'SMC HK',
'454-09': 'CMHK', '454-12': 'CMHK', '454-13': 'CMHK', '454-28': 'CMHK', '454-31': 'CMHK',
'454-16': 'csl.', '454-19': 'csl.', '454-20': 'csl.', '454-29': 'csl.',
'454-01': '中信國際電訊', '454-07': 'UNICOM HK', '454-08': 'Truphone', '454-11': 'CHKTL', '454-23': 'Lycamobile',
'440-00': 'Y!mobile', '440-10': 'docomo', '440-11': 'Rakuten', '440-20': 'SoftBank',
'440-50': ' au', '440-51': ' au', '440-52': ' au', '440-53': ' au', '440-54': ' au',
'441-00': 'WCP', '441-10': 'UQ WiMAX',
'450-03': 'SKT', '450-05': 'SKT',
'450-02': 'KT', '450-04': 'KT', '450-08': 'KT',
'450-06': 'LG U+', '450-10': 'LG U+',
'310-030': 'AT&T', '310-070': 'AT&T', '310-150': 'AT&T', '310-170': 'AT&T', '310-280': 'AT&T', '310-380': 'AT&T', '310-410': 'AT&T', '310-560': 'AT&T', '310-680': 'AT&T', '310-980': 'AT&T',
'310-160': 'T-Mobile', '310-200': 'T-Mobile', '310-210': 'T-Mobile', '310-220': 'T-Mobile', '310-230': 'T-Mobile', '310-240': 'T-Mobile', '310-250': 'T-Mobile', '310-260': 'T-Mobile', '310-270': 'T-Mobile', '310-300': 'T-Mobile', '310-310': 'T-Mobile', '310-660': 'T-Mobile', '310-800': 'T-Mobile', '311-660': 'T-Mobile', '311-882': 'T-Mobile', '311-490': 'T-Mobile', '312-530': 'T-Mobile', '311-870': 'T-Mobile', '311-880': 'T-Mobile',
'310-004': 'Verizon', '310-010': 'Verizon', '310-012': 'Verizon', '310-013': 'Verizon', '311-110': 'Verizon', '311-270': 'Verizon', '311-271': 'Verizon', '311-272': 'Verizon', '311-273': 'Verizon', '311-274': 'Verizon', '311-275': 'Verizon', '311-276': 'Verizon', '311-277': 'Verizon', '311-278': 'Verizon', '311-279': 'Verizon', '311-280': 'Verizon', '311-281': 'Verizon', '311-282': 'Verizon', '311-283': 'Verizon', '311-284': 'Verizon', '311-285': 'Verizon', '311-286': 'Verizon', '311-287': 'Verizon', '311-288': 'Verizon', '311-289': 'Verizon', '311-390': 'Verizon', '311-480': 'Verizon', '311-481': 'Verizon', '311-482': 'Verizon', '311-483': 'Verizon', '311-484': 'Verizon', '311-485': 'Verizon', '311-486': 'Verizon', '311-487': 'Verizon', '311-488': 'Verizon', '311-489': 'Verizon', '310-590': 'Verizon', '310-890': 'Verizon', '310-910': 'Verizon',
'310-120': 'Sprint',
'310-850': 'Aeris Comm. Inc.', '310-510': 'Airtel Wireless LLC', '312-090': 'Allied Wireless Communications Corporation', '310-710': 'Arctic Slope Telephone Association Cooperative Inc.', '311-440': 'Bluegrass Wireless LLC', '311-800': 'Bluegrass Wireless LLC', '311-810': 'Bluegrass Wireless LLC', '310-900': 'Cable & Communications Corp.', '311-590': 'California RSA No. 3 Limited Partnership', '311-500': 'Cambridge Telephone Company Inc.', '310-830': 'Caprock Cellular Ltd.', '312-270': 'Cellular Network Partnership LLC', '312-280': 'Cellular Network Partnership LLC', '310-360': 'Cellular Network Partnership LLC', '311-120': 'Choice Phone LLC', '310-480': 'Choice Phone LLC', '310-420': 'Cincinnati Bell Wireless LLC', '310-180': 'Cingular Wireless', '310-620': 'Coleman County Telco /Trans TX', '310-06': 'Consolidated Telcom', '310-60': 'Consolidated Telcom', '310-700': 'Cross Valliant Cellular Partnership', '312-030': 'Cross Wireless Telephone Co.', '311-140': 'Cross Wireless Telephone Co.', '312-040': 'Custer Telephone Cooperative Inc.', '310-440': 'Dobson Cellular Systems', '310-990': 'E.N.M.R. Telephone Coop.', '312-120': 'East Kentucky Network LLC', '312-130': 'East Kentucky Network LLC', '310-750': 'East Kentucky Network LLC', '310-090': 'Edge Wireless LLC', '310-610': 'Elkhart TelCo. / Epic Touch Co.', '311-311': 'Farmers', '311-460': 'Fisher Wireless Services Inc.', '311-370': 'GCI Communication Corp.', '310-430': 'GCI Communication Corp.', '310-920': 'Get Mobile Inc.', '311-340': 'Illinois Valley Cellular RSA 2 Partnership', '312-170': 'Iowa RSA No. 2 Limited Partnership', '311-410': 'Iowa RSA No. 2 Limited Partnership', '310-770': 'Iowa Wireless Services LLC', '310-650': 'Jasper', '310-870': 'Kaplan Telephone Company Inc.', '312-180': 'Keystone Wireless LLC', '310-690': 'Keystone Wireless LLC', '311-310': 'Lamar County Cellular', '310-016': 'Leap Wireless International Inc.', '310-040': 'Matanuska Tel. Assn. Inc.', '310-780': 'Message Express Co. / Airlink PCS', '311-330': 'Michigan Wireless LLC', '310-400': 'Minnesota South. Wirel. Co. / Hickory', '311-010': 'Missouri RSA No 5 Partnership', '312-010': 'Missouri RSA No 5 Partnership', '311-020': 'Missouri RSA No 5 Partnership', '312-220': 'Missouri RSA No 5 Partnership', '311-920': 'Missouri RSA No 5 Partnership', '310-350': 'Mohave Cellular LP', '310-570': 'MTPCS LLC', '310-290': 'NEP Cellcorp Inc.', '310-34': 'Nevada Wireless LLC', '310-600': 'New-Cell Inc.', '311-300': 'Nexus Communications Inc.', '310-130': 'North Carolina RSA 3 Cellular Tel. Co.', '312-230': 'North Dakota Network Company', '311-610': 'North Dakota Network Company', '310-450': 'Northeast Colorado Cellular Inc.', '311-710': 'Northeast Wireless Networks LLC', '310-011': 'Northstar', '310-670': 'Northstar', '311-420': 'Northwest Missouri Cellular Limited Partnership', '310-760': 'Panhandle Telephone Cooperative Inc.', '310-580': 'PCS ONE', '311-170': 'PetroCom', '311-670': 'Pine Belt Cellular, Inc.', '310-100': 'Plateau Telecommunications Inc.', '310-940': 'Poka Lambro Telco Ltd.', '310-500': 'Public Service Cellular Inc.', '312-160': 'RSA 1 Limited Partnership', '311-430': 'RSA 1 Limited Partnership', '311-350': 'Sagebrush Cellular Inc.', '310-46': 'SIMMETRY', '311-260': 'SLO Cellular Inc / Cellular One of San Luis', '310-320': 'Smith Bagley Inc.', '316-011': 'Southern Communications Services Inc.', '310-740': 'Telemetrix Inc.', '310-14': 'Testing', '310-860': 'Texas RSA 15B2 Limited Partnership', '311-050': 'Thumb Cellular Limited Partnership', '311-830': 'Thumb Cellular Limited Partnership', '310-460': 'TMP Corporation', '310-490': 'Triton PCS', '312-290': 'Uintah Basin Electronics Telecommunications Inc.', '311-860': 'Uintah Basin Electronics Telecommunications Inc.', '310-960': 'Uintah Basin Electronics Telecommunications Inc.', '310-020': 'Union Telephone Co.', '311-220': 'United States Cellular Corp.', '310-730': 'United States Cellular Corp.', '311-650': 'United Wireless Communications Inc.', '310-003': 'Unknown', '310-15': 'Unknown', '310-23': 'Unknown', '310-24': 'Unknown', '310-25': 'Unknown', '310-26': 'Unknown', '310-190': 'Unknown', '310-950': 'Unknown', '310-38': 'USA 3650 AT&T', '310-999': 'Various Networks', '310-520': 'VeriSign', '310-530': 'West Virginia Wireless', '310-340': 'Westlink Communications, LLC', '311-070': 'Wisconsin RSA #7 Limited Partnership', '310-390': 'Yorkville Telephone Cooperative',
'234-08': 'BT OnePhone UK', '234-10': 'O2-UK', '234-15': 'vodafone UK', '234-20': '3', '234-30': 'EE', '234-33': 'EE', '234-38': 'Virgin', '234-50': 'JT', '234-55': 'Sure', '234-58': 'Manx Telecom',
'515-01': 'Islacom', '515-02': 'Globe', '515-03': 'Smart', '515-04': 'Sun', '515-08': 'Next Mobile', '515-18': 'Cure', '515-24': 'ABS-CBN',
'452-01': 'Mobifone', '452-02': 'VinaPhone', '452-03': 'S-Fone', '452-04': 'Viettel', '452-05': 'VietNamobile', '452-06': 'E-mobile', '452-07': 'Gmobile',
};
}
function getCellularInfo() {
const radioGeneration = {
'GPRS': '2.5G',
'CDMA1x': '2.5G',
'EDGE': '2.75G',
'WCDMA': '3G',
'HSDPA': '3.5G',
'CDMAEVDORev0': '3.5G',
'CDMAEVDORevA': '3.5G',
'CDMAEVDORevB': '3.75G',
'HSUPA': '3.75G',
'eHRPD': '3.9G',
'LTE': '4G',
'NRNSA': '5G',
'NR': '5G',
};
let cellularInfo = '';
const carrierNames = loadCarrierNames();
if ($network['cellular-data']) {
const carrierId = $network['cellular-data'].carrier;
const radio = $network['cellular-data'].radio;
if ($network.wifi?.ssid == null && radio) {
cellularInfo = carrierNames[carrierId] ?
`${carrierNames[carrierId]} | ${radioGeneration[radio]} - ${radio} ` :
`Dữ liệu di động | ${radioGeneration[radio]} - ${radio}`;
}
}
return cellularInfo;
}
function getSSID() {
return $network.wifi?.ssid;
}
function getIP() {
const { v4, v6 } = $network;
let info = [];
if (!v4 && !v6) {
info = ['Mạng có thể chuyển đổi', 'Vui lòng đặt lại bảng điều khiển theo cách thủ công để cập nhật IP'];
} else {
if (v4?.primaryAddress) info.push(`IPv4: ${v4?.primaryAddress}`);
if (v6?.primaryAddress) info.push(`IPv6: ${v6?.primaryAddress}`);
if (v4?.primaryRouter && getSSID()) info.push(`Router IPv4: ${v4?.primaryRouter}`);
if (v6?.primaryRouter && getSSID()) info.push(`Router IPv6: ${v6?.primaryRouter}`);
}
info = info.join("\n");
return info + "\n";
}
function getNetworkInfo(retryTimes = 5, retryInterval = 1000) {
httpMethod.get('http://ip-api.com/json').then(response => {
if (Number(response.status) > 300) {
throw new Error(`Request error with http status code: ${response.status}\n${response.data}`);
}
const info = JSON.parse(response.data);
$done({
title: getSSID() ?? getCellularInfo(),
content:
`Địa chỉ IP:\n` +
getIP() +
`IP: ${info.query}\n` +
`Nhà cung cấp: ${info.isp}\n` +
`Vị trí: ${getFlagEmoji(info.countryCode)} | ${info.country} - ${info.city}`,
icon: getSSID() ? 'wifi' : 'simcard',
'icon-color': getSSID() ? '#005CAF' : '#F9BF45',
});
}).catch(error => {
if (String(error).startsWith("Network changed")) {
if (getSSID()) {
$network.wifi = undefined;
$network.v4 = undefined;
$network.v6 = undefined;
}
}
if (retryTimes > 0) {
logger.error(error);
logger.log(`Retry after ${retryInterval}ms`);
setTimeout(() => getNetworkInfo(--retryTimes, retryInterval), retryInterval);
} else {
logger.error(error);
$done({
title: '發生錯誤',
content: '無法獲得目前網路資訊\n請檢查網際網路狀態後重試',
icon: 'wifi.exclamationmark',
'icon-color': '#CB1B45',
});
}
});
}
(() => {
const retryTimes = 5;
const retryInterval = 1000;
const surgeMaxTimeout = 29500;
const scriptTimeout = retryTimes * 5000 + retryTimes * retryInterval;
setTimeout(() => {
logger.log("Script timeout");
$done({
title: "Yêu cầu đã hết thời gian",
content: "連線請求逾時\n請檢查網際網路狀態後重試",
icon: 'wifi.exclamationmark',
'icon-color': '#CB1B45',
});
}, scriptTimeout > surgeMaxTimeout ? surgeMaxTimeout : scriptTimeout);
logger.log("Script start");
getNetworkInfo(retryTimes, retryInterval);
})();