-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
225 lines (203 loc) · 7.83 KB
/
background.js
File metadata and controls
225 lines (203 loc) · 7.83 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// background.js — Season Proxy Switcher Service Worker
let currentAuthCredentials = null;
// ── Utilities ─────────────────────────────────────────
function cidrToMask(prefix) {
const mask = new Array(4).fill(0);
for (let i = 0; i < prefix; i++) {
mask[Math.floor(i / 8)] |= (128 >> (i % 8));
}
return mask.join('.');
}
// ── Message Listener ──────────────────────────────────
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === 'setProxy') {
setProxy(message.proxy).then(() => sendResponse({ success: true }));
return true; // async response
}
if (message.action === 'clearProxy') {
clearProxy().then(() => sendResponse({ success: true }));
return true;
}
});
// ── Proxy Control ─────────────────────────────────────
async function setProxy(proxyConfig) {
const { type, host, port, policyMode, domainList, username, password } = proxyConfig;
// Store auth credentials if provided
if (username) {
currentAuthCredentials = { username, password: password || '' };
} else {
currentAuthCredentials = null;
}
let scheme;
switch (type) {
case 'all': scheme = 'http'; break;
case 'http': scheme = 'http'; break;
case 'https': scheme = 'https'; break;
case 'socks4': scheme = 'socks4'; break;
case 'socks5': scheme = 'socks5'; break;
default: scheme = 'http';
}
const portNum = parseInt(port);
let config;
if (policyMode === 'whitelist') {
// Whitelist: 기본 직접 연결, 목록에 있는 도메인만 프록시 적용
let proxyStr;
if (type === 'socks4' || type === 'socks5') {
proxyStr = `SOCKS ${host}:${portNum}`;
} else {
proxyStr = `PROXY ${host}:${portNum}`;
}
const conditions = domainList.map(pattern => {
// CIDR notation: e.g. 192.168.0.0/24, 172.16.0.0/12
const cidrMatch = pattern.match(/^(\d+\.\d+\.\d+\.\d+)\/(\d+)$/);
if (cidrMatch) {
const ip = cidrMatch[1];
const prefix = parseInt(cidrMatch[2]);
const mask = cidrToMask(prefix);
return `isInNet(dnsResolve(host), "${ip}", "${mask}")`;
}
// Plain IP address
if (/^\d+\.\d+\.\d+\.\d+$/.test(pattern)) {
return `host === "${pattern}"`;
}
// Wildcard domain: *.example.com
if (pattern.startsWith('*.')) {
const domain = pattern.slice(2);
return `dnsDomainIs(host, ".${domain}") || dnsDomainIs(host, "${domain}")`;
}
return `dnsDomainIs(host, "${pattern}")`;
});
const pacScript = `
function FindProxyForURL(url, host) {
if (${conditions.length > 0 ? conditions.map(c => `(${c})`).join(' || ') : 'false'}) {
return "${proxyStr}";
}
return "DIRECT";
}
`;
config = {
mode: 'pac_script',
pacScript: {
data: pacScript,
},
};
} else if (type === 'all') {
// Blacklist + 일괄 적용: 모든 프로토콜에 동일한 프록시 적용
config = {
mode: 'fixed_servers',
rules: {
singleProxy: {
scheme: 'http',
host: host,
port: portNum,
},
bypassList: domainList.length > 0 ? domainList : ['localhost', '127.0.0.1'],
},
};
} else {
// Blacklist + 개별 프로토콜: 특정 프로토콜에만 프록시 적용
const proxyEntry = { scheme, host, port: portNum };
const rules = { bypassList: domainList.length > 0 ? domainList : ['localhost', '127.0.0.1'] };
if (type === 'http') {
rules.proxyForHttp = proxyEntry;
} else if (type === 'https') {
rules.proxyForHttps = proxyEntry;
} else if (type === 'socks4' || type === 'socks5') {
rules.fallbackProxy = proxyEntry;
}
config = {
mode: 'fixed_servers',
rules,
};
}
return new Promise((resolve, reject) => {
chrome.proxy.settings.set(
{ value: config, scope: 'regular' },
() => {
if (chrome.runtime.lastError) {
console.log('Proxy set error:', chrome.runtime.lastError);
reject(chrome.runtime.lastError);
} else {
console.log(`Proxy set: ${scheme}://${host}:${port} [${policyMode}]`);
updateBadge(true, policyMode);
resolve();
}
}
);
});
}
async function clearProxy() {
return new Promise((resolve, reject) => {
chrome.proxy.settings.clear(
{ scope: 'regular' },
() => {
if (chrome.runtime.lastError) {
console.log('Proxy clear error:', chrome.runtime.lastError);
reject(chrome.runtime.lastError);
} else {
console.log('Proxy cleared — direct connection');
currentAuthCredentials = null;
updateBadge(false);
resolve();
}
}
);
});
}
// ── Badge Indicator ───────────────────────────────────
function updateBadge(isActive, policyMode) {
if (isActive) {
const text = policyMode === 'whitelist' ? 'WL' : 'BL';
chrome.action.setBadgeText({ text });
chrome.action.setBadgeBackgroundColor({
color: policyMode === 'whitelist' ? '#805ad5' : '#48bb78',
});
} else {
chrome.action.setBadgeText({ text: '' });
}
}
// ── Startup: Restore Proxy State ──────────────────────
chrome.runtime.onStartup.addListener(restoreProxy);
chrome.runtime.onInstalled.addListener(restoreProxy);
async function restoreProxy() {
const data = await chrome.storage.local.get(['proxies', 'activeProxyId']);
const proxies = data.proxies || [];
const activeProxyId = data.activeProxyId || null;
if (activeProxyId) {
const proxy = proxies.find(p => p.id === activeProxyId);
if (proxy) {
await setProxy({
type: proxy.type,
host: proxy.host,
port: proxy.port,
username: proxy.username || '',
password: proxy.password || '',
policyMode: proxy.policyMode || 'blacklist',
domainList: proxy.domainList || [],
});
}
} else {
updateBadge(false);
}
}
// ── Error Listener ────────────────────────────────────
chrome.proxy.onProxyError.addListener((details) => {
console.log('Proxy error:', details);
});
// ── Auth Listener ─────────────────────────────────────
chrome.webRequest.onAuthRequired.addListener(
(details, asyncCallback) => {
if (currentAuthCredentials && details.isProxy) {
asyncCallback({
authCredentials: {
username: currentAuthCredentials.username,
password: currentAuthCredentials.password,
},
});
} else {
asyncCallback();
}
},
{ urls: ['<all_urls>'] },
['asyncBlocking']
);