-
Notifications
You must be signed in to change notification settings - Fork 2
/
g_background.js
177 lines (139 loc) · 4.11 KB
/
g_background.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
const addon_name = "LAN port scan forbidder";
const default_title = addon_name;
setGlobalEnable();
//----------------------------------------------------------
#ifndef CHROME
async function onBeforeRequest(details)
#else
function onBeforeRequest(details)
#endif
{
if (
#ifndef CHROME
await is_off(details)
#else
is_off(details)
#endif
)
return;
//const method = details.method;
const targetURL = details.url;
const resourceType = details.type;
//const documentUrl = details.documentUrl;
var originUrl;
if (isFirefox)
originUrl = details.originUrl;
else if (isChrome)
originUrl = details.initiator;
const tabid = details.tabId;
if ( ! originUrl )
return;
if ( resourceType == "main_frame" )
return;
for ( unhandled of ["moz-extension:","chrome-extension:", "about:", "file:", "chrome:", "javascript:", "data:"] )
if ( targetURL.toLowerCase().startsWith(unhandled) || originUrl.toLowerCase().startsWith(unhandled) )
return;
const originHost = getUrlHostNoPort(originUrl);
const targetHost = getUrlHostNoPort(targetURL);
if (originHost == targetHost)
return;
var parsed_originHost;
var parsed_targetHost;
if ( originHost == "localhost" )
return;
if ( ipaddr.isValid( originHost ) ) // origin is ip
{
//console.log("origin is ip");
parsed_originHost = ipaddr.parse(originHost);
if ( isLan( parsed_originHost ) )
{
//console.log("origin is lan");
return;
}
}
//------- Here, origin is not LAN -------------
if ( targetHost == "localhost" )
{
showNotify(originHost, targetURL);
return {cancel: true};
}
if ( ! ipaddr.isValid(targetHost) ) // target not ip
{
//console.log("target not ip");
return ;
}
parsed_targetHost = ipaddr.parse(targetHost);
//console.log( parsed_targetHost.range() );
if ( ifBlock( parsed_targetHost ) )
{
//console.log( "in block range");
showNotify(originHost, targetURL);
return {cancel: true};
}
if ( parsed_targetHost.kind() == "ipv6" && parsed_targetHost.isIPv4MappedAddress() )
{
const targetHost_toV4_parsed= parsed_targetHost.toIPv4MappedAddress();
if ( ifBlock(targetHost_toV4_parsed) )
{
//console.log("ipv6 mapped to ipv4 in block range");
showNotify(originHost, targetURL);
return {cancel: true};
}
}
}
function isLan(parsed_ip)
{
switch ( parsed_ip.range() )
{
case "private":
case "linkLocal":
case "loopback":
case "uniqueLocal":
return true;
break;
}
return false;
}
function ifBlock(parsed_ip)
{
switch ( parsed_ip.range() )
{
case "private":
case "unspecified":
case "linkLocal":
case "multicast":
case "loopback":
case "uniqueLocal":
case "broadcast":
case "multicast":
case "carrierGradeNat":
return true;
break;
}
return false;
}
function getUrlHostNoPort(s)
{
var arr = s.split("/");
var host_full = arr[2];
var result;
if (host_full.indexOf("]:") != -1 ) // ipv6 + port
{
result = host_full.substring( host_full.indexOf("[") + 1 , host_full.indexOf("]") );
}else if ( host_full.indexOf("]") != -1 ) { // ipv6 no port
result = host_full.replace('[','').replace(']','');
}else if ( host_full.indexOf(":") != -1 ) { // ipv4 or domain + port
result = host_full.split(":")[0];
}else{ // ipv4 or domain no port
result = host_full;
}
//console.log("host_full: ", host_full, "\nresult: ", result);
return result;
}
//===debug=========
function json_pretty(obj){
return JSON.stringify(obj, null, 2);
}
function console_obj(obj){
console.log( json_pretty( obj ) );
}