Skip to content

Commit

Permalink
feat: enable xmrig with remote control and token, generate token
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyrix126 committed Mar 16, 2024
1 parent 2499953 commit 45a99b1
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 11 deletions.
4 changes: 2 additions & 2 deletions TODO_XMRvsBeast.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
- [ ] distribute hashrate conforming to the algorithm.
- [x] check every 10 minutes average Xmrig HR of last 15 minutes
- [ ] ask Xmrig to mine on p2pool
- [ ] generate token for xmrig
- [ ] enable xmrig with remote access control
- [x] generate token for xmrig
- [x] enable xmrig with remote access control
- [x] check if at least a share in pplns Window
- [ ] calculate spared HR
- [ ] calculate time to be spared
Expand Down
12 changes: 8 additions & 4 deletions src/disk/state.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use anyhow::{Result};



use anyhow::Result;
use rand::{distributions::Alphanumeric, thread_rng, Rng};

use super::*;
use crate::{components::node::RemoteNode, disk::status::*};
Expand Down Expand Up @@ -242,6 +240,7 @@ pub struct Xmrig {
pub selected_rig: String,
pub selected_ip: String,
pub selected_port: String,
pub token: String,
}

#[derive(Clone, Eq, PartialEq, Debug, Deserialize, Serialize, Default)]
Expand Down Expand Up @@ -363,6 +362,11 @@ impl Default for Xmrig {
keepalive: false,
current_threads: 1,
max_threads: 1,
token: thread_rng()
.sample_iter(Alphanumeric)
.take(16)
.map(char::from)
.collect(),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/disk/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ mod test {
selected_rig = "Gupax"
selected_ip = "192.168.1.122"
selected_port = "3333"
token = "testtoken"
[xvb]
token = ""
Expand Down
22 changes: 19 additions & 3 deletions src/helper/xmrig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,18 @@ impl Helper {
let gui_api = Arc::clone(&lock!(helper).gui_api_xmrig);
let pub_api = Arc::clone(&lock!(helper).pub_api_xmrig);
let path = path.to_path_buf();
let token = state.token.clone();
thread::spawn(move || {
Self::spawn_xmrig_watchdog(process, gui_api, pub_api, args, path, sudo, api_ip_port);
Self::spawn_xmrig_watchdog(
process,
gui_api,
pub_api,
args,
path,
sudo,
api_ip_port,
&token,
);
});
}

Expand Down Expand Up @@ -189,6 +199,8 @@ impl Helper {
args.push("127.0.0.1".to_string()); // HTTP API IP
args.push("--http-port".to_string());
args.push("18088".to_string()); // HTTP API Port
args.push(format!("--http-access-token={}", state.token)); // HTTP API Port
args.push("--http-no-restricted".to_string());
if state.pause != 0 {
args.push("--pause-on-active".to_string());
args.push(state.pause.to_string());
Expand Down Expand Up @@ -313,6 +325,7 @@ impl Helper {
path: std::path::PathBuf,
sudo: Arc<Mutex<SudoState>>,
mut api_ip_port: String,
token: &str,
) {
// 1a. Create PTY
debug!("XMRig | Creating PTY...");
Expand Down Expand Up @@ -543,10 +556,11 @@ impl Helper {
start.elapsed(),
&process,
);

// Send an HTTP API request
debug!("XMRig Watchdog | Attempting HTTP API request...");
if let Ok(priv_api) = PrivXmrigApi::request_xmrig_api(client.clone(), &api_uri).await {
if let Ok(priv_api) =
PrivXmrigApi::request_xmrig_api(client.clone(), &api_uri, token).await
{
debug!("XMRig Watchdog | HTTP API request OK, attempting [update_from_priv()]");
PubXmrigApi::update_from_priv(&pub_api, priv_api);
} else {
Expand Down Expand Up @@ -729,9 +743,11 @@ impl PrivXmrigApi {
async fn request_xmrig_api(
client: hyper::Client<hyper::client::HttpConnector>,
api_uri: &str,
token: &str,
) -> std::result::Result<Self, anyhow::Error> {
let request = hyper::Request::builder()
.method("GET")
.header("Authorization", ["Bearer ", token].concat())
.uri(api_uri)
.body(hyper::Body::empty())?;
let response = tokio::time::timeout(
Expand Down
23 changes: 21 additions & 2 deletions src/helper/xvb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,9 @@ impl Helper {
break;
}
}
debug!("XvB Watchdog | Attempting HTTP private API request...");
// only if private API is accessible, NotMining here means that the token and address is not registered on the XvB website.
// only if private API is accessible, NotMining here means that the token and address is not registered on the XvB website and Syncing means P2pool node is not running and so that some informations for private info are not available.

Check warning on line 247 in src/helper/xvb.rs

View workflow job for this annotation

GitHub Actions / typo

"informations" should be "information".
if lock!(process).state == ProcessState::Alive {
debug!("XvB Watchdog | Attempting HTTP private API request...");
// reload private stats
match XvbPrivStats::request_api(
&client,
Expand Down Expand Up @@ -328,6 +328,22 @@ impl Helper {
{
lock!(pub_api).stats_priv.win_current = true
}
// if 10 minutes passed since last check
if lock!(gui_api).tick_distribute_hr > (60 * 10) {
// request XMrig to mine on P2pool
}
// if share is in PW,
// check average HR of last 15 minutes from XMrig.
// if HR + buffer >= mHR,
// calculate how much time can be spared
// if not hero option
// calculate how much time needed to be spared to be in most round type minimum HR + buffer
// fi
// sleep 10m less spared time then request XMrig to mine on XvB
// fi
// fi
// instant saved for next check
// fi
}

lock!(gui_api).tick = 0;
Expand Down Expand Up @@ -356,6 +372,7 @@ pub struct PubXvbApi {
pub output: String,
pub uptime: u64,
pub tick: u8,
pub tick_distribute_hr: u16,
pub stats_pub: XvbPubStats,
pub stats_priv: XvbPrivStats,
}
Expand Down Expand Up @@ -479,9 +496,11 @@ impl PubXvbApi {
output.push_str(&buf);
}
let tick = std::mem::take(&mut gui_api.tick);
let tick_distribute_hr = std::mem::take(&mut gui_api.tick_distribute_hr);
*gui_api = Self {
output,
tick,
tick_distribute_hr,
..pub_api.clone()
};
}
Expand Down

0 comments on commit 45a99b1

Please sign in to comment.