Skip to content

Commit

Permalink
Initial Code commit
Browse files Browse the repository at this point in the history
  • Loading branch information
HostedDinner committed Jan 28, 2018
1 parent 76d0474 commit b5f22a1
Show file tree
Hide file tree
Showing 25 changed files with 552 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/nbproject/private
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2018 Fabian N.
Copyright (c) 2018 Fabian Neffgen

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
3 changes: 3 additions & 0 deletions nbproject/project.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
file.reference.SixIndicator-src=src
files.encoding=UTF-8
source.folder=${file.reference.SixIndicator-src}
9 changes: 9 additions & 0 deletions nbproject/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://www.netbeans.org/ns/project/1">
<type>org.netbeans.modules.web.clientproject</type>
<configuration>
<data xmlns="http://www.netbeans.org/ns/clientside-project/1">
<name>SixIndicator</name>
</data>
</configuration>
</project>
17 changes: 17 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
About
==============

SixIndicator is a WebExtension Plugin which indicates via an icon, if you are viewing the website with IPv6 or IPv4.
When clicking on the icon, more information is shown, like the number of requests per domain and if these requests were made via IPv6 or IPv4.

This plugin is heavily inspired by Ashley Baldocks [SixOrNot](http://ashley.baldock.me/sixornot/) plugin, which is not maintained anymore, since Firefox has switched to WebExtensions.
It is completly written from scratch, but aims at looking similar than SixOrNot.
Another similar plugin is [WhatIP](https://github.com/aoikeiichi/WebExt-WhatIP), but it is missing the core feature, showing enhanced information.

Missing features (compoared to SixOrNot):

* Looking up other IP addresses for used domains. Showing only the Ip address, which was used
* Indicator, if website is IPv6-Only (due to above limitation)
* Local IP addresses
* HTTP/HTTPS indicator (will probably follow)
* Proxy info (would be possible and will probably follow)
253 changes: 253 additions & 0 deletions src/background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@

// variables / consts

const debugLog = false;

const requestFilter = {
urls: ["<all_urls>"]
};

const IPVERSIONS = {
IPV4: 'v4',
IPV6: 'v6',
UNKN: 'unknown',
CACHE: 'cache'
};

const ICONDIR = 'icons/';

let storageMap = new Map();
let activeTabId = null;
let popupConnectionPort = null;


// types

function TabStorage(){
this.hostnames = new Map();
this.main = new IpInfo('', '', '', IPVERSIONS.UNKN, false);
}

function IpInfo(url, ip, isCached){
this.url = url;
this.hostname = url !== '' ? new URL(url).hostname : '';
this.isCached = isCached;
if(ip !== null){
this.ip = ip;
this.ipVersion = getIPVersion(ip);
}else{
this.ip = '';
this.ipVersion = isCached ? IPVERSIONS.CACHE : IPVERSIONS.UNKN;
}
}

function CounterIpInfo(hostname, ip, isCached, isMain){
this.hostname = hostname;
this.isCached = isCached;
this.isMain = isMain;
if(ip !== null){
this.ip = ip;
this.ipVersion = getIPVersion(ip);
}else{
this.ip = '';
this.ipVersion = isCached ? IPVERSIONS.CACHE : IPVERSIONS.UNKN;
}
this.counter = 1;
/*this.incrementCounter = (isCached) => {
this.counter++;
if(this.isCached && !isCached){
this.isCached = false;
}
};*/
}



// functions

function updateActiveTabPageAction(){
updatePageAction(activeTabId);
}

function updatePageAction(tabId){
if(tabId !== -1){
let tabStorage = storageMap.get(tabId);
if(tabStorage !== undefined){

let title = '';
if(tabStorage.main.isCached){
title = [tabStorage.main.hostname, ' (Cached)'].join('');
}else{
title = [tabStorage.main.hostname, ' (', tabStorage.main.ip, ')'].join('');
}
let pathSVG = [ICONDIR, tabStorage.main.ipVersion, '.svg'].join('');

// send Message to information popup (if its connected at the moment)
if(popupConnectionPort !== null && tabId === activeTabId)
popupConnectionPort.postMessage({action: 'updateContent', tabStorage});

// sets the PageAction title and icon accordingly
browser.pageAction.setTitle({
tabId,
title
});
browser.pageAction.setIcon({
tabId,
path: {
'19': pathSVG,
'38': pathSVG
}
});
}

// show the icon
// if the sore was empty (e.g. new page) show the default icon
browser.pageAction.show(tabId);
}
}

/**
* Determines, if the given IP address is IPv4, Ipv6 or not determinable
* @param {String} ipAddress
* @returns {String}
*/
function getIPVersion(ipAddress){
let version = IPVERSIONS.UNKN;

if(ipAddress !== null){
if(ipAddress.indexOf(':') !== -1){
version = IPVERSIONS.IPV6;
}else if(ipAddress.indexOf('.') !== -1){
version = IPVERSIONS.IPV4;
}
}

return version;
}


// listeners

/*
* called for every request
*/
browser.webRequest.onResponseStarted.addListener((details) => {
let tabId = details.tabId;
let ip = details.ip;
let host = new URL(details.url).hostname;
let url = details.url;
let requestType = details.type;
let isCached = details.fromCache;
let isMain = requestType === 'main_frame';

// delete associated data, as we made a new main request
if(isMain){
storageMap.delete(tabId);
}


let tabStorage = storageMap.get(tabId);
if(tabStorage === undefined){
tabStorage = new TabStorage();
storageMap.set(tabId, tabStorage);
}

// check if this is the main request of this frame
// if so, remember the infos about the IP/Host
if(isMain){
let mainIpInfo = new IpInfo(url, ip, isCached);
tabStorage.main = mainIpInfo;
}


let ipsForHostname = tabStorage.hostnames.get(host);
if(ipsForHostname === undefined){
ipsForHostname = new Map();
tabStorage.hostnames.set(host, ipsForHostname);
}

let counterIpInfo = ipsForHostname.get(ip);
if(counterIpInfo === undefined){
counterIpInfo = new CounterIpInfo(host, ip, isCached, isMain);
}else{
counterIpInfo.counter++;
if(counterIpInfo.isCached && !isCached){
counterIpInfo.isCached = false;
}
//counterIpInfo.incrementCounter(isCached);
}
ipsForHostname.set(ip, counterIpInfo);

updatePageAction(tabId);

}, requestFilter);


/*
* Called, when a (new) tab gets activated
* keep showing the icon on every tab and not only on tabs wich have done at least one request
* in the case of a new tab the "?" is shown
*/
browser.tabs.onActivated.addListener((activeInfo) => {
activeTabId = activeInfo.tabId;
updatePageAction(activeInfo.tabId);
});

/**
* Called, when a tab is created.
* As we probably do not have any data about this tab, just show the icon. (unknown state)
*/
browser.tabs.onCreated.addListener((tabInfo) => {
browser.pageAction.show(tabInfo.id);
});


/*
* called when a tab is moved around
* Force showing the icon again, sometimes it gets destroyed (bug?)
*/
browser.tabs.onAttached.addListener((tabId, attachInfo) => {
browser.pageAction.show(tabId);
});


/*
* Called when a tab is updated.
*/
browser.tabs.onUpdated.addListener((tabId, changeInfo, tabInfo) => {
if(changeInfo.status !== undefined && changeInfo.status === 'complete')
browser.pageAction.show(tabId);
});


/*
* Handles the connection from our information page
* It will connect, if the user clicks the page action and will diconnect when the popup is closed
*/
browser.runtime.onConnect.addListener((port) => {
popupConnectionPort = port;
if(debugLog)
console.log('Page has connected');

popupConnectionPort.onMessage.addListener((message) => {

// dispatch message
// for example when getting somthing like message.action = "getXXX" or "requestContent"

let action = message.action;
if(action !== undefined){
switch(action){
case 'requestContent':
updateActiveTabPageAction();
break;
}
}
});


popupConnectionPort.onDisconnect.addListener((port) => {
popupConnectionPort = null;
if(debugLog)
console.log('Page has disconnected');
});
});
23 changes: 23 additions & 0 deletions src/icons/cache.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/icons/cache_19.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/icons/cache_38.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions src/icons/unknown.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/icons/unknown_19.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/icons/unknown_38.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions src/icons/v4.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/icons/v4_19.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/icons/v4_38.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions src/icons/v6.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/icons/v6_19.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/icons/v6_38.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit b5f22a1

Please sign in to comment.