Deploying to gh-pages from @ 333c8ef426c33a6e9a1999dad72e459e0a15ed5e 🚀

This commit is contained in:
realaravinth
2023-10-29 01:10:10 +00:00
parent a3427dc2a5
commit 0ac2f3c185
156 changed files with 1618 additions and 732 deletions

View File

@@ -418,6 +418,21 @@
<a href="#418" id="418">418</a>
<a href="#419" id="419">419</a>
<a href="#420" id="420">420</a>
<a href="#421" id="421">421</a>
<a href="#422" id="422">422</a>
<a href="#423" id="423">423</a>
<a href="#424" id="424">424</a>
<a href="#425" id="425">425</a>
<a href="#426" id="426">426</a>
<a href="#427" id="427">427</a>
<a href="#428" id="428">428</a>
<a href="#429" id="429">429</a>
<a href="#430" id="430">430</a>
<a href="#431" id="431">431</a>
<a href="#432" id="432">432</a>
<a href="#433" id="433">433</a>
<a href="#434" id="434">434</a>
<a href="#435" id="435">435</a>
</pre></div><pre class="rust"><code><span class="comment">// Copyright (C) 2022 Aravinth Manivannan &lt;realaravinth@batsense.net&gt;
// SPDX-FileCopyrightText: 2023 Aravinth Manivannan &lt;realaravinth@batsense.net&gt;
//
@@ -712,6 +727,21 @@
<span class="doccomment">/// Get all psuedo IDs
</span><span class="kw">async fn </span>analytics_get_all_psuedo_ids(<span class="kw-2">&amp;</span><span class="self">self</span>, page: usize) -&gt; DBResult&lt;Vec&lt;String&gt;&gt;;
<span class="doccomment">/// Track maximum nonce received against captcha levels
</span><span class="kw">async fn </span>update_max_nonce_for_level(
<span class="kw-2">&amp;</span><span class="self">self</span>,
captcha_key: <span class="kw-2">&amp;</span>str,
difficulty_factor: u32,
latest_nonce: u32,
) -&gt; DBResult&lt;()&gt;;
<span class="doccomment">/// Get maximum nonce tracked so far for captcha levels
</span><span class="kw">async fn </span>get_max_nonce_for_level(
<span class="kw-2">&amp;</span><span class="self">self</span>,
captcha_key: <span class="kw-2">&amp;</span>str,
difficulty_factor: u32,
) -&gt; DBResult&lt;u32&gt;;
}
<span class="attr">#[derive(Debug, Clone, Default, Deserialize, Serialize, PartialEq)]

View File

