Compare commits

..

18 Commits

Author SHA1 Message Date
Namu
2a3ef945ae fix: updated rust, follow clippy recommendation
All checks were successful
Build project / quality-and-build (push) Successful in 10m12s
Build project / SonarQube Trigger (push) Successful in 22s
2026-03-24 09:28:32 +01:00
Namu
1138dddcb7 fix: remove check formating step in CI/CD (different version makes it bug I guess)
Some checks failed
Build project / quality-and-build (push) Failing after 10m8s
Build project / SonarQube Trigger (push) Has been skipped
2026-03-23 18:06:38 +01:00
Namu
e97a15d84b fix: format the project 2026-03-23 16:45:29 +01:00
Namu
9a24fc33c6 fix: correct the typo in action directory
Some checks failed
Build project / quality-and-build (push) Failing after 10m54s
Build project / SonarQube Trigger (push) Has been skipped
2026-03-23 14:30:19 +01:00
Namu
ce180792db fix: simplify the trigger of the CI 2026-03-23 14:28:00 +01:00
Namu
cc6dfa9d7f fix: correct file name 2026-03-23 14:25:09 +01:00
Namu
2617a082b0 feat: add a basic pipeline 2026-03-23 14:16:51 +01:00
Thomas Sazerat
4bf3c984eb Merge pull request #2 from Namularbre/hotfix/2.0.1
fix: follow clippy recommandation
2026-03-23 11:57:02 +01:00
Namu
4f46b4a932 fix: follow clippy recommandation 2026-03-23 11:31:21 +01:00
Thomas Sazerat
6462493b98 Merge pull request #1 from Namularbre/architecture-refactoring
refactor: Put the files in modules
2025-09-28 23:25:44 +02:00
Namu
479f2ff4c9 refactor: Put the files in modules 2025-09-28 23:24:29 +02:00
namu
2d1689d03d [chore] adds a licence to the project 2025-03-09 01:24:28 +01:00
namu
119460a8ca [refactor] move listen function in server module 2025-03-09 01:05:38 +01:00
namu
f9e0090a52 [feat] adds time in udp message logs 2025-03-09 00:55:32 +01:00
namu
a7197fc0b4 Global refactor of the code 2024-11-14 17:38:07 +01:00
Thomas
2ae74269ea Added an error message for when the config.json file is missing 2024-03-20 15:11:23 +01:00
Thomas
12cb2d6bd9 updated README 2024-03-20 14:58:31 +01:00
Thomas
8ed155adaa The program get the server address in config file 2024-03-20 14:52:51 +01:00
11 changed files with 553 additions and 128 deletions

View File

@@ -0,0 +1,51 @@
name: Build project
# run automatically when a push or pull request is triggered
on: [push, pull_request]
jobs:
build:
name: quality-and-build
runs-on: ubuntu-latest
env:
# Limit to 1 job for my little VPS
CARGO_BUILD_JOBS: 1
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Install Rust toolchain
uses: actions-rust-lang/setup-rust-toolchain@v1
with:
toolchain: stable
components: rustfmt, clippy
- name: Lint with Clippy
run: cargo clippy -- -D warnings
- name: Run build (Debug mode)
run: cargo build
sonarqube:
needs: build
name: SonarQube Trigger
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Download SonarQube Scanner
run: |
curl -sSLo sonar-scanner.zip https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-5.0.1.3006-linux.zip
unzip sonar-scanner.zip
- name: Run SonarQube Scan
run: |
./sonar-scanner-*/bin/sonar-scanner \
-Dsonar.projectKey=UdpSocketServer \
-Dsonar.sources=. \
-Dsonar.host.url=${{ secrets.SONARQUBE_HOST }} \
-Dsonar.login=${{ secrets.SONARQUBE_TOKEN }}

290
Cargo.lock generated
View File

