-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm_recaptchat.cpp
232 lines (190 loc) · 7.46 KB
/
m_recaptchat.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
* InspIRCd -- Internet Relay Chat Daemon
*
* Copyright (C) 2025 reverse Chevronnet
*
* This file is part of InspIRCd. InspIRCd is free software; you can
* redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; version 2. MERMEWÑNDLHBDJSKAO HHDSALKYHDASLKYHVA
*/
/// $ModAuthor: reverse Chevronnet <[email protected]>
/// $ModDesc: Handles Google reCAPTCHA v2 verification via SQL and NickServ accounts.
/// $ModConfig: url="https://recaptcha.vicio.chat/verify/"
/// dbid="default" query="SELECT COUNT(*) FROM recaptcha_app_verificationtoken WHERE token = $1 AND created_at + INTERVAL '30 minutes' > NOW()"
/// whitelistchans="#help,#opers" whitelistports="6667,6697">
/// $ModDepends: core 4
/// $LinkerFlags: -lcrypto
#include "inspircd.h"
#include "modules/sql.h"
#include "extension.h"
#include <openssl/sha.h> // For SHA256
#include "modules/account.h" // Add Account API
class ModuleCaptchaCheck; // Forward declaration
// Command for /VERIFY <token>
class CommandVerificar final : public Command
{
private:
ModuleCaptchaCheck* parent;
public:
CommandVerificar(Module* Creator, ModuleCaptchaCheck* Parent)
: Command(Creator, "VERIFY", 1, 1), parent(Parent)
{
syntax = { "<verification_token>" };
}
CmdResult Handle(User* user, const Params& parameters) override;
};
// SQL Query for token validation
class ValidateTokenQuery final : public SQL::Query
{
private:
User* user;
BoolExtItem& captcha_verified;
public:
ValidateTokenQuery(Module* Creator, User* User, BoolExtItem& CaptchaVerified)
: SQL::Query(Creator), user(User), captcha_verified(CaptchaVerified) {}
void OnResult(SQL::Result& result) override
{
SQL::Row row;
if (result.GetRow(row))
{
const std::string count = row[0].value_or("0");
if (count == "1")
{
captcha_verified.Set(user, true);
user->WriteNotice("*** reCAPTCHA: Verification successful. You may now join channels.");
}
else
{
user->WriteNotice("*** reCAPTCHA: Verification failed. Token not found or expired.");
}
}
else
{
user->WriteNotice("*** reCAPTCHA: Verification failed. Token not found.");
}
}
void OnError(const SQL::Error& error) override
{
user->WriteNotice(INSP_FORMAT("*** reCAPTCHA: SQL Error: {}", error.ToString()));
}
};
class ModuleCaptchaCheck final : public Module
{
private:
std::string captcha_url;
std::string query;
dynamic_reference<SQL::Provider> sql;
BoolExtItem captcha_verified;
CommandVerificar cmdverificar;
Account::API accountapi;
public:
ModuleCaptchaCheck()
: Module(VF_VENDOR, "Handles Google reCAPTCHA v2 verification via SQL and NickServ."),
sql(this, "SQL"),
captcha_verified(this, "captcha-verified", ExtensionType::USER, true),
cmdverificar(this, this),
accountapi(this) {}
void ReadConfig(ConfigStatus& status) override
{
auto& tag = ServerInstance->Config->ConfValue("captchaconfig");
// Read reCAPTCHA URL and SQL query
captcha_url = tag->getString("url");
query = tag->getString("query", "SELECT COUNT(*) FROM recaptcha_app_verificationtoken WHERE token = $1 AND created_at + INTERVAL '30 minutes' > NOW()", 1);
// Setup SQL provider
std::string dbid = tag->getString("dbid", "default");
sql.SetProvider("SQL/" + dbid);
if (!sql)
{
throw ModuleException(this, INSP_FORMAT("*** reCAPTCHA: Could not find SQL provider with id '{}'.", dbid));
}
}
ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven, bool override) override
{
if (user->IsOper())
return MOD_RES_PASSTHRU;
// Check if the user is identified to NickServ
if (accountapi && accountapi->GetAccountName(user))
{
user->WriteNotice("*** reCAPTCHA: NickServ account verified. You may join channels.");
return MOD_RES_PASSTHRU;
}
auto& tag = ServerInstance->Config->ConfValue("captchaconfig");
std::set<std::string> whitelist_channels;
std::stringstream chanstream(tag->getString("whitelistchans"));
std::string channel_name;
while (std::getline(chanstream, channel_name, ','))
whitelist_channels.insert(channel_name);
if (whitelist_channels.find(cname) != whitelist_channels.end())
return MOD_RES_PASSTHRU;
std::set<int> whitelist_ports;
std::stringstream portstream(tag->getString("whitelistports"));
std::string port_str;
while (std::getline(portstream, port_str, ','))
whitelist_ports.insert(std::stoi(port_str));
if (whitelist_ports.find(user->server_sa.port()) != whitelist_ports.end())
return MOD_RES_PASSTHRU;
if (captcha_verified.Get(user))
return MOD_RES_PASSTHRU;
NotifyUserToVerify(user);
return MOD_RES_DENY;
}
void ValidateToken(User* user, const std::string& token)
{
if (!sql)
{
user->WriteNotice("*** reCAPTCHA: SQL database is not available.");
return;
}
// Replace $1 placeholder with the actual token value
std::string formatted_query = INSP_FORMAT(
"SELECT COUNT(*) FROM recaptcha_app_verificationtoken WHERE token = '{}' AND created_at + INTERVAL '30 minutes' > NOW()",
token);
sql->Submit(new ValidateTokenQuery(this, user, captcha_verified), formatted_query);
}
private:
void NotifyUserToVerify(User* user)
{
auto& tag = ServerInstance->Config->ConfValue("captchaconfig");
// Generate token
std::string token = GenerateToken();
std::string hash = GenerateHash(token);
// Debug logs
ServerInstance->Logs.Debug(MODNAME, "Generated token: {}", token);
ServerInstance->Logs.Debug(MODNAME, "Generated hash: {}", hash);
// Build verification message
std::string message = tag->getString("message", "*** reCAPTCHA: Verify your connection at {url}.");
std::string link = captcha_url + "?code=" + token + "&hn=" + hash;
size_t pos = message.find("{url}");
if (pos != std::string::npos)
message.replace(pos, 5, link);
user->WriteNotice(message);
}
std::string GenerateToken()
{
static const char hex_chars[] = "0123456789abcdef";
std::string token;
for (int i = 0; i < 64; ++i)
token += hex_chars[std::rand() % 16];
return token;
}
std::string GenerateHash(const std::string& token)
{
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256(reinterpret_cast<const unsigned char*>(token.c_str()), token.size(), hash);
std::string hash_string;
for (int i = 0; i < SHA256_DIGEST_LENGTH; ++i)
{
hash_string += "0123456789abcdef"[hash[i] >> 4];
hash_string += "0123456789abcdef"[hash[i] & 0xf];
}
return hash_string;
}
};
CmdResult CommandVerificar::Handle(User* user, const Params& parameters)
{
const std::string& token = parameters[0];
parent->ValidateToken(user, token);
return CmdResult::SUCCESS;
}
MODULE_INIT(ModuleCaptchaCheck)