Skip to content

Commit

Permalink
first stable version
Browse files Browse the repository at this point in the history
  • Loading branch information
ParzivalEugene committed Jul 25, 2024
1 parent a26eaf2 commit 8f457fd
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 30 deletions.
1 change: 0 additions & 1 deletion .env

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
.env
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@ FROM linuxserver/wireguard
COPY --from=builder /agent-wireguard/target/x86_64-unknown-linux-musl/release/agent-wireguard /agent-wireguard
ENTRYPOINT ["/agent-wireguard"]

EXPOSE 7070
EXPOSE 51820/udp
EXPOSE $MASTER_BACKEND_PORT
3 changes: 1 addition & 2 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ services:
restart: "always"
privileged: true
environment:
- "SERVER_PORT=51820"
- "SERVER_PORT=7070"
ports:
- "51820:51820/udp"
- "7070:7070"
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ async fn main() {
)
.route_layer(middleware::from_fn(track_metrics));

let listener = tokio::net::TcpListener::bind("0.0.0.0:7070").await.unwrap();
let listener = tokio::net::TcpListener::bind("127.0.0.1:7070")
.await
.unwrap();

info!(
"Main server listening on {}",
Expand Down
54 changes: 33 additions & 21 deletions src/scripts/add_config.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,45 @@
use crate::enums::errors::internal::{to_internal, InternalError};
use std::{
env,
fs::{self, File},
fs::File,
io::Write,
net::Ipv4Addr,
process::Command,
process::{Command, Stdio},
};

pub fn add_config(ip: Ipv4Addr) -> Result<String, InternalError> {
let temp_dir: &str = "temp";
fs::create_dir_all(temp_dir).map_err(to_internal)?;
env::set_current_dir(temp_dir).map_err(to_internal)?;

Command::new("wg")
let private_key_output = Command::new("wg")
.arg("genkey")
.arg("|")
.arg("tee")
.arg("privatekey")
.arg("|")
.arg("wg")
.arg("pubkey")
.output()
.map_err(to_internal)?;

let private_key: String = fs::read_to_string("privatekey").map_err(to_internal)?;
let public_key: String = fs::read_to_string("publickey").map_err(to_internal)?;
if !private_key_output.status.success() {
return Err(InternalError::InternalError);
}

let private_key =
String::from_utf8(private_key_output.stdout).map_err(|e| InternalError::InternalError)?;

let public_key_output = Command::new("wg")
.arg("pubkey")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.and_then(|mut child| {
child
.stdin
.as_mut()
.unwrap()
.write_all(private_key.as_bytes())?;
child.wait_with_output()
})
.map_err(to_internal)?;

if !public_key_output.status.success() {
return Err(InternalError::InternalError);
}

let public_key =
String::from_utf8(public_key_output.stdout).map_err(|e| InternalError::InternalError)?;

Command::new("wg")
.arg("set")
Expand All @@ -33,7 +48,7 @@ pub fn add_config(ip: Ipv4Addr) -> Result<String, InternalError> {
.arg(public_key.trim())
.arg("allowed-ips")
.arg(format!("{}/32", ip))
.output()
.spawn()
.map_err(to_internal)?;

Command::new("ip")
Expand All @@ -43,7 +58,7 @@ pub fn add_config(ip: Ipv4Addr) -> Result<String, InternalError> {
.arg(format!("{}/32", ip))
.arg("dev")
.arg("wg0")
.output()
.spawn()
.map_err(to_internal)?;

let mut wg0_conf: File = File::options()
Expand All @@ -55,8 +70,5 @@ pub fn add_config(ip: Ipv4Addr) -> Result<String, InternalError> {
writeln!(wg0_conf, "PublicKey = {}", public_key.trim()).map_err(to_internal)?;
writeln!(wg0_conf, "AllowedIPs = {}/32", ip).map_err(to_internal)?;

fs::remove_dir_all(temp_dir).map_err(to_internal)?;
env::set_current_dir("..").map_err(to_internal)?;

Ok(private_key.trim().to_string())
}
8 changes: 5 additions & 3 deletions src/services/config_service.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{enums::errors::internal::InternalError, scripts};
use std::net::Ipv4Addr;
use tracing::{error, info};
use tracing::error;

pub async fn add_config() -> Result<String, InternalError> {
let ip: Ipv4Addr = get_available_ip().await?;
Expand All @@ -19,8 +19,10 @@ pub async fn get_used_ips() -> Result<Vec<Ipv4Addr>, InternalError> {
})?;
let used_ips: Vec<Ipv4Addr> = used_ips_raw
.lines()
.map(|ip| {
let ip = ip.trim_end_matches("/32");
.map(|line| {
let mut parts = line.split_whitespace();
parts.next().unwrap();
let ip = parts.next().unwrap().trim_end_matches("/32");
ip.parse().unwrap()
})
.collect();
Expand Down

0 comments on commit 8f457fd

Please sign in to comment.