-
Notifications
You must be signed in to change notification settings - Fork 40
/
server.js
268 lines (223 loc) · 6.8 KB
/
server.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import browser from './browser-api'
const GITHUB_CONFIG_URL = 'https://raw.githubusercontent.com/censortracker/ctconf/main/config.json'
const getConfigAPIEndpoints = () => {
return [
{
endpointName: 'GitHub',
endpointUrl: GITHUB_CONFIG_URL,
},
]
}
const FALLBACK_COUNTRY_CODE = 'RU'
/**
* Fetches the country code from the given GeoIP API Endpoint.
* @param geoIPServiceURL {string} API endpoint for fetching country code.
* @returns {Promise<string|*>} Resolves with the country code.
*/
const inquireCountryCode = async (geoIPServiceURL) => {
try {
const response = await fetch(geoIPServiceURL)
const { countryCode } = await response.json()
return countryCode
} catch (error) {
console.error('[GeoIP] Error on fetching country code. Using fallback.')
return FALLBACK_COUNTRY_CODE
}
}
/**
* Fetches config from the server.
* @returns {Promise<{}|*>} Resolves with the config.
*/
const fetchConfig = async () => {
const { currentRegionCode } = await browser.storage.local.get({
currentRegionCode: '',
})
for (const { endpointName, endpointUrl } of getConfigAPIEndpoints()) {
try {
const response = await fetch(endpointUrl)
if (response.ok) {
const { meta, data = {} } = await response.json()
if (data.length === 0) {
console.warn(`[Config] Skipping ${endpointName}...`)
continue
}
let countryCode = FALLBACK_COUNTRY_CODE
if (currentRegionCode) {
countryCode = currentRegionCode
} else if (meta.geoIPServiceURL) {
countryCode = await inquireCountryCode(meta.geoIPServiceURL)
}
const config = data.find((cfg) => {
return cfg.countryCode === countryCode
})
if (!config) {
await browser.storage.local.set({ unsupportedCountry: true })
}
// For debugging purposes
config.configEndpointUrl = endpointUrl
config.configEndpointSource = endpointName
await browser.storage.local.set({
localConfig: config,
backendIsIntermittent: false,
})
return config
}
console.error(
`[Config] Error on fetching config from: ${endpointName}`,
)
} catch (error) {
console.error(`[Config] Failed to fetch config from ${endpointName}: ${error}`)
}
}
return {}
}
/**
* Fetches available config to connect to the proxy server.
* @param proxyUrl {string} API endpoint for fetching proxy config.
* @returns {Promise<void>} Resolves when the config is fetched.
*/
const fetchProxy = async ({ proxyUrl } = {}) => {
if (!proxyUrl) {
console.warn('[Proxy] «proxyUrl» is not present in config.')
return
}
const { badProxies } = await browser.storage.local.get({ badProxies: [] })
console.group('[Proxy] Fetching proxy...')
try {
if (badProxies.length > 0) {
const params = new URLSearchParams()
for (const badProxy of badProxies) {
params.append('exclude', badProxy)
}
proxyUrl += `?${params.toString()}`
console.log('Excluding bad proxies:')
console.table(badProxies)
}
const response = await fetch(proxyUrl)
const {
server,
port,
pingHost,
pingPort,
fallbackReason,
} = await response.json()
const fallbackProxyInUse = !!fallbackReason
console.warn(`Status: ${response.status}`)
const proxyPingURI = `${pingHost}:${pingPort}`
const proxyServerURI = `${server}:${port}`
console.log(`Proxy server fetched: ${proxyServerURI}!`)
if (fallbackProxyInUse) {
console.warn(`Using fallback «${proxyServerURI}» for the reason: ${fallbackReason}`)
} else {
await browser.storage.local.set({ proxyIsAlive: true })
await browser.storage.local.remove([
'fallbackReason',
'fallbackProxyInUse',
'fallbackProxyError',
])
}
await browser.storage.local.set({
proxyPingURI,
proxyServerURI,
currentProxyServer: server,
fallbackReason,
fallbackProxyInUse,
proxyLastFetchTs: Date.now(),
})
} catch (error) {
console.error(
`Error on fetching proxy server: ${error}`,
)
}
console.groupEnd()
}
/**
* Fetches database of blocked websites from registry.
* @param registryUrl Registry URL.
* @param specifics Specific attributes.
* @returns {Promise<void>} Resolves when the database is fetched.
*/
const fetchRegistry = async ({ registryUrl, specifics = {} } = {}) => {
if (!registryUrl) {
console.warn('[Registry] «registryUrl» is not present in config.')
return
}
console.group('[Registry] Fetching registry...')
const apis = [{
url: registryUrl,
storageKey: 'domains',
}]
if ('cooperationRefusedORIUrl' in specifics) {
apis.push({
url: specifics.cooperationRefusedORIUrl,
storageKey: 'disseminators',
})
}
for (const { storageKey, url } of apis) {
try {
const response = await fetch(url)
const data = await response.json()
console.log(`Fetched: ${url}`)
await browser.storage.local.set({ [storageKey]: data })
} catch (error) {
console.error(`Error on fetching data from: ${url}`)
}
}
console.groupEnd()
}
/**
* Fetches the ignored domains from the server.
* @param ignoreUrl {string} API endpoint for fetching ignored domains.
* @returns {Promise<void>} Resolves when the ignored domains are fetched.
*/
const fetchIgnore = async ({ ignoreUrl } = {}) => {
if (!ignoreUrl) {
console.warn('[Ignore] «ignoreUrl» is not present in config.')
return
}
fetch(ignoreUrl)
.then((response) => response.json())
.then((domains) => {
browser.storag.local.get({ ignoredHosts: [] })
.then(({ ignoredHosts }) => {
for (const domain of domains) {
if (!ignoredHosts.includes(domain)) {
ignoredHosts.push(domain)
}
}
browser.storag.local.set({ ignoredHosts })
.then(() => {
console.log('[Ignore] Globally ignored domains fetched.')
})
})
})
.catch((error) => {
console.error(`[Ignore] Error on fetching ignored hosts: ${error}`)
})
}
export const synchronize = async ({
syncRegistry = true,
syncIgnore = true,
syncProxy = true,
} = {}) => {
console.groupCollapsed('[Server] Synchronizing config...')
const config = await fetchConfig()
if (Object.keys(config).length > 0) {
const { proxyUrl, ignoreUrl, registryUrl, specifics } = config
if (syncIgnore) {
await fetchIgnore({ ignoreUrl })
}
if (syncProxy) {
await fetchProxy({ proxyUrl })
}
if (syncRegistry) {
await fetchRegistry({ registryUrl, specifics })
}
} else {
await browser.storage.local.set({ backendIsIntermittent: true })
}
console.groupEnd()
}
export default {
synchronize,
}