@@ -346,6 +346,33 @@
<a href="#346" id="346">346</a>
<a href="#347" id="347">347</a>
<a href="#348" id="348">348</a>
<a href="#349" id="349">349</a>
<a href="#350" id="350">350</a>
<a href="#351" id="351">351</a>
<a href="#352" id="352">352</a>
<a href="#353" id="353">353</a>
<a href="#354" id="354">354</a>
<a href="#355" id="355">355</a>
<a href="#356" id="356">356</a>
<a href="#357" id="357">357</a>
<a href="#358" id="358">358</a>
<a href="#359" id="359">359</a>
<a href="#360" id="360">360</a>
<a href="#361" id="361">361</a>
<a href="#362" id="362">362</a>
<a href="#363" id="363">363</a>
<a href="#364" id="364">364</a>
<a href="#365" id="365">365</a>
<a href="#366" id="366">366</a>
<a href="#367" id="367">367</a>
<a href="#368" id="368">368</a>
<a href="#369" id="369">369</a>
<a href="#370" id="370">370</a>
<a href="#371" id="371">371</a>
<a href="#372" id="372">372</a>
<a href="#373" id="373">373</a>
<a href="#374" id="374">374</a>
<a href="#375" id="375">375</a>
</pre></div><pre class="rust"><code><span class="comment">// Copyright (C) 2022 Aravinth Manivannan &lt;realaravinth@batsense.net&gt;
// SPDX-FileCopyrightText: 2023 Aravinth Manivannan &lt;realaravinth@batsense.net&gt;
//
@@ -658,6 +685,33 @@
</span>.unwrap();
<span class="comment">// analytics end
// nonce tracking start
</span><span class="macro">assert_eq!</span>(
db.get_max_nonce_for_level(c.key, l[<span class="number">0</span>].difficulty_factor)
.<span class="kw">await
</span>.unwrap(),
<span class="number">0
</span>);
db.update_max_nonce_for_level(c.key, l[<span class="number">0</span>].difficulty_factor, <span class="number">1000</span>)
.<span class="kw">await
</span>.unwrap();
<span class="macro">assert_eq!</span>(
db.get_max_nonce_for_level(c.key, l[<span class="number">0</span>].difficulty_factor)
.<span class="kw">await
</span>.unwrap(),
<span class="number">1000
</span>);
db.update_max_nonce_for_level(c.key, l[<span class="number">0</span>].difficulty_factor, <span class="number">10_000</span>)
.<span class="kw">await
</span>.unwrap();
<span class="macro">assert_eq!</span>(
db.get_max_nonce_for_level(c.key, l[<span class="number">0</span>].difficulty_factor)
.<span class="kw">await
</span>.unwrap(),
<span class="number">10_000
</span>);
<span class="comment">// nonce tracking end
</span><span class="macro">assert_eq!</span>(db.fetch_solve(p.username, c.key).<span class="kw">await</span>.unwrap().len(), <span class="number">1</span>);
<span class="macro">assert_eq!</span>(
db.fetch_config_fetched(p.username, c.key)

View File

@@ -1156,6 +1156,103 @@
<a href="#1156" id="1156">1156</a>
<a href="#1157" id="1157">1157</a>
<a href="#1158" id="1158">1158</a>
<a href="#1159" id="1159">1159</a>
<a href="#1160" id="1160">1160</a>
<a href="#1161" id="1161">1161</a>
<a href="#1162" id="1162">1162</a>
<a href="#1163" id="1163">1163</a>
<a href="#1164" id="1164">1164</a>
<a href="#1165" id="1165">1165</a>
<a href="#1166" id="1166">1166</a>
<a href="#1167" id="1167">1167</a>
<a href="#1168" id="1168">1168</a>
<a href="#1169" id="1169">1169</a>
<a href="#1170" id="1170">1170</a>
<a href="#1171" id="1171">1171</a>
<a href="#1172" id="1172">1172</a>
<a href="#1173" id="1173">1173</a>
<a href="#1174" id="1174">1174</a>
<a href="#1175" id="1175">1175</a>
<a href="#1176" id="1176">1176</a>
<a href="#1177" id="1177">1177</a>
<a href="#1178" id="1178">1178</a>
<a href="#1179" id="1179">1179</a>
<a href="#1180" id="1180">1180</a>
<a href="#1181" id="1181">1181</a>
<a href="#1182" id="1182">1182</a>
<a href="#1183" id="1183">1183</a>
<a href="#1184" id="1184">1184</a>
<a href="#1185" id="1185">1185</a>
<a href="#1186" id="1186">1186</a>
<a href="#1187" id="1187">1187</a>
<a href="#1188" id="1188">1188</a>
<a href="#1189" id="1189">1189</a>
<a href="#1190" id="1190">1190</a>
<a href="#1191" id="1191">1191</a>
<a href="#1192" id="1192">1192</a>
<a href="#1193" id="1193">1193</a>
<a href="#1194" id="1194">1194</a>
<a href="#1195" id="1195">1195</a>
<a href="#1196" id="1196">1196</a>
<a href="#1197" id="1197">1197</a>
<a href="#1198" id="1198">1198</a>
<a href="#1199" id="1199">1199</a>
<a href="#1200" id="1200">1200</a>
<a href="#1201" id="1201">1201</a>
<a href="#1202" id="1202">1202</a>
<a href="#1203" id="1203">1203</a>
<a href="#1204" id="1204">1204</a>
<a href="#1205" id="1205">1205</a>
<a href="#1206" id="1206">1206</a>
<a href="#1207" id="1207">1207</a>
<a href="#1208" id="1208">1208</a>
<a href="#1209" id="1209">1209</a>
<a href="#1210" id="1210">1210</a>
<a href="#1211" id="1211">1211</a>
<a href="#1212" id="1212">1212</a>
<a href="#1213" id="1213">1213</a>
<a href="#1214" id="1214">1214</a>
<a href="#1215" id="1215">1215</a>
<a href="#1216" id="1216">1216</a>
<a href="#1217" id="1217">1217</a>
<a href="#1218" id="1218">1218</a>
<a href="#1219" id="1219">1219</a>
<a href="#1220" id="1220">1220</a>
<a href="#1221" id="1221">1221</a>
<a href="#1222" id="1222">1222</a>
<a href="#1223" id="1223">1223</a>
<a href="#1224" id="1224">1224</a>
<a href="#1225" id="1225">1225</a>
<a href="#1226" id="1226">1226</a>
<a href="#1227" id="1227">1227</a>
<a href="#1228" id="1228">1228</a>
<a href="#1229" id="1229">1229</a>
<a href="#1230" id="1230">1230</a>
<a href="#1231" id="1231">1231</a>
<a href="#1232" id="1232">1232</a>
<a href="#1233" id="1233">1233</a>
<a href="#1234" id="1234">1234</a>
<a href="#1235" id="1235">1235</a>
<a href="#1236" id="1236">1236</a>
<a href="#1237" id="1237">1237</a>
<a href="#1238" id="1238">1238</a>
<a href="#1239" id="1239">1239</a>
<a href="#1240" id="1240">1240</a>
<a href="#1241" id="1241">1241</a>
<a href="#1242" id="1242">1242</a>
<a href="#1243" id="1243">1243</a>
<a href="#1244" id="1244">1244</a>
<a href="#1245" id="1245">1245</a>
<a href="#1246" id="1246">1246</a>
<a href="#1247" id="1247">1247</a>
<a href="#1248" id="1248">1248</a>
<a href="#1249" id="1249">1249</a>
<a href="#1250" id="1250">1250</a>
<a href="#1251" id="1251">1251</a>
<a href="#1252" id="1252">1252</a>
<a href="#1253" id="1253">1253</a>
<a href="#1254" id="1254">1254</a>
<a href="#1255" id="1255">1255</a>
</pre></div><pre class="rust"><code><span class="comment">// Copyright (C) 2022 Aravinth Manivannan &lt;realaravinth@batsense.net&gt;
// SPDX-FileCopyrightText: 2023 Aravinth Manivannan &lt;realaravinth@batsense.net&gt;
//
@@ -1591,6 +1688,39 @@
futs.push(fut);
}
try_join_all(futs)
.<span class="kw">await
</span>.map_err(|e| map_row_not_found_err(e, DBError::CaptchaNotFound))<span class="question-mark">?</span>;
<span class="kw">let </span><span class="kw-2">mut </span>futs = Vec::with_capacity(levels.len());
<span class="kw">for </span>level <span class="kw">in </span>levels.iter() {
<span class="kw">let </span>difficulty_factor = level.difficulty_factor <span class="kw">as </span>i32;
<span class="kw">let </span>visitor_threshold = level.visitor_threshold <span class="kw">as </span>i32;
<span class="kw">let </span>fut = <span class="macro">sqlx::query!</span>(
<span class="string">&quot;INSERT INTO
mcaptcha_track_nonce (level_id, nonce)
VALUES ((
SELECT
level_id
FROM
mcaptcha_levels
WHERE
config_id = (SELECT config_id FROM mcaptcha_config WHERE captcha_key = ?)
AND
difficulty_factor = ?
AND
visitor_threshold = ?
), ?);&quot;</span>,
<span class="kw-2">&amp;</span>captcha_key,
difficulty_factor,
visitor_threshold,
<span class="number">0</span>,
)
.execute(<span class="kw-2">&amp;</span><span class="self">self</span>.pool);
futs.push(fut);
}
try_join_all(futs)
.<span class="kw">await
</span>.map_err(|e| map_row_not_found_err(e, DBError::CaptchaNotFound))<span class="question-mark">?</span>;
@@ -2245,6 +2375,70 @@
<span class="prelude-val">Ok</span>(res.drain(<span class="number">0</span>..).map(|r| r.psuedo_id).collect())
}
<span class="doccomment">/// Track maximum nonce received against captcha levels
</span><span class="kw">async fn </span>update_max_nonce_for_level(
<span class="kw-2">&amp;</span><span class="self">self</span>,
captcha_key: <span class="kw-2">&amp;</span>str,
difficulty_factor: u32,
latest_nonce: u32,
) -&gt; DBResult&lt;()&gt; {
<span class="kw">let </span>latest_nonce = latest_nonce <span class="kw">as </span>i64;
<span class="macro">sqlx::query!</span>(
<span class="string">&quot;UPDATE mcaptcha_track_nonce SET nonce = ?
WHERE level_id = (
SELECT
level_id
FROM
mcaptcha_levels
WHERE
config_id = (SELECT config_id FROM mcaptcha_config WHERE captcha_key = ?)
AND
difficulty_factor = ?
)
AND nonce &lt;= ?;&quot;</span>,
latest_nonce,
<span class="kw-2">&amp;</span>captcha_key,
difficulty_factor <span class="kw">as </span>i64,
latest_nonce
)
.execute(<span class="kw-2">&amp;</span><span class="self">self</span>.pool).<span class="kw">await
</span>.map_err(|e| map_row_not_found_err(e, DBError::CaptchaNotFound))<span class="question-mark">?</span>;
<span class="prelude-val">Ok</span>(())
}
<span class="doccomment">/// Get maximum nonce tracked so far for captcha levels
</span><span class="kw">async fn </span>get_max_nonce_for_level(
<span class="kw-2">&amp;</span><span class="self">self</span>,
captcha_key: <span class="kw-2">&amp;</span>str,
difficulty_factor: u32,
) -&gt; DBResult&lt;u32&gt; {
<span class="kw">struct </span>X {
nonce: i32,
}
<span class="kw">let </span>res = <span class="macro">sqlx::query_as!</span>(
X,
<span class="string">&quot;SELECT nonce FROM mcaptcha_track_nonce
WHERE level_id = (
SELECT
level_id
FROM
mcaptcha_levels
WHERE
config_id = (SELECT config_id FROM mcaptcha_config WHERE captcha_key = ?)
AND
difficulty_factor = ?
);&quot;</span>,
<span class="kw-2">&amp;</span>captcha_key,
difficulty_factor <span class="kw">as </span>i32,
)
.fetch_one(<span class="kw-2">&amp;</span><span class="self">self</span>.pool).<span class="kw">await
</span>.map_err(|e| map_row_not_found_err(e, DBError::CaptchaNotFound))<span class="question-mark">?</span>;
<span class="prelude-val">Ok</span>(res.nonce <span class="kw">as </span>u32)
}
}
<span class="attr">#[derive(Clone)]

