enforces signed command to talk to the bot over http post
This commit is contained in:
parent
6b8348083f
commit
00473b434d
1 changed files with 42 additions and 26 deletions
|
|
@ -25,7 +25,6 @@ namespace BetterSecondBot.HttpServer
|
||||||
protected HTTPCommandsInterfaceGet get_controler;
|
protected HTTPCommandsInterfaceGet get_controler;
|
||||||
protected HTTPCommandsInterfacePost post_controler;
|
protected HTTPCommandsInterfacePost post_controler;
|
||||||
protected string url = "";
|
protected string url = "";
|
||||||
protected bool require_signed_commands;
|
|
||||||
public bool ShutdownHTTP { get; set; }
|
public bool ShutdownHTTP { get; set; }
|
||||||
public bool HTTPCnCmode { get; set; }
|
public bool HTTPCnCmode { get; set; }
|
||||||
|
|
||||||
|
|
@ -68,7 +67,6 @@ namespace BetterSecondBot.HttpServer
|
||||||
protected void start_http_server(JsonConfig setConfig, SecondBot LinkBot)
|
protected void start_http_server(JsonConfig setConfig, SecondBot LinkBot)
|
||||||
{
|
{
|
||||||
Config = setConfig;
|
Config = setConfig;
|
||||||
require_signed_commands = Config.HttpRequireSigned;
|
|
||||||
Bot = LinkBot;
|
Bot = LinkBot;
|
||||||
int port = Math.Abs(Config.Httpport);
|
int port = Math.Abs(Config.Httpport);
|
||||||
if (Config.Httpkey.Length < 12)
|
if (Config.Httpkey.Length < 12)
|
||||||
|
|
@ -118,7 +116,6 @@ namespace BetterSecondBot.HttpServer
|
||||||
}
|
}
|
||||||
else if (req.HttpMethod == "POST")
|
else if (req.HttpMethod == "POST")
|
||||||
{
|
{
|
||||||
|
|
||||||
string text;
|
string text;
|
||||||
using (var reader = new StreamReader(req.InputStream, req.ContentEncoding))
|
using (var reader = new StreamReader(req.InputStream, req.ContentEncoding))
|
||||||
{
|
{
|
||||||
|
|
@ -135,37 +132,55 @@ namespace BetterSecondBot.HttpServer
|
||||||
post_args.Add(bits[0].Trim(), bits[1].Trim());
|
post_args.Add(bits[0].Trim(), bits[1].Trim());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool format_ok = true;
|
||||||
|
bool accept_request = false;
|
||||||
|
string why_request_failed = "Bad signing request";
|
||||||
|
|
||||||
|
string command = "";
|
||||||
|
string arg = "";
|
||||||
|
string signing_code = "";
|
||||||
|
resp.StatusCode = 200;
|
||||||
if (post_args.ContainsKey("command") == true)
|
if (post_args.ContainsKey("command") == true)
|
||||||
{
|
{
|
||||||
if (post_args.ContainsKey("passkey") == true)
|
command = post_args["command"];
|
||||||
{
|
|
||||||
if (Config.Httpkey == post_args["passkey"])
|
|
||||||
{
|
|
||||||
string command = post_args["command"];
|
|
||||||
resp.StatusCode = 200;
|
|
||||||
string arg = "";
|
|
||||||
if (post_args.ContainsKey("args") == true)
|
if (post_args.ContainsKey("args") == true)
|
||||||
{
|
{
|
||||||
arg = post_args["args"];
|
arg = post_args["args"];
|
||||||
}
|
}
|
||||||
|
if (post_args.ContainsKey("code") == true)
|
||||||
|
{
|
||||||
|
signing_code = post_args["code"];
|
||||||
|
if (format_ok == true)
|
||||||
|
{
|
||||||
|
string raw = "" + command + "" + arg + "" + Config.Httpkey + "";
|
||||||
|
string hashcheck = helpers.GetSHA1(raw);
|
||||||
|
if (hashcheck == signing_code)
|
||||||
|
{
|
||||||
|
accept_request = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
why_request_failed = "Post missing arg value";
|
||||||
|
format_ok = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
why_request_failed = "Post missing command value";
|
||||||
|
format_ok = false;
|
||||||
|
}
|
||||||
|
if (accept_request == true)
|
||||||
|
{
|
||||||
data = Encoding.UTF8.GetBytes(String.Join("###", post_controler.Call(command, arg)));
|
data = Encoding.UTF8.GetBytes(String.Join("###", post_controler.Call(command, arg)));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
data = Encoding.UTF8.GetBytes("totaly fucked");
|
resp.StatusCode = 417;
|
||||||
|
data = Encoding.UTF8.GetBytes(why_request_failed);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
data = Encoding.UTF8.GetBytes("post request missing command");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
data = Encoding.UTF8.GetBytes("post request missing command");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
resp.ContentType = "text/html";
|
resp.ContentType = "text/html";
|
||||||
resp.ContentEncoding = Encoding.UTF8;
|
resp.ContentEncoding = Encoding.UTF8;
|
||||||
resp.ContentLength64 = data.LongLength;
|
resp.ContentLength64 = data.LongLength;
|
||||||
|
|
@ -173,6 +188,7 @@ namespace BetterSecondBot.HttpServer
|
||||||
resp.Close();
|
resp.Close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
protected void open_http_service()
|
protected void open_http_service()
|
||||||
{
|
{
|
||||||
get_controler = new HTTPCommandsInterfaceGet(Bot,this);
|
get_controler = new HTTPCommandsInterfaceGet(Bot,this);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue