-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
205 lines (188 loc) · 4.82 KB
/
index.ts
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
import { Got } from 'got'
import cheerio from 'cheerio'
import qs from 'qs'
import * as defaults from '../defaults.js'
import { parseDatetime } from '../utils.js'
// export all
export * as firmware from './firmware.js'
export * as network from './network.js'
/**
* Get briefing metadata from main page
*
* @param instance The got instance to use
* @returns Publicated router metadata you can see on main page
*/
export const getBriefing = async (
instance: Got = defaults.instance
) => {
const res = await instance
.get(defaults.URIs.mainView, {
headers: {
// Referer check is present on the system
referer: defaults.URIs.loginHandle
}
})
.text()
const $ = cheerio.load(res, {
lowerCaseTags: true
})
const ip = $('span.item_text:contains("IP -")').text()
const [
modeText,
connectionText,
address
] = ip
.split('-')
.map(fragment => fragment.trim())
const version = $('span.item_text:contains("Version")')
.text()
.replace('Version ', '')
return {
ip: {
address,
isDynamic: modeText === '동적 IP',
isConnected: connectionText === '연결됨'
},
version
}
}
/* eslint-disable no-unused-vars */
export enum EWlanBandType {
W2 = '2.4GHz',
W5 = '5GHz'
}
/* eslint-enable no-unused-vars */
export interface IWlanStatus {
bandType: EWlanBandType
name: string
password: string
isOnline: boolean
isEncrypted: boolean
isExtending: boolean
}
/**
* Reflect information from the view to wireless LAN status object
*
* @param bandType The type of wireless band
* @param name The name of hosted AP
* @param password The password of hosted AP
* @param statusText The status text from the view
* @param extendStatusText The extending status text from the view
* @returns Reformed wireless LAN status object
*/
const getReflectedWlanFromStatus = (
bandType: EWlanBandType,
name: string,
password: string,
statusText: string,
extendStatusText: string
) => {
const result: IWlanStatus = {
bandType,
name,
password,
isOnline: statusText.includes('동작중'),
isEncrypted: statusText.includes('암호화 사용'),
isExtending: !extendStatusText.includes('중단')
}
return result
}
/**
* Get the general status of the router
*
* @param instance The got instance to use
* @returns The general information of the router
*/
export const getStatus = async (
instance: Got = defaults.instance
) => {
const res = await instance
.get(defaults.URIs.serviceView, {
headers: {
// Referer check is present on the system
referer: defaults.URIs.serviceView +
`?${qs.stringify({
tmenu: defaults.services.EServiceCategory.FRAME,
smenu: 'info'
})}`
},
searchParams: {
tmenu: defaults.services.EServiceCategory.DATA,
smenu: 'system_info_status'
}
})
.text()
const $ = cheerio.load(res)
const [
connectionText,
connectionTypeText,
externalIpAddress,
connectionTimeText,
routerIpAddress,
dhcpStatusText,
ipRangeText,
wlan5gStatusText,
wlan5gName,
wlan5gExtendStatusText,
wlan2gStatusText,
wlan2gName,
wlan2gExtendStatusText,
version,
externalManagementStatusText,
,
routerUptimeText
] = $('td.td_item')
.toArray()
.map(el => $(el)
.text()
.trim()
.replace(/[^ㄱ-ㅎ가-힣\w. ]/g, '')
)
.filter(line => line.length)
// Grep remote management port
const remoteManagementPort = Number((/(\d+)/.exec(externalManagementStatusText) || [])[0])
const isConnected = connectionText.includes('정상적으로 연결')
const isDynamic = connectionTypeText.includes('동적 IP')
const isDhcpRunning = dhcpStatusText.includes('동작 중')
const isRemoteManagementEnabled = !externalManagementStatusText.includes('설정되어 있지 않음')
// Get wireless LAN data
const wlan2g = getReflectedWlanFromStatus(
EWlanBandType.W2,
wlan2gName,
$('input#pwValue_wlan0').attr('value') ?? '',
wlan2gStatusText,
wlan2gExtendStatusText
)
const wlan5g = getReflectedWlanFromStatus(
EWlanBandType.W5,
wlan5gName,
$('input#pwValue_wlan2').attr('value') ?? '',
wlan5gStatusText,
wlan5gExtendStatusText
)
// Parse given timing texts
const connectionTime = parseDatetime(connectionTimeText)
const routerUptime = parseDatetime(routerUptimeText)
// Reform IP range
const ipRange = ipRangeText
.split(' ')
.map(item => item.trim())
.filter(item => item.length)
return {
version,
routerIpAddress,
externalIpAddress,
wlans: {
[EWlanBandType.W2]: wlan2g,
[EWlanBandType.W5]: wlan5g
},
connectionTime,
routerUptime,
ipRange,
remoteManagementPort,
isConnected,
isDynamic,
isDhcpRunning,
isRemoteManagementEnabled
}
}