This commit is contained in:
firebadnofire 2024-04-16 14:17:13 -04:00
parent df042843b3
commit fb58747539
Signed by: firebadnofire
SSH Key Fingerprint: SHA256:bnN1TGRauJN84CxL1IZ/2uHNvJualwYkFjOKaaOilJE
5 changed files with 143 additions and 6 deletions

14
.gitignore vendored Normal file
View File

@ -0,0 +1,14 @@
# Generated by Cargo
# will have compiled files and executables
debug/
target/
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

9
Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "rms-server"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
#tokio = { version = "1", features = ["full"] }

27
LICENSE
View File

@ -1,9 +1,26 @@
MIT License
Libre Open Source Software (LOSS) License
Copyright (c) 2024 firebadnofire
| ||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|| |_
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
Permissions:
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
You are granted the freedom to use, modify, and distribute the software.
Any distribution of modified versions must be made available under this same license.
The source code of the software must always be accessible to users along with any modifications.
Limitations:
The licensed software, or any modified version, cannot be used for training or creating machine learning algorithms.
Any patent claims related to the software shall not be enforced against anyone using, modifying, or distributing the software in compliance with this license.
Disclaimer:
BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
Termination:
If you violate the terms of this license, your rights granted under it will be automatically terminated.

View File

@ -1,3 +1,5 @@
# RMS-server
Rust Messenger Service - Server
Rust Messenger Service - Server
Check out: <a href="https://gitea.archuser.org/firebadnofire/RMS-client">RMS-client</a>

95
src/main.rs Normal file
View File

@ -0,0 +1,95 @@
use std::env;
use std::fs::OpenOptions;
use std::io::{Read, Write};
use std::net::TcpListener;
use std::path::Path;
use std::thread;
use std::process;
fn main() {
let args: Vec<String> = env::args().collect();
let mut port = "7890"; // default port
// Command line argument parsing for port
if args.len() > 1 {
for i in 0..args.len() - 1 {
if args[i] == "-p" || args[i] == "--port" {
port = &args[i + 1];
}
}
}
let listener = TcpListener::bind(format!("0.0.0.0:{}", port)).unwrap_or_else(|_| {
log_verbose("Failed to bind on port ".to_owned() + port);
process::exit(1);
});
// Ensure the log directory exists
std::fs::create_dir_all("./log").expect("Failed to create log directory");
log_verbose(format!("Server listening on port {}", port));
for stream in listener.incoming() {
match stream {
Ok(stream) => {
let peer_addr = stream.peer_addr().unwrap(); // It's safe to unwrap here as connection is already established
log_verbose(format!("Client connected: {}", peer_addr));
thread::spawn(move || {
handle_client(stream);
});
}
Err(e) => {
log_verbose(format!("Connection failed: {}", e));
}
}
}
}
// Function to handle a single client
fn handle_client(mut stream: std::net::TcpStream) {
let mut buffer = [0; 1024];
let peer_addr = stream.peer_addr().unwrap().to_string(); // Log client IP
loop {
match stream.read(&mut buffer) {
Ok(0) => {
log_verbose(format!("Client disconnected: {}", peer_addr));
break;
},
Ok(size) => {
let msg = String::from_utf8_lossy(&buffer[..size]);
println!("{}", msg); // Print what client sends
log_message(format!("Received from {}: {}", peer_addr, msg)); // Log to messages file
},
Err(e) => {
log_verbose(format!("Failed to receive data from {}: {}", peer_addr, e));
break;
}
}
}
}
// Log verbose information to a file
fn log_verbose(message: String) {
let mut file = OpenOptions::new()
.create(true)
.write(true)
.append(true)
.open(Path::new("./log/RMS-server.log"))
.expect("Failed to open verbose log file");
writeln!(file, "{}", message).expect("Failed to write to verbose log file");
}
// Log raw messages to a separate file
fn log_message(message: String) {
println!("{}", message); // Print to stdout
let mut file = OpenOptions::new()
.create(true)
.write(true)
.append(true)
.open(Path::new("./log/rawmessages.log"))
.expect("Failed to open message log file");
writeln!(file, "{}", message).expect("Failed to write to message log file");
}