-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
136 lines (122 loc) · 4.23 KB
/
main.cpp
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "lib/mongoose/mongoose.h"
#include "lib/sqlite/sqlite3.h"
#include "lib/dumbstr/dumbstr.h"
#include "lib/json.hpp"
#include "lib/captcha/botwall.h"
#include "IPchan.h"
#include <cstdlib>
#include <iostream>
#include <map>
#include <any>
#include <string>
#include <vector>
#include <fstream>
#define XCLACKSOVERHEAD "X-Clacks-Overhead: GNU Terry Pratchett, GNU Aaron Swartz, GNU Hal Finney, GNU Norm Macdonald, GNU Gilbert Gottfried, GNU Aniki, GNU Terry Davis, GNU jstark, GNU John McAfee, GNU asshurtmacfags\n"
typedef mg_connection connection;
typedef mg_http_message message;
sqlite3* db;
nlohmann::json cfg;
void callback(connection* c, int ev, void* ev_data, void* fn_data)
{
if (ev == MG_EV_HTTP_MSG)
{
std::string headers = XCLACKSOVERHEAD;
message* msg = (message*)ev_data;
mg_str* oip = mg_http_get_header(msg, "CF-Connecting-IP");
ipv4 cfip;
if (oip)
{
std::string oips = std::string(oip->ptr);
oips = oips.substr(0, oips.find("\n"));
int it = 0;
std::string acc;
for (int i = 0; i < oips.length(); i++)
{
if (oips.at(i) == '.')
{
cfip.adr[it] = std::stoi(acc);
it++;
acc = "";
continue;
}
acc.push_back(oips.at(i));
}cfip.adr[it] = std::stoi(acc);
}
ipv4 ip = oip ? cfip : *((ipv4*)(&(c->rem.ip)));
unsigned int ipid = ip2ipid(&ip);
std::string ipv4 = dumbfmt({
std::to_string(ip.adr[0]),
".", std::to_string(ip.adr[1]),
".", std::to_string(ip.adr[2]),
".", std::to_string(ip.adr[3])
});
if(mg_http_match_uri(msg, "/"))
{
headers.append("Content-Type: text/html;charset=shift_jis\n");
board b = {.ipid = (unsigned int)ipid, .threadcount = 1};
std::pair<std::string, std::string> // token, b64'd chllenge
captcha= botwall::generate_captcha(ipid, cfg["captcha_secret"]);
std::string body = dumbfmt_file("./static/index.html",
{
{"ipid", std::to_string(ipid)},
{"ipv4", ipv4},
{"threads", b.make_board_fe(db, captcha.second, captcha.first)},
{"threadtitles", b.make_threadlist_fe(db)},
{"challenge", captcha.second},
{"captcha_token", captcha.first}
}
);
mg_http_reply(c, 200, headers.c_str(),
body.c_str());
}
else if (mg_http_match_uri(msg, "/post"))
{
std::string res = thread::add_post(db, msg->body, cfg["captcha_secret"]);
headers.append("Location: /\n");
mg_http_reply(c, 303, headers.c_str(),
res.c_str());
}
else if (mg_http_match_uri(msg, "/post-thread"))
{
std::string res = board::add_thread(db, msg->body, cfg["captcha_secret"]);
headers.append("Location: /\n");
mg_http_reply(c, 303, headers.c_str(),
res.c_str());
}
else if (mg_http_match_uri(msg, "/CAPTCHA.bmp"))
{
mg_http_serve_opts o = {.mime_types="image/bmp"};
mg_http_serve_file(c, msg, "./static/imgs/aids.bmp", &o);
}
else
{
mg_http_reply(c, 404, headers.c_str(),
"the name's huwer, as in who are the fuck is you?");
}
}
}
int main(int argc, char* argv[])
{
std::srand(time(0));
mg_mgr mongoose;
mg_mgr_init(&mongoose);
bool inited = true;
if (access("./ipchan.db", F_OK) == -1)
inited = false;
if (sqlite3_open("./ipchan.db", &db))
{
std::cout << sqlite3_errmsg(db) << std::endl;
sqlite3_close(db);
return -1;
}
if (!inited)
db_schema_init(db);
std::fstream f;
f.open("./config.json");
cfg = nlohmann::json::parse(f);
f.close();
mg_http_listen(&mongoose, cfg["host"].get<std::string>().c_str(), callback, &mongoose);
while (true) {mg_mgr_poll(&mongoose, 1000);}
sqlite3_close(db);
return 0;
}