-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
83 lines (79 loc) · 4.64 KB
/
background.js
File metadata and controls
83 lines (79 loc) · 4.64 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
function typed_array_equals(a,b) {
for(var i in a) if(a[i] != b[i]) return false;
return true;
}
function proxify(proxyspecs,socks){
for(var i in proxyspecs){
(function(proxyspec){
// create listening socket
chrome.sockets.tcpServer.create({}, function(createListenInfo) {
// listen on listening socket
chrome.sockets.tcpServer.listen(createListenInfo.socketId,"127.0.0.1",proxyspec.localport,function(listenResultCode) {
// add listener on listening socket accept
chrome.sockets.tcpServer.onAccept.addListener(function(acceptInfo){
if(acceptInfo.socketId != createListenInfo.socketId) return; // listener for this accepting socket only
// create a socket to socks
chrome.sockets.tcp.create({}, function(createConnectInfo) {
// connect to socks5 tcp
chrome.sockets.tcp.connect(createConnectInfo.socketId, socks.host, socks.port,function(connectResultCode){
// send socks5 initialization : [version=5,one_authentication_method=1,no_authentication=0]
chrome.sockets.tcp.send(createConnectInfo.socketId,(new Uint8Array([5,1,0])).buffer,function(){
// add a listener which remove itself to handle server authentication response
chrome.sockets.tcp.onReceive.addListener(function receiveSocksAuth(recinfo){
if(recinfo.socketId != createConnectInfo.socketId) return; // receive on socks5 socket
chrome.sockets.tcp.onReceive.removeListener(receiveSocksAuth);
var message = new Uint8Array(recinfo.data);
if(message[0] != 5 || message[1] != 0) return; // check socks5 resp: must be [version=5,no authentication=0]
// generate&send socks5 TCP connection query : [version=5+tcpconnect=1+reserved=0+dnsaddress=3+addr_len+addr+2byte port]
var query = new ArrayBuffer(7+proxyspec.host.length);
var d8=new Uint8Array(query); d8.set([5,1,0,3,proxyspec.host.length]); // set first bytes + addr len
for(var j=0; j<proxyspec.host.length;j++) d8[j+5]=proxyspec.host.charCodeAt(j); // put addr bytes
new DataView(query,d8.length-2,2).setUint16(0,proxyspec.port); // set port uint16 big endian
chrome.sockets.tcp.send(createConnectInfo.socketId,query,function(){
// add a listener which remove itself to handle server Socks connection query response
chrome.sockets.tcp.onReceive.addListener(function receiveSocksResp(recinfo){
if(recinfo.socketId != createConnectInfo.socketId) return; // receive on socks5 socket
chrome.sockets.tcp.onReceive.removeListener(receiveSocksResp);
d8[1] = 0; // check expected response : the same as query but with a 0 meaning OK at the place of the query type
if(!typed_array_equals(new Uint8Array(recinfo.data),d8)) return;
// if all is OK, then add the final listener which transmit data both ways and unpause accept client socket
chrome.sockets.tcp.onReceive.addListener(function(recinfo){
if(recinfo.socketId == acceptInfo.clientSocketId){
chrome.sockets.tcp.send(createConnectInfo.socketId, recinfo.data,function(){});
}else if(recinfo.socketId == createConnectInfo.socketId){
chrome.sockets.tcp.send(acceptInfo.clientSocketId, recinfo.data,function(){});
}
});
chrome.sockets.tcp.setPaused(acceptInfo.clientSocketId,false, function(){});
});
});
});
});
});
});
});
});
});
})(proxyspecs[i]);
}
}
chrome.app.runtime.onLaunched.addListener(function() {
//proxify(localhostify,socks);
chrome.app.window.create('main.html', {
id: 'config_server',
//bounds: { width: 800, height: 600, left: 100, top: 100 },
minWidth: 800, minHeight: 600
}, function(win) {
win.contentWindow.addEventListener('reload',function(e){
chrome.sockets.tcp.getSockets(function(sockets){
for(var i in sockets)
chrome.sockets.tcp.close(sockets[i].socketId);
});
chrome.sockets.tcpServer.getSockets(function(sockets){
for(var i in sockets)
chrome.sockets.tcpServer.close(sockets[i].socketId);
});
proxify(e.detail.specs,e.detail.socks);
});
});
});