View File

@@ -1166,6 +1166,100 @@
<a href="#1166" id="1166">1166</a>
<a href="#1167" id="1167">1167</a>
<a href="#1168" id="1168">1168</a>
<a href="#1169" id="1169">1169</a>
<a href="#1170" id="1170">1170</a>
<a href="#1171" id="1171">1171</a>
<a href="#1172" id="1172">1172</a>
<a href="#1173" id="1173">1173</a>
<a href="#1174" id="1174">1174</a>
<a href="#1175" id="1175">1175</a>
<a href="#1176" id="1176">1176</a>
<a href="#1177" id="1177">1177</a>
<a href="#1178" id="1178">1178</a>
<a href="#1179" id="1179">1179</a>
<a href="#1180" id="1180">1180</a>
<a href="#1181" id="1181">1181</a>
<a href="#1182" id="1182">1182</a>
<a href="#1183" id="1183">1183</a>
<a href="#1184" id="1184">1184</a>
<a href="#1185" id="1185">1185</a>
<a href="#1186" id="1186">1186</a>
<a href="#1187" id="1187">1187</a>
<a href="#1188" id="1188">1188</a>
<a href="#1189" id="1189">1189</a>
<a href="#1190" id="1190">1190</a>
<a href="#1191" id="1191">1191</a>
<a href="#1192" id="1192">1192</a>
<a href="#1193" id="1193">1193</a>
<a href="#1194" id="1194">1194</a>
<a href="#1195" id="1195">1195</a>
<a href="#1196" id="1196">1196</a>
<a href="#1197" id="1197">1197</a>
<a href="#1198" id="1198">1198</a>
<a href="#1199" id="1199">1199</a>
<a href="#1200" id="1200">1200</a>
<a href="#1201" id="1201">1201</a>
<a href="#1202" id="1202">1202</a>
<a href="#1203" id="1203">1203</a>
<a href="#1204" id="1204">1204</a>
<a href="#1205" id="1205">1205</a>
<a href="#1206" id="1206">1206</a>
<a href="#1207" id="1207">1207</a>
<a href="#1208" id="1208">1208</a>
<a href="#1209" id="1209">1209</a>
<a href="#1210" id="1210">1210</a>
<a href="#1211" id="1211">1211</a>
<a href="#1212" id="1212">1212</a>
<a href="#1213" id="1213">1213</a>
<a href="#1214" id="1214">1214</a>
<a href="#1215" id="1215">1215</a>
<a href="#1216" id="1216">1216</a>
<a href="#1217" id="1217">1217</a>
<a href="#1218" id="1218">1218</a>
<a href="#1219" id="1219">1219</a>
<a href="#1220" id="1220">1220</a>
<a href="#1221" id="1221">1221</a>
<a href="#1222" id="1222">1222</a>
<a href="#1223" id="1223">1223</a>
<a href="#1224" id="1224">1224</a>
<a href="#1225" id="1225">1225</a>
<a href="#1226" id="1226">1226</a>
<a href="#1227" id="1227">1227</a>
<a href="#1228" id="1228">1228</a>
<a href="#1229" id="1229">1229</a>
<a href="#1230" id="1230">1230</a>
<a href="#1231" id="1231">1231</a>
<a href="#1232" id="1232">1232</a>
<a href="#1233" id="1233">1233</a>
<a href="#1234" id="1234">1234</a>
<a href="#1235" id="1235">1235</a>
<a href="#1236" id="1236">1236</a>
<a href="#1237" id="1237">1237</a>
<a href="#1238" id="1238">1238</a>
<a href="#1239" id="1239">1239</a>
<a href="#1240" id="1240">1240</a>
<a href="#1241" id="1241">1241</a>
<a href="#1242" id="1242">1242</a>
<a href="#1243" id="1243">1243</a>
<a href="#1244" id="1244">1244</a>
<a href="#1245" id="1245">1245</a>
<a href="#1246" id="1246">1246</a>
<a href="#1247" id="1247">1247</a>
<a href="#1248" id="1248">1248</a>
<a href="#1249" id="1249">1249</a>
<a href="#1250" id="1250">1250</a>
<a href="#1251" id="1251">1251</a>
<a href="#1252" id="1252">1252</a>
<a href="#1253" id="1253">1253</a>
<a href="#1254" id="1254">1254</a>
<a href="#1255" id="1255">1255</a>
<a href="#1256" id="1256">1256</a>
<a href="#1257" id="1257">1257</a>
<a href="#1258" id="1258">1258</a>
<a href="#1259" id="1259">1259</a>
<a href="#1260" id="1260">1260</a>
<a href="#1261" id="1261">1261</a>
<a href="#1262" id="1262">1262</a>
</pre></div><pre class="rust"><code><span class="comment">// Copyright (C) 2022 Aravinth Manivannan &lt;realaravinth@batsense.net&gt;
// SPDX-FileCopyrightText: 2023 Aravinth Manivannan &lt;realaravinth@batsense.net&gt;
//
@@ -1613,6 +1707,38 @@
futs.push(fut);
}
try_join_all(futs)
.<span class="kw">await
</span>.map_err(|e| map_row_not_found_err(e, DBError::CaptchaNotFound))<span class="question-mark">?</span>;
<span class="kw">let </span><span class="kw-2">mut </span>futs = Vec::with_capacity(levels.len());
<span class="kw">for </span>level <span class="kw">in </span>levels.iter() {
<span class="kw">let </span>difficulty_factor = level.difficulty_factor <span class="kw">as </span>i32;
<span class="kw">let </span>visitor_threshold = level.visitor_threshold <span class="kw">as </span>i32;
<span class="kw">let </span>fut = <span class="macro">sqlx::query!</span>(
<span class="string">&quot;INSERT INTO
mcaptcha_track_nonce (level_id, nonce)
VALUES ((
SELECT
level_id
FROM
mcaptcha_levels
WHERE
config_id = (SELECT config_id FROM mcaptcha_config WHERE key = ($1))
AND
difficulty_factor = $2
AND
visitor_threshold = $3
), $4);&quot;</span>,
<span class="kw-2">&amp;</span>captcha_key,
difficulty_factor,
visitor_threshold,
<span class="number">0</span>,
)
.execute(<span class="kw-2">&amp;</span><span class="self">self</span>.pool);
futs.push(fut);
}
try_join_all(futs)
.<span class="kw">await
</span>.map_err(|e| map_row_not_found_err(e, DBError::CaptchaNotFound))<span class="question-mark">?</span>;
@@ -2265,6 +2391,68 @@
<span class="prelude-val">Ok</span>(res.drain(<span class="number">0</span>..).map(|r| r.psuedo_id).collect())
}
<span class="doccomment">/// Track maximum nonce received against captcha levels
</span><span class="kw">async fn </span>update_max_nonce_for_level(
<span class="kw-2">&amp;</span><span class="self">self</span>,
captcha_key: <span class="kw-2">&amp;</span>str,
difficulty_factor: u32,
latest_nonce: u32,
) -&gt; DBResult&lt;()&gt; {
<span class="macro">sqlx::query!</span>(
<span class="string">&quot;UPDATE mcaptcha_track_nonce SET nonce = $3
WHERE level_id = (
SELECT
level_id
FROM
mcaptcha_levels
WHERE
config_id = (SELECT config_id FROM mcaptcha_config WHERE key = ($1))
AND
difficulty_factor = $2
)
AND nonce &lt;= $3;&quot;</span>,
<span class="kw-2">&amp;</span>captcha_key,
difficulty_factor <span class="kw">as </span>i32,
latest_nonce <span class="kw">as </span>i32,
)
.execute(<span class="kw-2">&amp;</span><span class="self">self</span>.pool).<span class="kw">await
</span>.map_err(|e| map_row_not_found_err(e, DBError::CaptchaNotFound))<span class="question-mark">?</span>;
<span class="prelude-val">Ok</span>(())
}
<span class="doccomment">/// Get maximum nonce tracked so far for captcha levels
</span><span class="kw">async fn </span>get_max_nonce_for_level(
<span class="kw-2">&amp;</span><span class="self">self</span>,
captcha_key: <span class="kw-2">&amp;</span>str,
difficulty_factor: u32,
) -&gt; DBResult&lt;u32&gt; {
<span class="kw">struct </span>X {
nonce: i32,
}
<span class="kw">let </span>res = <span class="macro">sqlx::query_as!</span>(
X,
<span class="string">&quot;SELECT nonce FROM mcaptcha_track_nonce
WHERE level_id = (
SELECT
level_id
FROM
mcaptcha_levels
WHERE
config_id = (SELECT config_id FROM mcaptcha_config WHERE key = ($1))
AND
difficulty_factor = $2
);&quot;</span>,
<span class="kw-2">&amp;</span>captcha_key,
difficulty_factor <span class="kw">as </span>i32,
)
.fetch_one(<span class="kw-2">&amp;</span><span class="self">self</span>.pool).<span class="kw">await
</span>.map_err(|e| map_row_not_found_err(e, DBError::CaptchaNotFound))<span class="question-mark">?</span>;
<span class="prelude-val">Ok</span>(res.nonce <span class="kw">as </span>u32)
}
}
<span class="attr">#[derive(Clone)]

