Initial
This commit is contained in:
commit
116807ca04
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
/target
|
||||
415
Cargo.lock
generated
Normal file
415
Cargo.lock
generated
Normal file
|
|
@ -0,0 +1,415 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "anstream"
|
||||
version = "0.6.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "43d5b281e737544384e969a5ccad3f1cdd24b48086a0fc1b2a5262a26b8f4f4a"
|
||||
dependencies = [
|
||||
"anstyle",
|
||||
"anstyle-parse",
|
||||
"anstyle-query",
|
||||
"anstyle-wincon",
|
||||
"colorchoice",
|
||||
"is_terminal_polyfill",
|
||||
"utf8parse",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstyle"
|
||||
version = "1.0.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5192cca8006f1fd4f7237516f40fa183bb07f8fbdfedaa0036de5ea9b0b45e78"
|
||||
|
||||
[[package]]
|
||||
name = "anstyle-parse"
|
||||
version = "0.2.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2"
|
||||
dependencies = [
|
||||
"utf8parse",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstyle-query"
|
||||
version = "1.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc"
|
||||
dependencies = [
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstyle-wincon"
|
||||
version = "3.0.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d"
|
||||
dependencies = [
|
||||
"anstyle",
|
||||
"once_cell_polyfill",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.100"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a23eb6b1614318a8071c9b2521f36b424b2c83db5eb3a0fead4a6c0809af6e61"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "2.10.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3"
|
||||
|
||||
[[package]]
|
||||
name = "bytes"
|
||||
version = "1.11.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b35204fbdc0b3f4446b89fc1ac2cf84a8a68971995d0bf2e925ec7cd960f9cb3"
|
||||
|
||||
[[package]]
|
||||
name = "camino"
|
||||
version = "1.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e629a66d692cb9ff1a1c664e41771b3dcaf961985a9774c0eb0bd1b51cf60a48"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
|
||||
|
||||
[[package]]
|
||||
name = "cfg_aliases"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724"
|
||||
|
||||
[[package]]
|
||||
name = "clap"
|
||||
version = "4.5.54"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c6e6ff9dcd79cff5cd969a17a545d79e84ab086e444102a591e288a8aa3ce394"
|
||||
dependencies = [
|
||||
"clap_builder",
|
||||
"clap_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_builder"
|
||||
version = "4.5.54"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fa42cf4d2b7a41bc8f663a7cab4031ebafa1bf3875705bfaf8466dc60ab52c00"
|
||||
dependencies = [
|
||||
"anstream",
|
||||
"anstyle",
|
||||
"clap_lex",
|
||||
"strsim",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_derive"
|
||||
version = "4.5.49"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2a0b5487afeab2deb2ff4e03a807ad1a03ac532ff5a2cee5d86884440c7f7671"
|
||||
dependencies = [
|
||||
"heck",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_lex"
|
||||
version = "0.7.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a1d728cc89cf3aee9ff92b05e62b19ee65a02b5702cff7d5a377e32c6ae29d8d"
|
||||
|
||||
[[package]]
|
||||
name = "colorchoice"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75"
|
||||
|
||||
[[package]]
|
||||
name = "errno"
|
||||
version = "0.3.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "heck"
|
||||
version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
|
||||
|
||||
[[package]]
|
||||
name = "is_terminal_polyfill"
|
||||
version = "1.70.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695"
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.179"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c5a2d376baa530d1238d133232d15e239abad80d05838b4b59354e5268af431f"
|
||||
|
||||
[[package]]
|
||||
name = "mio"
|
||||
version = "1.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a69bcab0ad47271a0234d9422b131806bf3968021e5dc9328caf2d4cd58557fc"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"wasi",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nix"
|
||||
version = "0.30.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "74523f3a35e05aba87a1d978330aef40f67b0304ac79c1c00b294c9830543db6"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"cfg-if",
|
||||
"cfg_aliases",
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "once_cell_polyfill"
|
||||
version = "1.70.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"
|
||||
|
||||
[[package]]
|
||||
name = "pin-project-lite"
|
||||
version = "0.2.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.105"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "535d180e0ecab6268a3e718bb9fd44db66bbbc256257165fc699dadf70d16fe7"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.43"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dc74d9a594b72ae6656596548f56f667211f8a97b3d4c3d467150794690dc40a"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rednet"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"camino",
|
||||
"clap",
|
||||
"lazy_static",
|
||||
"nix",
|
||||
"signal-hook",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "signal-hook"
|
||||
version = "0.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2a37d01603c37b5466f808de79f845c7116049b0579adb70a6b7d47c1fa3a952"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"signal-hook-registry",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "signal-hook-registry"
|
||||
version = "1.4.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c4db69cba1110affc0e9f7bcd48bbf87b3f4fc7c61fc9155afd4c469eb3d6c1b"
|
||||
dependencies = [
|
||||
"errno",
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "socket2"
|
||||
version = "0.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "17129e116933cf371d018bb80ae557e889637989d8638274fb25622827b03881"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"windows-sys 0.60.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "strsim"
|
||||
version = "0.11.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.113"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "678faa00651c9eb72dd2020cbdf275d92eccb2400d568e419efdd64838145cb4"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tokio"
|
||||
version = "1.49.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "72a2903cd7736441aac9df9d7688bd0ce48edccaadf181c3b90be801e81d3d86"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"libc",
|
||||
"mio",
|
||||
"pin-project-lite",
|
||||
"socket2",
|
||||
"tokio-macros",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tokio-macros"
|
||||
version = "2.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "af407857209536a95c8e56f8231ef2c2e2aff839b22e07a1ffcbc617e9db9fa5"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.22"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5"
|
||||
|
||||
[[package]]
|
||||
name = "utf8parse"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.11.1+wasi-snapshot-preview1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b"
|
||||
|
||||
[[package]]
|
||||
name = "windows-link"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
|
||||
|
||||
[[package]]
|
||||
name = "windows-sys"
|
||||
version = "0.60.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f2f500e4d28234f72040990ec9d39e3a6b950f9f22d3dba18416c35882612bcb"
|
||||
dependencies = [
|
||||
"windows-targets",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-sys"
|
||||
version = "0.61.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
|
||||
dependencies = [
|
||||
"windows-link",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-targets"
|
||||
version = "0.53.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4945f9f551b88e0d65f3db0bc25c33b8acea4d9e41163edf90dcd0b19f9069f3"
|
||||
dependencies = [
|
||||
"windows-link",
|
||||
"windows_aarch64_gnullvm",
|
||||
"windows_aarch64_msvc",
|
||||
"windows_i686_gnu",
|
||||
"windows_i686_gnullvm",
|
||||
"windows_i686_msvc",
|
||||
"windows_x86_64_gnu",
|
||||
"windows_x86_64_gnullvm",
|
||||
"windows_x86_64_msvc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_gnullvm"
|
||||
version = "0.53.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a9d8416fa8b42f5c947f8482c43e7d89e73a173cead56d044f6a56104a6d1b53"
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_msvc"
|
||||
version = "0.53.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b9d782e804c2f632e395708e99a94275910eb9100b2114651e04744e9b125006"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_gnu"
|
||||
version = "0.53.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "960e6da069d81e09becb0ca57a65220ddff016ff2d6af6a223cf372a506593a3"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_gnullvm"
|
||||
version = "0.53.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fa7359d10048f68ab8b09fa71c3daccfb0e9b559aed648a8f95469c27057180c"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_msvc"
|
||||
version = "0.53.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1e7ac75179f18232fe9c285163565a57ef8d3c89254a30685b57d83a38d326c2"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnu"
|
||||
version = "0.53.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9c3842cdd74a865a8066ab39c8a7a473c0778a3f29370b5fd6b4b9aa7df4a499"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnullvm"
|
||||
version = "0.53.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0ffa179e2d07eee8ad8f57493436566c7cc30ac536a3379fdf008f47f6bb7ae1"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_msvc"
|
||||
version = "0.53.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d6bbff5f0aada427a1e5a6da5f1f98158182f26556f345ac9e04d36d0ebed650"
|
||||
13
Cargo.toml
Normal file
13
Cargo.toml
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
[package]
|
||||
name = "rednet"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.100"
|
||||
camino = "1.2.2"
|
||||
clap = { version = "4.5.54", features = ["derive"] }
|
||||
lazy_static = "1.5.0"
|
||||
nix = { version = "0.30.1", features = ["term"] }
|
||||
signal-hook = "0.4.1"
|
||||
tokio = { version = "1.49.0", features = ["io-util", "macros", "net", "rt-multi-thread", "time"] }
|
||||
38
src/cli_args.rs
Normal file
38
src/cli_args.rs
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
use clap::Parser;
|
||||
|
||||
|
||||
#[derive(Debug, Parser)]
|
||||
pub struct CliArgs {
|
||||
|
||||
/// force IPv4
|
||||
#[arg(short='4', long=None, default_value_t=false, conflicts_with = "force_ipv6")]
|
||||
pub force_ipv4: bool,
|
||||
/// force IPv6
|
||||
#[arg(short='6', long=None, default_value_t=false, conflicts_with = "force_ipv4")]
|
||||
pub force_ipv6: bool,
|
||||
/// use 8-bit data path (don’t strip high bit)
|
||||
#[arg(short='8', long=None, default_value_t=false)]
|
||||
pub use_8bit_data_path: bool,
|
||||
/// disable escape character (ignored)
|
||||
#[arg(short='E', long=None, default_value_t=false)]
|
||||
pub disable_esc_char: bool,
|
||||
/// set escape character (ignored)
|
||||
#[arg(short='e', long=None, default_value="^]")]
|
||||
pub escape_char: String,
|
||||
/// record session to file (net trace)
|
||||
#[arg(short='n', long=None)]
|
||||
pub net_trace_f: Option<camino::Utf8PathBuf>,
|
||||
/// enable socket debugging (ignored)
|
||||
#[arg(short='d', long=None, default_value_t=false)]
|
||||
pub socket_debug: bool,
|
||||
/// socket timeout
|
||||
#[arg(short='t', long=None, default_value_t=5)]
|
||||
pub socket_timeout: u64,
|
||||
|
||||
|
||||
#[clap(default_value = "0.0.0.0")]
|
||||
pub host: String,
|
||||
|
||||
#[clap(default_value_t = 23)]
|
||||
pub port: u16
|
||||
}
|
||||
351
src/main.rs
Normal file
351
src/main.rs
Normal file
|
|
@ -0,0 +1,351 @@
|
|||
use std::{fs::File, io::{Read, Seek, SeekFrom, Write}, net::{Shutdown, TcpStream, ToSocketAddrs}, path::PathBuf, sync::Mutex, time::Duration};
|
||||
|
||||
use camino::Utf8PathBuf;
|
||||
use clap::Parser;
|
||||
use lazy_static::lazy_static;
|
||||
use nix::sys::termios::{self, Termios};
|
||||
|
||||
mod cli_args;
|
||||
|
||||
lazy_static!(
|
||||
static ref SETTINGS: Mutex<Settings> = Mutex::new(Settings::default());
|
||||
);
|
||||
|
||||
|
||||
mod cmd {
|
||||
#![allow(dead_code)]
|
||||
pub const IAC: u8 = 255;
|
||||
pub const DO: u8 = 253;
|
||||
pub const DONT: u8 = 254;
|
||||
pub const WILL: u8 = 251;
|
||||
pub const WONT: u8 = 252;
|
||||
pub const SB: u8 = 250;
|
||||
pub const SE: u8 = 240;
|
||||
|
||||
pub const BINARY: u8 = 0; // Dont strip the 8th bit
|
||||
pub const ECHO: u8 = 1; // Echo your keystrokes
|
||||
pub const SUPPRESS_GO_AHEAD: u8 = 3; // SGA; suppress “go ahead” protocol
|
||||
pub const STATUS: u8 = 5; // Status request
|
||||
pub const TIMING_MARK: u8 = 6; // Timing mark
|
||||
pub const TERMINAL_TYPE: u8 = 24; // Terminal type subnegotiation
|
||||
pub const WINDOW_SIZE: u8 = 31; //NAWS: send terminal width/height
|
||||
pub const TERMINAL_SPEED: u8 = 32; //Terminal speed
|
||||
pub const REMOTE_FLOW_CONTROL: u8 = 33; // RFC 1081 flow control
|
||||
pub const LINEMODE: u8 = 34; // Line mode options
|
||||
pub const ENVIRONMENT_VARS: u8 = 36; // Environment variable exchange
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
enum State {
|
||||
Data,
|
||||
Iac,
|
||||
IacCmd(u8),
|
||||
Subneg,
|
||||
SubnegIac,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
struct Settings {
|
||||
pub echo: bool,
|
||||
pub strip8b: bool,
|
||||
}
|
||||
|
||||
static NET_TRACE_H: Mutex<Option<File>> = Mutex::new(None);
|
||||
|
||||
fn write_net_trace(bytes: &[u8]) -> anyhow::Result<()> {
|
||||
let h = NET_TRACE_H.lock().unwrap();
|
||||
if let Some(mut h) = h.as_ref() {
|
||||
h.write_all(bytes)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn write_net_trace_backspace() -> anyhow::Result<()> {
|
||||
let h_guard = NET_TRACE_H.lock().unwrap();
|
||||
if let Some(mut h) = h_guard.as_ref() {
|
||||
let len = h.metadata()?.len();
|
||||
if len == 0 {
|
||||
return Ok(());
|
||||
}
|
||||
h.set_len(len - 1)?;
|
||||
h.seek(SeekFrom::End(0))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
fn main_loop(cli: &cli_args::CliArgs) -> anyhow::Result<()> {
|
||||
let mut addrs = (cli.host.clone(), cli.port).to_socket_addrs()?.collect::<Vec<std::net::SocketAddr>>();
|
||||
if let Some(f) = &cli.net_trace_f {
|
||||
let h = std::fs::OpenOptions::new()
|
||||
.write(true)
|
||||
.truncate(false)
|
||||
.open(f)?;
|
||||
*NET_TRACE_H.lock().unwrap() = Some(h);
|
||||
|
||||
}
|
||||
|
||||
{
|
||||
let mut settings = SETTINGS.lock().unwrap();
|
||||
settings.strip8b = !cli.use_8bit_data_path;
|
||||
}
|
||||
|
||||
if cli.force_ipv4 {
|
||||
addrs = addrs
|
||||
.into_iter()
|
||||
.filter(|a| matches!(a, std::net::SocketAddr::V4(_)))
|
||||
.collect::<Vec<std::net::SocketAddr>>();
|
||||
}
|
||||
|
||||
if cli.force_ipv6 {
|
||||
addrs = addrs
|
||||
.into_iter()
|
||||
.filter(|a| matches!(a, std::net::SocketAddr::V6(_)))
|
||||
.collect::<Vec<std::net::SocketAddr>>();
|
||||
}
|
||||
|
||||
if addrs.is_empty() {
|
||||
println!("Server lookup failure: {}:telnet, Name or service not known", cli.host);
|
||||
}
|
||||
|
||||
let mut stream = None;
|
||||
for addr in addrs {
|
||||
println!("Connecting to {}:{} ...\r", cli.host, cli.port);
|
||||
match TcpStream::connect(addr) {
|
||||
Ok(_stream) => {
|
||||
stream = Some(_stream);
|
||||
break;
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Error: Failed to connect to {addr}: {e}");
|
||||
eprintln!("Trying next address");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let Some(mut stream) = stream else {
|
||||
eprintln!("Error: Failed to connect to host {}, bailing!", cli.host);
|
||||
return Ok(())
|
||||
};
|
||||
|
||||
stream.set_read_timeout(Some(Duration::from_secs(cli.socket_timeout)))?;
|
||||
stream.set_write_timeout(Some(Duration::from_secs(cli.socket_timeout)))?;
|
||||
|
||||
let mut buffer = [0u8; 1024];
|
||||
let mut state = State::Data;
|
||||
|
||||
let mut write_stream = stream.try_clone()?;
|
||||
|
||||
write_stream.write_all(&[cmd::IAC, cmd::WILL, cmd::ECHO])?;
|
||||
|
||||
std::thread::spawn(move || {
|
||||
let should_echo = {SETTINGS.lock().unwrap().echo};
|
||||
let mut stdin = std::io::stdin();
|
||||
let mut stdout = std::io::stdout();
|
||||
let mut buf = [0u8; 1];
|
||||
let mut real_buf = Vec::new();
|
||||
loop {
|
||||
let n = match stdin.read(&mut buf) {
|
||||
Ok(n) => n,
|
||||
Err(_) => {
|
||||
let _ = write_stream.shutdown(Shutdown::Both);
|
||||
break;
|
||||
},
|
||||
};
|
||||
if n == 0 { continue; }
|
||||
match buf[0] {
|
||||
3 => {
|
||||
let _ = write_stream.shutdown(Shutdown::Both);
|
||||
break;
|
||||
},
|
||||
8 | 127 => {
|
||||
if !real_buf.is_empty() {
|
||||
real_buf.pop();
|
||||
write_net_trace_backspace().unwrap();
|
||||
if should_echo {
|
||||
let _ = stdout.write_all(b"\x08 \x08");
|
||||
let _ = stdout.flush();
|
||||
}
|
||||
}
|
||||
let _ = write_stream.write_all(&buf);
|
||||
}
|
||||
|
||||
c => {
|
||||
write_net_trace(&[c]).unwrap();
|
||||
if should_echo {
|
||||
let _ = stdout.write(&[c]);
|
||||
let _ = stdout.flush();
|
||||
}
|
||||
real_buf.push(buf[0]);
|
||||
if write_stream.write_all(&[c]).is_err() {
|
||||
let _ = write_stream.shutdown(Shutdown::Both);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
loop {
|
||||
let settings = {
|
||||
SETTINGS.lock().unwrap().clone()
|
||||
};
|
||||
let n = match stream.read(&mut buffer) {
|
||||
Ok(0) => break,
|
||||
Ok(n) => n,
|
||||
Err(ref e) if e.kind() == std::io::ErrorKind::WouldBlock => continue,
|
||||
Err(e) => return Err(e.into()),
|
||||
};
|
||||
|
||||
let mut buf = Vec::from(buffer);
|
||||
buf.resize(n, 0);
|
||||
buf.reverse();
|
||||
while let Some(mut byte) = buf.pop() {
|
||||
// println!("{:?}", state);
|
||||
// println!("{}", byte);
|
||||
match state {
|
||||
State::Data => {
|
||||
if byte == cmd::IAC {
|
||||
state = State::Iac;
|
||||
} else {
|
||||
if settings.strip8b {
|
||||
byte = byte & 0x7F;
|
||||
}
|
||||
print!("{}", byte as char);
|
||||
std::io::stdout().flush().unwrap();
|
||||
write_net_trace(&[byte])?;
|
||||
}
|
||||
}
|
||||
State::Iac => match byte {
|
||||
cmd::IAC => {
|
||||
print!("{}", cmd::IAC as char);
|
||||
std::io::stdout().flush().unwrap();
|
||||
state = State::Data;
|
||||
}
|
||||
cmd::DO | cmd::DONT | cmd::WILL | cmd::WONT => {
|
||||
state = State::IacCmd(byte);
|
||||
}
|
||||
cmd::SB => {
|
||||
state = State::Subneg;
|
||||
}
|
||||
_ => state = State::Data,
|
||||
},
|
||||
State::IacCmd(cmd) => {
|
||||
match cmd {
|
||||
cmd::DO => {
|
||||
match byte {
|
||||
cmd::ECHO => {
|
||||
stream.write_all(&[cmd::IAC, cmd::WILL, cmd::ECHO])?;
|
||||
SETTINGS.lock().unwrap().echo = true;
|
||||
},
|
||||
cmd::BINARY => {
|
||||
stream.write_all(&[cmd::IAC, cmd::WILL, cmd::BINARY])?;
|
||||
if !cli.use_8bit_data_path {
|
||||
SETTINGS.lock().unwrap().strip8b = false;
|
||||
}
|
||||
},
|
||||
c => stream.write_all(&[cmd::IAC, cmd::WONT, c])?,
|
||||
}
|
||||
},
|
||||
cmd::WILL => {
|
||||
match byte {
|
||||
cmd::ECHO => {
|
||||
stream.write_all(&[cmd::IAC, cmd::DO, cmd::ECHO])?;
|
||||
SETTINGS.lock().unwrap().echo = false;
|
||||
}
|
||||
cmd::BINARY => {
|
||||
stream.write_all(&[cmd::IAC, cmd::DO, cmd::BINARY])?;
|
||||
if !cli.use_8bit_data_path {
|
||||
SETTINGS.lock().unwrap().strip8b = false;
|
||||
}
|
||||
},
|
||||
c => stream.write_all(&[cmd::IAC, cmd::DONT, c])?,
|
||||
}
|
||||
},
|
||||
cmd::WONT => {
|
||||
match byte {
|
||||
cmd::ECHO => {
|
||||
stream.write_all(&[cmd::IAC, cmd::DONT, cmd::ECHO])?;
|
||||
SETTINGS.lock().unwrap().echo = true;
|
||||
}
|
||||
cmd::BINARY => {
|
||||
stream.write_all(&[cmd::IAC, cmd::DONT, cmd::BINARY])?;
|
||||
if !cli.use_8bit_data_path {
|
||||
SETTINGS.lock().unwrap().strip8b = true;
|
||||
}
|
||||
},
|
||||
c => stream.write_all(&[cmd::IAC, cmd::DONT, c])?,
|
||||
}
|
||||
},
|
||||
cmd::DONT => {
|
||||
match byte {
|
||||
cmd::ECHO => {
|
||||
stream.write_all(&[cmd::IAC, cmd::WONT, cmd::ECHO])?;
|
||||
SETTINGS.lock().unwrap().echo = false;
|
||||
},
|
||||
cmd::BINARY => {
|
||||
stream.write_all(&[cmd::IAC, cmd::WONT, cmd::BINARY])?;
|
||||
if !cli.use_8bit_data_path {
|
||||
SETTINGS.lock().unwrap().strip8b = true;
|
||||
}
|
||||
},
|
||||
c => stream.write_all(&[cmd::IAC, cmd::WONT, c])?,
|
||||
}
|
||||
},
|
||||
_ => print!("Unknown cmd: '{byte}'"),
|
||||
};
|
||||
state = State::Data;
|
||||
}
|
||||
State::Subneg => {
|
||||
if byte == cmd::IAC {
|
||||
state = State::SubnegIac;
|
||||
}
|
||||
}
|
||||
State::SubnegIac => {
|
||||
if byte == cmd::SE {
|
||||
state = State::Data;
|
||||
} else if byte == cmd::IAC {
|
||||
state = State::Subneg;
|
||||
} else {
|
||||
state = State::Subneg;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn enable_raw_mode() -> anyhow::Result<Termios> {
|
||||
let stdin = std::io::stdin();
|
||||
let orig_term = termios::tcgetattr(&stdin)
|
||||
.map_err(|e| anyhow::anyhow!("tcgetattr failed: {}", e))?;
|
||||
let mut raw_term = orig_term.clone();
|
||||
termios::cfmakeraw(&mut raw_term);
|
||||
termios::tcsetattr(&stdin, nix::sys::termios::SetArg::TCSANOW, &raw_term)
|
||||
.map_err(|e| anyhow::anyhow!("tcsetattr failed: {}", e))?;
|
||||
Ok(orig_term)
|
||||
}
|
||||
|
||||
fn restore_terminal(orig_term: &Termios) -> anyhow::Result<()> {
|
||||
let stdin = std::io::stdin();
|
||||
print!("\r");
|
||||
nix::sys::termios::tcsetattr(&stdin, nix::sys::termios::SetArg::TCSANOW, &orig_term)
|
||||
.map_err(|e| anyhow::anyhow!("restore failed: {}", e))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
let cli = cli_args::CliArgs::parse();
|
||||
let orig_term = enable_raw_mode()?;
|
||||
if let Err(e) = main_loop(&cli) {
|
||||
let _ = restore_terminal(&orig_term);
|
||||
eprintln!("An error happened: {e}");
|
||||
return Ok(())
|
||||
}
|
||||
let _ = restore_terminal(&orig_term);
|
||||
Ok(())
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user