forked from marcsAtSkyhunter/Capper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremoteProxy.js
105 lines (100 loc) · 3.54 KB
/
remoteProxy.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
/*
Copyright 2014 Marc Stiegler. This library is free software;
you can redistribute it and/or modify it under the terms of the GNU
Lesser General Public License (LGPL) as published by the Free Software
Foundation; either version 2.1 of the License, or (at your option) any
later version. This library is distributed in the hope that it will
be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
*/
/*global require, setTimeout */
var https = require("https");
var Q = require("q");
var url = require("url");
function log(text) {console.log(text);}
var makeProxy;
function deepProxyConvert(obj) {
"use strict";
if (!obj || typeof obj !== "object") {return obj;}
for (var key in obj) {
if (obj[key] && typeof (obj[key]) === "object" ) {
if (obj[key]["@"]) {
obj[key] = makeProxy(obj[key]);
} else {
obj[key] = deepProxyConvert(obj[key]);
}
}
}
return obj;
}
makeProxy = function(JSONkey) {
"use strict";
var keyparts = JSONkey["@"].split("#s=");
var cred = keyparts[1];
var mainUrl = keyparts[0];
var urlParts = url.parse(mainUrl);
var options = {
hostname: urlParts.hostname,
port: urlParts.port,
path: urlParts.pathname,
rejectUnauthorized: false,
method: "POST"
};
options.agent = new https.Agent(options);
function post(method, optArgs) {
var args = [];
for (var i = 1; i < arguments.length; i++) {
if (arguments[i].isProxy ) {
args.push({"@":arguments[i].webkey});
} else {args.push(arguments[i]);}
}
var vowPair = Q.defer();
var data ="";
options.path = urlParts.pathname+"?s=" + cred + "&q=" + method;
var req = https.request(options, function(res) {
//log("statusCode: ", res.statusCode);
//log("headers: ", res.headers);
res.on('data', function(d) {
//log(d);
data += d;
});
res.on("end", function() {
//log("got end msg");
try {
var ans = JSON.parse(data);
if ("=" in ans) {
vowPair.fulfill(deepProxyConvert(ans["="]));
} else if ("!" in ans) {
vowPair.reject(ans["!"]);
}else if ("@" in ans){
vowPair.fulfill(makeProxy(ans));
} else {
log("remoteProxy invalid response not capper/waterken protocol");
vowPair.reject(
"invalid response not capper/waterken protocol");
}
} catch (err) {
vowPair.reject("bad response: " + err);
log("bad remoteProxy response:" + err);
}
});
});
req.on("error", function(err){
var msg = "remoteProxy request err: " + err + " on " +
JSONkey["@"] + " " + method;
log(msg);
vowPair.reject(msg);
});
req.write(JSON.stringify(args));
req.end();
return vowPair.promise;
}
return Object.freeze({
webkey: JSONkey["@"],
isProxy: true,
post: post
});
};
function keyToProxy(keyString) {return makeProxy({"@": keyString});}
module.exports = keyToProxy;