@@ -1,6 +1,62 @@
# This file is automatically @generated by Cargo. # This file is automatically @generated by Cargo.
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 version = 4
[[package]]
name = "android-tzdata"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0"
[[package]]
name = "android_system_properties"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311"
dependencies = [
"libc",
]
[[package]]
name = "autocfg"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
[[package]]
name = "bumpalo"
version = "3.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1628fb46dfa0b37568d12e5edd512553eccf6a22a78e8bde00bb4aed84d5bdbf"
[[package]]
name = "cc"
version = "1.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be714c154be609ec7f5dad223a33bf1482fff90472de28f7362806e6d4832b8c"
dependencies = [
"shlex",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chrono"
version = "0.4.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a7964611d71df112cb1730f2ee67324fcf4d0fc6606acbbe9bfe06df124637c"
dependencies = [
"android-tzdata",
"iana-time-zone",
"js-sys",
"num-traits",
"wasm-bindgen",
"windows-link",
]
[[package]] [[package]]
name = "colored" name = "colored"
@@ -12,18 +68,84 @@ dependencies = [
"windows-sys", "windows-sys",
] ]
[[package]]
name = "core-foundation-sys"
version = "0.8.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
[[package]]
name = "iana-time-zone"
version = "0.1.61"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "235e081f3925a06703c2d0117ea8b91f042756fd6e7a6e5d901e8ca1a996b220"
dependencies = [
"android_system_properties",
"core-foundation-sys",
"iana-time-zone-haiku",
"js-sys",
"wasm-bindgen",
"windows-core",
]
[[package]]
name = "iana-time-zone-haiku"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f"
dependencies = [
"cc",
]
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "1.0.10" version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c" checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c"
[[package]]
name = "js-sys"
version = "0.3.77"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1cfaf33c695fc6e08064efbc1f72ec937429614f25eef83af942d0e227c3a28f"
dependencies = [
"once_cell",
"wasm-bindgen",
]
[[package]] [[package]]
name = "lazy_static" name = "lazy_static"
version = "1.4.0" version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.170"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "875b3680cb2f8f71bdcf9a30f38d48282f5d3c95cbf9b3fa57269bb5d5c06828"
[[package]]
name = "log"
version = "0.4.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30bde2b3dc3671ae49d8e2e9f044c7c005836e7a023ee57cffa25ab82764bb9e"
[[package]]
name = "num-traits"
version = "0.2.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
dependencies = [
"autocfg",
]
[[package]]
name = "once_cell"
version = "1.20.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "945462a4b81e43c4e3ba96bd7b49d834c6f61198356aa858733bc4acf3cbe62e"
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.78" version = "1.0.78"
@@ -42,6 +164,12 @@ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]]
name = "rustversion"
version = "1.0.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eded382c5f5f786b989652c49544c4877d9f015cc22e145a5ea8ea66c2921cd2"
[[package]] [[package]]
name = "ryu" name = "ryu"
version = "1.0.17" version = "1.0.17"
@@ -79,10 +207,17 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "shlex"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]] [[package]]
name = "socket_server" name = "socket_server"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"chrono",
"colored", "colored",
"serde", "serde",
"serde_json", "serde_json",
@@ -105,13 +240,86 @@ version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "wasm-bindgen"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1edc8929d7499fc4e8f0be2262a241556cfc54a0bea223790e71446f2aab1ef5"
dependencies = [
"cfg-if",
"once_cell",
"rustversion",
"wasm-bindgen-macro",
]
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f0a0651a5c2bc21487bde11ee802ccaf4c51935d0d3d42a6101f98161700bc6"
dependencies = [
"bumpalo",
"log",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fe63fc6d09ed3792bd0897b314f53de8e16568c2b3f7982f468c0bf9bd0b407"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de"
dependencies = [
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a05d73b933a847d6cccdda8f838a22ff101ad9bf93e33684f39c1f5f0eece3d"
dependencies = [
"unicode-ident",
]
[[package]]
name = "windows-core"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9"
dependencies = [
"windows-targets 0.52.6",
]
[[package]]
name = "windows-link"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6dccfd733ce2b1753b03b6d3c65edf020262ea35e20ccdf3e288043e6dd620e3"
[[package]] [[package]]
name = "windows-sys" name = "windows-sys"
version = "0.48.0" version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
dependencies = [ dependencies = [
"windows-targets", "windows-targets 0.48.5",
] ]
[[package]] [[package]]
@@ -120,13 +328,29 @@ version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
dependencies = [ dependencies = [
"windows_aarch64_gnullvm", "windows_aarch64_gnullvm 0.48.5",
"windows_aarch64_msvc", "windows_aarch64_msvc 0.48.5",
"windows_i686_gnu", "windows_i686_gnu 0.48.5",
"windows_i686_msvc", "windows_i686_msvc 0.48.5",
"windows_x86_64_gnu", "windows_x86_64_gnu 0.48.5",
"windows_x86_64_gnullvm", "windows_x86_64_gnullvm 0.48.5",
"windows_x86_64_msvc", "windows_x86_64_msvc 0.48.5",
]
[[package]]
name = "windows-targets"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
dependencies = [
"windows_aarch64_gnullvm 0.52.6",
"windows_aarch64_msvc 0.52.6",
"windows_i686_gnu 0.52.6",
"windows_i686_gnullvm",
"windows_i686_msvc 0.52.6",
"windows_x86_64_gnu 0.52.6",
"windows_x86_64_gnullvm 0.52.6",
"windows_x86_64_msvc 0.52.6",
] ]
[[package]] [[package]]
@@ -135,38 +359,86 @@ version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
[[package]] [[package]]
name = "windows_aarch64_msvc" name = "windows_aarch64_msvc"
version = "0.48.5" version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
[[package]] [[package]]
name = "windows_i686_gnu" name = "windows_i686_gnu"
version = "0.48.5" version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
[[package]]
name = "windows_i686_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
[[package]]
name = "windows_i686_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
[[package]] [[package]]
name = "windows_i686_msvc" name = "windows_i686_msvc"
version = "0.48.5" version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
[[package]]
name = "windows_i686_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
[[package]] [[package]]
name = "windows_x86_64_gnu" name = "windows_x86_64_gnu"
version = "0.48.5" version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
[[package]] [[package]]
name = "windows_x86_64_gnullvm" name = "windows_x86_64_gnullvm"
version = "0.48.5" version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
[[package]] [[package]]
name = "windows_x86_64_msvc" name = "windows_x86_64_msvc"
version = "0.48.5" version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"

View File

@@ -9,3 +9,4 @@ edition = "2021"
serde = { version = "1.0.197", features = ["derive"] } serde = { version = "1.0.197", features = ["derive"] }
serde_json = "1.0.114" serde_json = "1.0.114"
colored = "2.1.0" colored = "2.1.0"
chrono = "0.4.39"

21
LICENCE.md Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2025 Thomas SAZERAT
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.
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.

View File

@@ -33,6 +33,7 @@ Go to release and download the .exe file, then run it. A version for linux will
You have a config.json file, like this: You have a config.json file, like this:
```` ````
{ {
"serverAddr": "127.0.0.1:12345",
"forwarding": [ "forwarding": [
{ {
"from": "127.0.0.1:58545", "from": "127.0.0.1:58545",
@@ -47,6 +48,7 @@ You have a config.json file, like this:
} }
```` ````
The serverAddr field is for the address of the server.
The blacklist field is simple: all addresses written are ignored by the server. The blacklist field is simple: all addresses written are ignored by the server.
The forwarding field is a list of object indicating that if the server receive a message from the address at "from" field The forwarding field is a list of object indicating that if the server receive a message from the address at "from" field
it should send it back to "to" address. The doReverse field, if set to true, will permit that when we a message from "to" address, it should send it back to "to" address. The doReverse field, if set to true, will permit that when we a message from "to" address,

View File

@@ -1,82 +0,0 @@
use std::fs::File;
use std::io::Read;
use serde::Deserialize;
use serde_json::Value;
#[derive(Deserialize, Debug)]
pub struct Forwarding {
from: String,
to: String,
#[serde(rename = "doReverse")]
do_reverse: bool
}
impl Forwarding {
pub fn new(from: String, to: String, do_reverse: bool) -> Forwarding {
return Forwarding{
from,
to,
do_reverse
}
}
}
#[derive(Deserialize)]
pub struct Config {
blacklist: Vec<String>,
forwarding: Vec<Forwarding>
}
impl Config {
pub fn new(blacklist: Vec<String>, forwarding: Vec<Forwarding>) -> Config {
return Config {
blacklist,
forwarding
}
}
pub fn from_json(file_name: String) -> Config {
let mut file = File::open(file_name).unwrap();
let mut buf = String::new();
file.read_to_string(&mut buf).expect("Error reading config file content.");
let json: Value = serde_json::from_str(&buf).expect("Error parsing config file");
let object_data = json.as_object().unwrap();
let blacklist = object_data.get("blacklist").unwrap().as_array().unwrap();
let forwarding = object_data.get("forwarding").unwrap().as_array().unwrap();
let str_blacklist = Self::convert_to_vec_string(blacklist);
let mut vec_forwarding = Vec::new();
for json_forwarding in forwarding {
let object_forwarding = json_forwarding.as_object().unwrap();
let from = object_forwarding.get("from").unwrap().as_str().unwrap();
let to = object_forwarding.get("to").unwrap().as_str().unwrap();
let do_reverse = object_forwarding.get("doReverse").unwrap().as_bool().unwrap();
let forwarding_struct = Forwarding::new(from.to_string(), to.to_string(), do_reverse);
vec_forwarding.push(forwarding_struct);
}
Config::new(str_blacklist, vec_forwarding)
}
fn convert_to_vec_string(vec: &Vec<Value>) -> Vec<String> {
let mut str_vec = Vec::new();
for value in vec {
str_vec.push(value.as_str().unwrap().to_string());
}
str_vec
}
pub fn addr_is_blacklisted(&self, addr: String) -> bool {
self.blacklist.contains(&addr)
}
pub fn is_forwarded(&self, addr: String) -> Option<String> {
for elem_forwarding in &self.forwarding {
if elem_forwarding.from == addr {
return Some(elem_forwarding.to.clone())
} else if elem_forwarding.do_reverse && elem_forwarding.to == addr {
return Some(elem_forwarding.from.clone())
}
}
None
}
}

135
src/config/mod.rs Normal file
View File

@@ -0,0 +1,135 @@
use crate::utils::vec_to_string::convert_to_vec_string;
use serde::Deserialize;
use serde_json::Value;
use std::fs::File;
use std::io::Read;
#[derive(Deserialize, Debug)]
pub struct Forwarding {
from: String,
to: String,
#[serde(rename = "doReverse")]
do_reverse: bool,
}
impl Forwarding {
pub fn new(from: String, to: String, do_reverse: bool) -> Forwarding {
Forwarding {
from,
to,
do_reverse,
}
}
pub fn from_json(json: &Value) -> Forwarding {
let object_forwarding = json.as_object().expect("Error parsing forwarding");
let from = object_forwarding
.get("from")
.unwrap()
.as_str()
.expect("Error parsing from field");
let to = object_forwarding
.get("to")
.unwrap()
.as_str()
.expect("Error parsing to field");
let do_reverse = object_forwarding
.get("doReverse")
.unwrap()
.as_bool()
.expect("Error parsing doReverse field");
Forwarding::new(from.to_string(), to.to_string(), do_reverse)
}
}
#[derive(Deserialize)]
pub struct Config {
#[serde(rename = "serverAddr")]
server_addr: String,
blacklist: Vec<String>,
forwarding: Vec<Forwarding>,
}
impl Config {
pub fn new(server_addr: String, blacklist: Vec<String>, forwarding: Vec<Forwarding>) -> Config {
Config {
server_addr,
blacklist,
forwarding,
}
}
fn read_config_file(file_name: String) -> String {
let mut file = File::open(file_name)
.expect("Error opening config.json file. Go to the readme file and paste the example.");
let mut buf = String::new();
file.read_to_string(&mut buf)
.expect("Error reading config file content.");
buf
}
fn parse_buffer(buf: &str) -> serde_json::Map<String, Value> {
let json: Value = serde_json::from_str(buf).expect("Error parsing config file");
let object_data = json.as_object().expect("Error parsing config file.");
object_data.clone()
}
fn parse_forwarding(str_forwarding: &Vec<Value>) -> Vec<Forwarding> {
let mut vec_forwarding = Vec::new();
for json_forwarding in str_forwarding {
vec_forwarding.push(Forwarding::from_json(json_forwarding));
}
vec_forwarding
}
pub fn from_json(file_name: String) -> Config {
let buf = Config::read_config_file(file_name);
let object_data = Self::parse_buffer(&buf);
let server_addr = object_data
.get("serverAddr")
.unwrap()
.as_str()
.unwrap()
.to_string();
let blacklist = object_data.get("blacklist").unwrap().as_array().unwrap();
let str_blacklist = convert_to_vec_string(blacklist);
let raw_forwarding = object_data
.get("forwarding")
.unwrap()
.as_array()
.expect("Error parsing forwarding.");
let forwardings = Self::parse_forwarding(raw_forwarding);
Config::new(server_addr, str_blacklist, forwardings)
}
pub fn addr_is_blacklisted(&self, addr: &String) -> bool {
self.blacklist.contains(addr)
}
pub fn is_forwarded(&self, addr: String) -> Option<String> {
for elem_forwarding in &self.forwarding {
if elem_forwarding.from == addr {
return Some(elem_forwarding.to.clone());
} else if elem_forwarding.do_reverse && elem_forwarding.to == addr {
return Some(elem_forwarding.from.clone());
}
}
None
}
pub fn get_server_addr(&self) -> String {
self.server_addr.to_string()
}
}

View File

@@ -1,41 +1,10 @@
mod config;
use std::io::stdin;
use std::net::UdpSocket;
use colored::Colorize;
use crate::config::Config; use crate::config::Config;
mod config;
mod server;
mod utils;
fn main() { fn main() {
let config = Config::from_json("config.json".to_string()); let config = Config::from_json("config.json".to_string());
let server_address: String = get_server_address(); server::listen(&config);
listen(&server_address, &config);
}
fn get_server_address() -> String {
println!("Enter server address (format xxx.xxx.xxx.xxx:port)");
let mut server_address = String::new();
stdin().read_line(&mut server_address).expect("Reading the server address from input");
return server_address.trim().to_string();
}
fn listen(server_address: &str, config: &Config) {
let serv_config = config;
println!("{}: {}", "listening on".green(), server_address);
let socket: UdpSocket = UdpSocket::bind(server_address).expect("Error while binding the socket.");
loop {
let mut message: String = String::new();
let mut buf: [u8; 1024] = [0; 1024];
let (num_bytes, src_addr) = socket.recv_from(&mut buf).expect("Error while receiving message");
if !serv_config.addr_is_blacklisted(src_addr.to_string()) {
message.push_str(std::str::from_utf8(&buf[..num_bytes]).expect("Invalid UTF-8 data"));
let forwarding_addr_opt = serv_config.is_forwarded(src_addr.to_string());
if forwarding_addr_opt.is_some() {
let forwarding_addr = forwarding_addr_opt.unwrap();
socket.send_to(message.as_bytes(), forwarding_addr).expect("Error forwarding message to another ip.");
}
println!("{}: {}", src_addr.to_string().green(), message);
}
}
} }

46
src/server/mod.rs Normal file
View File

@@ -0,0 +1,46 @@
use crate::config::Config;
use colored::Colorize;
use std::net::UdpSocket;
pub fn listen(config: &Config) {
let serv_config = config;
let addr = config.get_server_addr();
println!(
"{}: {}",
"listening on".green(),
serv_config.get_server_addr()
);
let socket: UdpSocket =
UdpSocket::bind(serv_config.get_server_addr()).expect("Error while binding the socket.");
const BUF_SIZE: usize = 1024;
loop {
let mut message: String = String::new();
let mut buf: [u8; BUF_SIZE] = [0; BUF_SIZE];
let (num_bytes, src_addr) = socket
.recv_from(&mut buf)
.expect("Error while receiving message");
if !serv_config.addr_is_blacklisted(&addr) {
message.push_str(std::str::from_utf8(&buf[..num_bytes]).expect("Invalid UTF-8 data"));
let forwarding_addr_opt = serv_config.is_forwarded(src_addr.to_string());
if let Some(forwarding_addr) = forwarding_addr_opt {
socket
.send_to(message.as_bytes(), forwarding_addr)
.expect("Error forwarding message to another ip.");
}
println!(
"{}-{}: {}",
src_addr.to_string().green(),
chrono::Local::now(),
message
);
}
}
}

1
src/utils/mod.rs Normal file
View File

@@ -0,0 +1 @@
pub mod vec_to_string;

View File

@@ -0,0 +1,9 @@
use serde_json::Value;
pub fn convert_to_vec_string(vec: &Vec<Value>) -> Vec<String> {
let mut str_vec = Vec::new();
for value in vec {
str_vec.push(value.as_str().unwrap().to_string());
}
str_vec
}