X-Git-Url: https://git.jacobcasper.com/?p=sockgit.git;a=blobdiff_plain;f=src%2Fmain.rs;fp=src%2Fmain.rs;h=2b1152328e638494f389876e9b3d483d27795ae5;hp=0000000000000000000000000000000000000000;hb=3263e5466a97e72e9de5afe634aa866aa107f652;hpb=421788a86f2ecef84c98eebc74a1f8a8fe2001e9 diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..2b11523 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,56 @@ +use std::io; +use std::env; +use std::path; +use std::error; +use git2; + +fn check_ip() -> Result<(), io::Error> { + match env::var("REMOTE_ADDR") { + Ok(remote_ip) => match env::var("WHITELIST_IP") { + Ok(whitelist_ip) => { + if remote_ip == whitelist_ip { + Ok(()) + } else { + Err(io::Error::new(io::ErrorKind::ConnectionRefused, format!("Blocked connection from {}", remote_ip))) + } + } + Err(e) => Err(io::Error::new(io::ErrorKind::ConnectionRefused, e)), + } + Err(e) => Err(io::Error::new(io::ErrorKind::ConnectionRefused, e)), + } +} + +fn main() -> Result<(), Box> { + match check_ip() { + Ok(()) => { + let mut repo_name = String::new(); + io::stdin().read_line(&mut repo_name)?; + // remove trailing newline + repo_name.pop(); + + let mut opts = git2::RepositoryInitOptions::new(); + git2::RepositoryInitOptions::bare(&mut opts, true) + .mode(git2::RepositoryInitMode::SHARED_GROUP) + .no_reinit(true) + .template_path(path::Path::new("./templates")); + + let repo = git2::Repository::init_opts(format!("{}.git", repo_name), &opts)?; + + let public_user = env::var("USER").unwrap(); + let public_name = env::var("PUBLIC").unwrap(); + let public_path = env::var("PATH").unwrap(); + repo.remote( + &public_name, + &format!( + "{user}@{public}:{path}/{repo}.git", + user = public_user, + public = public_name, + path = public_path, + repo = repo_name + ) + )?; + Ok(()) + } + Err(e) => Err(Box::new(e)), + } +}