View File

@@ -279,17 +279,6 @@
<a href="#279" id="279">279</a>
<a href="#280" id="280">280</a>
<a href="#281" id="281">281</a>
<a href="#282" id="282">282</a>
<a href="#283" id="283">283</a>
<a href="#284" id="284">284</a>
<a href="#285" id="285">285</a>
<a href="#286" id="286">286</a>
<a href="#287" id="287">287</a>
<a href="#288" id="288">288</a>
<a href="#289" id="289">289</a>
<a href="#290" id="290">290</a>
<a href="#291" id="291">291</a>
<a href="#292" id="292">292</a>
</pre></div><pre class="rust"><code><span class="comment">// Copyright (C) 2022 Aravinth Manivannan &lt;realaravinth@batsense.net&gt;
// SPDX-FileCopyrightText: 2023 Aravinth Manivannan &lt;realaravinth@batsense.net&gt;
//
@@ -297,6 +286,7 @@
//use actix::prelude::*;
</span><span class="kw">use </span>actix_web::{web, HttpResponse, Responder};
<span class="kw">use </span>libmcaptcha::pow::PoWConfig;
<span class="kw">use </span>libmcaptcha::{
defense::LevelBuilder, master::messages::AddSiteBuilder, DefenseBuilder,
MCaptchaBuilder,
@@ -313,9 +303,15 @@
<span class="kw">pub </span>key: String,
}
<span class="comment">// API keys are mcaptcha actor names
<span class="attr">#[derive(Clone, Serialize, Deserialize, Debug)]
</span><span class="kw">pub struct </span>ApiPoWConfig {
<span class="kw">pub </span>string: String,
<span class="kw">pub </span>difficulty_factor: u32,
<span class="kw">pub </span>salt: String,
<span class="kw">pub </span>max_recorded_nonce: u32,
}
</span><span class="doccomment">/// get PoW configuration for an mcaptcha key
<span class="doccomment">/// get PoW configuration for an mcaptcha key
</span><span class="attr">#[my_codegen::post(path = <span class="string">&quot;V1_API_ROUTES.pow.get_config()&quot;</span>)]
</span><span class="kw">pub async fn </span>get_config(
payload: web::Json&lt;GetConfigPayload&gt;,
@@ -327,53 +323,35 @@
}
<span class="kw">let </span>payload = payload.into_inner();
<span class="kw">match </span>data.captcha.get_pow(payload.key.clone()).<span class="kw">await </span>{
<span class="prelude-val">Ok</span>(<span class="prelude-val">Some</span>(config)) =&gt; {
data.stats.record_fetch(<span class="kw-2">&amp;</span>data, <span class="kw-2">&amp;</span>payload.key).<span class="kw">await</span><span class="question-mark">?</span>;
<span class="prelude-val">Ok</span>(HttpResponse::Ok().json(config))
}
<span class="prelude-val">Ok</span>(<span class="prelude-val">None</span>) =&gt; {
init_mcaptcha(<span class="kw-2">&amp;</span>data, <span class="kw-2">&amp;</span>payload.key).<span class="kw">await</span><span class="question-mark">?</span>;
<span class="kw">let </span>config = data
.captcha
.get_pow(payload.key.clone())
.<span class="kw">await
</span>.expect(<span class="string">&quot;mcaptcha should be initialized and ready to go&quot;</span>);
<span class="comment">// background it. would require data::Data to be static
// to satidfy lifetime
</span>data.stats.record_fetch(<span class="kw-2">&amp;</span>data, <span class="kw-2">&amp;</span>payload.key).<span class="kw">await</span><span class="question-mark">?</span>;
<span class="prelude-val">Ok</span>(HttpResponse::Ok().json(config))
}
<span class="prelude-val">Err</span>(e) =&gt; <span class="prelude-val">Err</span>(e.into()),
}
<span class="kw">let </span>config: ServiceResult&lt;PoWConfig&gt; =
<span class="kw">match </span>data.captcha.get_pow(payload.key.clone()).<span class="kw">await </span>{
<span class="prelude-val">Ok</span>(<span class="prelude-val">Some</span>(config)) =&gt; <span class="prelude-val">Ok</span>(config),
<span class="prelude-val">Ok</span>(<span class="prelude-val">None</span>) =&gt; {
init_mcaptcha(<span class="kw-2">&amp;</span>data, <span class="kw-2">&amp;</span>payload.key).<span class="kw">await</span><span class="question-mark">?</span>;
<span class="kw">let </span>config = data
.captcha
.get_pow(payload.key.clone())
.<span class="kw">await
</span>.expect(<span class="string">&quot;mcaptcha should be initialized and ready to go&quot;</span>);
<span class="prelude-val">Ok</span>(config.unwrap())
}
<span class="prelude-val">Err</span>(e) =&gt; <span class="prelude-val">Err</span>(e.into()),
};
<span class="kw">let </span>config = config<span class="question-mark">?</span>;
<span class="kw">let </span>max_nonce = data
.db
.get_max_nonce_for_level(<span class="kw-2">&amp;</span>payload.key, config.difficulty_factor)
.<span class="kw">await</span><span class="question-mark">?</span>;
data.stats.record_fetch(<span class="kw-2">&amp;</span>data, <span class="kw-2">&amp;</span>payload.key).<span class="kw">await</span><span class="question-mark">?</span>;
<span class="comment">// match res.exists {
// Some(true) =&gt; {
// match data.captcha.get_pow(payload.key.clone()).await {
// Ok(Some(config)) =&gt; {
// record_fetch(&amp;payload.key, &amp;data.db).await;
// Ok(HttpResponse::Ok().json(config))
// }
// Ok(None) =&gt; {
// init_mcaptcha(&amp;data, &amp;payload.key).await?;
// let config = data
// .captcha
// .get_pow(payload.key.clone())
// .await
// .expect(&quot;mcaptcha should be initialized and ready to go&quot;);
// // background it. would require data::Data to be static
// // to satidfy lifetime
// record_fetch(&amp;payload.key, &amp;data.db).await;
// Ok(HttpResponse::Ok().json(config))
// }
// Err(e) =&gt; Err(e.into()),
// }
// }
//
// Some(false) =&gt; Err(ServiceError::TokenNotFound),
// None =&gt; Err(ServiceError::TokenNotFound),
// }
</span>}
<span class="kw">let </span>config = ApiPoWConfig {
string: config.string,
difficulty_factor: config.difficulty_factor,
salt: config.salt,
max_recorded_nonce: max_nonce,
};
<span class="prelude-val">Ok</span>(HttpResponse::Ok().json(config))
}
<span class="doccomment">/// Call this when [MCaptcha][libmcaptcha::MCaptcha] is not in master.
///
/// This fn gets mcaptcha config from database, builds [Defense][libmcaptcha::Defense],

