2018-08-12 13:46:08 +00:00
|
|
|
/*
|
2020-02-02 05:24:51 +00:00
|
|
|
Copyright (c) 2014-2020, 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 <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <pthread.h>
|
|
|
|
|
2020-01-04 02:14:42 +00:00
|
|
|
#include <event2/event.h>
|
|
|
|
#include <event2/buffer.h>
|
|
|
|
#include <event2/http.h>
|
2018-08-12 13:46:08 +00:00
|
|
|
|
|
|
|
#include "log.h"
|
|
|
|
#include "pool.h"
|
|
|
|
#include "webui.h"
|
|
|
|
|
|
|
|
extern unsigned char webui_html[];
|
|
|
|
extern unsigned int webui_html_len;
|
|
|
|
|
2020-01-04 02:14:42 +00:00
|
|
|
static pthread_t handle;
|
|
|
|
static struct event_base *webui_base;
|
|
|
|
static struct evhttp *webui_httpd;
|
|
|
|
static struct evhttp_bound_socket *webui_listener;
|
2018-08-12 13:46:08 +00:00
|
|
|
|
2020-01-04 02:14:42 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
send_json_stats(struct evhttp_request *req, void *arg)
|
2018-08-12 13:46:08 +00:00
|
|
|
{
|
2020-01-04 02:14:42 +00:00
|
|
|
struct evbuffer *buf = evhttp_request_get_output_buffer(req);
|
|
|
|
wui_context_t *context = (wui_context_t*) arg;
|
|
|
|
struct evkeyvalq *hdrs_in = NULL;
|
|
|
|
struct evkeyvalq *hdrs_out = NULL;
|
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;
|
2020-01-17 12:40:09 +00:00
|
|
|
uint64_t nd = context->pool_stats->network_difficulty;
|
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;
|
2020-01-17 12:40:09 +00:00
|
|
|
uint64_t rh = context->pool_stats->round_hashes;
|
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;
|
2020-01-04 02:14:42 +00:00
|
|
|
|
|
|
|
hdrs_in = evhttp_request_get_input_headers(req);
|
|
|
|
const char *cookies = evhttp_find_header(hdrs_in, "Cookie");
|
2020-01-05 21:54:22 +00:00
|
|
|
if (cookies)
|
2018-08-12 13:46:08 +00:00
|
|
|
{
|
2020-01-05 21:54:22 +00:00
|
|
|
char *wa = strstr(cookies, "wa=");
|
|
|
|
if (wa)
|
|
|
|
{
|
|
|
|
wa += 3;
|
|
|
|
mh = miner_hr(wa);
|
|
|
|
uint64_t balance = miner_balance(wa);
|
|
|
|
mb = (double) balance / 1000000000000.0;
|
|
|
|
}
|
2018-08-12 13:46:08 +00:00
|
|
|
}
|
2020-01-04 02:14:42 +00:00
|
|
|
|
|
|
|
evbuffer_add_printf(buf, "{"
|
2019-03-03 20:26:11 +00:00
|
|
|
"\"pool_hashrate\":%"PRIu64","
|
2020-01-17 12:40:09 +00:00
|
|
|
"\"round_hashes\":%"PRIu64","
|
2019-03-03 20:26:11 +00:00
|
|
|
"\"network_hashrate\":%"PRIu64","
|
2020-01-17 12:40:09 +00:00
|
|
|
"\"network_difficulty\":%"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,"
|
2020-01-05 22:53:51 +00:00
|
|
|
"\"pool_ssl_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"
|
2020-01-17 12:40:09 +00:00
|
|
|
"}", ph, rh, nh, nd, height, ltf, lbf, pbf,
|
2019-03-03 20:26:11 +00:00
|
|
|
context->payment_threshold, context->pool_fee,
|
2020-01-05 22:53:51 +00:00
|
|
|
context->pool_port, context->pool_ssl_port,
|
|
|
|
ss, context->pool_stats->connected_miners,
|
2019-03-03 20:26:11 +00:00
|
|
|
mh, mb);
|
2020-01-04 02:14:42 +00:00
|
|
|
hdrs_out = evhttp_request_get_output_headers(req);
|
|
|
|
evhttp_add_header(hdrs_out, "Content-Type", "application/json");
|
|
|
|
evhttp_send_reply(req, HTTP_OK, "OK", buf);
|
2018-08-12 13:46:08 +00:00
|
|
|
}
|
|
|
|
|
2020-01-04 02:14:42 +00:00
|
|
|
static void
|
|
|
|
process_request(struct evhttp_request *req, void *arg)
|
2019-03-03 20:26:11 +00:00
|
|
|
{
|
2020-01-04 02:14:42 +00:00
|
|
|
const char *url = evhttp_request_get_uri(req);
|
|
|
|
struct evbuffer *buf = NULL;
|
|
|
|
struct evkeyvalq *hdrs_out = NULL;
|
|
|
|
|
2019-03-03 20:26:11 +00:00
|
|
|
if (strstr(url, "/stats") != NULL)
|
2020-01-04 02:14:42 +00:00
|
|
|
{
|
|
|
|
send_json_stats(req, arg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = evhttp_request_get_output_buffer(req);
|
|
|
|
evbuffer_add(buf, webui_html, webui_html_len);
|
|
|
|
hdrs_out = evhttp_request_get_output_headers(req);
|
|
|
|
evhttp_add_header(hdrs_out, "Content-Type", "text/html");
|
|
|
|
evhttp_send_reply(req, HTTP_OK, "OK", buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
|
|
|
thread_main(void *ctx)
|
|
|
|
{
|
|
|
|
wui_context_t *context = (wui_context_t*) ctx;
|
|
|
|
webui_listener = evhttp_bind_socket_with_handle(
|
2020-04-18 23:00:41 +00:00
|
|
|
webui_httpd, context->pool_listen, context->port);
|
2020-01-04 02:14:42 +00:00
|
|
|
if(!webui_listener)
|
|
|
|
{
|
2020-01-06 00:42:25 +00:00
|
|
|
log_error("Failed to bind for port: %u", context->port);
|
2020-01-04 02:14:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
evhttp_set_gencb(webui_httpd, process_request, ctx);
|
|
|
|
event_base_dispatch(webui_base);
|
|
|
|
event_base_free(webui_base);
|
|
|
|
return 0;
|
2019-03-03 20:26:11 +00:00
|
|
|
}
|
|
|
|
|
2018-08-12 13:46:08 +00:00
|
|
|
int
|
|
|
|
start_web_ui(wui_context_t *context)
|
|
|
|
{
|
|
|
|
log_debug("Starting Web UI");
|
2020-01-04 02:14:42 +00:00
|
|
|
if (webui_base || handle)
|
|
|
|
{
|
|
|
|
log_error("Already running");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
webui_base = event_base_new();
|
|
|
|
if (!webui_base)
|
|
|
|
{
|
2020-01-06 00:42:25 +00:00
|
|
|
log_error("Failed to create httpd event base");
|
|
|
|
return -1;
|
2020-01-04 02:14:42 +00:00
|
|
|
}
|
|
|
|
webui_httpd = evhttp_new(webui_base);
|
|
|
|
if (!webui_httpd)
|
|
|
|
{
|
2020-01-06 00:42:25 +00:00
|
|
|
log_error("Failed to create evhttp event");
|
|
|
|
return -1;
|
2020-01-04 02:14:42 +00:00
|
|
|
}
|
|
|
|
int rc = pthread_create(&handle, NULL, thread_main, context);
|
|
|
|
if (!rc)
|
|
|
|
pthread_detach(handle);
|
|
|
|
return rc;
|
2018-08-12 13:46:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
2020-01-04 02:14:42 +00:00
|
|
|
if (webui_listener && webui_httpd)
|
|
|
|
evhttp_del_accept_socket(webui_httpd, webui_listener);
|
|
|
|
if (webui_httpd)
|
|
|
|
evhttp_free(webui_httpd);
|
|
|
|
if (webui_base)
|
|
|
|
event_base_loopbreak(webui_base);
|
2018-08-12 13:46:08 +00:00
|
|
|
}
|
|
|
|
|