-
Notifications
You must be signed in to change notification settings - Fork 53
/
worker.js
136 lines (128 loc) · 4.29 KB
/
worker.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
// url
const FREENOM = 'https://my.freenom.com'
const CLIENT_AREA = `${FREENOM}/clientarea.php`
const LOGIN_URL = `${FREENOM}/dologin.php`
const DOMAIN_STATUS_URL = `${FREENOM}/domains.php?a=renewals`
const RENEW_REFERER_URL = `${FREENOM}/domains.php?a=renewdomain`
const RENEW_DOMAIN_URL = `${FREENOM}/domains.php?submitrenewals=true`
// default headers
const headers = {
'content-type': 'application/x-www-form-urlencoded',
'user-agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/103.0.5060.134 Safari/537.36',
}
async function login() {
headers['referer'] = CLIENT_AREA
const resp = await fetch(LOGIN_URL, {
method: 'POST',
body: `username=${SECRET_USERNAME}&password=${SECRET_PASSWORD}`,
headers: headers,
redirect: 'manual',
})
const setCookie = resp.headers
.get('set-cookie')
.replace(/([Hh]ttp[Oo]nly(,|)|([Pp]ath|expires|Max-Age)=.*?;)/g, '')
.replace('WHMCSUser=deleted;', '')
.replace(/\s+/g, '')
.split(';')
.reduce((pre, cur) => {
const [k, v] = cur.split('=')
if (k && v) pre[k] = v
return pre
}, {})
const cookie = []
for (const key in setCookie) {
cookie.push(`${key}=${setCookie[key]}`)
}
return cookie.join(';')
}
async function getDomainInfo() {
const res = { token: null, domains: {} }
// request
headers['referer'] = CLIENT_AREA
const resp = await fetch(DOMAIN_STATUS_URL, { headers: headers })
const html = await resp.text()
// login check
if (/<a href="logout.php">Logout<\/a>/i.test(html) == false) {
console.error('get login status failed')
return res
}
// get page token
const tokenMatch = /name="token" value="(.*?)"/i.exec(html)
if (tokenMatch.index == -1) {
console.error('get page token failed')
return res
}
res.token = tokenMatch[1]
// get domains
for (const item of html.match(/<tr>[^&]+&domain=.*?<\/tr>/g)) {
const domain = /<tr><td>(.*?)<\/td><td>[^<]+<\/td><td>/i.exec(item)[1]
const days = /<span[^>]+>(\d+).Days<\/span>/i.exec(item)[1]
const renewalId = /[^&]+&domain=(\d+?)"/i.exec(item)[1]
res.domains[domain] = { days, renewalId }
}
return res
}
async function renewDomains(domainInfo) {
const token = domainInfo.token
for (const domain in domainInfo.domains) {
const days = domainInfo.domains[domain].days
if (parseInt(days) < 14) {
const renewalId = domainInfo.domains[domain].renewalId
headers['referer'] = `${RENEW_REFERER_URL}&domain=${renewalId}`
const resp = await fetch(RENEW_DOMAIN_URL, {
method: 'POST',
body: `token=${token}&renewalid=${renewalId}&renewalperiod[${renewalId}]=12M&paymentmethod=credit`,
headers: headers,
})
const html = await resp.text()
console.log(
domain,
/Order Confirmation/i.test(html) ? 'Renewal Success' : 'Renewal Failed'
)
} else {
console.log(`Domain ${domain} still has ${days} days until renewal.`)
}
}
}
async function handleSchedule(scheduledDate) {
console.log('scheduled date', scheduledDate)
const cookie = await login()
console.log('cookie', cookie)
headers['cookie'] = cookie
const domainInfo = await getDomainInfo()
console.log('token', domainInfo.token)
console.log('domains', domainInfo.domains)
await renewDomains(domainInfo)
}
addEventListener('scheduled', (event) => {
event.waitUntil(handleSchedule(event.scheduledTime))
})
async function handleRequest() {
const cookie = await login()
console.log('cookie', cookie)
headers['cookie'] = cookie
const domainInfo = await getDomainInfo()
const domains = domainInfo.domains
const domainHtml = []
for (const domain in domains) {
const days = domains[domain].days
domainHtml.push(`<p>Domain ${domain} still has ${days} days until renewal.</p>`)
}
const html = `
<!DOCTYPE html>
<html>
<head><title>Freenom Renew Workers</title></head>
<body>
Project Repository:https://github.com/PencilNavigator/freenom-workers<br/>
${domainHtml.join('')}
</body>
</html>
`
return new Response(html, {
headers: { 'content-type': 'text/html; charset=utf-8' },
})
}
addEventListener('fetch', (event) => {
event.respondWith(handleRequest())
})