-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathdatum_api.c
More file actions
1887 lines (1670 loc) · 73.8 KB
/
datum_api.c
File metadata and controls
1887 lines (1670 loc) · 73.8 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
/*
*
* DATUM Gateway
* Decentralized Alternative Templates for Universal Mining
*
* This file is part of OCEAN's Bitcoin mining decentralization
* project, DATUM.
*
* https://ocean.xyz
*
* ---
*
* Copyright (c) 2024-2025 Bitcoin Ocean, LLC & Jason Hughes
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
// This is quick and dirty for now. Will be improved over time.
#include <assert.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <microhttpd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <pthread.h>
#include <inttypes.h>
#include <jansson.h>
#include "datum_api.h"
#include "datum_blocktemplates.h"
#include "datum_conf.h"
#include "datum_gateway.h"
#include "datum_jsonrpc.h"
#include "datum_utils.h"
#include "datum_stratum.h"
#include "datum_sockets.h"
#include "datum_protocol.h"
#include "web_resources.h"
const char * const homepage_html_end = "</body></html>";
#define DATUM_API_HOMEPAGE_MAX_SIZE 128000
const char *cbnames[] = {
"Blank",
"Tiny",
"Default",
"Respect",
"Yuge",
"Antmain2"
};
typedef struct MHD_Response *(*create_response_func_t)();
static struct MHD_Response *datum_api_create_empty_mhd_response() {
return MHD_create_response_from_buffer(0, "", MHD_RESPMEM_PERSISTENT);
}
static void html_leading_zeros(char * const buffer, const size_t buffer_size, const char * const numstr) {
int zeros = 0;
while (numstr[zeros] == '0') {
++zeros;
}
if (zeros) {
snprintf(buffer, buffer_size, "<span class='leading_zeros'>%.*s</span>%s", zeros, numstr, &numstr[zeros]);
}
}
void datum_api_var_DATUM_SHARES_ACCEPTED(char *buffer, size_t buffer_size, const T_DATUM_API_DASH_VARS *vardata) {
// Show pool shares when in pooled mode, stratum client shares when in solo mode
// This allows solo miners to see their actual mining activity instead of zeros
if (datum_config.datum_pooled_mining_only) {
snprintf(buffer, buffer_size, "%llu (%llu diff)", (unsigned long long)datum_accepted_share_count, (unsigned long long)datum_accepted_share_diff);
} else {
snprintf(buffer, buffer_size, "%llu (%llu diff)", (unsigned long long)stratum_client_accepted_share_count, (unsigned long long)stratum_client_accepted_share_diff);
}
}
void datum_api_var_DATUM_SHARES_REJECTED(char *buffer, size_t buffer_size, const T_DATUM_API_DASH_VARS *vardata) {
// Show pool shares when in pooled mode, stratum client shares when in solo mode
// This allows solo miners to see their actual mining activity instead of zeros
if (datum_config.datum_pooled_mining_only) {
snprintf(buffer, buffer_size, "%llu (%llu diff)", (unsigned long long)datum_rejected_share_count, (unsigned long long)datum_rejected_share_diff);
} else {
snprintf(buffer, buffer_size, "%llu (%llu diff)", (unsigned long long)stratum_client_rejected_share_count, (unsigned long long)stratum_client_rejected_share_diff);
}
}
void datum_api_var_DATUM_CONNECTION_STATUS(char *buffer, size_t buffer_size, const T_DATUM_API_DASH_VARS *vardata) {
const char *colour = "lime";
const char *s, *s2 = "";
const char * const bt_err = datum_blocktemplates_error;
if (bt_err) {
colour = "red";
s = "ERROR: ";
s2 = bt_err;
} else if (!vardata->sjob) {
colour = "silver";
s = "Initialising...";
} else if (datum_protocol_is_active()) {
s = "Connected and Ready";
} else if (datum_config.datum_pooled_mining_only && datum_config.datum_pool_host[0]) {
colour = "red";
s = "Not Ready";
} else {
if (datum_config.datum_pool_host[0]) {
colour = "yellow";
}
s = "Non-Pooled Mode";
}
snprintf(buffer, buffer_size, "<svg viewBox='0 0 100 100' role='img' style='width:1em;height:1em'><circle cx='50' cy='60' r='35' style='fill:%s' /></svg> %s%s", colour, s, s2);
}
void datum_api_var_DATUM_POOL_HOST(char *buffer, size_t buffer_size, const T_DATUM_API_DASH_VARS *vardata) {
if (datum_config.datum_pool_host[0]) {
snprintf(buffer, buffer_size, "%s:%u", datum_config.datum_pool_host, (unsigned)datum_config.datum_pool_port);
} else {
snprintf(buffer, buffer_size, "N/A");
}
}
void datum_api_var_DATUM_POOL_TAG(char *buffer, size_t buffer_size, const T_DATUM_API_DASH_VARS *vardata) {
size_t i;
buffer[0] = '"';
i = strncpy_html_escape(&buffer[1], datum_protocol_is_active()?datum_config.override_mining_coinbase_tag_primary:datum_config.mining_coinbase_tag_primary, buffer_size-3);
buffer[i+1] = '"';
buffer[i+2] = 0;
}
void datum_api_var_DATUM_MINER_TAG(char *buffer, size_t buffer_size, const T_DATUM_API_DASH_VARS *vardata) {
size_t i;
buffer[0] = '"';
i = strncpy_html_escape(&buffer[1], datum_config.mining_coinbase_tag_secondary, buffer_size-3);
buffer[i+1] = '"';
buffer[i+2] = 0;
}
void datum_api_var_DATUM_POOL_DIFF(char *buffer, size_t buffer_size, const T_DATUM_API_DASH_VARS *vardata) {
snprintf(buffer, buffer_size, "%llu", (unsigned long long)datum_config.override_vardiff_min);
}
void datum_api_var_DATUM_POOL_PUBKEY(char *buffer, size_t buffer_size, const T_DATUM_API_DASH_VARS *vardata) {
snprintf(buffer, buffer_size, "%s", datum_config.datum_pool_pubkey);
}
void datum_api_var_STRATUM_ACTIVE_THREADS(char *buffer, size_t buffer_size, const T_DATUM_API_DASH_VARS *vardata) {
snprintf(buffer, buffer_size, "%d", vardata->STRATUM_ACTIVE_THREADS);
}
void datum_api_var_STRATUM_TOTAL_CONNECTIONS(char *buffer, size_t buffer_size, const T_DATUM_API_DASH_VARS *vardata) {
snprintf(buffer, buffer_size, "%d", vardata->STRATUM_TOTAL_CONNECTIONS);
}
void datum_api_var_STRATUM_TOTAL_SUBSCRIPTIONS(char *buffer, size_t buffer_size, const T_DATUM_API_DASH_VARS *vardata) {
snprintf(buffer, buffer_size, "%d", vardata->STRATUM_TOTAL_SUBSCRIPTIONS);
}
void datum_api_var_STRATUM_HASHRATE_ESTIMATE(char *buffer, size_t buffer_size, const T_DATUM_API_DASH_VARS *vardata) {
snprintf(buffer, buffer_size, "%.2f Th/sec", vardata->STRATUM_HASHRATE_ESTIMATE);
}
void datum_api_var_DATUM_PROCESS_UPTIME(char *buffer, size_t buffer_size, const T_DATUM_API_DASH_VARS *vardata) {
uint64_t uptime_seconds = get_process_uptime_seconds();
uint64_t days = uptime_seconds / (24 * 3600);
unsigned int hours = (uptime_seconds % (24 * 3600)) / 3600;
unsigned int minutes = (uptime_seconds % 3600) / 60;
unsigned int seconds = uptime_seconds % 60;
if (days > 0) {
snprintf(buffer, buffer_size, "%"PRIu64" days, %u hours, %u minutes, %u seconds",
days, hours, minutes, seconds);
} else if (hours > 0) {
snprintf(buffer, buffer_size, "%u hours, %u minutes, %u seconds",
hours, minutes, seconds);
} else if (minutes > 0) {
snprintf(buffer, buffer_size, "%u minutes, %u seconds",
minutes, seconds);
} else {
snprintf(buffer, buffer_size, "%u seconds", seconds);
}
}
void datum_api_var_STRATUM_JOB_INFO(char *buffer, size_t buffer_size, const T_DATUM_API_DASH_VARS *vardata) {
if (!vardata->sjob) return;
snprintf(buffer, buffer_size, "%s (%d) @ %.3f", vardata->sjob->job_id, vardata->sjob->global_index, (double)vardata->sjob->tsms / 1000.0);
}
void datum_api_var_STRATUM_JOB_BLOCK_HEIGHT(char *buffer, size_t buffer_size, const T_DATUM_API_DASH_VARS *vardata) {
snprintf(buffer, buffer_size, "%llu", (unsigned long long)vardata->sjob->block_template->height);
}
void datum_api_var_STRATUM_JOB_BLOCK_VALUE(char *buffer, size_t buffer_size, const T_DATUM_API_DASH_VARS *vardata) {
snprintf(buffer, buffer_size, "%.8f BTC", (double)vardata->sjob->block_template->coinbasevalue / (double)100000000.0);
}
void datum_api_var_STRATUM_JOB_TARGET(char *buffer, size_t buffer_size, const T_DATUM_API_DASH_VARS *vardata) {
html_leading_zeros(buffer, buffer_size, vardata->sjob->block_template->block_target_hex);
}
void datum_api_var_STRATUM_JOB_PREVBLOCK(char *buffer, size_t buffer_size, const T_DATUM_API_DASH_VARS *vardata) {
html_leading_zeros(buffer, buffer_size, vardata->sjob->block_template->previousblockhash);
}
void datum_api_var_STRATUM_JOB_WITNESS(char *buffer, size_t buffer_size, const T_DATUM_API_DASH_VARS *vardata) {
snprintf(buffer, buffer_size, "%s", vardata->sjob->block_template->default_witness_commitment);
}
void datum_api_var_STRATUM_JOB_DIFF(char *buffer, size_t buffer_size, const T_DATUM_API_DASH_VARS *vardata) {
snprintf(buffer, buffer_size, "%.3Lf", calc_network_difficulty(vardata->sjob->nbits));
}
void datum_api_var_STRATUM_JOB_VERSION(char *buffer, size_t buffer_size, const T_DATUM_API_DASH_VARS *vardata) {
snprintf(buffer, buffer_size, "%s (%u)", vardata->sjob->version, (unsigned)vardata->sjob->version_uint);
}
void datum_api_var_STRATUM_JOB_BITS(char *buffer, size_t buffer_size, const T_DATUM_API_DASH_VARS *vardata) {
snprintf(buffer, buffer_size, "%s", vardata->sjob->nbits);
}
void datum_api_var_STRATUM_JOB_TIMEINFO(char *buffer, size_t buffer_size, const T_DATUM_API_DASH_VARS *vardata) {
snprintf(buffer, buffer_size, "Current: %llu / Min: %llu", (unsigned long long)vardata->sjob->block_template->curtime, (unsigned long long)vardata->sjob->block_template->mintime);
}
void datum_api_var_STRATUM_JOB_LIMITINFO(char *buffer, size_t buffer_size, const T_DATUM_API_DASH_VARS *vardata) {
snprintf(buffer, buffer_size, "Size: %lu, Weight: %lu, SigOps: %lu", (unsigned long)vardata->sjob->block_template->sizelimit, (unsigned long)vardata->sjob->block_template->weightlimit, (unsigned long)vardata->sjob->block_template->sigoplimit);
}
void datum_api_var_STRATUM_JOB_SIZE(char *buffer, size_t buffer_size, const T_DATUM_API_DASH_VARS *vardata) {
snprintf(buffer, buffer_size, "%lu", (unsigned long)vardata->sjob->block_template->txn_total_size);
}
void datum_api_var_STRATUM_JOB_WEIGHT(char *buffer, size_t buffer_size, const T_DATUM_API_DASH_VARS *vardata) {
snprintf(buffer, buffer_size, "%lu", (unsigned long)vardata->sjob->block_template->txn_total_weight);
}
void datum_api_var_STRATUM_JOB_SIGOPS(char *buffer, size_t buffer_size, const T_DATUM_API_DASH_VARS *vardata) {
snprintf(buffer, buffer_size, "%lu", (unsigned long)vardata->sjob->block_template->txn_total_sigops);
}
void datum_api_var_STRATUM_JOB_TXNCOUNT(char *buffer, size_t buffer_size, const T_DATUM_API_DASH_VARS *vardata) {
snprintf(buffer, buffer_size, "%u", (unsigned)vardata->sjob->block_template->txn_count);
}
DATUM_API_VarEntry var_entries[] = {
{"DATUM_SHARES_ACCEPTED", datum_api_var_DATUM_SHARES_ACCEPTED},
{"DATUM_SHARES_REJECTED", datum_api_var_DATUM_SHARES_REJECTED},
{"DATUM_CONNECTION_STATUS", datum_api_var_DATUM_CONNECTION_STATUS},
{"DATUM_POOL_HOST", datum_api_var_DATUM_POOL_HOST},
{"DATUM_POOL_TAG", datum_api_var_DATUM_POOL_TAG},
{"DATUM_MINER_TAG", datum_api_var_DATUM_MINER_TAG},
{"DATUM_POOL_DIFF", datum_api_var_DATUM_POOL_DIFF},
{"DATUM_POOL_PUBKEY", datum_api_var_DATUM_POOL_PUBKEY},
{"DATUM_PROCESS_UPTIME", datum_api_var_DATUM_PROCESS_UPTIME},
{"STRATUM_ACTIVE_THREADS", datum_api_var_STRATUM_ACTIVE_THREADS},
{"STRATUM_TOTAL_CONNECTIONS", datum_api_var_STRATUM_TOTAL_CONNECTIONS},
{"STRATUM_TOTAL_SUBSCRIPTIONS", datum_api_var_STRATUM_TOTAL_SUBSCRIPTIONS},
{"STRATUM_HASHRATE_ESTIMATE", datum_api_var_STRATUM_HASHRATE_ESTIMATE},
{"STRATUM_JOB_INFO", datum_api_var_STRATUM_JOB_INFO},
{"STRATUM_JOB_BLOCK_HEIGHT", datum_api_var_STRATUM_JOB_BLOCK_HEIGHT},
{"STRATUM_JOB_BLOCK_VALUE", datum_api_var_STRATUM_JOB_BLOCK_VALUE},
{"STRATUM_JOB_PREVBLOCK", datum_api_var_STRATUM_JOB_PREVBLOCK},
{"STRATUM_JOB_TARGET", datum_api_var_STRATUM_JOB_TARGET},
{"STRATUM_JOB_WITNESS", datum_api_var_STRATUM_JOB_WITNESS},
{"STRATUM_JOB_DIFF", datum_api_var_STRATUM_JOB_DIFF},
{"STRATUM_JOB_VERSION", datum_api_var_STRATUM_JOB_VERSION},
{"STRATUM_JOB_BITS", datum_api_var_STRATUM_JOB_BITS},
{"STRATUM_JOB_TIMEINFO", datum_api_var_STRATUM_JOB_TIMEINFO},
{"STRATUM_JOB_LIMITINFO", datum_api_var_STRATUM_JOB_LIMITINFO},
{"STRATUM_JOB_SIZE", datum_api_var_STRATUM_JOB_SIZE},
{"STRATUM_JOB_WEIGHT", datum_api_var_STRATUM_JOB_WEIGHT},
{"STRATUM_JOB_SIGOPS", datum_api_var_STRATUM_JOB_SIGOPS},
{"STRATUM_JOB_TXNCOUNT", datum_api_var_STRATUM_JOB_TXNCOUNT},
{NULL, NULL} // Mark the end of the array
};
DATUM_API_VarFunc datum_api_find_var_func(const char * const var_start, const size_t var_name_len) {
for (int i = 0; var_entries[i].var_name != NULL; i++) {
if (strncmp(var_entries[i].var_name, var_start, var_name_len) == 0 && !var_entries[i].var_name[var_name_len]) {
return var_entries[i].func;
}
}
return NULL; // Variable not found
}
size_t datum_api_fill_var(const char * const var_start, const size_t var_name_len, char * const replacement, const size_t replacement_max_len, const T_DATUM_API_DASH_VARS * const vardata) {
DATUM_API_VarFunc func = datum_api_find_var_func(var_start, var_name_len);
if (!func) {
DLOG_ERROR("%s: Unknown variable '%.*s'", __func__, (int)var_name_len, var_start);
return 0;
}
// Skip running STRATUM_JOB functions if there's no sjob
if (var_start[8] == 'J' && !vardata->sjob) {
// Leave blank for now
return 0;
}
assert(replacement_max_len > 0);
replacement[0] = 0;
func(replacement, replacement_max_len, vardata);
return strlen(replacement);
}
size_t datum_api_fill_vars(const char *input, char *output, size_t max_output_size, const DATUM_API_VarFillFunc var_fill_func, const T_DATUM_API_DASH_VARS *vardata) {
const char* p = input;
size_t output_len = 0;
size_t var_name_len = 0;
const char *var_start;
const char *var_end;
while (*p && output_len < max_output_size - 1) {
if (strncmp(p, "${", 2) == 0) {
p += 2; // Skip "${"
var_start = p;
var_end = strchr(p, '}');
if (!var_end) {
DLOG_ERROR("%s: Missing closing } for variable", __func__);
break;
}
var_name_len = var_end - var_start;
char * const replacement = &output[output_len];
size_t replacement_max_len = max_output_size - output_len;
if (replacement_max_len > 256) replacement_max_len = 256;
const size_t replacement_len = var_fill_func(var_start, var_name_len, replacement, replacement_max_len, vardata);
output_len += replacement_len;
output[output_len] = 0;
p = var_end + 1; // Move past '}'
} else {
output[output_len++] = *p++;
output[output_len] = 0;
}
}
output[output_len] = 0;
return output_len;
}
size_t strncpy_html_escape(char *dest, const char *src, size_t n) {
size_t i = 0;
while (*src && i < n) {
switch (*src) {
case '&':
if (i + 5 <= n) { // &
dest[i++] = '&';
dest[i++] = 'a';
dest[i++] = 'm';
dest[i++] = 'p';
dest[i++] = ';';
} else {
return i; // Stop if there's not enough space
}
break;
case '<':
if (i + 4 <= n) { // <
dest[i++] = '&';
dest[i++] = 'l';
dest[i++] = 't';
dest[i++] = ';';
} else {
return i; // Stop if there's not enough space
}
break;
case '>':
if (i + 4 <= n) { // >
dest[i++] = '&';
dest[i++] = 'g';
dest[i++] = 't';
dest[i++] = ';';
} else {
return i; // Stop if there's not enough space
}
break;
case '"':
if (i + 6 <= n) { // "
dest[i++] = '&';
dest[i++] = 'q';
dest[i++] = 'u';
dest[i++] = 'o';
dest[i++] = 't';
dest[i++] = ';';
} else {
return i; // Stop if there's not enough space
}
break;
default:
dest[i++] = *src;
break;
}
src++;
}
// Null-terminate the destination string if there's space
if (i < n) {
dest[i] = '\0';
}
return i;
}
static void http_resp_prevent_caching(struct MHD_Response * const response) {
MHD_add_response_header(response, "Cache-Control", "no-cache, no-store, must-revalidate");
MHD_add_response_header(response, "Pragma", "no-cache");
MHD_add_response_header(response, "Expires", "0");
}
static enum MHD_Result datum_api_formdata_to_json_cb(void * const cls, const enum MHD_ValueKind kind, const char * const key, const char * const filename, const char * const content_type, const char * const transfer_encoding, const char * const data, const uint64_t off, const size_t size) {
if (!key) return MHD_YES;
if (off) return MHD_YES;
assert(cls);
json_t * const j = cls;
json_object_set_new(j, key, json_stringn(data, size));
return MHD_YES;
}
bool datum_api_formdata_to_json(struct MHD_Connection * const connection, char * const post, const int len, json_t * const j) {
struct MHD_PostProcessor * const pp = MHD_create_post_processor(connection, 32768, datum_api_formdata_to_json_cb, j);
if (!pp) {
return false;
}
if (MHD_YES != MHD_post_process(pp, post, len)) {
MHD_destroy_post_processor(pp);
return false;
}
MHD_destroy_post_processor(pp);
return true;
}
int datum_api_submit_uncached_response(struct MHD_Connection * const connection, const unsigned int status_code, struct MHD_Response * const response) {
http_resp_prevent_caching(response);
int ret = MHD_queue_response(connection, status_code, response);
MHD_destroy_response(response);
return ret;
}
int datum_api_do_error(struct MHD_Connection * const connection, const unsigned int status_code) {
struct MHD_Response *response = datum_api_create_empty_mhd_response();
return datum_api_submit_uncached_response(connection, status_code, response);
}
bool datum_api_check_admin_password_only(struct MHD_Connection * const connection, const char * const password, const create_response_func_t auth_failure_response_creator) {
if (datum_secure_strequals(datum_config.api_admin_password, datum_config.api_admin_password_len, password) && datum_config.api_admin_password_len) {
return true;
}
DLOG_DEBUG("Wrong password in request");
datum_api_submit_uncached_response(connection, MHD_HTTP_FORBIDDEN, auth_failure_response_creator());
return false;
}
static enum MHD_DigestAuthAlgorithm datum_api_pick_digest_algo(struct MHD_Connection * const connection, const bool nonce_is_stale) {
const char * const ua = MHD_lookup_connection_value(connection, MHD_HEADER_KIND, "User-Agent");
if (strstr(ua, "AppleWebKit/") && !(strstr(ua, "Chrome/") || strstr(ua, "Brave/") || strstr(ua, "Edge/"))) {
static bool safari_warned = false;
if (!(nonce_is_stale && safari_warned)) {
DLOG_WARN("Detected login request from Apple Safari. For some reason, this browser only supports obsolete and insecure MD5 digest authentication. Login at your own risk!");
safari_warned = true;
}
return MHD_DIGEST_ALG_MD5;
}
return MHD_DIGEST_ALG_SHA256;
}
bool datum_api_check_admin_password_httponly(struct MHD_Connection * const connection, const create_response_func_t auth_failure_response_creator) {
int ret;
char * const username = MHD_digest_auth_get_username(connection);
const char * const realm = "DATUM Gateway";
if (username) {
ret = MHD_digest_auth_check2(connection, realm, username, datum_config.api_admin_password, 300, MHD_DIGEST_ALG_AUTO);
free(username);
} else {
ret = MHD_NO;
}
if (ret != MHD_YES) {
const bool nonce_is_stale = (ret == MHD_INVALID_NONCE);
if (username && !nonce_is_stale) {
DLOG_DEBUG("Wrong password in HTTP authentication");
}
const enum MHD_DigestAuthAlgorithm algo = datum_api_pick_digest_algo(connection, nonce_is_stale);
struct MHD_Response * const response = auth_failure_response_creator();
ret = MHD_queue_auth_fail_response2(connection, realm, datum_config.api_csrf_token, response, nonce_is_stale ? MHD_YES : MHD_NO, algo);
MHD_destroy_response(response);
return false;
}
return true;
}
bool datum_api_check_admin_password(struct MHD_Connection * const connection, const json_t * const j, const create_response_func_t auth_failure_response_creator) {
const json_t * const j_password = json_object_get(j, "password");
if (json_is_string(j_password)) {
return datum_api_check_admin_password_only(connection, json_string_value(j_password), auth_failure_response_creator);
}
// Only accept HTTP authentication if there's an anti-CSRF token
const json_t * const j_csrf = json_object_get(j, "csrf");
if (!json_is_string(j_csrf)) {
DLOG_DEBUG("Missing CSRF token in request");
datum_api_submit_uncached_response(connection, MHD_HTTP_FORBIDDEN, auth_failure_response_creator());
return false;
}
if (!datum_secure_strequals(datum_config.api_csrf_token, sizeof(datum_config.api_csrf_token)-1, json_string_value(j_csrf))) {
DLOG_DEBUG("Wrong CSRF token in request");
datum_api_submit_uncached_response(connection, MHD_HTTP_FORBIDDEN, auth_failure_response_creator());
return false;
}
return datum_api_check_admin_password_httponly(connection, auth_failure_response_creator);
}
static struct MHD_Response *datum_api_create_response_authfail(const char * const head, const size_t head_sz) {
const size_t max_sz = head_sz + www_auth_failed_html_sz + www_foot_html_sz + 1;
size_t sz = 0;
char * const output = malloc(max_sz);
if (!output) {
return datum_api_create_empty_mhd_response();
}
memcpy(&output[sz], head, head_sz);
sz += head_sz;
memcpy(&output[sz], www_auth_failed_html, www_auth_failed_html_sz);
sz += www_auth_failed_html_sz;
memcpy(&output[sz], www_foot_html, www_foot_html_sz);
sz += www_foot_html_sz;
struct MHD_Response * const response = MHD_create_response_from_buffer(sz, output, MHD_RESPMEM_MUST_FREE);
MHD_add_response_header(response, "Content-Type", "text/html");
return response;
}
static struct MHD_Response *datum_api_create_response_authfail_clients() {
return datum_api_create_response_authfail(www_clients_top_html, www_clients_top_html_sz);
}
size_t datum_api_fill_authfail_error(const char * const var_start, const size_t var_name_len, char * const replacement, const size_t replacement_max_len, const T_DATUM_API_DASH_VARS * const vardata) {
assert(replacement_max_len >= www_auth_failed_html_sz);
memcpy(replacement, www_auth_failed_html, www_auth_failed_html_sz);
return www_auth_failed_html_sz;
}
static struct MHD_Response *datum_api_create_response_authfail_config() {
const size_t max_sz = www_config_errors_html_sz + www_auth_failed_html_sz;
char * const output = malloc(max_sz);
if (!output) {
return datum_api_create_empty_mhd_response();
}
const size_t sz = datum_api_fill_vars(www_config_errors_html, output, max_sz, datum_api_fill_authfail_error, NULL);
struct MHD_Response * const response = MHD_create_response_from_buffer(sz, output, MHD_RESPMEM_MUST_FREE);
MHD_add_response_header(response, "Content-Type", "text/html");
return response;
}
static struct MHD_Response *datum_api_create_response_authfail_threads() {
return datum_api_create_response_authfail(www_threads_top_html, www_threads_top_html_sz);
}
static int datum_api_asset(struct MHD_Connection * const connection, const char * const mimetype, const char * const data, const size_t datasz, const char * const etag) {
const char * const if_none_match_header = MHD_lookup_connection_value(connection, MHD_HEADER_KIND, "If-None-Match");
if (if_none_match_header && 0 == strcmp(if_none_match_header, etag)) {
struct MHD_Response *response = datum_api_create_empty_mhd_response();
MHD_add_response_header(response, "Etag", etag);
int ret = MHD_queue_response(connection, MHD_HTTP_NOT_MODIFIED, response);
MHD_destroy_response(response);
return ret;
}
struct MHD_Response * const response = MHD_create_response_from_buffer(datasz, (void*)data, MHD_RESPMEM_PERSISTENT);
MHD_add_response_header(response, "Content-Type", mimetype);
MHD_add_response_header(response, "Etag", etag);
const int ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
MHD_destroy_response (response);
return ret;
}
void datum_api_cmd_empty_thread(int tid) {
if (global_stratum_app && (tid >= 0) && (tid < global_stratum_app->max_threads)) {
DLOG_WARN("API Request to empty stratum thread %d!", tid);
global_stratum_app->datum_threads[tid].empty_request = true;
}
}
void datum_api_cmd_kill_client(int tid, int cid) {
if (global_stratum_app && (tid >= 0) && (tid < global_stratum_app->max_threads)) {
if ((cid >= 0) && (cid < global_stratum_app->max_clients_thread)) {
DLOG_WARN("API Request to disconnect stratum client %d/%d!", tid, cid);
global_stratum_app->datum_threads[tid].client_data[cid].kill_request = true;
global_stratum_app->datum_threads[tid].has_client_kill_request = true;
}
}
}
void datum_api_cmd_kill_client2(const char * const data, const size_t size, const char ** const redirect_p) {
const char * const end = &data[size];
const char *underscore_pos = memchr(data, '_', size);
if (!underscore_pos) return;
const size_t tid_size = underscore_pos - data;
const int tid = datum_atoi_strict(data, tid_size);
const char *p = &underscore_pos[1];
underscore_pos = memchr(p, '_', end - p);
if (!underscore_pos) underscore_pos = end;
const int cid = datum_atoi_strict(p, underscore_pos - p);
// Valid input; unconditionally redirect back to clients dashboard
*redirect_p = "/clients";
if (tid < 0 || tid >= global_stratum_app->max_threads || cid < 0 || cid >= global_stratum_app->max_clients_thread) {
return;
}
if (underscore_pos != end) {
// Check it's the same client intended
p = &underscore_pos[1];
underscore_pos = memchr(p, '_', end - p);
if (!underscore_pos) underscore_pos = end;
const uint64_t connect_tsms = datum_atoi_strict_u64(p, underscore_pos - p);
const T_DATUM_MINER_DATA * const m = global_stratum_app->datum_threads[tid].client_data[cid].app_client_data;
if (connect_tsms != m->connect_tsms) {
DLOG_WARN("API Request to disconnect FORMER stratum client %d/%d (ignored; connect tsms req=%lu vs cur=%lu)", tid, cid, (unsigned long)connect_tsms, (unsigned long)m->connect_tsms);
return;
}
p = &underscore_pos[1];
const uint64_t unique_id = datum_atoi_strict_u64(p, end - p);
if (unique_id != m->unique_id) {
DLOG_WARN("API Request to disconnect FORMER stratum client %d/%d (ignored; unique id req=%lu vs cur=%lu)", tid, cid, (unsigned long)unique_id, (unsigned long)m->unique_id);
return;
}
}
datum_api_cmd_kill_client(tid, cid);
}
int datum_api_cmd(struct MHD_Connection *connection, char *post, int len) {
struct MHD_Response *response;
char output[1024];
int sz = 0;
json_t *root, *cmd, *param;
json_error_t error;
const char *cstr;
int tid,cid;
if ((len) && (post)) {
DLOG_DEBUG("POST DATA: %s", post);
if (post[0] == '{') {
// attempt to parse JSON command
root = json_loadb(post, len, 0, &error);
if (root) {
if (json_is_object(root) && (cmd = json_object_get(root, "cmd"))) {
if (!datum_api_check_admin_password(connection, root, datum_api_create_empty_mhd_response)) {
json_decref(root);
return MHD_YES;
}
if (json_is_string(cmd)) {
cstr = json_string_value(cmd);
DLOG_DEBUG("JSON CMD: %s",cstr);
switch(cstr[0]) {
case 'e': {
if (!strcmp(cstr,"empty_thread")) {
param = json_object_get(root, "tid");
if (json_is_integer(param)) {
datum_api_cmd_empty_thread(json_integer_value(param));
}
break;
}
break;
}
case 'k': {
if (!strcmp(cstr,"kill_client")) {
param = json_object_get(root, "tid");
if (json_is_integer(param)) {
tid = json_integer_value(param);
param = json_object_get(root, "cid");
if (json_is_integer(param)) {
cid = json_integer_value(param);
datum_api_cmd_kill_client(tid,cid);
}
}
break;
}
break;
}
default: break;
}
}
}
json_decref(root);
}
} else {
root = json_object();
if (!datum_api_formdata_to_json(connection, post, len, root)) {
json_decref(root);
return datum_api_do_error(connection, MHD_HTTP_INTERNAL_SERVER_ERROR);
}
param = json_object_get(root, "empty_thread");
if (!datum_api_check_admin_password(connection, root, param ? datum_api_create_response_authfail_threads : datum_api_create_response_authfail_clients)) {
json_decref(root);
return MHD_YES;
}
const char *redirect = "/";
// param set for "empty_thread" above
if (param) {
tid = datum_atoi_strict(json_string_value(param), json_string_length(param));
if (tid != -1) {
datum_api_cmd_empty_thread(tid);
redirect = "/threads";
}
}
param = json_object_get(root, "kill_client");
if (param) {
const char * const data = json_string_value(param);
const size_t size = json_string_length(param);
datum_api_cmd_kill_client2(data, size, &redirect);
}
response = datum_api_create_empty_mhd_response();
MHD_add_response_header(response, "Location", redirect);
return datum_api_submit_uncached_response(connection, MHD_HTTP_FOUND, response);
}
}
sprintf(output, "{}");
response = MHD_create_response_from_buffer (sz, (void *) output, MHD_RESPMEM_MUST_COPY);
MHD_add_response_header(response, "Content-Type", "application/json");
return datum_api_submit_uncached_response(connection, MHD_HTTP_OK, response);
}
int datum_api_coinbaser(struct MHD_Connection *connection) {
struct MHD_Response *response;
T_DATUM_STRATUM_JOB *sjob;
int j, i, max_sz = 0, sz = 0;
char tempaddr[256];
uint64_t tv = 0;
char *output = NULL;
pthread_rwlock_rdlock(&stratum_global_job_ptr_lock);
j = global_latest_stratum_job_index;
sjob = (j >= 0 && j < MAX_STRATUM_JOBS) ? global_cur_stratum_jobs[j] : NULL;
pthread_rwlock_unlock(&stratum_global_job_ptr_lock);
max_sz = www_coinbaser_top_html_sz + www_foot_html_sz + (sjob ? (sjob->available_coinbase_outputs_count * 512) : 0) + 2048; // approximate max size of each row
output = calloc(max_sz+16,1);
if (!output) {
return MHD_NO;
}
sz = snprintf(output, max_sz-1-sz, "%s", www_coinbaser_top_html);
sz += snprintf(&output[sz], max_sz-1-sz, "<TABLE><TR><TD><U>Value</U></TD> <TD><U>Address</U></TD></TR>");
if (sjob) {
for(i=0;i<sjob->available_coinbase_outputs_count;i++) {
output_script_2_addr(sjob->available_coinbase_outputs[i].output_script, sjob->available_coinbase_outputs[i].output_script_len, tempaddr);
sz += snprintf(&output[sz], max_sz-1-sz, "<TR><TD>%.8f BTC</TD><TD>%s</TD></TR>", (double)sjob->available_coinbase_outputs[i].value_sats / (double)100000000.0, tempaddr);
tv += sjob->available_coinbase_outputs[i].value_sats;
}
if (tv < sjob->coinbase_value) {
output_script_2_addr(sjob->pool_addr_script, sjob->pool_addr_script_len, tempaddr);
sz += snprintf(&output[sz], max_sz-1-sz, "<TR><TD>%.8f BTC</TD><TD>%s</TD></TR>", (double)(sjob->coinbase_value - tv) / (double)100000000.0, tempaddr);
}
}
sz += snprintf(&output[sz], max_sz-1-sz, "</TABLE>");
sz += snprintf(&output[sz], max_sz-1-sz, "%s", www_foot_html);
response = MHD_create_response_from_buffer (sz, (void *) output, MHD_RESPMEM_MUST_FREE);
MHD_add_response_header(response, "Content-Type", "text/html");
return datum_api_submit_uncached_response(connection, MHD_HTTP_OK, response);
}
int datum_api_thread_dashboard(struct MHD_Connection *connection) {
struct MHD_Response *response;
int sz=0, max_sz = 0, j, ii;
char *output = NULL;
T_DATUM_MINER_DATA *m = NULL;
uint64_t tsms;
double hr;
unsigned char astat;
double thr = 0.0;
int subs,conns;
const int max_threads = global_stratum_app ? global_stratum_app->max_threads : 0;
max_sz = www_threads_top_html_sz + www_foot_html_sz + (max_threads * 512) + 2048; // approximate max size of each row
output = calloc(max_sz+16,1);
if (!output) {
return MHD_NO;
}
const bool have_admin = datum_config.api_admin_password_len;
tsms = current_time_millis();
sz = snprintf(output, max_sz-1-sz, "%s", www_threads_top_html);
sz += snprintf(&output[sz], max_sz-1-sz, "<form action='/cmd' method='post'><input type='hidden' name='csrf' value='%s' /><TABLE><TR><TD><U>TID</U></TD> <TD><U>Connection Count</U></TD> <TD><U>Sub Count</U></TD> <TD><U>Approx. Hashrate</U></TD> <TD><U>Command</U></TD></TR>", datum_config.api_csrf_token);
for (j = 0; j < max_threads; ++j) {
thr = 0.0;
subs = 0;
conns = 0;
for(ii=0;ii<global_stratum_app->max_clients_thread;ii++) {
if (global_stratum_app->datum_threads[j].client_data[ii].fd > 0) {
conns++;
m = (T_DATUM_MINER_DATA *)global_stratum_app->datum_threads[j].client_data[ii].app_client_data;
if (m->subscribed) {
subs++;
astat = m->stats.active_index?0:1; // inverted
hr = 0.0;
if ((m->stats.last_swap_ms > 0) && (m->stats.diff_accepted[astat] > 0)) {
hr = ((double)m->stats.diff_accepted[astat] / (double)((double)m->stats.last_swap_ms/1000.0)) * 0.004294967296; // Th/sec based on shares/sec
}
if (((double)(tsms - m->stats.last_swap_tsms)/1000.0) < 180.0) {
thr += hr;
}
}
}
}
if (conns) {
sz += snprintf(&output[sz], max_sz-1-sz, "<TR><TD>%d</TD> <TD>%d</TD> <TD>%d</TD> <TD>%.2f Th/s</TD><TD><button ", j, conns, subs, thr);
if (have_admin) {
sz += snprintf(&output[sz], max_sz-1-sz, "name='empty_thread' value='%d' onclick=\"sendPostRequest('/cmd', {cmd:'empty_thread',tid:%d}); return false;\"", j, j);
} else {
sz += snprintf(&output[sz], max_sz-1-sz, "disabled");
}
sz += snprintf(&output[sz], max_sz-1-sz, ">Disconnect All</button></TD></TR>");
}
}
sz += snprintf(&output[sz], max_sz-1-sz, "</TABLE></form>");
if (have_admin) {
sz += snprintf(&output[sz], max_sz-1-sz, "<script>");
sz += snprintf(&output[sz], max_sz-1-sz, www_assets_post_js, datum_config.api_csrf_token);
sz += snprintf(&output[sz], max_sz-1-sz, "</script>");
}
sz += snprintf(&output[sz], max_sz-1-sz, "%s", www_foot_html);
response = MHD_create_response_from_buffer (sz, (void *) output, MHD_RESPMEM_MUST_FREE);
MHD_add_response_header(response, "Content-Type", "text/html");
return datum_api_submit_uncached_response(connection, MHD_HTTP_OK, response);
}
int datum_api_client_dashboard(struct MHD_Connection *connection) {
struct MHD_Response *response;
int connected_clients = 0;
int i, sz = 0, max_sz = 0, j, ii;
char *output = NULL;
T_DATUM_MINER_DATA *m = NULL;
uint64_t tsms;
double hr;
unsigned char astat;
double thr = 0.0;
const int max_threads = global_stratum_app ? global_stratum_app->max_threads : 0;
for (i = 0; i < max_threads; ++i) {
connected_clients+=global_stratum_app->datum_threads[i].connected_clients;
}
max_sz = www_clients_top_html_sz + www_foot_html_sz + (connected_clients * 1024) + 2048; // approximate max size of each row
output = calloc(max_sz+16,1);
if (!output) {
return MHD_NO;
}
tsms = current_time_millis();
sz = snprintf(output, max_sz-1-sz, "%s", www_clients_top_html);
if (!datum_config.api_admin_password_len) {
sz += snprintf(&output[sz], max_sz-1-sz, "This page requires admin access (add \"admin_password\" to \"api\" section of config file)");
sz += snprintf(&output[sz], max_sz-1-sz, "%s", www_foot_html);
response = MHD_create_response_from_buffer(sz, output, MHD_RESPMEM_MUST_FREE);
MHD_add_response_header(response, "Content-Type", "text/html");
return datum_api_submit_uncached_response(connection, MHD_HTTP_OK, response);
}
if (!datum_api_check_admin_password_httponly(connection, datum_api_create_response_authfail_clients)) {
return MHD_YES;
}
sz += snprintf(&output[sz], max_sz-1-sz, "<form action='/cmd' method='post'><input type='hidden' name='csrf' value='%s' /><TABLE><TR><TD><U>TID/CID</U></TD> <TD><U>RemHost</U></TD> <TD><U>Auth Username</U></TD> <TD><U>Subbed</U></TD> <TD><U>Last Accepted</U></TD> <TD><U>VDiff</U></TD> <TD><U>DiffA (A)</U></TD> <TD><U>DiffR (R)</U></TD> <TD><U>Hashrate (age)</U></TD> <TD><U>Coinbase</U></TD> <TD><U>UserAgent</U> </TD><TD><U>Command</U></TD></TR>", datum_config.api_csrf_token);
for (j = 0; j < max_threads; ++j) {
for(ii=0;ii<global_stratum_app->max_clients_thread;ii++) {
if (global_stratum_app->datum_threads[j].client_data[ii].fd > 0) {
m = (T_DATUM_MINER_DATA *)global_stratum_app->datum_threads[j].client_data[ii].app_client_data;
sz += snprintf(&output[sz], max_sz-1-sz, "<TR><TD>%d/%d</TD>", j,ii);
sz += snprintf(&output[sz], max_sz-1-sz, "<TD>%s</TD>", global_stratum_app->datum_threads[j].client_data[ii].rem_host);
sz += snprintf(&output[sz], max_sz-1-sz, "<TD>");
sz += strncpy_html_escape(&output[sz], m->last_auth_username, max_sz-1-sz);
sz += snprintf(&output[sz], max_sz-1-sz, "</TD>");
if (m->subscribed) {
sz += snprintf(&output[sz], max_sz-1-sz, "<TD> <span style=\"font-family: monospace;\">%4.4x</span> %.1fs</TD>", m->sid, (double)(tsms - m->subscribe_tsms)/1000.0);
if (m->stats.last_share_tsms) {
sz += snprintf(&output[sz], max_sz-1-sz, "<TD>%.1fs</TD>", (double)(tsms - m->stats.last_share_tsms)/1000.0);
} else {
sz += snprintf(&output[sz], max_sz-1-sz, "<TD>N/A</TD>");
}
sz += snprintf(&output[sz], max_sz-1-sz, "<TD>%"PRIu64"</TD>", m->current_diff);
sz += snprintf(&output[sz], max_sz-1-sz, "<TD>%"PRIu64" (%"PRIu64")</TD>", m->share_diff_accepted, m->share_count_accepted);
hr = 0.0;
if (m->share_diff_accepted > 0) {
hr = ((double)m->share_diff_rejected / (double)(m->share_diff_accepted + m->share_diff_rejected))*100.0;
}
sz += snprintf(&output[sz], max_sz-1-sz, "<TD>%"PRIu64" (%"PRIu64") %.2f%%</TD>", m->share_diff_rejected, m->share_count_rejected, hr);
astat = m->stats.active_index?0:1; // inverted
hr = 0.0;
if ((m->stats.last_swap_ms > 0) && (m->stats.diff_accepted[astat] > 0)) {
hr = ((double)m->stats.diff_accepted[astat] / (double)((double)m->stats.last_swap_ms/1000.0)) * 0.004294967296; // Th/sec based on shares/sec
}
if (((double)(tsms - m->stats.last_swap_tsms)/1000.0) < 180.0) {
thr += hr;
}
if (m->share_diff_accepted > 0) {
sz += snprintf(&output[sz], max_sz-1-sz, "<TD>%.2f Th/s (%.1fs)</TD>", hr, (double)(tsms - m->stats.last_swap_tsms)/1000.0);
} else {
sz += snprintf(&output[sz], max_sz-1-sz, "<TD>N/A</TD>");
}
if (m->coinbase_selection < (sizeof(cbnames) / sizeof(cbnames[0]))) {
sz += snprintf(&output[sz], max_sz-1-sz, "<TD>%s</TD>", cbnames[m->coinbase_selection]);
} else {
sz += snprintf(&output[sz], max_sz-1-sz, "<TD>Unknown</TD>");
}
sz += snprintf(&output[sz], max_sz-1-sz, "<TD>");
sz += strncpy_html_escape(&output[sz], m->useragent, max_sz-1-sz);
sz += snprintf(&output[sz], max_sz-1-sz, "</TD>");
} else {
sz += snprintf(&output[sz], max_sz-1-sz, "<TD COLSPAN=\"8\">Not Subscribed</TD>");
}
sz += snprintf(&output[sz], max_sz-1-sz, "<TD><button name='kill_client' value='%d_%d_%lu_%lu' onclick=\"sendPostRequest('/cmd', {cmd:'kill_client',tid:%d,cid:%d,t:%lu,id:%lu}); return false;\">Kick</button></TD></TR>", j, ii, (unsigned long)m->connect_tsms, (unsigned long)m->unique_id, j, ii, (unsigned long)m->connect_tsms, (unsigned long)m->unique_id);
}
}
}
sz += snprintf(&output[sz], max_sz-1-sz, "</TABLE></form><p class=\"table-footer\">Total active hashrate estimate: %.2f Th/s</p><script>", thr);
sz += snprintf(&output[sz], max_sz-1-sz, www_assets_post_js, datum_config.api_csrf_token);
sz += snprintf(&output[sz], max_sz-1-sz, "</script>%s", www_foot_html);
// return the home page with some data and such
response = MHD_create_response_from_buffer (sz, (void *) output, MHD_RESPMEM_MUST_FREE);
MHD_add_response_header(response, "Content-Type", "text/html");
return datum_api_submit_uncached_response(connection, MHD_HTTP_OK, response);
}
size_t datum_api_fill_config_var(const char *var_start, const size_t var_name_len, char * const replacement, const size_t replacement_max_len, const T_DATUM_API_DASH_VARS * const vardata) {
const char *colon_pos = memchr(var_start, ':', var_name_len);
const char *var_start_2 = colon_pos ? &colon_pos[1] : var_start;
const char * const var_end = &var_start[var_name_len];
const size_t var_name_len_2 = var_end - var_start_2;
const char * const underscore_pos = memchr(var_start_2, '_', var_name_len_2);
int val;
if (var_name_len_2 == 3 && 0 == strncmp(var_start_2, "*ro", 3)) {
val = !(datum_config.api_modify_conf && datum_config.api_admin_password_len);
if (!colon_pos) {
var_start = "readonly:";
colon_pos = &var_start[8];
}
} else if (var_name_len_2 == 24 && 0 == strncmp(var_start_2, "*datum_pool_pass_workers", 24)) {
val = datum_config.datum_pool_pass_workers && !datum_config.datum_pool_pass_full_users;
} else if (var_name_len_2 == 16 && 0 == strncmp(var_start_2, "*datum_pool_host", 16)) {
const char *s = NULL;
if (datum_config.datum_pool_host[0]) {
s = datum_config.datum_pool_host;
} else if (datum_config.config_json) {
const json_t * const config = datum_config.config_json;
json_t *j = json_object_get(config, "datum");
if (j) j = json_is_object(j) ? json_object_get(j, "pool_host(old)") : NULL;
if (j && json_is_string(j) && json_string_length(j) <= 1023) {
s = json_string_value(j);
}
}
if (!s) {
const T_DATUM_CONFIG_ITEM * const cfginfo = datum_config_get_option_info("datum", 5, "pool_host", 9);
s = cfginfo->default_string[0];
}
size_t copy_sz = strlen(s);
if (copy_sz >= replacement_max_len) copy_sz = replacement_max_len - 1;
memcpy(replacement, s, copy_sz);
return copy_sz;
} else if (var_name_len_2 == 27 && 0 == strncmp(var_start_2, "*username_behaviour_private", 27)) {
val = !(datum_config.datum_pool_pass_workers || datum_config.datum_pool_pass_full_users);
} else if (var_name_len_2 == 22 && 0 == strncmp(var_start_2, "*reward_sharing_prefer", 22)) {