Ready use the JSON-RPC 2.0 based stateless proxy

This commit is contained in:
Namhyeon Go 2024-11-23 21:26:04 +09:00
parent 3fa72f86d6
commit 43b5781cbd
2 changed files with 165 additions and 150 deletions

View File

@ -478,14 +478,22 @@ var HTTPObject = function(engine) {
return (this.proxy.userAgent != null ? this.proxy.userAgent : this.userAgent);
};
this.serialize = function() {
this.serialize = function(url) {
var data = "";
if (this.isJSONRequest() && typeof this.requestBody === "object") {
return JSON.stringify(this.requestBody);
data = JSON.stringify(this.requestBody);
} else if (typeof this.requestBody === "object") {
return this.serializeURL(this.requestBody);
data = this.serializeURL(this.requestBody);
} else {
return this.evaluate(this.requestBody);
data = this.evaluate(this.requestBody);
}
if (this.proxy.type == "stateless-jsonrpc2") {
// todo
}
return data;
};
this.serializeURL = function(params) {
@ -506,21 +514,21 @@ var HTTPObject = function(engine) {
// Bind parameters
if (Object.keys(this.parameters).length > 0) {
// Type 2
var parameters = {};
var params = {};
for (var k in this.parameters) {
if (url.indexOf(':' + k) > -1) {
url = url.replace(':' + k, this.evaluate(this.parameters[k]));
} else {
parameters[k] = this.evaluate(this.parameters[k]);
params[k] = this.evaluate(this.parameters[k]);
}
}
// Type 1
if (Object.keys(parameters).length > 0) {
if (Object.keys(params).length > 0) {
if (url.indexOf('?') > -1) {
url += '&' + this.serializeURL(parameters);
url += '&' + this.serializeURL(params);
} else {
url += '?' + this.serializeURL(parameters);
url += '?' + this.serializeURL(params);
}
}
}
@ -620,6 +628,16 @@ var HTTPObject = function(engine) {
var responseText = null;
var debuggingText = null;
// check exists the opened states
if (this.states.length == 0) {
console.error("No available states");
return;
}
// get opened URL from last states
var state = this.states[this.states.length - 1];
var url = this.getProxiedUrl(state.url);
// [lib/http] cURL error with non-escaped ampersand on Command Prompt #103
var replaceAndExcludeCaretAnd = function(inputString) {
var result = "";
@ -660,7 +678,7 @@ var HTTPObject = function(engine) {
break;
default:
this._interface.send(this.serialize());
this._interface.send(this.serialize(url));
}
// Waiting a response
@ -669,148 +687,144 @@ var HTTPObject = function(engine) {
// Get response text
responseText = this._interface.responseText;
} else if (this.engine == "CURL") {
if (this.states.length > 0) {
// Make CURL context
var state = this.states[this.states.length - 1];
var cmd = [];
var url = this.getProxiedUrl(state.url);
// Build the cURL command line context
var cmd = [];
if (this.isDebugging) {
cmd.push("-v");
}
if (this.isCompressedResponse) {
cmd.push("--compressed");
}
if (this.isFollowRedirect) {
cmd.push("-L");
}
cmd.push("-X");
cmd.push(this.getMethod());
if (Object.keys(this.headers).length > 0) {
for (var key in this.headers) {
var value = this.evaluate(this.headers[key]);
if (value != null) {
cmd.push("-H");
cmd.push(key + ": " + value);
}
}
}
if (this.cookie != null) {
cmd.push("-b");
cmd.push(this.evaluate(this.cookie));
}
if (this.isLoggingCookie) {
cmd.push("-c");
cmd.push(this.storedCookie.path);
}
cmd.push("-A");
cmd.push(this.evaluate(this.getUserAgent()));
// --connect-timeout
if (this.connectTimeout > 0) {
cmd.push("--connect-timeout");
cmd.push(this.connectTimeout);
}
// --max-time
if (this.maxTime > 0) {
cmd.push("--max-time");
cmd.push(this.maxTime);
}
// Add the credential parameters
switch (this.credential.method.toUpperCase()) {
case "BASIC":
cmd.push("-u");
cmd.push(this.credential.username + ":" + this.credential.password);
break;
}
// Add the request body if this is not GET method
if (this.getMethod() !== "GET") {
cmd.push("-d");
cmd.push(replaceAndExcludeCaretAnd(this.serialize()));
}
// Add proxy: <[protocol://][user:password@]proxyhost[:port]>
if (this.proxy != null && this.proxy.enabled && this.proxy.type == "stateful") {
cmd.push("-x");
if (this.proxy.credential != null) {
cmd.push([
this.proxy.protocol,
"://",
this.proxy.credential.username,
":",
this.proxy.credential.password,
"@",
this.proxy.host,
":",
this.proxy.port
].join(""));
} else {
cmd.push([
this.proxy.protocol,
"://",
this.proxy.host,
":",
this.proxy.port
].join(""));
}
}
// if it is download
if (this.saveTo != null) {
cmd.push("-o");
cmd.push(this.saveTo);
}
// If not verify SSL
if (!this.isVerifySSL) {
if (this.proxy.enabled) {
cmd.push("--proxy-insecure");
}
cmd.push("-k");
cmd.push("--ssl-no-revoke");
}
// if the count of this.curlOptions greater than 0
if (this.curlOptions.length > 0) {
cmd = cmd.concat(this.curlOptions);
}
// set the URL
cmd.push(url);
// Get response text
responseText = this._interface.setCharset(this.charset).exec(cmd);
// Reload a cookie in the pipe
if (this.isLoggingCookie) {
this.storedCookie.reload();
}
// If enabled the charset(text encoding) detector
if (this.isUseDetectCharset) {
var detectedCharset = this.detectCharset(responseText);
console.log("Detected charset:", detectedCharset);
if (detectedCharset != null && this.charset != detectedCharset) {
var _interface = SHELL.create();
responseText = _interface.setCharset(detectedCharset).exec(cmd);
debuggingText = _interface.stderr.read();
}
}
// Get debuging text
debuggingText = this._interface.stderr.read();
if (this.isDebugging) {
cmd.push("-v");
}
if (this.isCompressedResponse) {
cmd.push("--compressed");
}
if (this.isFollowRedirect) {
cmd.push("-L");
}
cmd.push("-X");
cmd.push(this.getMethod());
if (Object.keys(this.headers).length > 0) {
for (var key in this.headers) {
var value = this.evaluate(this.headers[key]);
if (value != null) {
cmd.push("-H");
cmd.push(key + ": " + value);
}
}
}
if (this.cookie != null) {
cmd.push("-b");
cmd.push(this.evaluate(this.cookie));
}
if (this.isLoggingCookie) {
cmd.push("-c");
cmd.push(this.storedCookie.path);
}
cmd.push("-A");
cmd.push(this.evaluate(this.getUserAgent()));
// --connect-timeout
if (this.connectTimeout > 0) {
cmd.push("--connect-timeout");
cmd.push(this.connectTimeout);
}
// --max-time
if (this.maxTime > 0) {
cmd.push("--max-time");
cmd.push(this.maxTime);
}
// Add the credential parameters
switch (this.credential.method.toUpperCase()) {
case "BASIC":
cmd.push("-u");
cmd.push(this.credential.username + ":" + this.credential.password);
break;
}
// Add the request body if this is not GET method
if (this.getMethod() !== "GET") {
cmd.push("-d");
cmd.push(replaceAndExcludeCaretAnd(this.serialize(url)));
}
// Add proxy: <[protocol://][user:password@]proxyhost[:port]>
if (this.proxy != null && this.proxy.enabled && this.proxy.type == "stateful") {
cmd.push("-x");
if (this.proxy.credential != null) {
cmd.push([
this.proxy.protocol,
"://",
this.proxy.credential.username,
":",
this.proxy.credential.password,
"@",
this.proxy.host,
":",
this.proxy.port
].join(""));
} else {
cmd.push([
this.proxy.protocol,
"://",
this.proxy.host,
":",
this.proxy.port
].join(""));
}
}
// if it is download
if (this.saveTo != null) {
cmd.push("-o");
cmd.push(this.saveTo);
}
// If not verify SSL
if (!this.isVerifySSL) {
if (this.proxy.enabled) {
cmd.push("--proxy-insecure");
}
cmd.push("-k");
cmd.push("--ssl-no-revoke");
}
// if the count of this.curlOptions greater than 0
if (this.curlOptions.length > 0) {
cmd = cmd.concat(this.curlOptions);
}
// set the URL
cmd.push(url);
// Get response text
responseText = this._interface.setCharset(this.charset).exec(cmd);
// Reload a cookie in the pipe
if (this.isLoggingCookie) {
this.storedCookie.reload();
}
// If enabled the charset(text encoding) detector
if (this.isUseDetectCharset) {
var detectedCharset = this.detectCharset(responseText);
console.log("Detected charset:", detectedCharset);
if (detectedCharset != null && this.charset != detectedCharset) {
var _interface = SHELL.create();
responseText = _interface.setCharset(detectedCharset).exec(cmd);
debuggingText = _interface.stderr.read();
}
}
// Get debuging text
debuggingText = this._interface.stderr.read();
// clear manually
this._interface.clear();

View File

@ -11,6 +11,7 @@ function JsonRpc2(url) {
this.setUserAgent = function(agent) {
this.userAgent = agent;
};
this.invoke = function(method, params, id) {
var result;
var response = HTTP.create("MSXML")