Skip to content

Commit

Permalink
use GithubConfig to wrap token and discord webhook
Browse files Browse the repository at this point in the history
github_release and github_verify now plain async's rather than middleware
  • Loading branch information
Jerboa-app committed Jan 5, 2024
1 parent 013aac2 commit 0739fca
Show file tree
Hide file tree
Showing 11 changed files with 486 additions and 302 deletions.
1 change: 1 addition & 0 deletions src/discord/model.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[derive(Clone)]
pub struct Webhook
{
addr: String
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ pub mod discord;
pub mod web;
pub mod server;

pub mod util;

#[cfg(feature = "http")]
pub mod server_http;

Expand Down
81 changes: 78 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use pulse::server::serve;
use pulse::{server::Server, discord::model::Webhook};

#[tokio::main]
async fn main() {
Expand All @@ -25,6 +25,81 @@ async fn main() {
std::process::exit(1);
};

serve(token).await;

let disc_url = if args.iter().any(|x| x == "-w")
{
let i = args.iter().position(|x| x == "-w").unwrap();
if i+1 < args.len()
{
args[i+1].clone()
}
else
{
println!("Discord webhook url not provided, please provide -w https://discord.com/api/webhooks/xxx/yyy");
std::process::exit(1);
}
}
else
{
println!("Discord webhook url not provided, please provide -w https://discord.com/api/webhooks/xxx/yyy");
std::process::exit(1);
};

let args: Vec<String> = std::env::args().collect();

let port = if args.iter().any(|x| x == "-p")
{
let i = args.iter().position(|x| x == "-p").unwrap();
if i+1 < args.len()
{
args[i+1].parse::<u16>().unwrap()
}
else
{
3030
}
}
else
{
3030
};

let cert_path = if args.iter().any(|x| x == "-c")
{
let i = args.iter().position(|x| x == "-c").unwrap();
if i+1 < args.len()
{
args[i+1].clone()
}
else
{
"./cert.pem".to_string()
}
}
else
{
"./cert.pem".to_string()
};

let key_path = if args.iter().any(|x| x == "-k")
{
let i = args.iter().position(|x| x == "-k").unwrap();
if i+1 < args.len()
{
args[i+1].clone()
}
else
{
"./key.pem".to_string()
}
}
else
{
"./key.pem".to_string()
};


let server = Server::new(0,0,0,0,port,token, Webhook::new(disc_url));

server.serve(cert_path, key_path).await;

}
75 changes: 6 additions & 69 deletions src/server.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@

use crate::discord::model::Webhook;
use crate::web::throttle::{IpThrottler, handle_throttle};
use crate::web::response::util::{reflect, stdout_log};
use crate::web::response::github_verify::github_verify;
use crate::web::response::github::{filter_github, GithubConfig};

use std::clone;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::path::PathBuf;
use std::sync::{Arc, Mutex};

use axum::extract::State;
use axum::
{
routing::post,
Expand All @@ -32,7 +30,8 @@ impl Server
c: u8,
d: u8,
port: u16,
token: String
token: String,
disc: Webhook
)
-> Server
{
Expand All @@ -45,15 +44,14 @@ impl Server

let throttle_state = Arc::new(Mutex::new(requests));

let authenticated_state = token;

let github = GithubConfig::new(token, disc);

Server
{
addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(a,b,c,d)), port),
router: Router::new()
.route("/", post(|| async move { }))
.layer(middleware::from_fn_with_state(authenticated_state.clone(), github_verify))
.layer(middleware::from_fn_with_state(github, filter_github))
.layer(middleware::from_fn_with_state(throttle_state.clone(), handle_throttle))

}
Expand Down Expand Up @@ -89,65 +87,4 @@ impl Server
.unwrap();
}

}

pub async fn serve(token: String) {

let args: Vec<String> = std::env::args().collect();

let port = if args.iter().any(|x| x == "-p")
{
let i = args.iter().position(|x| x == "-p").unwrap();
if i+1 < args.len()
{
args[i+1].parse::<u16>().unwrap()
}
else
{
3030
}
}
else
{
3030
};

let cert_path = if args.iter().any(|x| x == "-c")
{
let i = args.iter().position(|x| x == "-c").unwrap();
if i+1 < args.len()
{
args[i+1].clone()
}
else
{
"./cert.pem".to_string()
}
}
else
{
"./cert.pem".to_string()
};

let key_path = if args.iter().any(|x| x == "-k")
{
let i = args.iter().position(|x| x == "-k").unwrap();
if i+1 < args.len()
{
args[i+1].clone()
}
else
{
"./key.pem".to_string()
}
}
else
{
"./key.pem".to_string()
};

let server = Server::new(0,0,0,0,port,token);

server.serve(cert_path, key_path).await

}
28 changes: 28 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use std::fmt::Write;
use regex::Regex;

pub fn dump_bytes(v: &[u8]) -> String
{
let mut byte_string = String::new();
for &byte in v
{
write!(&mut byte_string, "{:0>2X}", byte).expect("byte dump error");
};
byte_string
}

pub fn read_bytes(v: String) -> Vec<u8>
{
(0..v.len()).step_by(2)
.map
(
|index| u8::from_str_radix(&v[index..index+2], 16).unwrap()
)
.collect()
}

pub fn strip_control_characters(s: String) -> String
{
let re = Regex::new(r"[\u0000-\u001F]").unwrap().replace_all(&s, "");
return re.to_string()
}
Loading

0 comments on commit 0739fca

Please sign in to comment.