runet-censorship-bypass/extensions/chromium/minimalistic-pac-setter/extnesion/sync-pac-script-with-antizapret.js

99 lines
2.1 KiB
JavaScript
Raw Normal View History

2015-12-07 13:53:47 +03:00
'use strict';
/*
Task 1. Gets IP for host proxy.antizapret.prostovpn.org with dns-lg.com.
This IP is used in block-informer to inform user when proxy is ON.
Task 2. Downloads PAC proxy script from Antizapret and sets it in Chromium settings.
Task 3. Schedules tasks 1 & 2 for every 2 hours.
*/
2015-12-07 13:53:47 +03:00
var antizapret = {
pacUrl: 'http://antizapret.prostovpn.org/proxy.pac',
proxyHost: 'proxy.antizapret.prostovpn.org',
proxyIp: '195.154.110.37',
};
function httpGet(url, cb) {
var cb = cb || (() => {});
var req = new XMLHttpRequest();
var ifAsync = true;
req.open('GET', url, ifAsync);
req.onload = event => {
if (req.status === 404)
return cb(event);
return cb(null, req.responseText)
};
req.onerror = cb;
req.send();
}
function getIpForHost(host, cb) {
httpGet(
'http://www.dns-lg.com/google1/'+host+'/A',
cb
2015-12-07 13:53:47 +03:00
);
}
2015-12-07 13:53:47 +03:00
function updateAntizapretProxyIp() {
getIpForHost(
antizapret.proxyHost,
(err, res) => {
if (err)
console.log(err)
else
antizapret.proxyIp = JSON.parse(res).answer[0].rdata;
}
);
}
function setPacScriptFromAntizapret() {
httpGet(
2015-12-07 13:53:47 +03:00
antizapret.pacUrl,
(err, res) => {
if (err)
return;
chrome.proxy.settings.clear({}, () => {
var config = {
mode: 'pac_script',
pacScript: {
mandatory: false,
data: res
}
};
chrome.proxy.settings.set( {value: config} );
});
}
);
}
2015-12-07 13:53:47 +03:00
function syncWithAntizapret() {
updateAntizapretProxyIp();
setPacScriptFromAntizapret();
2015-12-07 13:53:47 +03:00
}
chrome.runtime.onInstalled.addListener( details => {
switch(details.reason) {
case 'install':
case 'update':
syncWithAntizapret();
2015-12-07 13:53:47 +03:00
var reason = 'Периодичное обновление PAC-скрипта Антизапрет';
chrome.alarms.onAlarm.addListener(
alarm => {
if (alarm.name === reason) {
syncWithAntizapret();
}
2015-12-07 13:53:47 +03:00
}
);
chrome.alarms.create(reason, {
periodInMinutes: 2*60
});
}
});