-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
172 lines (122 loc) · 4.59 KB
/
app.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
const
Browser = require('./src/browser'),
config = require('./config'),
definitions = require('./src/definitions'),
schedule = require('node-schedule'),
{ timeout } = require('./src/helpers'),
logger = new (require('./src/logger'))({
file_directory: `${__dirname}/logs`
})
// Main process
const init = async () => {
logger.info("Starting renew process")
// Main script
const browser = new Browser()
logger.info("Creating browser page")
const page = await browser.create_page()
// Navigate to noip login page
logger.info("Navigating to NoIp login page")
await page.navigate(definitions.urls.login)
// Login
logger.info("Filling in credentials")
await page.fill_input_field(definitions.selectors.username, config.no_ip.username)
await page.fill_input_field(definitions.selectors.password, config.no_ip.password)
logger.info("Logging in")
await page.click_button(definitions.selectors.login_button)
// TODO: Handle failed logins
// Caused by:
// Bad credentials
// Too many login attempts
logger.info("Navigating to hostnames page")
await page.navigate(definitions.urls.hostnames, 2000)
if (config.no_ip.update_hostnames) {
logger.info("Updating hostnames")
await update_hostnames(page)
}
if (config.no_ip.confirm_hostnames) {
logger.info("Confirming hostnames")
await confirm_hostnames(page)
}
await timeout(5000)
logger.info("Renew process successful")
await browser.close()
}
// Clicks the confirm hostname button
const confirm_hostnames = async (page, throw_error=false) => {
// Confirm element exists
if (!await page.element_exists(definitions.selectors.hostname_table)) {
const error_message = `Could not find hostname table`
logger.error(error_message)
throw new Error(error_message)
}
try {
await page.source.evaluate( async (selectors) => {
const wait = async (timeout) => {
return new Promise((resolve) => {
setTimeout(resolve, timeout)
})
}
const elements_to_click = document.querySelectorAll(selectors.confirm_hostname_buttons)
for (let element_counter = 0; element_counter < elements_to_click.length; element_counter++) {
const element_to_click = elements_to_click[element_counter]
await wait(5000)
// Click the confirm hostname button
element_to_click.click()
}
}, definitions.selectors)
return true
} catch(err) {
const error_message = `Unknown error`
logger.error(error_message)
logger.error(err)
if (throw_error) {
throw new Error(error_message)
}
return false
}
}
// Opens each hostname modal and then clicks the update hostname button
const update_hostnames = async (page, throw_error=false) => {
// Confirm element exists
if (!await page.element_exists(definitions.selectors.hostname_table)) {
const error_message = `Could not find hostname table`
logger.error(error_message)
throw new Error(error_message)
}
try {
await page.source.evaluate( async (selectors) => {
const wait = async (timeout) => {
return new Promise((resolve) => {
setTimeout(resolve, timeout)
})
}
const elements = document.querySelectorAll(selectors.hostname_links)
for (let element_counter = 0; element_counter < elements.length; element_counter++) {
const element = elements[element_counter]
await wait(5000)
// Open the update modal
element.click()
await wait(1000)
// Select and click the update hostname button
const update_hostname_button = document.querySelector(selectors.update_hostname_button)
update_hostname_button.click()
}
}, definitions.selectors)
return true
} catch(err) {
const error_message = `Unknown error`
logger.error(error_message)
logger.error(err)
if (throw_error) {
throw new Error(error_message)
}
return false
}
}
if (config.debug_mode) {
logger.info("===================")
logger.info("Debug session start")
logger.info("===================")
init()
}
const job = schedule.scheduleJob(config.cron_string, init)