hacker-challenge-sgx-general/rewrite/src/http_server.rs

112 lines
3.1 KiB
Rust

#![allow(dead_code)]
use crate::datastore;
use crate::datastore::Store;
use actix_web::{
// http::StatusCode, error::ResponseError, Result,
get,
post,
web,
App,
HttpResponse,
HttpServer,
Responder,
};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
const HOMEPAGE: &str = r#"Welcome, beloved hacker!
This node is part of the DeTEE hacker-challenge network. This network allows you
to mint the token {token_address},
and owns the mint address {mint_address}.
To be able to mint, you will need to send SOL to {mint_address}, however:
BE AWARE THAT THE ONLY WAY TO GET THE SOL OUT IS BY HACKING THIS NETWORK!
DeTEE REPRESENTATIVES DON'T KNOW HOW TO HACK IT! THAT'S ACTUALLY WHY THIS GOT CREATED...
Allowed operations:
/nodes <- information about nodes and counters of network activity
/mint (address) <- mint DHCT tokens to the address; the wallet needs sol for this operation
Feel free to use this as a faucet for HCKT tokens, that you can use for testing in any dApp.
The objective of the hacker challenge is to get the SOL out of {mint_address}.
If you hack it, contact us at https://detee.cloud cause we have lots of rewards.
"#;
#[get("/")]
async fn homepage() -> impl Responder {
HttpResponse::Ok().body(HOMEPAGE)
}
#[derive(Serialize)]
pub struct NodesResp {
pub ip: String,
pub joined_at: String,
pub last_keepalive: String,
pub mint_requests: u64,
pub mints: u64,
pub ratls_attacks: u64,
pub total_ratls_conns: u64,
pub public: bool,
}
impl From<(String, datastore::NodeInfo)> for NodesResp {
fn from((ip, node_info): (String, datastore::NodeInfo)) -> Self {
let joined_at: DateTime<Utc> = node_info.started_at.into();
let last_keepalive: DateTime<Utc> = node_info.keepalive.into();
let joined_at = joined_at.format("%Y-%m-%d %H:%M:%S").to_string();
let last_keepalive = last_keepalive.format("%Y-%m-%d %H:%M:%S").to_string();
crate::http_server::NodesResp {
ip,
joined_at,
last_keepalive,
mints: node_info.mints,
total_ratls_conns: node_info.ratls_conns,
ratls_attacks: node_info.ratls_attacks,
public: node_info.public,
mint_requests: node_info.mint_requests,
}
}
}
#[get("/nodes")]
async fn get_nodes(ds: web::Data<Arc<Store>>) -> HttpResponse {
HttpResponse::Ok().json(
ds.get_node_list()
.into_iter()
.map(Into::<NodesResp>::into)
.collect::<Vec<NodesResp>>(),
)
}
#[derive(Deserialize)]
struct MintReq {
wallet: String,
}
#[post("/mint")]
async fn mint(ds: web::Data<Arc<Store>>, _: web::Json<MintReq>) -> impl Responder {
println!("wat");
ds.increase_mint_requests();
HttpResponse::Ok().json({})
}
pub async fn init(ds: Arc<Store>) {
HttpServer::new(move || {
App::new()
.app_data(web::Data::new(ds.clone()))
.service(homepage)
.service(get_nodes)
.service(mint)
})
.bind("0.0.0.0:31372")
.unwrap()
.run()
.await
.unwrap();
}