Skip to content

Instantly share code, notes, and snippets.

@dpsk
Created August 13, 2019 12:39
Show Gist options
  • Save dpsk/56674341b307aa2cef8c047dff1aeecf to your computer and use it in GitHub Desktop.
Save dpsk/56674341b307aa2cef8c047dff1aeecf to your computer and use it in GitHub Desktop.
Gvirabi = {
obfuscateHost: function(host) {
var obfuscated = '';
for (var i = 0; i < host.length; i++) {
obfuscated += host[i] + "g";
}
return obfuscated;
},
getCookieObj: function(obj) {
if (obj != document) {
return obj;
} else {
return Gvirabi;
}
},
get cookie() {
return document.cookie;
},
set cookie(val) {
if (Gvirabi.options.indexOf('co') !== -1) {
return "";
}
var domainReplaced = val.replace(/domain=(.*?)(;|$)/i,
function(match, domain, suffix) {
if (Gvirabi.options.indexOf('ob') !== -1) {
domain = Gvirabi.obfuscateHost(domain);
}
return "domain=" + domain + Gvirabi.gvirabedSettings + Gvirabi.configDomain + suffix;
});
// TODO: Move gvirabi to secure server and remove this line
var secureRemoved = domainReplaced.replace(/secure(;|$)/i, '');
//var httponlyRemoved = secureRemoved.replace(/httponly(;|$)/i,'');
return document.cookie = secureRemoved;
},
getDomainObj: function(obj){
if(obj != document){
return obj;
} else {
return Gvirabi;
}
},
get domain() {
return document.domain;
},
set domain(val) {
// Ignoring document.domain change
// TODO: Detect pattern of domain change and act.
},
showProxyNotifierIfRequired: function() {
if (self != top) {
// We are in iframe
return;
}
if (document.cookie.indexOf('user_knows_about_proxy') !== -1) {
return;
}
var msgFrame = document.createElement('iframe');
msgFrame.id = 'proxy_notifier';
msgFrame.scrolling = 'no';
msgFrame.width = '100%';
msgFrame.height = '100%';
msgFrame.frameBorder = '0';
msgFrame.src = '/gvirabi-notifier.html?2';
msgFrame.style.margin = '0';
msgFrame.style.padding = '0';
msgFrame.style.top = '0';
msgFrame.style.left = '0';
msgFrame.style.position = 'fixed';
msgFrame.style.zIndex = '2147483647';
document.body.appendChild(msgFrame);
},
removeProxyNotifier: function(forAMonth) {
var expiresAppendate = '';
if (forAMonth) {
// Expire after 30 days
var today = new Date();
var expire = new Date();
expire.setTime(today.getTime() + 3600000 * 24 * 30);
expiresAppendate = '; expires=' + expire.toGMTString();
}
document.cookie = 'user_knows_about_proxy=1; path=/; domain=.' + Gvirabi.cookieDomain + expiresAppendate;
var iframe = document.getElementById('proxy_notifier');
if (iframe) {
document.body.removeChild(iframe);
}
},
pageLoaded: function(){
setTimeout(function(){
Gvirabi.fixAllUrls();
}, 500);
var xhr = new XMLHttpRequest();
xhr.open("GET", "/gvirabi-log-page-load");
xhr.send();
},
logAdSlotLoad: function(provider, size){
var xhr = new XMLHttpRequest();
xhr.open("GET", "/gvirabi-log-ad-slot-load?provider="+provider+"&size="+size);
xhr.send();
},
readLocalCookie: function(name){
cookieObj={};
Gvirabi.cookie.split('; ').forEach(
function(c){
var s=c.split('=');
cookieObj[s[0]]=s[1];
});
return cookieObj[name];
},
logPopunder:function(provider){
var xhr = new XMLHttpRequest();
xhr.open("GET", "/gvirabi-log-popunder-load?provider="+provider);
xhr.send();
},
fixAllUrls:function(){
var forms=document.getElementsByTagName('form')
for(var f in forms){
forms[f].action=Gvirabi.fixUrl(forms[f].action)
}
},
fixUrl:function(url){
if(document.location.protocol!=='https:' && /^https:/i.test(url))
return url.replace('https', 'http')
return url
},
httpRequest : function(url, method, data, cb) {
var xhr = new XMLHttpRequest();
if(cb){
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
cb(xhr.status, xhr.responseText);
}
};
}
xhr.origOpen(method, url, true);
if(method==="POST") xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.timeout = 1000*60;
xhr.send(data);
return xhr;
},
parseUrl : function(url) {
var parser = document.createElement('a');
parser.href = url;
return {
protocol : parser.protocol,
hostname : parser.hostname,
port : parser.port,
pathname : parser.pathname,
search : parser.search,
hash : parser.hash,
host : parser.host,
}
},
formatUrl : function(splitUrl) {
var formatted = splitUrl.protocol + '//' +
splitUrl.hostname
if(splitUrl.port) formatted += ':' + splitUrl.port
formatted += splitUrl.pathname +
splitUrl.search +
splitUrl.hash
return formatted
},
createOptionsString : function() {
if(Gvirabi.options.length === 0) return ''
return '.'+['opt'].concat(Gvirabi.options).join('-')
},
engvirabUrl : function(url){
if(url.match(/^\/\//)){
if(Gvirabi.gvirabedSettings.match(/\.https/))
url = 'https:'+url;
else url = 'http:'+url;
}
var splitUrl = Gvirabi.parseUrl(url)
if(splitUrl.host){
// Obfuscations
if(Gvirabi.options.indexOf('ob1')!==-1){
splitUrl.host = exports.obfuscateHost(splitUrl.host);
}
var protocolAppendate = "";
if(splitUrl.protocol === 'https:')
protocolAppendate = ".https"
var httpsRequest = location.protocol === 'https:'
splitUrl.protocol=httpsRequest?'https:':'http:';
var portAppendate = "";
if(splitUrl.port){
portAppendate = "."+splitUrl.port;
}
splitUrl.hostname = splitUrl.host+Gvirabi.createOptionsString()
+protocolAppendate+portAppendate+'.'+Gvirabi.configDomain;
splitUrl.port = location.port
var formattedUrl = Gvirabi.formatUrl(splitUrl),
lastSlashUrl;
if(splitUrl.path === '/' && originalUrl[originalUrl.length-1] !== '/')
lastSlashUrl = formattedUrl.substr(0, formattedUrl.length-1)
else
lastSlashUrl = formattedUrl
return lastSlashUrl;
}
return originalUrl;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment