initial commit

This commit is contained in:
mycognosist 2022-08-27 16:22:16 +01:00
commit 38e0767942
8 changed files with 146 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/target
notes

7
Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "torbutt"
version = "0.1.0"

6
Cargo.toml Normal file
View File

@ -0,0 +1,6 @@
[package]
name = "torbutt"
version = "0.1.0"
edition = "2021"
[dependencies]

3
README.md Normal file
View File

@ -0,0 +1,3 @@
# torbutt
Just a place to experiment with Tor and SSB.

63
src/args.rs Normal file
View File

@ -0,0 +1,63 @@
use std::{env, ffi::OsString, path::PathBuf, process::exit, str::FromStr};
fn usage() -> ! {
let name = env::args().next().unwrap();
eprintln!(
"usage: torbutt [-p PORT]
FLAGS (default -s if none set)
-s deploy TCP server
-c deploy TCP client
ARGS:
-p port for TCP connection"
);
exit(1)
}
#[derive(Default)]
pub struct Args {
pub port: Option<u16>,
}
impl Args {
pub fn default() -> Self {
Args { port: 8022 }
}
pub fn from_env() -> Self {
let mut out = Self::default();
let mut args = env::args_os().skip(1);
while let Some(arg) = args.next() {
let s = arg.to_string_lossy();
let mut ch_iter = s.chars();
if ch_iter.next() != Some('-') {
out.positional.push(arg);
continue;
}
ch_iter.for_each(|m| match m {
// Edit these lines //
'c' => out.config = parse_os_arg(args.next()),
'd' => out.out_dir = parse_os_arg(args.next()),
'g' => out.include_gemini = true,
'h' => out.include_html = true,
// Stop editing //
_ => {
usage();
}
})
}
// other validation
if out.positional.len() < 1 {
usage()
}
out
}
}
#[allow(dead_code)]
fn parse_arg<T: FromStr>(a: Option<OsString>) -> T {
a.and_then(|a| a.into_string().ok())
.and_then(|a| a.parse().ok())
.unwrap_or_else(|| usage())
}

38
src/main.rs Normal file
View File

@ -0,0 +1,38 @@
mod tcp_client;
mod tcp_server;
use std::{env, process};
fn usage() -> ! {
eprintln!(
"usage: torbutt MODE PORT
MODE The mode in which to run torbutt (server or client)
PORT The port on which to bind a TCP stream
EXAMPLE:
torbutt server 8022"
);
process::exit(1)
}
fn main() {
let args: Vec<String> = env::args().collect();
if args.len() != 3 {
usage()
};
let mode = &args[1];
let port = &args[2];
println!("Running in {} mode on port {}", mode, port);
match mode.as_str() {
"client" => tcp_client::connect(port).unwrap(),
"server" => tcp_server::listen(port).unwrap(),
&_ => usage(),
};
}

12
src/tcp_client.rs Normal file
View File

@ -0,0 +1,12 @@
use std::io::prelude::*;
use std::net::TcpStream;
pub fn connect(port: &str) -> std::io::Result<()> {
let addr = format!("127.0.0.1:{}", port);
let mut stream = TcpStream::connect(addr)?;
stream.write_all(&[1])?;
stream.read_exact(&mut [0; 128])?;
Ok(())
}

15
src/tcp_server.rs Normal file
View File

@ -0,0 +1,15 @@
use std::net::TcpListener;
pub fn listen(port: &str) -> std::io::Result<()> {
let addr = format!("127.0.0.1:{}", port);
let listener = TcpListener::bind(addr).unwrap();
for stream in listener.incoming() {
println!(
"received connection from {}",
stream.unwrap().peer_addr().unwrap()
);
}
Ok(())
}