-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsocksProxy.ts
148 lines (126 loc) · 5.28 KB
/
socksProxy.ts
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
import ipaddr from 'ipaddr.js';
import { RequestDetails, ProxyDetails } from './socksProxy.types';
import { getProxyPermissions } from './permissions';
import { initBrowserAction, updatedTabListener } from './browserAction';
const getGlobalProxyDetails = async (): Promise<ProxyDetails> => {
const response = await browser.storage.local.get('globalProxyDetails');
if ('globalProxyDetails' in response) {
return JSON.parse(response.globalProxyDetails);
}
return { socksEnabled: false };
};
const getHostProxyDetails = async (): Promise<ProxyDetails> => {
const { hostProxiesDetails } = await browser.storage.local.get('hostProxiesDetails');
if (hostProxiesDetails) {
const hostProxiesDetailsParsed = JSON.parse(hostProxiesDetails);
const proxiedHosts = Object.keys(hostProxiesDetailsParsed);
const activeTab = await browser.tabs.query({ active: true });
const activeTabHost = new URL(activeTab[0].url!).host;
if (
proxiedHosts.includes(activeTabHost) &&
hostProxiesDetailsParsed[activeTabHost].socksEnabled
) {
return hostProxiesDetailsParsed[activeTabHost];
}
}
return { socksEnabled: false };
};
export const getActiveProxyDetails = async () => {
const globalProxyDetails = await getGlobalProxyDetails();
const hostProxyDetails = await getHostProxyDetails();
return hostProxyDetails.socksEnabled ? hostProxyDetails : globalProxyDetails;
};
export const initProxyRequests = () => {
browser.proxy.onRequest.addListener(handleProxyRequest, { urls: ['<all_urls>'] });
};
export const initProxyListeners = async () => {
const proxyPermissionsGranted = await getProxyPermissions();
if (proxyPermissionsGranted) {
await removeProxyListeners();
await addProxyListeners();
}
};
export const cleanProxyListeners = async () => {
const proxyPermissionsGranted = await getProxyPermissions();
if (!proxyPermissionsGranted) {
await removeProxyListeners();
}
};
const addProxyListeners = async () => {
initBrowserAction();
initProxyRequests();
};
const removeProxyListeners = async () => {
browser.tabs.onUpdated.removeListener(updatedTabListener);
browser.proxy.onRequest.removeListener(handleProxyRequest);
};
// TODO decide what how to handle fallback proxy (if proxy is invalid, it will fallback to Firefox proxy if configured)
// https://bugzilla.mozilla.org/show_bug.cgi?id=1750561
const handleProxyRequest = async (details: browser.proxy._OnRequestDetails) => {
const { globalProxy } = await browser.storage.local.get('globalProxy');
const { globalProxyDetails } = await browser.storage.local.get('globalProxyDetails');
const { excludedHosts } = await browser.storage.local.get('excludedHosts');
const { hostProxies } = await browser.storage.local.get('hostProxies');
const { hostProxiesDetails } = await browser.storage.local.get('hostProxiesDetails');
const { randomProxyActive } = await browser.storage.local.get('randomProxyActive');
const globalConfigParsed = JSON.parse(globalProxy);
const globalProxyDetailsParsed: ProxyDetails = JSON.parse(globalProxyDetails);
const excludedHostsParsed: string[] = JSON.parse(excludedHosts);
const hostProxiesParsed = JSON.parse(hostProxies);
const hostProxiesDetailsParsed = JSON.parse(hostProxiesDetails);
const randomProxyActiveParsed = JSON.parse(randomProxyActive);
const proxiedHosts = Object.keys(hostProxiesParsed);
const currentHost = getCurrentHost(details);
if (excludedHostsParsed.includes(currentHost) || isLocalOrReservedIP(currentHost)) {
return { type: 'direct' };
} else if (
proxiedHosts.includes(currentHost) &&
hostProxiesDetailsParsed[currentHost].socksEnabled
) {
return hostProxiesParsed[currentHost];
} else if (randomProxyActiveParsed) {
// TODO implement random proxy
return { type: 'direct' };
} else if (globalProxyDetailsParsed.socksEnabled) {
return globalConfigParsed;
}
return { type: 'direct' };
};
const getCurrentHost = (details: RequestDetails) => {
if (details.frameAncestors && details.frameAncestors.length > 0) {
// when the request initiate from an iframe, it has a parent frame
// the host is determined from its top parent frame (frameID === 0)
const frame = details.frameAncestors.find((frame) => frame.frameId === 0);
if (frame) {
return new URL(frame.url).hostname;
}
} else if (isLocalOrReservedIP(new URL(details.url).hostname)) {
// This is to handle localhost/reserved IP ranges
return new URL(details.url).hostname;
} else if (details.documentUrl) {
// when the request comes froms a a page(top level frame),
// then the host is determined from the document URL
return new URL(details.documentUrl).hostname;
}
// When a request is initiated in the browser background,
// the host is derived from the request URL itself
return new URL(details.url).hostname;
};
export const isLocalOrReservedIP = (hostname: string) => {
if (hostname.includes('localhost')) return true;
if (!ipaddr.isValid(hostname)) return false;
try {
const addr = ipaddr.parse(hostname);
const range = addr.range();
return (
range === 'private' ||
range === 'multicast' ||
range === 'linkLocal' ||
range === 'loopback' ||
range === 'uniqueLocal'
);
} catch (e: unknown) {
console.error('Invalid IP address:', e);
return false;
}
};