-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathweb_server.cpp
More file actions
9134 lines (8231 loc) · 398 KB
/
Copy pathweb_server.cpp
File metadata and controls
9134 lines (8231 loc) · 398 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: AGPL-3.0-or-later
#include "web_server.hpp"
#include "stratum_server.hpp"
#include <memory>
#include "address_utils.hpp"
#include "socket.hpp"
// Real coin daemon access (optional - only active when set_coin_node() is called)
#include <impl/ltc/coin/rpc.hpp>
#include <impl/ltc/coin/node_interface.hpp>
#include <core/coin/node_iface.hpp>
// Phase 4: embedded coin node interface
#include <impl/ltc/coin/template_builder.hpp>
#include <impl/ltc/share_messages.hpp>
#include <core/hash.hpp> // Hash(a,b) double-SHA256 for merkle computation
#include <core/random.hpp> // core::random::random_float for probabilistic fee
#include <core/target_utils.hpp> // chain::bits_to_target
#include <btclibs/util/strencodings.h> // ParseHex, HexStr
#include <crypto/scrypt.h> // scrypt_1024_1_1_256 for Litecoin PoW
#include <crypto/sha256.h> // CSHA256 for P2PK→P2PKH conversion
#include <crypto/ripemd160.h> // CRIPEMD160 for Hash160
#include <c2pool/merged/merged_mining.hpp> // Integrated merged mining
#include <impl/ltc/config_pool.hpp> // PoolConfig::is_testnet for donation addr
#include <iomanip>
#include <sstream>
#include <set>
#include <ctime>
#include <chrono>
#include <cmath>
#include <fstream>
#ifndef _WIN32
#include <unistd.h>
#endif
#include <boost/algorithm/string.hpp>
#include "btclibs/base58.h"
#include "btclibs/bech32.h"
#include "filesystem.hpp"
namespace core {
static std::string to_hex(const std::vector<unsigned char>& data)
{
return HexStr(std::span<const unsigned char>(data.data(), data.size()));
}
// ── Security helpers ───────────────────────────────────────────────────
// URL-decode percent-encoded strings (%20 → space, etc.)
static std::string url_decode(const std::string& str)
{
std::string result;
result.reserve(str.size());
for (size_t i = 0; i < str.size(); ++i) {
if (str[i] == '%' && i + 2 < str.size()) {
int hi = 0, lo = 0;
auto hexval = [](char c) -> int {
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
return -1;
};
hi = hexval(str[i + 1]);
lo = hexval(str[i + 2]);
if (hi >= 0 && lo >= 0) {
result += static_cast<char>((hi << 4) | lo);
i += 2;
continue;
}
} else if (str[i] == '+') {
result += ' ';
continue;
}
result += str[i];
}
return result;
}
// Validate that a string is a hex hash (exactly 64 hex chars)
static bool is_valid_hex_hash(const std::string& s)
{
if (s.size() != 64) return false;
for (char c : s) {
if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')))
return false;
}
return true;
}
// Validate that a string looks like a cryptocurrency address (base58/bech32 charset, reasonable length)
static bool is_valid_address_chars(const std::string& s)
{
if (s.empty() || s.size() > 128) return false;
for (char c : s) {
if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
return false;
}
return true;
}
// Validate graph source/view against allowed charset (alphanumeric + underscore, max 64 chars)
static bool is_valid_graph_param(const std::string& s)
{
if (s.empty() || s.size() > 64) return false;
for (char c : s) {
if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_'))
return false;
}
return true;
}
/// HttpSession Implementation
HttpSession::HttpSession(tcp::socket socket, std::shared_ptr<MiningInterface> mining_interface)
: socket_(std::move(socket))
, mining_interface_(mining_interface)
{
}
void HttpSession::run()
{
read_request();
}
void HttpSession::read_request()
{
auto self = shared_from_this();
http::async_read(socket_, buffer_, request_,
[self](beast::error_code ec, std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
if(ec == http::error::end_of_stream)
return self->handle_error(ec, "read_request");
if(ec)
return self->handle_error(ec, "read_request");
self->process_request();
});
}
void HttpSession::process_request()
{
// Create HTTP response
http::response<http::string_body> response{http::status::ok, request_.version()};
response.set(http::field::server, mining_interface_->get_pool_version());
response.set(http::field::content_type, "application/json");
// CORS — only set if operator configured an origin
const auto& cors_origin = mining_interface_->get_cors_origin();
if (!cors_origin.empty()) {
response.set(http::field::access_control_allow_origin, cors_origin);
response.set(http::field::access_control_allow_methods, "GET, POST, OPTIONS");
response.set(http::field::access_control_allow_headers, "Content-Type");
}
// Security headers
response.set("X-Content-Type-Options", "nosniff");
response.set("X-Frame-Options", "DENY");
response.set("Referrer-Policy", "no-referrer");
// Admin endpoints accept both GET and POST so CLI tools can POST
// mutations without falling through to the JSON-RPC handler.
// Covers /api/admin/pool/* AND /api/admin/coin/*.
bool is_admin_path = std::string(request_.target()).substr(0, 11) == "/api/admin/";
try {
std::string response_body;
if (request_.method() == http::verb::options) {
// Handle CORS preflight
response_body = "";
}
else if (request_.method() == http::verb::get
|| (is_admin_path && request_.method() == http::verb::post)) {
// Path-based REST routing for p2pool-compatible endpoints
std::string raw_target(request_.target());
std::string target(raw_target);
// Strip query string
auto qpos = target.find('?');
if (qpos != std::string::npos) target = target.substr(0, qpos);
auto getQueryParam = [&raw_target](const std::string& key) -> std::string {
const auto qp = raw_target.find('?');
if (qp == std::string::npos) {
return {};
}
const std::string query = raw_target.substr(qp + 1);
const std::string prefix = key + "=";
auto pos = query.find(prefix);
if (pos == std::string::npos) {
return {};
}
pos += prefix.size();
auto end = query.find('&', pos);
if (end == std::string::npos) {
end = query.size();
}
return url_decode(query.substr(pos, end - pos));
};
// ── Auth gate for sensitive endpoints ──────────────────────
if (mining_interface_->auth_required()) {
bool needs_auth = (target.substr(0, 9) == "/control/"
|| target == "/web/log"
|| target == "/logs/export");
if (needs_auth) {
const std::string token = getQueryParam("token");
if (!mining_interface_->verify_auth_token(token)) {
response.result(http::status::unauthorized);
response.body() = R"({"error":"Unauthorized – supply ?token=<auth_token>"})";
response.prepare_payload();
send_response(std::move(response));
return;
}
}
}
nlohmann::json rest_result;
if (target == "/local_rate")
rest_result = mining_interface_->rest_local_rate();
else if (target == "/global_rate")
rest_result = mining_interface_->rest_global_rate();
else if (target == "/current_payouts")
rest_result = mining_interface_->rest_current_payouts();
else if (target == "/users")
rest_result = mining_interface_->rest_users();
else if (target == "/fee")
rest_result = mining_interface_->rest_fee();
else if (target == "/recent_blocks")
rest_result = mining_interface_->rest_recent_blocks();
else if (target == "/checkpoint")
rest_result = mining_interface_->rest_checkpoint();
else if (target == "/checkpoints")
rest_result = mining_interface_->rest_checkpoints();
else if (target == "/uptime")
rest_result = mining_interface_->rest_uptime();
else if (target == "/connected_miners")
rest_result = mining_interface_->rest_connected_miners();
else if (target == "/stratum_stats")
rest_result = mining_interface_->rest_stratum_stats();
else if (target == "/global_stats")
rest_result = mining_interface_->rest_global_stats();
else if (target == "/sharechain/stats")
rest_result = mining_interface_->rest_sharechain_stats();
else if (target == "/sharechain/window") {
// Layer 1+2: cached response + ETag/304
auto client_ip = socket_.remote_endpoint().address().to_string();
if (!mining_interface_->rate_check(client_ip, 4)) { // max 4/min
response.result(http::status::too_many_requests);
response_body = R"({"error":"Rate limited"})";
} else {
auto [body, etag] = mining_interface_->get_cached_window_response();
std::string client_etag;
auto it = request_.find(http::field::if_none_match);
if (it != request_.end()) client_etag = std::string(it->value());
if (!etag.empty() && client_etag == "\"" + etag + "\"") {
response.result(http::status::not_modified);
response_body = "";
} else {
response.set(http::field::etag, "\"" + etag + "\"");
response.set(http::field::cache_control, "no-cache");
response_body = body;
}
}
}
else if (target == "/sharechain/tip")
rest_result = mining_interface_->rest_sharechain_tip();
else if (target == "/sharechain/delta") {
auto client_ip = socket_.remote_endpoint().address().to_string();
if (!mining_interface_->rate_check(client_ip, 30)) { // max 30/min
response.result(http::status::too_many_requests);
response_body = R"({"error":"Rate limited"})";
} else {
rest_result = mining_interface_->rest_sharechain_delta(getQueryParam("since"));
}
}
else if (target == "/sharechain/stream") {
// Layer 4: SSE — send headers and keep connection open
auto client_ip = socket_.remote_endpoint().address().to_string();
if (!mining_interface_->rate_check(client_ip, 2)) { // max 2 SSE connections/min per IP
response.result(http::status::too_many_requests);
response_body = R"({"error":"Rate limited"})";
} else {
// Send SSE headers manually, then register for push
std::string sse_headers =
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/event-stream\r\n"
"Cache-Control: no-cache\r\n"
"Connection: keep-alive\r\n"
"X-Accel-Buffering: no\r\n"
"\r\n"
"data: {\"connected\":true}\n\n";
auto buf = std::make_shared<std::string>(sse_headers);
auto self = shared_from_this();
auto sock_ptr = std::make_shared<tcp::socket>(std::move(socket_));
boost::asio::async_write(*sock_ptr, boost::asio::buffer(*buf),
[buf, sock_ptr, mi = mining_interface_](beast::error_code ec, std::size_t) {
if (!ec) mi->sse_register(sock_ptr);
});
return; // Don't close connection
}
}
else if (target == "/control/mining/start")
rest_result = mining_interface_->rest_control_mining_start();
else if (target == "/control/mining/stop")
rest_result = mining_interface_->rest_control_mining_stop();
else if (target == "/control/mining/restart")
rest_result = mining_interface_->rest_control_mining_restart();
else if (target == "/control/mining/ban")
rest_result = mining_interface_->rest_control_mining_ban(getQueryParam("target"));
else if (target == "/control/mining/unban")
rest_result = mining_interface_->rest_control_mining_unban(getQueryParam("target"));
else if (target == "/web/log") {
// Return plain text log (not JSON)
response.set(http::field::content_type, "text/plain; charset=utf-8");
response.body() = mining_interface_->rest_web_log();
response.prepare_payload();
send_response(std::move(response));
return;
}
else if (target == "/logs/export") {
const std::string scope = getQueryParam("scope");
int64_t from_ts = 0, to_ts = 0;
try { from_ts = std::stoll(getQueryParam("from")); } catch (...) {}
try { to_ts = std::stoll(getQueryParam("to")); } catch (...) {}
const std::string fmt = getQueryParam("format");
response.set(http::field::content_type,
(fmt == "csv") ? "text/csv; charset=utf-8" :
(fmt == "jsonl") ? "application/x-ndjson; charset=utf-8" :
"text/plain; charset=utf-8");
response.body() = mining_interface_->rest_logs_export(scope, from_ts, to_ts, fmt);
response.prepare_payload();
send_response(std::move(response));
return;
}
// ── p2pool legacy compatibility endpoints ──────────────────────
else if (target == "/local_stats")
rest_result = mining_interface_->rest_local_stats();
else if (target == "/miner_thresholds")
rest_result = mining_interface_->rest_miner_thresholds();
else if (target == "/web/version")
rest_result = mining_interface_->rest_web_version();
else if (target == "/web/currency_info")
rest_result = mining_interface_->rest_web_currency_info();
else if (target == "/payout_addr")
rest_result = mining_interface_->rest_payout_addr();
else if (target == "/payout_addrs")
rest_result = mining_interface_->rest_payout_addrs();
else if (target == "/web/best_share_hash")
rest_result = mining_interface_->rest_web_best_share_hash();
else if (target == "/web/sync_status")
rest_result = mining_interface_->rest_sync_status();
else if (target == "/p2pool_global_stats")
rest_result = mining_interface_->rest_p2pool_global_stats();
// ── Additional p2pool-compatible REST endpoints ────────────────
else if (target == "/rate")
rest_result = mining_interface_->rest_rate();
else if (target == "/difficulty")
rest_result = mining_interface_->rest_difficulty();
else if (target == "/user_stales")
rest_result = mining_interface_->rest_user_stales();
else if (target == "/peer_addresses") {
response.set(http::field::content_type, "text/plain; charset=utf-8");
response.body() = mining_interface_->rest_peer_addresses();
response.prepare_payload();
send_response(std::move(response));
return;
}
else if (target == "/peer_versions")
rest_result = mining_interface_->rest_peer_versions();
else if (target == "/peer_txpool_sizes")
rest_result = mining_interface_->rest_peer_txpool_sizes();
else if (target == "/peer_list")
rest_result = mining_interface_->rest_peer_list();
else if (target == "/pings")
rest_result = mining_interface_->rest_pings();
else if (target == "/stale_rates")
rest_result = mining_interface_->rest_stale_rates();
else if (target == "/node_info")
rest_result = mining_interface_->rest_node_info();
else if (target == "/api/node_topology")
rest_result = mining_interface_->rest_node_topology();
else if (target == "/luck_stats")
rest_result = mining_interface_->rest_luck_stats();
else if (target == "/ban_stats")
rest_result = mining_interface_->rest_ban_stats();
else if (target == "/stratum_security")
rest_result = mining_interface_->rest_stratum_security();
else if (target == "/best_share")
rest_result = mining_interface_->rest_best_share();
else if (target == "/version_signaling")
rest_result = mining_interface_->rest_version_signaling();
else if (target == "/v36_status")
rest_result = mining_interface_->rest_v36_status();
else if (target == "/tracker_debug")
rest_result = mining_interface_->rest_tracker_debug();
// Merged mining endpoints
else if (target == "/merged_stats")
rest_result = mining_interface_->rest_merged_stats();
else if (target == "/current_merged_payouts")
rest_result = mining_interface_->rest_current_merged_payouts();
else if (target == "/pplns/current")
rest_result = mining_interface_->rest_pplns_current();
else if (target.rfind("/pplns/miner/", 0) == 0) {
// /pplns/miner/<address> — extract address from path.
std::string addr = target.substr(std::string("/pplns/miner/").size());
// URL-decode (spec §5.2 "MUST be URL-encoded by the client").
std::string decoded;
decoded.reserve(addr.size());
for (size_t i = 0; i < addr.size(); ++i) {
if (addr[i] == '%' && i + 2 < addr.size()) {
auto hex = addr.substr(i + 1, 2);
try {
decoded.push_back(static_cast<char>(
std::stoi(hex, nullptr, 16)));
i += 2;
} catch (...) {
decoded.push_back(addr[i]);
}
} else if (addr[i] == '+') {
decoded.push_back(' ');
} else {
decoded.push_back(addr[i]);
}
}
rest_result = mining_interface_->rest_pplns_miner(decoded);
}
else if (target == "/recent_merged_blocks")
rest_result = mining_interface_->rest_recent_merged_blocks();
else if (target == "/all_merged_blocks")
rest_result = mining_interface_->rest_all_merged_blocks();
else if (target == "/discovered_merged_blocks")
rest_result = mining_interface_->rest_discovered_merged_blocks();
else if (target == "/broadcaster_status")
rest_result = mining_interface_->rest_broadcaster_status();
else if (target == "/merged_broadcaster_status")
rest_result = mining_interface_->rest_merged_broadcaster_status();
else if (target == "/network_difficulty")
rest_result = mining_interface_->rest_network_difficulty();
// /web/ sub-endpoints (share chain inspection)
else if (target == "/web/heads")
rest_result = mining_interface_->rest_web_heads();
else if (target == "/web/verified_heads")
rest_result = mining_interface_->rest_web_verified_heads();
else if (target == "/web/tails")
rest_result = mining_interface_->rest_web_tails();
else if (target == "/web/verified_tails")
rest_result = mining_interface_->rest_web_verified_tails();
else if (target == "/web/my_share_hashes")
rest_result = mining_interface_->rest_web_my_share_hashes();
else if (target == "/web/my_share_hashes50")
rest_result = mining_interface_->rest_web_my_share_hashes50();
else if (target == "/web/log_json")
rest_result = mining_interface_->rest_web_log_json();
// Path-parameterised endpoints (starts_with matching)
// Each path parameter is URL-decoded and validated before use.
else if (target.substr(0, 13) == "/miner_stats/") {
std::string addr = url_decode(target.substr(13));
if (!is_valid_address_chars(addr)) {
response.result(http::status::bad_request);
response.body() = R"({"error":"Invalid address parameter"})";
response.prepare_payload();
send_response(std::move(response));
return;
}
rest_result = mining_interface_->rest_miner_stats(addr);
}
else if (target.substr(0, 15) == "/miner_payouts/") {
std::string addr = url_decode(target.substr(15));
if (!is_valid_address_chars(addr)) {
response.result(http::status::bad_request);
response.body() = R"({"error":"Invalid address parameter"})";
response.prepare_payload();
send_response(std::move(response));
return;
}
rest_result = mining_interface_->rest_miner_payouts(addr);
}
else if (target.substr(0, 18) == "/patron_sendmany/") {
std::string total = url_decode(target.substr(18));
if (!is_valid_address_chars(total)) {
response.result(http::status::bad_request);
response.body() = R"({"error":"Invalid parameter"})";
response.prepare_payload();
send_response(std::move(response));
return;
}
response.set(http::field::content_type, "text/plain; charset=utf-8");
response.body() = mining_interface_->rest_patron_sendmany(total).dump();
response.prepare_payload();
send_response(std::move(response));
return;
}
else if (target.substr(0, 11) == "/web/share/") {
std::string hash = url_decode(target.substr(11));
if (!is_valid_hex_hash(hash)) {
response.result(http::status::bad_request);
response.body() = R"({"error":"Invalid hash – expected 64 hex characters"})";
response.prepare_payload();
send_response(std::move(response));
return;
}
rest_result = mining_interface_->rest_web_share(hash);
}
else if (target.substr(0, 20) == "/web/payout_address/") {
std::string hash = url_decode(target.substr(20));
if (!is_valid_hex_hash(hash)) {
response.result(http::status::bad_request);
response.body() = R"({"error":"Invalid hash – expected 64 hex characters"})";
response.prepare_payload();
send_response(std::move(response));
return;
}
rest_result = mining_interface_->rest_web_payout_address(hash);
}
else if (target.substr(0, 16) == "/web/graph_data/") {
// /web/graph_data/<source>/<view>
std::string rest_path = url_decode(target.substr(16));
auto slash_pos = rest_path.find('/');
std::string source = (slash_pos != std::string::npos) ? rest_path.substr(0, slash_pos) : rest_path;
std::string view = (slash_pos != std::string::npos) ? rest_path.substr(slash_pos + 1) : "";
if (!is_valid_graph_param(source) || (!view.empty() && !is_valid_graph_param(view))) {
response.result(http::status::bad_request);
response.body() = R"({"error":"Invalid graph source/view parameter"})";
response.prepare_payload();
send_response(std::move(response));
return;
}
rest_result = mining_interface_->rest_web_graph_data(source, view);
}
// ── Coin peer sharing (public, rate-limited) ──────────────
else if (target == "/api/coin_peers") {
auto remote_ip = socket_.remote_endpoint().address().to_string();
auto result = mining_interface_->rest_coin_peers(remote_ip);
if (result.contains("error")) {
response.result(http::status::too_many_requests);
response.set(http::field::retry_after, "10");
}
response.body() = result.dump();
response.prepare_payload();
send_response(std::move(response));
return;
}
else {
// ── Admin API endpoints (loopback-only) ───────────────────
if (target.substr(0, 16) == "/api/admin/pool/") {
auto remote_addr = socket_.remote_endpoint().address();
if (!remote_addr.is_loopback()) {
response.result(http::status::forbidden);
response.body() = R"({"error":"Admin API is local-only"})";
response.prepare_payload();
send_response(std::move(response));
return;
}
if (!mining_interface_->has_admin_fns()) {
response.result(http::status::not_found);
response.body() = R"({"error":"Admin API not enabled"})";
response.prepare_payload();
send_response(std::move(response));
return;
}
std::string ep = target.substr(16); // after "/api/admin/pool/"
// strip query string
auto qpos = ep.find('?');
if (qpos != std::string::npos) ep = ep.substr(0, qpos);
auto parse_host_port = [&](uint16_t& port_out) -> std::string {
std::string host = getQueryParam("host");
std::string ip_q = getQueryParam("ip");
port_out = 0;
if (!host.empty()) {
auto colon = host.rfind(':');
if (colon != std::string::npos) {
try { port_out = static_cast<uint16_t>(std::stoul(host.substr(colon + 1))); } catch (...) {}
host = host.substr(0, colon);
}
} else {
host = ip_q;
}
std::string port_q = getQueryParam("port");
if (!port_q.empty()) {
try { port_out = static_cast<uint16_t>(std::stoul(port_q)); } catch (...) {}
}
return host;
};
if (ep == "bans/list") {
rest_result = mining_interface_->call_admin_list_bans();
} else if (ep == "bans/add") {
std::string ip = getQueryParam("ip");
int dur = 0;
try { auto d = getQueryParam("duration"); if (!d.empty()) dur = std::stoi(d); } catch (...) {}
rest_result = mining_interface_->call_admin_ban_ip(ip, dur);
} else if (ep == "bans/remove") {
std::string ip = getQueryParam("ip");
rest_result = mining_interface_->call_admin_unban_ip(ip);
} else if (ep == "whitelist/list") {
rest_result = mining_interface_->call_admin_list_whitelist();
} else if (ep == "whitelist/add") {
uint16_t p = 0;
std::string h = parse_host_port(p);
rest_result = mining_interface_->call_admin_whitelist_add(h, p);
} else if (ep == "whitelist/remove") {
uint16_t p = 0;
std::string h = parse_host_port(p);
rest_result = mining_interface_->call_admin_whitelist_remove(h, p);
} else if (ep == "peers/list") {
rest_result = mining_interface_->call_admin_list_peers();
} else if (ep == "peers/drop") {
std::string ip = getQueryParam("ip");
rest_result = mining_interface_->call_admin_drop_peer(ip);
} else if (ep == "peers/dial") {
uint16_t p = 0;
std::string h = parse_host_port(p);
rest_result = mining_interface_->call_admin_dial_peer(h, p);
} else {
rest_result = nlohmann::json{{"ok", false}, {"error", "Unknown admin endpoint"}};
}
}
// ── Admin API: embedded-coin peer management ──────────────
else if (target.substr(0, 16) == "/api/admin/coin/") {
auto remote_addr = socket_.remote_endpoint().address();
if (!remote_addr.is_loopback()) {
response.result(http::status::forbidden);
response.body() = R"({"error":"Admin API is local-only"})";
response.prepare_payload();
send_response(std::move(response));
return;
}
if (!mining_interface_->has_admin_coin_fns()) {
response.result(http::status::not_found);
response.body() = R"({"error":"Coin admin API not enabled"})";
response.prepare_payload();
send_response(std::move(response));
return;
}
std::string ep = target.substr(16); // after "/api/admin/coin/"
auto qpos = ep.find('?');
if (qpos != std::string::npos) ep = ep.substr(0, qpos);
std::string chain = getQueryParam("chain");
if (chain.empty()) chain = mining_interface_->primary_chain_key();
if (ep == "peers/list") {
rest_result = mining_interface_->call_admin_coin_list_peers(chain);
} else if (ep == "peer/add") {
std::string host = getQueryParam("host");
uint16_t port = 0;
// Host may be "A.B.C.D:PORT" (combined) OR "A.B.C.D" with separate ?port=
if (!host.empty()) {
auto colon = host.rfind(':');
if (colon != std::string::npos) {
try { port = static_cast<uint16_t>(std::stoul(host.substr(colon + 1))); } catch (...) {}
host = host.substr(0, colon);
}
}
std::string port_q = getQueryParam("port");
if (!port_q.empty()) {
try { port = static_cast<uint16_t>(std::stoul(port_q)); } catch (...) {}
}
rest_result = mining_interface_->call_admin_coin_add_peer(chain, host, port);
} else {
rest_result = nlohmann::json{{"ok", false}, {"error", "Unknown coin admin endpoint"}};
}
}
else
// ── Explorer API endpoints (loopback-only) ────────────────
if (target.substr(0, 14) == "/api/explorer/") {
// Loopback guard: only accept requests from 127.0.0.1 / ::1
auto remote_addr = socket_.remote_endpoint().address();
if (!remote_addr.is_loopback()) {
response.result(http::status::forbidden);
response.body() = R"({"error":"Explorer API is local-only"})";
response.prepare_payload();
send_response(std::move(response));
return;
}
if (!mining_interface_->is_explorer_enabled()) {
response.result(http::status::not_found);
response.body() = R"({"error":"Explorer not enabled"})";
response.prepare_payload();
send_response(std::move(response));
return;
}
std::string chain = getQueryParam("chain");
if (chain.empty()) chain = mining_interface_->primary_chain_key();
std::string ep = target.substr(14);
if (ep == "getblockchaininfo" && mining_interface_->has_explorer_chaininfo_fn()) {
rest_result = mining_interface_->call_explorer_chaininfo(chain);
} else if (ep == "getblockhash" && mining_interface_->has_explorer_blockhash_fn()) {
std::string height_str = getQueryParam("height");
uint32_t h = 0;
try { h = static_cast<uint32_t>(std::stoul(height_str)); } catch (...) {}
auto hash = mining_interface_->call_explorer_blockhash(h, chain);
if (hash.empty()) {
rest_result = nlohmann::json{{"error", "Block not found"}};
} else {
rest_result = nlohmann::json{{"result", hash}};
}
} else if (ep == "getblock" && mining_interface_->has_explorer_getblock_fn()) {
std::string hash = getQueryParam("hash");
// Also support height lookup
if (hash.empty()) {
std::string h_str = getQueryParam("height");
if (!h_str.empty() && mining_interface_->has_explorer_blockhash_fn()) {
uint32_t h = 0;
try { h = static_cast<uint32_t>(std::stoul(h_str)); } catch (...) {}
hash = mining_interface_->call_explorer_blockhash(h, chain);
}
}
if (hash.empty()) {
rest_result = nlohmann::json{{"error", "Missing hash or height parameter"}};
} else {
rest_result = mining_interface_->call_explorer_getblock(hash, chain);
}
} else if (ep == "getmempoolinfo" && mining_interface_->has_explorer_mempoolinfo_fn()) {
rest_result = mining_interface_->call_explorer_mempoolinfo(chain);
} else if (ep == "getrawmempool" && mining_interface_->has_explorer_rawmempool_fn()) {
bool verbose = getQueryParam("verbose") == "true";
uint32_t limit = 500;
try { auto ls = getQueryParam("limit"); if (!ls.empty()) limit = std::min(5000u, static_cast<uint32_t>(std::stoul(ls))); } catch (...) {}
rest_result = mining_interface_->call_explorer_rawmempool(chain, verbose, limit);
} else if (ep == "getmempoolentry" && mining_interface_->has_explorer_mempoolentry_fn()) {
std::string txid_param = getQueryParam("txid");
if (txid_param.empty()) {
rest_result = nlohmann::json{{"error", "Missing txid parameter"}};
} else {
rest_result = mining_interface_->call_explorer_mempoolentry(txid_param, chain);
}
} else {
rest_result = nlohmann::json{{"error", "Unknown explorer endpoint"}};
}
}
else {
// ── Static file serving (dashboard gate) ───────────────────
const auto& dashboard_dir = mining_interface_->get_dashboard_dir();
if (!dashboard_dir.empty()) {
std::string file_path = target;
if (file_path == "/" || file_path.empty())
file_path = mining_interface_->is_node_ready()
? "/dashboard.html" : "/loading.html";
// During startup, redirect all HTML pages to loading.html
// (except loading.html itself). Only loading.html + sync_status
// + currency_info work during early startup.
if (file_path != "/loading.html" && file_path.find(".html") != std::string::npos) {
bool node_ready = mining_interface_->is_node_ready();
if (!node_ready) {
response.result(http::status::temporary_redirect);
response.set(http::field::location, "/loading.html");
response.body() = "";
response.prepare_payload();
send_response(std::move(response));
return;
}
}
std::error_code fec;
std::filesystem::path base = std::filesystem::weakly_canonical(dashboard_dir);
std::filesystem::path requested = base / file_path.substr(1);
auto resolved = std::filesystem::canonical(requested, fec);
if (!fec && resolved.string().substr(0, base.string().size()) == base.string()
&& std::filesystem::is_regular_file(resolved))
{
// MIME type detection
std::string ext = resolved.extension().string();
std::string mime = "application/octet-stream";
if (ext == ".html" || ext == ".htm") mime = "text/html; charset=utf-8";
else if (ext == ".js" || ext == ".mjs") mime = "application/javascript; charset=utf-8";
else if (ext == ".css") mime = "text/css; charset=utf-8";
else if (ext == ".json") mime = "application/json; charset=utf-8";
else if (ext == ".ico") mime = "image/x-icon";
else if (ext == ".png") mime = "image/png";
else if (ext == ".svg") mime = "image/svg+xml";
else if (ext == ".txt") mime = "text/plain; charset=utf-8";
else if (ext == ".woff2") mime = "font/woff2";
else if (ext == ".woff") mime = "font/woff";
else if (ext == ".map") mime = "application/json";
std::ifstream file(resolved, std::ios::binary);
std::string contents((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
// Inject analytics tag into HTML pages if configured
const auto& analytics_id = mining_interface_->get_analytics_id();
if (!analytics_id.empty() && (ext == ".html" || ext == ".htm")) {
auto pos = contents.find("</head>");
if (pos == std::string::npos) pos = contents.find("</HEAD>");
if (pos != std::string::npos) {
std::string tag =
"<!-- analytics -->\n"
"<script async src=\"https://www.googletagmanager.com/gtag/js?id=" + analytics_id + "\"></script>\n"
"<script>\n"
"window.dataLayer=window.dataLayer||[];\n"
"function gtag(){dataLayer.push(arguments);}\n"
"gtag('js',new Date());\n"
"gtag('config','" + analytics_id + "');\n"
"</script>\n";
contents.insert(pos, tag);
}
}
// Explorer nav link injection removed — each HTML page has
// client-side JS that checks currency_info.explorer_enabled
// and injects the link dynamically. Server-side injection was
// fragile (depended on exact ">Classic</a>" text) and caused
// duplicate buttons on pages that had both mechanisms.
response.set(http::field::content_type, mime);
response.set(http::field::cache_control, "public, max-age=3600");
response.body() = std::move(contents);
response.prepare_payload();
send_response(std::move(response));
return;
}
// Dashboard IS configured, the request matched no REST
// endpoint above, and no such on-disk asset exists -> a real
// 404. Previously this fell through to getinfo() below, so
// unknown paths (explorer.html, blocks.html, typos, scanner
// probes) leaked the node-info JSON instead of Not Found.
response.result(http::status::not_found);
response.set(http::field::content_type, "text/plain; charset=utf-8");
response.body() = "404 Not Found";
response.prepare_payload();
send_response(std::move(response));
return;
}
// Fallback to getinfo JSON (API-only mode: no --dashboard-dir set)
rest_result = mining_interface_->getinfo();
} // end static file serving else
} // end explorer/static dispatch
if (!rest_result.is_null())
response_body = rest_result.dump();
}
else if (request_.method() == http::verb::post) {
// Handle JSON-RPC POST request.
// Accept both 1.0 and 2.0 — upgrade 1.0 to 2.0 for the library.
std::string request_body = request_.body();
{
auto pos = request_body.find("\"1.0\"");
if (pos != std::string::npos) {
auto ctx = request_body.rfind("jsonrpc", pos);
if (ctx != std::string::npos && pos - ctx < 15)
request_body.replace(pos, 5, "\"2.0\"");
}
}
response_body = mining_interface_->HandleRequest(request_body);
}
else {
response.result(http::status::method_not_allowed);
response_body = R"({"error":"Method not allowed"})";
}
response.body() = response_body;
response.prepare_payload();
} catch (const std::exception& e) {
LOG_ERROR << "Error processing request: " << e.what();
response.result(http::status::internal_server_error);
response.body() = R"({"error":"Internal server error"})";
response.prepare_payload();
}
send_response(std::move(response));
}
void HttpSession::send_response(http::response<http::string_body> response)
{
auto self = shared_from_this();
auto response_ptr = std::make_shared<http::response<http::string_body>>(std::move(response));
http::async_write(socket_, *response_ptr,
[self, response_ptr](beast::error_code ec, std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
if(ec)
return self->handle_error(ec, "send_response");
// Gracefully close the connection
beast::error_code close_ec;
self->socket_.shutdown(tcp::socket::shutdown_send, close_ec);
});
}
void HttpSession::handle_error(beast::error_code ec, char const* what)
{
if (ec != beast::errc::operation_canceled && ec != beast::errc::broken_pipe) {
LOG_WARNING << "HTTP Session error in " << what << ": " << ec.message();
}
}
/// MiningInterface Implementation
MiningInterface::MiningInterface(bool testnet, std::shared_ptr<IMiningNode> node, Blockchain blockchain)
: m_work_id_counter(1)
, m_testnet(testnet)
, m_blockchain(blockchain)
, m_node(node)
, m_address_validator(blockchain, testnet ? Network::TESTNET : Network::MAINNET)
, m_payout_manager(std::make_unique<c2pool::payout::PayoutManager>(1.0, 86400)) // 1% fee, 24h window
, m_solo_mode(false)
, m_solo_address("")
{
setup_methods();
}
void MiningInterface::setup_methods()
{
// Core mining methods - explicitly cast to MethodHandle
Add("getwork", jsonrpccxx::MethodHandle([this](const nlohmann::json& params) -> nlohmann::json {
return getwork();
}));
Add("submitwork", jsonrpccxx::MethodHandle([this](const nlohmann::json& params) -> nlohmann::json {
if (params.size() < 3) {
throw jsonrpccxx::JsonRpcException(-1, "submitwork requires 3 parameters");
}
return submitwork(params[0], params[1], params[2]);
}));
Add("getblocktemplate", jsonrpccxx::MethodHandle([this](const nlohmann::json& params) -> nlohmann::json {
nlohmann::json template_params = params.empty() ? nlohmann::json::array() : params;
return getblocktemplate(template_params);
}));
Add("submitblock", jsonrpccxx::MethodHandle([this](const nlohmann::json& params) -> nlohmann::json {
if (params.empty()) {
throw jsonrpccxx::JsonRpcException(-1, "submitblock requires hex data parameter");
}
return submitblock(params[0]);
}));
// Pool info methods
Add("getinfo", jsonrpccxx::MethodHandle([this](const nlohmann::json& params) -> nlohmann::json {
return getinfo();
}));
Add("getstats", jsonrpccxx::MethodHandle([this](const nlohmann::json& params) -> nlohmann::json {
return getstats();
}));
Add("getpeerinfo", jsonrpccxx::MethodHandle([this](const nlohmann::json& params) -> nlohmann::json {
return getpeerinfo();
}));
// Stratum methods
Add("mining.subscribe", jsonrpccxx::MethodHandle([this](const nlohmann::json& params) -> nlohmann::json {
std::string user_agent = params.empty() ? "" : params[0];
return mining_subscribe(user_agent);
}));
Add("mining.authorize", jsonrpccxx::MethodHandle([this](const nlohmann::json& params) -> nlohmann::json {
if (params.size() < 2) {
throw jsonrpccxx::JsonRpcException(-1, "mining.authorize requires username and password");
}
return mining_authorize(params[0], params[1]);
}));
Add("mining.submit", jsonrpccxx::MethodHandle([this](const nlohmann::json& params) -> nlohmann::json {
if (params.size() < 5) {
throw jsonrpccxx::JsonRpcException(-1, "mining.submit requires 5 parameters");
}
return mining_submit(params[0], params[1], "", params[2], params[3], params[4]);
}));
// Enhanced payout and coinbase methods
Add("validate_address", jsonrpccxx::MethodHandle([this](const nlohmann::json& params) -> nlohmann::json {
if (params.empty()) {
throw jsonrpccxx::JsonRpcException(-1, "validate_address requires address parameter");
}
return validate_address(params[0]);
}));
Add("build_coinbase", jsonrpccxx::MethodHandle([this](const nlohmann::json& params) -> nlohmann::json {
if (params.empty()) {
throw jsonrpccxx::JsonRpcException(-1, "build_coinbase requires parameters object");
}
return build_coinbase(params);
}));
Add("validate_coinbase", jsonrpccxx::MethodHandle([this](const nlohmann::json& params) -> nlohmann::json {