Compare commits
23
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b1a40554f9 | ||
|
|
808af585c9 | ||
|
|
9ade9799f6 | ||
|
|
bb8f5fc0d4 | ||
|
|
eaac88c973 | ||
|
|
b1a6b69867 | ||
|
|
7460cd314c | ||
|
|
9f0f5d8a3d | ||
|
|
09fd969a58 | ||
|
|
64676bfd5c | ||
|
|
a9ce9cc00b | ||
|
|
2d9cd3eefe | ||
|
|
a0808728ad | ||
|
|
ed06a50aa8 | ||
|
|
3691679be4 | ||
|
|
829db5445e | ||
|
|
ac174d0f39 | ||
|
|
375afb4ae7 | ||
|
|
2546dd6ba1 | ||
|
|
42f5ebd7bc | ||
|
|
29a270a01d | ||
|
|
598245457e | ||
|
|
a6c03b9857 |
No files matched your search
+75
@@ -59,3 +59,78 @@ Icon
|
|||||||
Network Trash Folder
|
Network Trash Folder
|
||||||
Temporary Items
|
Temporary Items
|
||||||
.apdisk
|
.apdisk
|
||||||
|
|
||||||
|
### JetBrains template
|
||||||
|
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
|
||||||
|
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
||||||
|
|
||||||
|
# User-specific stuff
|
||||||
|
.idea/**/workspace.xml
|
||||||
|
.idea/**/tasks.xml
|
||||||
|
.idea/**/usage.statistics.xml
|
||||||
|
.idea/**/dictionaries
|
||||||
|
.idea/**/shelf
|
||||||
|
|
||||||
|
# Generated files
|
||||||
|
.idea/**/contentModel.xml
|
||||||
|
|
||||||
|
# Sensitive or high-churn files
|
||||||
|
.idea/**/dataSources/
|
||||||
|
.idea/**/dataSources.ids
|
||||||
|
.idea/**/dataSources.local.xml
|
||||||
|
.idea/**/sqlDataSources.xml
|
||||||
|
.idea/**/dynamic.xml
|
||||||
|
.idea/**/uiDesigner.xml
|
||||||
|
.idea/**/dbnavigator.xml
|
||||||
|
|
||||||
|
# Gradle
|
||||||
|
.idea/**/gradle.xml
|
||||||
|
.idea/**/libraries
|
||||||
|
|
||||||
|
# Gradle and Maven with auto-import
|
||||||
|
# When using Gradle or Maven with auto-import, you should exclude module files,
|
||||||
|
# since they will be recreated, and may cause churn. Uncomment if using
|
||||||
|
# auto-import.
|
||||||
|
# .idea/artifacts
|
||||||
|
# .idea/compiler.xml
|
||||||
|
# .idea/jarRepositories.xml
|
||||||
|
# .idea/modules.xml
|
||||||
|
# .idea/*.iml
|
||||||
|
# .idea/modules
|
||||||
|
# *.iml
|
||||||
|
# *.ipr
|
||||||
|
|
||||||
|
# CMake
|
||||||
|
cmake-build-*/
|
||||||
|
|
||||||
|
# Mongo Explorer plugin
|
||||||
|
.idea/**/mongoSettings.xml
|
||||||
|
|
||||||
|
# File-based project format
|
||||||
|
*.iws
|
||||||
|
|
||||||
|
# IntelliJ
|
||||||
|
out/
|
||||||
|
|
||||||
|
# mpeltonen/sbt-idea plugin
|
||||||
|
.idea_modules/
|
||||||
|
|
||||||
|
# JIRA plugin
|
||||||
|
atlassian-ide-plugin.xml
|
||||||
|
|
||||||
|
# Cursive Clojure plugin
|
||||||
|
.idea/replstate.xml
|
||||||
|
|
||||||
|
# Crashlytics plugin (for Android Studio and IntelliJ)
|
||||||
|
com_crashlytics_export_strings.xml
|
||||||
|
crashlytics.properties
|
||||||
|
crashlytics-build.properties
|
||||||
|
fabric.properties
|
||||||
|
|
||||||
|
# Editor-based Rest Client
|
||||||
|
.idea/httpRequests
|
||||||
|
|
||||||
|
# Android studio 3.1+ serialized cache file
|
||||||
|
.idea/caches/build_file_checksums.ser
|
||||||
|
|
||||||
|
.vscode/launch.json
|
||||||
@@ -6,3 +6,4 @@ edition = "2021"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
crypto_box = "0.8.2"
|
||||||
+81
-6
@@ -1,10 +1,85 @@
|
|||||||
|
use std::io::Write;
|
||||||
use std::net::TcpStream;
|
use std::net::TcpStream;
|
||||||
|
use crypto_box::{
|
||||||
|
aead::{Aead, AeadCore, OsRng},
|
||||||
|
SalsaBox, PublicKey, SecretKey
|
||||||
|
};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let stream = TcpStream::connect("127.0.0.1:8080");
|
let port: u32 = 7878;
|
||||||
if stream.is_ok() {
|
//let stream = TcpStream::connect("172.30.16.1:8080");
|
||||||
println!("Connected to the server!");
|
//let stream = TcpStream::connect("27.0.0.1:8080");
|
||||||
} else {
|
match TcpStream::connect(format!("localhost:{}", port)) {
|
||||||
println!("Couldn't connect to server...");
|
Ok(mut stream) => {
|
||||||
|
println!("Successfully connected to server");
|
||||||
|
|
||||||
|
let bob_init_pub_key = PublicKey::from([
|
||||||
|
0xe8, 0x98, 0xc, 0x86, 0xe0, 0x32, 0xf1, 0xeb,
|
||||||
|
0x29, 0x75, 0x5, 0x2e, 0x8d, 0x65, 0xbd, 0xdd,
|
||||||
|
0x15, 0xc3, 0xb5, 0x96, 0x41, 0x17, 0x4e, 0xc9,
|
||||||
|
0x67, 0x8a, 0x53, 0x78, 0x9d, 0x92, 0xc7, 0x54,
|
||||||
|
]);
|
||||||
|
let (salsa_box, pub_key) = generate_box(bob_init_pub_key.clone());
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let mut buffer = String::new();
|
||||||
|
match std::io::stdin().read_line(&mut buffer) {
|
||||||
|
Ok(n) => {
|
||||||
|
if n == 0 {
|
||||||
|
break; // Beenden bei EOF
|
||||||
|
}
|
||||||
|
//
|
||||||
|
// Encryption
|
||||||
|
//
|
||||||
|
|
||||||
|
// Get a random nonce to encrypt the message under
|
||||||
|
let nonce = SalsaBox::generate_nonce(&mut OsRng);
|
||||||
|
|
||||||
|
// Message to encrypt
|
||||||
|
let plaintext = buffer.trim().as_bytes();
|
||||||
|
|
||||||
|
// Encrypt the message using the box
|
||||||
|
let ciphertext = salsa_box.encrypt(&nonce, &plaintext[..]).expect("Fehler");
|
||||||
|
|
||||||
|
println!("Sending {0} as {1:?}", buffer.trim(), plaintext);
|
||||||
|
stream.write(buffer.as_bytes()).unwrap();
|
||||||
|
|
||||||
|
//
|
||||||
|
// Decryption
|
||||||
|
//
|
||||||
|
|
||||||
|
// Decrypt the message, using the same randomly generated nonce
|
||||||
|
let decrypted_plaintext = salsa_box.decrypt(&nonce, &ciphertext[..]).expect("Fehler");
|
||||||
|
let dec_plain_plaintext = std::str::from_utf8(&*decrypted_plaintext).expect("Nö");
|
||||||
|
assert_eq!(&plaintext[..], &decrypted_plaintext[..]);
|
||||||
|
|
||||||
|
println!("Sent {0:?} as cypher: {1:?}, decrypted: {2:?}, {3}", plaintext, ciphertext, decrypted_plaintext, dec_plain_plaintext);
|
||||||
|
}
|
||||||
|
Err(error) => {
|
||||||
|
println!("error: {error}");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
println!("Failed to connect: {}", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} // the stream is closed here
|
println!("Terminated.");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn generate_box(partner_public_key: PublicKey) -> (SalsaBox, PublicKey) {
|
||||||
|
// Generate a random secret key.
|
||||||
|
// NOTE: The secret key bytes can be accessed by calling `secret_key.as_bytes()`
|
||||||
|
let own_secret_key = SecretKey::generate(&mut OsRng);
|
||||||
|
|
||||||
|
// Get the public key for the secret key we just generated
|
||||||
|
let own_public_key = own_secret_key.public_key().clone();
|
||||||
|
|
||||||
|
// Create a `SalsaBox` by performing Diffie-Hellman key agreement between
|
||||||
|
// the two keys.
|
||||||
|
let salsa_box = SalsaBox::new(&partner_public_key, &own_secret_key);
|
||||||
|
(salsa_box, own_public_key)
|
||||||
|
}
|
||||||
|
|
||||||
+51
-1
@@ -1,3 +1,53 @@
|
|||||||
|
use std::{
|
||||||
|
io::{prelude::*, BufReader, Lines},
|
||||||
|
net::{TcpListener, TcpStream},
|
||||||
|
};
|
||||||
|
use std::thread::available_parallelism;
|
||||||
|
use crate::thread_pool::ThreadPool;
|
||||||
|
|
||||||
|
mod thread_pool;
|
||||||
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println! ("server");
|
// start server on localhost (for remote access use: 0.0.0.0)
|
||||||
|
let listener = TcpListener::bind("127.0.0.1:7878").unwrap();
|
||||||
|
|
||||||
|
// create thread pool (size = no. of cpu cores)
|
||||||
|
let pool = ThreadPool::new(available_parallelism().unwrap().get());
|
||||||
|
|
||||||
|
// handling function for incoming requests
|
||||||
|
for stream in listener.incoming() {
|
||||||
|
let stream = stream.unwrap();
|
||||||
|
|
||||||
|
// pass request to thread pool
|
||||||
|
pool.execute(|| {
|
||||||
|
handle_connection(stream);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
println!("Shutting down.");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn handle_connection(mut stream: TcpStream) {
|
||||||
|
// clone TcpStream for simultaneous receiving and sending
|
||||||
|
let mut stream2: TcpStream = stream.try_clone().unwrap();
|
||||||
|
|
||||||
|
// initialize reading buffer (for better performance!)
|
||||||
|
let buf_reader: BufReader<&mut TcpStream> = BufReader::new(&mut stream);
|
||||||
|
// request: read received text line by line (continuous until connection closed)
|
||||||
|
let request: Lines<BufReader<&mut TcpStream>> = buf_reader.lines();
|
||||||
|
|
||||||
|
println!("Request: {:#?}", request);
|
||||||
|
|
||||||
|
// processing function for every text line received by request (terminates when conn closed)
|
||||||
|
for elem in request
|
||||||
|
{
|
||||||
|
// store received line in variable s
|
||||||
|
let s = elem.unwrap();
|
||||||
|
println!("{:?}", s);
|
||||||
|
|
||||||
|
// send received line back to sender (append LF)
|
||||||
|
stream2.write_all(s.as_bytes()).unwrap();
|
||||||
|
stream2.write_all(b"\n").unwrap();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,116 @@
|
|||||||
|
use std::{
|
||||||
|
sync::{mpsc, Arc, Mutex},
|
||||||
|
thread
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
pub struct ThreadPool {
|
||||||
|
workers: Vec<Worker>,
|
||||||
|
sender: Option<mpsc::Sender<Job>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Box: pointer for heap allocation (smart pointer); dyn: unknown type to compile time
|
||||||
|
type Job = Box<dyn FnOnce() + Send + 'static>;
|
||||||
|
|
||||||
|
|
||||||
|
impl ThreadPool {
|
||||||
|
/// Create a new ThreadPool.
|
||||||
|
///
|
||||||
|
/// The size is the number of threads in the pool.
|
||||||
|
///
|
||||||
|
/// # Panics
|
||||||
|
///
|
||||||
|
/// The `new` function will panic if the size is zero.
|
||||||
|
pub fn new(size: usize) -> ThreadPool {
|
||||||
|
assert!(size > 0);
|
||||||
|
|
||||||
|
// create asynchronous channel with sender/receiver pair (for jobs!)
|
||||||
|
let (sender, receiver) = mpsc::channel();
|
||||||
|
|
||||||
|
// protect receiver with thread-safe shared (Arc) mutex to avoid multiple access
|
||||||
|
let receiver = Arc::new(Mutex::new(receiver));
|
||||||
|
|
||||||
|
// pre(!)allocate space for workers
|
||||||
|
let mut workers = Vec::with_capacity(size);
|
||||||
|
|
||||||
|
// initialize all workers...
|
||||||
|
for id in 0..size {
|
||||||
|
// create worker with unique id and pointer to the shared mutex
|
||||||
|
workers.push(Worker::new(id, Arc::clone(&receiver)));
|
||||||
|
}
|
||||||
|
|
||||||
|
// set thread pool variables
|
||||||
|
ThreadPool {
|
||||||
|
workers,
|
||||||
|
// Some --> enum variant (Option): indicates that sender may not exist (=be null)
|
||||||
|
sender: Some(sender),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn execute<F>(&self, f: F)
|
||||||
|
where
|
||||||
|
// FnOnce() --> only callable once; Send --> transfer to another thread; 'static --> lifetime
|
||||||
|
F: FnOnce() + Send + 'static,
|
||||||
|
{
|
||||||
|
let job = Box::new(f);
|
||||||
|
|
||||||
|
// commit job to sender object --> pass to threads for execution
|
||||||
|
self.sender.as_ref().unwrap().send(job).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for ThreadPool {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
// drop sender first
|
||||||
|
drop(self.sender.take());
|
||||||
|
|
||||||
|
// then drop workers
|
||||||
|
for worker in &mut self.workers {
|
||||||
|
println!("Shutting down worker {}", worker.id);
|
||||||
|
|
||||||
|
// join worker thread
|
||||||
|
if let Some(thread) = worker.thread.take() {
|
||||||
|
thread.join().unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
struct Worker {
|
||||||
|
id: usize,
|
||||||
|
thread: Option<thread::JoinHandle<()>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Worker {
|
||||||
|
/// Create a new Worker..
|
||||||
|
///
|
||||||
|
/// The id is a unique identifier.
|
||||||
|
/// The receiver is a shared pointer to a receiver protected by Mutex.
|
||||||
|
fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Job>>>) -> Worker {
|
||||||
|
// spawn thread and enter infinite loop
|
||||||
|
let thread = thread::spawn(move || loop {
|
||||||
|
// lock mutex and wait for job
|
||||||
|
let message = receiver.lock().unwrap().recv();
|
||||||
|
|
||||||
|
match message {
|
||||||
|
// normal operation --> execute job
|
||||||
|
Ok(job) => {
|
||||||
|
println!("Worker {id} got a job; executing.");
|
||||||
|
|
||||||
|
job();
|
||||||
|
}
|
||||||
|
// exit gracefully when pool is closed --> recv will return error then
|
||||||
|
Err(_) => {
|
||||||
|
println!("Worker {id} disconnected; shutting down.");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Worker {
|
||||||
|
id,
|
||||||
|
thread: Some(thread),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user