caterpillar/console.html

212 lines
9.0 KiB
HTML
Raw Normal View History

2024-06-20 07:48:52 +00:00
<!doctype html>
<html>
<head>
<title>Caterpillar Proxy Web Console</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
2024-06-21 05:35:27 +00:00
<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">
2024-06-20 07:48:52 +00:00
<link href="https://cdnjs.cloudflare.com/ajax/libs/jquery.terminal/2.42.0/css/jquery.terminal.min.css" rel="stylesheet"/>
</head>
<body>
<h1>Caterpillar Proxy Web Console</h1>
<div id="console"></div>
2024-06-21 03:41:18 +00:00
<p><a href="https://github.com/gnh1201/caterpillar">Fork me. gnh1201/caterpillar (GitHub)</a></p>
2024-06-20 07:48:52 +00:00
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery.terminal/2.42.0/js/jquery.terminal.min.js"></script>
<script type="text/javascript">//<!--<![CDATA[
var env = {
"target": "http://localhost/",
2024-06-21 07:19:10 +00:00
"method": "",
"filename": null
2024-06-20 07:48:52 +00:00
};
2024-06-21 02:32:08 +00:00
var pretty_jsonify = function(data) {
return JSON.stringify(data, null, 4);
};
2024-06-21 07:19:10 +00:00
var download_text = function(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
2024-06-20 07:48:52 +00:00
var jsonrpc2_request = function(term, method, params) {
var requestData = {
jsonrpc: "2.0",
method: method,
params: params,
id: null
};
$.ajax({
url: env.target,
type: 'POST',
contentType: 'application/json',
2024-06-21 12:58:50 +00:00
dataType: 'text',
2024-06-20 07:48:52 +00:00
data: JSON.stringify(requestData),
beforeSend: function(xhr) {
xhr.setRequestHeader("X-User-Agent", "php-httpproxy/0.1.5 (Client; WebConsole; abuse@catswords.net)");
},
success: function(response) {
2024-06-21 12:58:50 +00:00
var responseData = {
"error": {
2024-06-21 13:21:21 +00:00
"message": "Unknown error"
2024-06-21 12:58:50 +00:00
}
};
var process_corrupted_json = function(s) {
2024-06-21 13:02:53 +00:00
// for dirty response (e.g., magic header, advertise logo)
2024-06-21 12:58:50 +00:00
try {
var start = s.indexOf('{');
var end = s.lastIndexOf('}');
if (start > -1 && end > -1 && end > start) {
responseData = JSON.parse(s.substring(start, end));
}
} catch (e) {
2024-06-21 13:21:21 +00:00
responseData.error.message = e.message
+ "\r\nRaw response data:"
+ "\r\n" + response;
2024-06-21 12:58:50 +00:00
}
};
try {
2024-06-21 13:21:21 +00:00
if (response.trim() == "") {
responseData.error.message = "Received an empty response data";
} else {
2024-06-21 12:58:50 +00:00
responseData = JSON.parse(response);
}
} catch (e) {
responseData.error.message = e.message;
process_corrupted_json(response);
}
2024-06-21 07:19:10 +00:00
var text = "";
2024-06-21 12:58:50 +00:00
if ("error" in responseData) {
text = responseData.error.message;
2024-06-20 08:01:32 +00:00
} else {
2024-06-21 12:58:50 +00:00
if (typeof responseData.result.data === "object") {
text = pretty_jsonify(responseData.result.data);
2024-06-21 03:41:18 +00:00
} else {
2024-06-21 12:58:50 +00:00
text = responseData.result.data;
2024-06-21 03:41:18 +00:00
}
2024-06-20 08:01:32 +00:00
}
2024-06-21 07:19:10 +00:00
term.echo(text);
// save as a file
if (env.filename != null) {
download_text(env.filename, text);
}
2024-06-20 07:48:52 +00:00
},
error: function(xhr, status, error) {
term.echo(error);
}
});
};
jQuery(function($, undefined) {
$('#console').terminal({
set: function(k, v) {
2024-06-21 02:24:18 +00:00
if (k == "env") {
this.echo("env is the reserved word");
return;
}
2024-06-21 07:19:10 +00:00
env[k] = v || null;
2024-06-20 10:05:28 +00:00
if (k == "method") {
this.set_prompt('method([[b;red;black]' + env.method + '])> ');
2024-06-21 03:41:18 +00:00
if (env.method == "relay_mysql_query") {
var _env = {
"mysql_hostname": "localhost",
"mysql_username": "root",
"mysql_password": "",
"mysql_database": "mysql",
"mysql_port": "3306",
"mysql_charset": "utf8"
};
for (k in _env) {
if (!(k in env)) {
env[k] = _env[k];
}
}
}
2024-06-20 10:05:28 +00:00
}
2024-06-20 07:48:52 +00:00
},
show: function(k) {
2024-06-21 02:24:18 +00:00
var v = env[k];
if (typeof env[k] === "object") {
2024-06-21 02:32:08 +00:00
this.echo(pretty_jsonify(v));
2024-06-21 02:24:18 +00:00
} else if (k == "env") {
2024-06-21 02:32:08 +00:00
this.echo(pretty_jsonify(env));
2024-06-21 02:24:18 +00:00
} else {
this.echo(v);
2024-06-20 07:48:52 +00:00
}
},
do: function(...args) {
if (env.method == "") {
this.echo("Please set a method");
return;
}
2024-06-21 03:41:18 +00:00
// method(relay_invoke_method)
2024-06-20 07:48:52 +00:00
if (env.method == "relay_invoke_method") {
if (args.length < 1) {
this.echo("Please set a callback");
return;
}
jsonrpc2_request(this, env.method, {
"callback": args[0],
"args": args.slice(1)
});
return;
}
2024-06-21 03:41:18 +00:00
// method(relay_dns_get_record)
if (env.method == "relay_dns_get_record") {
if (args.length < 1) {
this.echo("Please set a hostname");
return;
}
jsonrpc2_request(this, env.method, {
"hostname": args[0]
});
return;
}
// method(relay_mysql_query)
if (env.method == "relay_mysql_query") {
var _this = this;
this.read("Enter MySQL query:\r\n", function(query) {
jsonrpc2_request(_this, env.method, {
"hostname": env.mysql_hostname,
"username": env.mysql_username,
"password": env.mysql_password,
"database": env.mysql_database,
"port": env.mysql_port,
"charset": env.mysql_charset,
"query": query
});
});
return;
}
// method(*)
2024-06-20 07:48:52 +00:00
jsonrpc2_request(this, env.method, {});
}
}, {
height: 480,
width: 640,
prompt: '> ',
checkArity: false
});
});
//]]>--></script>
</body>
</html>