View File

@@ -254,6 +254,10 @@
<a href="#254" id="254">254</a>
<a href="#255" id="255">255</a>
<a href="#256" id="256">256</a>
<a href="#257" id="257">257</a>
<a href="#258" id="258">258</a>
<a href="#259" id="259">259</a>
<a href="#260" id="260">260</a>
</pre></div><pre class="rust"><code><span class="comment">// Copyright (C) 2022 Aravinth Manivannan &lt;realaravinth@batsense.net&gt;
// SPDX-FileCopyrightText: 2023 Aravinth Manivannan &lt;realaravinth@batsense.net&gt;
//
@@ -321,6 +325,7 @@
<span class="kw">let </span>payload = payload.into_inner();
<span class="kw">let </span>worker_type = payload.worker_type.clone();
<span class="kw">let </span>time = payload.time;
<span class="kw">let </span>nonce = payload.nonce;
<span class="kw">let </span>(res, difficulty_factor) = data.captcha.verify_pow(payload.into(), ip).<span class="kw">await</span><span class="question-mark">?</span>;
data.stats.record_solve(<span class="kw-2">&amp;</span>data, <span class="kw-2">&amp;</span>key).<span class="kw">await</span><span class="question-mark">?</span>;
<span class="kw">if let </span>(<span class="prelude-val">Some</span>(time), <span class="prelude-val">Some</span>(worker_type)) = (time, worker_type) {
@@ -331,6 +336,9 @@
};
data.db.analysis_save(<span class="kw-2">&amp;</span>key, <span class="kw-2">&amp;</span>analytics).<span class="kw">await</span><span class="question-mark">?</span>;
}
data.db
.update_max_nonce_for_level(<span class="kw-2">&amp;</span>key, difficulty_factor, nonce <span class="kw">as </span>u32)
.<span class="kw">await</span><span class="question-mark">?</span>;
<span class="kw">let </span>payload = ValidationToken { token: res };
<span class="prelude-val">Ok</span>(HttpResponse::Ok().json(payload))
}