2018-08-12 13:46:08 +00:00
|
|
|
/*
|
2019-05-11 03:44:28 +00:00
|
|
|
Copyright (c) 2014-2019, The Monero Project
|
2018-08-12 13:46:08 +00:00
|
|
|
|
2019-05-11 03:44:28 +00:00
|
|
|
All rights reserved.
|
2018-08-12 13:46:08 +00:00
|
|
|
|
2019-05-11 03:44:28 +00:00
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
2018-08-12 13:46:08 +00:00
|
|
|
|
2019-05-11 03:44:28 +00:00
|
|
|
1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
list of conditions and the following disclaimer.
|
2018-08-12 13:46:08 +00:00
|
|
|
|
2019-05-11 03:44:28 +00:00
|
|
|
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
this list of conditions and the following disclaimer in the documentation
|
|
|
|
and/or other materials provided with the distribution.
|
2018-08-12 13:46:08 +00:00
|
|
|
|
2019-05-11 03:44:28 +00:00
|
|
|
3. Neither the name of the copyright holder nor the names of its contributors
|
|
|
|
may be used to endorse or promote products derived from this software without
|
|
|
|
specific prior written permission.
|
2018-08-12 13:46:08 +00:00
|
|
|
|
2019-05-11 03:44:28 +00:00
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2018-08-12 13:46:08 +00:00
|
|
|
|
2019-05-11 03:44:28 +00:00
|
|
|
Parts of the project are originally copyright (c) 2012-2013 The Cryptonote
|
|
|
|
developers.
|
2018-08-12 13:46:08 +00:00
|
|
|
*/
|
|
|
|
|
2019-03-03 20:26:11 +00:00
|
|
|
#define __STDC_FORMAT_MACROS
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
2018-08-12 13:46:08 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/select.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <microhttpd.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
|
|
|
|
#include <lmdb.h>
|
|
|
|
|
|
|
|
#include "log.h"
|
|
|
|
#include "pool.h"
|
|
|
|
#include "webui.h"
|
|
|
|
|
|
|
|
#define TAG_MAX 17
|
2019-07-05 21:28:04 +00:00
|
|
|
#define PAGE_MAX 8192
|
2019-03-03 20:26:11 +00:00
|
|
|
#define JSON_MAX 512
|
2018-08-12 13:46:08 +00:00
|
|
|
|
|
|
|
extern unsigned char webui_html[];
|
|
|
|
extern unsigned int webui_html_len;
|
|
|
|
|
|
|
|
static struct MHD_Daemon *mhd_daemon;
|
|
|
|
|
|
|
|
int
|
2019-03-03 20:26:11 +00:00
|
|
|
send_json_stats (void *cls, struct MHD_Connection *connection)
|
2018-08-12 13:46:08 +00:00
|
|
|
{
|
|
|
|
struct MHD_Response *response;
|
|
|
|
int ret;
|
|
|
|
wui_context_t *context = (wui_context_t*) cls;
|
2019-03-03 20:26:11 +00:00
|
|
|
char json[JSON_MAX];
|
2018-08-12 13:46:08 +00:00
|
|
|
uint64_t ph = context->pool_stats->pool_hashrate;
|
2019-03-03 20:26:11 +00:00
|
|
|
uint64_t nh = context->pool_stats->network_hashrate;
|
2019-05-04 04:00:27 +00:00
|
|
|
uint64_t height = context->pool_stats->network_height;
|
2019-05-12 16:50:40 +00:00
|
|
|
uint64_t ltf = context->pool_stats->last_template_fetched;
|
2019-03-03 20:26:11 +00:00
|
|
|
uint64_t lbf = context->pool_stats->last_block_found;
|
|
|
|
uint32_t pbf = context->pool_stats->pool_blocks_found;
|
2019-07-05 21:28:04 +00:00
|
|
|
unsigned ss = context->allow_self_select;
|
2019-03-03 20:26:11 +00:00
|
|
|
uint64_t mh = 0;
|
|
|
|
double mb = 0.0;
|
2019-05-10 05:59:47 +00:00
|
|
|
const char *wa = MHD_lookup_connection_value(connection,
|
|
|
|
MHD_COOKIE_KIND, "wa");
|
2018-08-12 13:46:08 +00:00
|
|
|
if (wa != NULL)
|
|
|
|
{
|
2019-03-03 20:26:11 +00:00
|
|
|
mh = miner_hr(wa);
|
2018-08-12 13:46:08 +00:00
|
|
|
uint64_t balance = miner_balance(wa);
|
2019-03-03 20:26:11 +00:00
|
|
|
mb = (double) balance / 1000000000000.0;
|
2018-08-12 13:46:08 +00:00
|
|
|
}
|
2019-03-03 20:26:11 +00:00
|
|
|
snprintf(json, JSON_MAX, "{"
|
|
|
|
"\"pool_hashrate\":%"PRIu64","
|
|
|
|
"\"network_hashrate\":%"PRIu64","
|
2019-05-04 04:00:27 +00:00
|
|
|
"\"network_height\":%"PRIu64","
|
2019-05-12 16:50:40 +00:00
|
|
|
"\"last_template_fetched\":%"PRIu64","
|
2019-03-03 20:26:11 +00:00
|
|
|
"\"last_block_found\":%"PRIu64","
|
|
|
|
"\"pool_blocks_found\":%d,"
|
|
|
|
"\"payment_threshold\":%.2f,"
|
2019-05-12 21:31:54 +00:00
|
|
|
"\"pool_fee\":%.3f,"
|
2019-03-03 20:26:11 +00:00
|
|
|
"\"pool_port\":%d,"
|
2019-07-05 21:28:04 +00:00
|
|
|
"\"allow_self_select\":%u,"
|
2019-03-03 20:26:11 +00:00
|
|
|
"\"connected_miners\":%d,"
|
|
|
|
"\"miner_hashrate\":%"PRIu64","
|
|
|
|
"\"miner_balance\":%.8f"
|
2019-05-12 16:50:40 +00:00
|
|
|
"}", ph, nh, height, ltf, lbf, pbf,
|
2019-03-03 20:26:11 +00:00
|
|
|
context->payment_threshold, context->pool_fee,
|
2019-07-05 21:28:04 +00:00
|
|
|
context->pool_port, ss, context->pool_stats->connected_miners,
|
2019-03-03 20:26:11 +00:00
|
|
|
mh, mb);
|
|
|
|
response = MHD_create_response_from_buffer(strlen(json),
|
|
|
|
(void*) json, MHD_RESPMEM_MUST_COPY);
|
2019-10-25 17:00:16 +00:00
|
|
|
MHD_add_response_header (response, "Content-Type", "application/json");
|
2018-08-12 13:46:08 +00:00
|
|
|
ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
|
|
|
|
MHD_destroy_response(response);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-03-03 20:26:11 +00:00
|
|
|
int
|
|
|
|
answer_to_connection (void *cls, struct MHD_Connection *connection,
|
|
|
|
const char *url,
|
|
|
|
const char *method, const char *version,
|
|
|
|
const char *upload_data,
|
|
|
|
size_t *upload_data_size, void **con_cls)
|
|
|
|
{
|
|
|
|
if (strstr(url, "/stats") != NULL)
|
|
|
|
return send_json_stats(cls, connection);
|
|
|
|
|
|
|
|
struct MHD_Response *response;
|
|
|
|
response = MHD_create_response_from_buffer(webui_html_len,
|
|
|
|
(void*) webui_html, MHD_RESPMEM_PERSISTENT);
|
|
|
|
int ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
|
|
|
|
MHD_destroy_response(response);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-08-12 13:46:08 +00:00
|
|
|
int
|
|
|
|
start_web_ui(wui_context_t *context)
|
|
|
|
{
|
|
|
|
log_debug("Starting Web UI");
|
2019-05-10 05:59:47 +00:00
|
|
|
mhd_daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY,
|
|
|
|
context->port, NULL, NULL,
|
2018-08-12 13:46:08 +00:00
|
|
|
&answer_to_connection, (void*) context, MHD_OPTION_END);
|
|
|
|
return mhd_daemon != NULL ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-05-11 22:00:50 +00:00
|
|
|
stop_web_ui(void)
|
2018-08-12 13:46:08 +00:00
|
|
|
{
|
|
|
|
log_debug("Stopping Web UI");
|
|
|
|
if (mhd_daemon != NULL)
|
|
|
|
MHD_stop_daemon(mhd_daemon);
|
|
|
|
}
|
|
|
|
|