Peer to peer server and client

This commit is contained in:
Myx
2024-08-20 21:10:46 +02:00
parent 271a01ce30
commit d93fd1db58
7 changed files with 755 additions and 53 deletions

85
.github/workflows/rust.yml vendored Normal file
View File

@@ -0,0 +1,85 @@
name: Build and Release
on:
push:
branches:
- master
jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
target: [x86_64-unknown-linux-gnu, x86_64-apple-darwin, x86_64-pc-windows-gnu]
include:
- os: ubuntu-latest
target: x86_64-unknown-linux-gnu
- os: macos-latest
target: x86_64-apple-darwin
- os: windows-latest
target: x86_64-pc-windows-gnu
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
- name: Build
run: cargo build --release --target ${{ matrix.target }}
- name: Upload artifact
uses: actions/upload-artifact@v2
with:
name: ${{ matrix.os }}-binary
path: target/${{ matrix.target }}/release/your_binary_name
release:
needs: build
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Get Previous Tag
id: previoustag
uses: WyriHaximus/github-action-get-previous-tag@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Get Next SemVer
id: semvers
uses: WyriHaximus/github-action-next-semvers@v1
with:
version: ${{ steps.previoustag.outputs.tag }}
- name: Download artifacts
uses: actions/download-artifact@v2
with:
name: ${{ matrix.os }}-binary
path: ./artifacts
- name: Create Release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ steps.semvers.outputs.v_patch }}
release_name: Release ${{ steps.semvers.outputs.v_patch }}
draft: false
prerelease: false
- name: Upload Release Asset
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./artifacts
asset_name: BeetleWire_${{ matrix.os }}
asset_content_type: application/octet-stream

461
Cargo.lock generated
View File

@@ -17,6 +17,15 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "aho-corasick"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
dependencies = [
"memchr",
]
[[package]]
name = "autocfg"
version = "1.3.0"
@@ -44,6 +53,12 @@ version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8"
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bitflags"
version = "2.6.0"
@@ -86,6 +101,28 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "colored"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cbf2150cce219b664a8a70df7a1f933836724b503f8a413af9365b4dcc4d90b8"
dependencies = [
"lazy_static",
"windows-sys 0.48.0",
]
[[package]]
name = "console"
version = "0.15.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e1f83fc076bd6dd27517eacdf25fef6c4dfe5f1d7448bafaaf3a26f13b5e4eb"
dependencies = [
"encode_unicode",
"lazy_static",
"libc",
"windows-sys 0.52.0",
]
[[package]]
name = "cpufeatures"
version = "0.2.13"
@@ -95,6 +132,31 @@ dependencies = [
"libc",
]
[[package]]
name = "crossterm"
version = "0.22.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c85525306c4291d1b73ce93c8acf9c339f9b213aef6c1d85c3830cbf1c16325c"
dependencies = [
"bitflags 1.3.2",
"crossterm_winapi",
"libc",
"mio 0.7.14",
"parking_lot 0.11.2",
"signal-hook",
"signal-hook-mio",
"winapi",
]
[[package]]
name = "crossterm_winapi"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acdd7c62a3665c7f6830a51635d9ac9b23ed385797f70a83bb8bafe9c572ab2b"
dependencies = [
"winapi",
]
[[package]]
name = "digest"
version = "0.9.0"
@@ -104,6 +166,12 @@ dependencies = [
"generic-array",
]
[[package]]
name = "encode_unicode"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f"
[[package]]
name = "fnv"
version = "1.0.7"
@@ -223,18 +291,57 @@ dependencies = [
"unicode-normalization",
]
[[package]]
name = "indicatif"
version = "0.16.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2d207dc617c7a380ab07ff572a6e52fa202a2a8f355860ac9c38e23f8196be1b"
dependencies = [
"console",
"lazy_static",
"number_prefix",
"regex",
]
[[package]]
name = "instant"
version = "0.1.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e0242819d153cba4b4b05a5a8f2a7e9bbf97b6055b2a002b395c96b5ff3c0222"
dependencies = [
"cfg-if",
]
[[package]]
name = "itoa"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b"
[[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.158"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439"
[[package]]
name = "local-ip-address"
version = "0.4.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9eab7f092fb08006040e656c2adce6670e6a516bea831a8b2b3d0fb24d4488f5"
dependencies = [
"libc",
"neli",
"thiserror",
"windows-sys 0.42.0",
]
[[package]]
name = "lock_api"
version = "0.4.12"
@@ -266,6 +373,19 @@ dependencies = [
"adler",
]
[[package]]
name = "mio"
version = "0.7.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8067b404fe97c70829f082dec8bcf4f71225d7eaea1d8645349cb76fa06205cc"
dependencies = [
"libc",
"log",
"miow",
"ntapi",
"winapi",
]
[[package]]
name = "mio"
version = "1.0.2"
@@ -275,9 +395,43 @@ dependencies = [
"hermit-abi",
"libc",
"wasi",
"windows-sys",
"windows-sys 0.52.0",
]
[[package]]
name = "miow"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9f1c5b025cda876f66ef43a113f91ebc9f4ccef34843000e0adf6ebbab84e21"
dependencies = [
"winapi",
]
[[package]]
name = "neli"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9053554eb5dcb7e10d9cdab1206965bde870eed5d0d341532ca035e3ba221508"
dependencies = [
"byteorder",
"libc",
]
[[package]]
name = "ntapi"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c28774a7fd2fbb4f0babd8237ce554b73af68021b5f695a3cebd6c59bac0980f"
dependencies = [
"winapi",
]
[[package]]
name = "number_prefix"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3"
[[package]]
name = "object"
version = "0.36.3"
@@ -293,6 +447,17 @@ version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381"
[[package]]
name = "parking_lot"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d17b78036a60663b797adeaee46f5c9dfebb86948d1255007a1d6be0271ff99"
dependencies = [
"instant",
"lock_api",
"parking_lot_core 0.8.6",
]
[[package]]
name = "parking_lot"
version = "0.12.3"
@@ -300,7 +465,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27"
dependencies = [
"lock_api",
"parking_lot_core",
"parking_lot_core 0.9.10",
]
[[package]]
name = "parking_lot_core"
version = "0.8.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60a2cfe6f0ad2bfc16aefa463b497d5c7a5ecd44a23efa72aa342d90177356dc"
dependencies = [
"cfg-if",
"instant",
"libc",
"redox_syscall 0.2.16",
"smallvec",
"winapi",
]
[[package]]
@@ -311,9 +490,9 @@ checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8"
dependencies = [
"cfg-if",
"libc",
"redox_syscall",
"redox_syscall 0.5.3",
"smallvec",
"windows-targets",
"windows-targets 0.52.6",
]
[[package]]
@@ -411,27 +590,103 @@ dependencies = [
"getrandom",
]
[[package]]
name = "redox_syscall"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a"
dependencies = [
"bitflags 1.3.2",
]
[[package]]
name = "redox_syscall"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a908a6e00f1fdd0dfd9c0eb08ce85126f6d8bbda50017e74bc4a4b7d4a926a4"
dependencies = [
"bitflags",
"bitflags 2.6.0",
]
[[package]]
name = "regex"
version = "1.10.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4219d74c6b67a3654a9fbebc4b419e22126d13d2f3c4a07ee0cb61ff79a79619"
dependencies = [
"aho-corasick",
"memchr",
"regex-automata",
"regex-syntax",
]
[[package]]
name = "regex-automata"
version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b"
[[package]]
name = "rustc-demangle"
version = "0.1.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f"
[[package]]
name = "ryu"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f"
[[package]]
name = "scopeguard"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]]
name = "serde"
version = "1.0.208"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cff085d2cb684faa248efb494c39b68e522822ac0de72ccf08109abde717cfb2"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.208"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24008e81ff7613ed8e5ba0cfaf24e2c2f1e5b8a0495711e44fcd4882fca62bcf"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.125"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "83c8e735a073ccf5be70aa8066aa984eaf2fa000db6c8d0100ae605b366d31ed"
dependencies = [
"itoa",
"memchr",
"ryu",
"serde",
]
[[package]]
name = "sha-1"
version = "0.9.8"
@@ -451,6 +706,27 @@ version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "signal-hook"
version = "0.3.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8621587d4798caf8eb44879d42e56b9a93ea5dcd315a6487c357130095b62801"
dependencies = [
"libc",
"signal-hook-registry",
]
[[package]]
name = "signal-hook-mio"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34db1a06d485c9142248b7a054f034b349b212551f3dfd19c94d45a754a217cd"
dependencies = [
"libc",
"mio 0.7.14",
"signal-hook",
]
[[package]]
name = "signal-hook-registry"
version = "1.4.2"
@@ -482,7 +758,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce305eb0b4296696835b71df73eb912e0f1ffd2556a501fcede6e0c50349191c"
dependencies = [
"libc",
"windows-sys",
"windows-sys 0.52.0",
]
[[package]]
@@ -540,13 +816,13 @@ dependencies = [
"backtrace",
"bytes",
"libc",
"mio",
"parking_lot",
"mio 1.0.2",
"parking_lot 0.12.3",
"pin-project-lite",
"signal-hook-registry",
"socket2",
"tokio-macros",
"windows-sys",
"windows-sys 0.52.0",
]
[[package]]
@@ -648,13 +924,74 @@ version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows-sys"
version = "0.42.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7"
dependencies = [
"windows_aarch64_gnullvm 0.42.2",
"windows_aarch64_msvc 0.42.2",
"windows_i686_gnu 0.42.2",
"windows_i686_msvc 0.42.2",
"windows_x86_64_gnu 0.42.2",
"windows_x86_64_gnullvm 0.42.2",
"windows_x86_64_msvc 0.42.2",
]
[[package]]
name = "windows-sys"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
dependencies = [
"windows-targets 0.48.5",
]
[[package]]
name = "windows-sys"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
dependencies = [
"windows-targets",
"windows-targets 0.52.6",
]
[[package]]
name = "windows-targets"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
dependencies = [
"windows_aarch64_gnullvm 0.48.5",
"windows_aarch64_msvc 0.48.5",
"windows_i686_gnu 0.48.5",
"windows_i686_msvc 0.48.5",
"windows_x86_64_gnu 0.48.5",
"windows_x86_64_gnullvm 0.48.5",
"windows_x86_64_msvc 0.48.5",
]
[[package]]
@@ -663,28 +1000,64 @@ version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_aarch64_gnullvm 0.52.6",
"windows_aarch64_msvc 0.52.6",
"windows_i686_gnu 0.52.6",
"windows_i686_gnullvm",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
"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]]
name = "windows_aarch64_gnullvm"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8"
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
[[package]]
name = "windows_aarch64_msvc"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43"
[[package]]
name = "windows_aarch64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
[[package]]
name = "windows_i686_gnu"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f"
[[package]]
name = "windows_i686_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
[[package]]
name = "windows_i686_gnu"
version = "0.52.6"
@@ -697,24 +1070,72 @@ version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
[[package]]
name = "windows_i686_msvc"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060"
[[package]]
name = "windows_i686_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
[[package]]
name = "windows_i686_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
[[package]]
name = "windows_x86_64_gnu"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36"
[[package]]
name = "windows_x86_64_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
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]]
name = "windows_x86_64_gnullvm"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
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]]
name = "windows_x86_64_msvc"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0"
[[package]]
name = "windows_x86_64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.6"
@@ -725,7 +1146,13 @@ checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
name = "your_project_name"
version = "0.1.0"
dependencies = [
"colored",
"crossterm",
"futures-util",
"indicatif",
"local-ip-address",
"serde",
"serde_json",
"tokio",
"tokio-tungstenite",
"tungstenite",

View File

@@ -1,5 +1,5 @@
[package]
name = "your_project_name"
name = "BeetleWire"
version = "0.1.0"
edition = "2021"
@@ -8,4 +8,10 @@ tokio = { version = "1", features = ["full"] }
tokio-tungstenite = "0.15"
tungstenite = "0.14"
url = "2.2"
futures-util = "0.3"
futures-util = "0.3"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
colored = "2.0"
indicatif = "0.16"
crossterm = "0.22"
local-ip-address = "0.4"

1
Shared/readme.md Normal file
View File

@@ -0,0 +1 @@
Drop files here to share. Downloaded files also appear here.

View File

@@ -1,18 +1,160 @@
use futures_util::{StreamExt, TryStreamExt}; // Add TryStreamExt
use colored::Colorize;
use crossterm::{execute, terminal::{Clear, ClearType}};
use futures_util::{SinkExt, StreamExt};
use tokio_tungstenite::connect_async;
use tokio_tungstenite::tungstenite::protocol::Message;
use url::Url;
use serde_json::Value;
use std::io::{self, Write};
use indicatif::{ProgressBar, ProgressStyle};
use tokio::time::Instant;
use local_ip_address::local_ip;
pub async fn run_client(addr: &str) {
let url = Url::parse(&format!("ws://{}", addr)).expect("Invalid URL");
let (ws_stream, _) = connect_async(url).await.expect("Failed to connect");
let (_, mut read) = ws_stream.split(); // Use try_split instead of split
fn clear_terminal() {
execute!(io::stdout(), Clear(ClearType::All)).expect("Failed to clear terminal");
}
while let Some(msg) = read.next().await {
let msg = msg.expect("Failed to read message");
if let Message::Binary(data) = msg {
tokio::fs::write("received_file.png", data).await.expect("Failed to write file");
println!("File received and saved as received_file.png");
async fn get_server_address(default_addr: String) -> String {
loop {
print!("Enter the server address (e.g., 127.0.0.1:1337) or press Enter to use default ({}): ", default_addr);
io::stdout().flush().unwrap();
let mut server_addr = String::new();
io::stdin().read_line(&mut server_addr).expect("Failed to read input");
let server_addr_trimmed = server_addr.trim();
if server_addr_trimmed.is_empty() {
return default_addr;
} else {
return server_addr_trimmed.to_string();
}
}
}
async fn connect_to_server(addr: String) -> (impl SinkExt<Message> + Unpin, impl StreamExt<Item = Result<Message, tokio_tungstenite::tungstenite::Error>> + Unpin) {
let url = Url::parse(&format!("ws://{}", addr)).expect("Invalid URL");
let (ws_stream, _) = connect_async(url).await.expect("Failed to connect");
ws_stream.split()
}
async fn receive_file_list(read: &mut (impl StreamExt<Item = Result<Message, tokio_tungstenite::tungstenite::Error>> + Unpin)) -> Vec<String> {
if let Some(msg) = read.next().await {
let msg = msg.expect("Failed to read message");
if let Message::Text(text) = msg {
let file_list: Value = serde_json::from_str(&text).expect("Failed to parse JSON");
if let Some(files) = file_list.as_array() {
println!("Received file list:");
let file_names: Vec<String> = files
.iter()
.filter_map(|file| file.as_str().map(String::from))
.collect();
for (index, file_name) in file_names.iter().enumerate() {
println!("{}: {}", index.to_string().green().bold(), file_name.magenta().bold());
}
return file_names;
}
}
}
vec![]
}
fn prompt_file_selection(files: &[String]) -> Option<String> {
loop {
print!("Enter the number of the file you want to download: ");
io::stdout().flush().unwrap();
let mut input = String::new();
io::stdin().read_line(&mut input).expect("Failed to read input");
match input.trim().parse::<usize>() {
Ok(file_index) if file_index < files.len() => {
clear_terminal();
return Some(files[file_index].clone());
}
_ => println!("Invalid input, please enter a valid file number."),
}
}
}
async fn download_file(file_name: String, write: &mut (impl SinkExt<Message> + Unpin), read: &mut (impl StreamExt<Item = Result<Message, tokio_tungstenite::tungstenite::Error>> + Unpin)) {
let _ = write.send(Message::Text(file_name.clone())).await;
if let Some(file_msg) = read.next().await {
let file_msg = file_msg.expect("Failed to read file message");
if let Message::Binary(data) = file_msg {
let total_size = data.len() as u64;
let progress_bar: ProgressBar = ProgressBar::new(total_size);
progress_bar.set_style(ProgressStyle::default_bar()
.template("{msg} [{bar:40.cyan/blue}] {bytes}/{total_bytes} ({eta})")
.progress_chars("#>-"));
progress_bar.set_message("Downloading");
let start = Instant::now();
let file_path = format!("Shared/{}", file_name);
tokio::fs::write(&file_path, &data).await.expect("Failed to write file");
let elapsed = start.elapsed().as_secs_f64();
let speed = total_size as f64 / elapsed / 1024.0; // KB/s
let message = format!("Downloaded at {:.2} KB/s", speed);
progress_bar.finish_with_message(message.clone());
println!("File received and saved as {}", file_path);
}
}
}
fn handle_user_choice() -> bool {
loop {
println!("{}", "1: Download another file".magenta().bold());
println!("{}", "2: Connect to another server".magenta().bold());
print!("Enter your choice: ");
io::stdout().flush().unwrap();
let mut choice = String::new();
io::stdin().read_line(&mut choice).expect("Failed to read input");
match choice.trim() {
"1" => return false, // Continue to download another file
"2" => return true, // Exit to connect to another server
_ => println!("Invalid choice, please try again."),
}
}
}
async fn close_connection(write: &mut (impl SinkExt<Message> + Unpin)) {
let _ = write.send(Message::Close(None)).await;
}
pub async fn run_client() {
let local_ip = local_ip().expect("Failed to get local IP address");
let default_addr = format!("{}:{}", local_ip, "1337");
let mut server_address = get_server_address(default_addr.clone()).await;
let (mut write_stream, mut read_stream) = connect_to_server(server_address.clone()).await;
loop {
println!("Connected to the server at {}", server_address);
let files = receive_file_list(&mut read_stream).await;
if files.is_empty() {
println!("No files found. Press Enter to continue.");
let mut input = String::new();
io::stdin().read_line(&mut input).expect("Failed to read input");
} else {
if let Some(file_name) = prompt_file_selection(&files) {
download_file(file_name, &mut write_stream, &mut read_stream).await;
}
}
if handle_user_choice() {
close_connection(&mut write_stream).await; // Close the existing connection
server_address = get_server_address(default_addr.clone()).await;
let (new_write, new_read) = connect_to_server(server_address.clone()).await;
write_stream = new_write;
read_stream = new_read;
println!("Connected to the server at {}", server_address);
} else {
let (new_write, new_read) = connect_to_server(server_address.clone()).await;
write_stream = new_write;
read_stream = new_read;
}
clear_terminal();
}
}

View File

@@ -1,15 +1,30 @@
use std::{fs, path::Path};
use colored::Colorize;
use local_ip_address::local_ip;
use tokio::task;
mod server;
mod client;
use tokio::task;
#[tokio::main]
async fn main() {
let server_addr = "127.0.0.1:8080";
let file_path = "c:/shared/file.jpg";
// Create the Shared folder if it doesn't exist
let shared_folder = "Shared";
if !Path::new(shared_folder).exists() {
fs::create_dir(shared_folder).expect("Failed to create Shared folder");
}
let server = task::spawn(server::run_server(server_addr, file_path));
let client = task::spawn(client::run_client(server_addr));
// Get the local IPv4 address
let local_ip = local_ip().expect("Failed to get local IP address");
let server = format!("{}:{}", local_ip, "1337");
// Use server address here
println!("{}", format!("Hosting files at address: {}", server).green());
let file_path: &str = shared_folder;
let server: task::JoinHandle<()> = task::spawn(server::run_server(server.to_string().clone(), file_path));
let client: task::JoinHandle<()> = task::spawn(client::run_client());
let _ = tokio::join!(server, client);
}

View File

@@ -1,27 +1,53 @@
use futures_util::{SinkExt, StreamExt};
use tokio::net::TcpListener;
use tokio_tungstenite::accept_async;
use tokio_tungstenite::tungstenite::protocol::Message;
use std::path::Path;
use tokio::fs::File;
use tokio::io::AsyncReadExt;
pub async fn run_server(addr: &str, file_path: &str) {
let listener = TcpListener::bind(addr).await.expect("Failed to bind");
use futures_util::{SinkExt, StreamExt};
use serde_json::json;
use tokio::{fs::{self, File}, io::AsyncReadExt, net::TcpListener};
use tokio_tungstenite::accept_async;
use tungstenite::Message;
pub async fn run_server(addr: String, dir_path: &str) {
let listener = TcpListener::bind(addr.clone()).await.expect("Failed to bind");
println!("Server listening on {}", addr);
while let Ok((stream, _)) = listener.accept().await {
let file_path = file_path.to_string();
let dir_path = dir_path.to_string();
tokio::spawn(async move {
let ws_stream = accept_async(stream).await.expect("Error during the websocket handshake");
let (mut write, _) = ws_stream.split();
let path = Path::new(&file_path);
let mut file = File::open(path).await.expect("Failed to open file");
let mut buffer = Vec::new();
file.read_to_end(&mut buffer).await.expect("Failed to read file");
write.send(Message::Binary(buffer)).await.expect("Failed to send file");
let (mut write, mut read) = ws_stream.split();
// Debugging output to verify the directory path
println!("Reading directory: {}", dir_path);
// List files in the directory
let mut file_list = Vec::new();
match fs::read_dir(&dir_path).await {
Ok(mut entries) => {
while let Some(entry) = entries.next_entry().await.expect("Failed to read entry") {
if entry.path().is_file() {
if let Some(file_name) = entry.file_name().to_str() {
file_list.push(file_name.to_string());
}
}
}
}
Err(e) => {
eprintln!("Failed to read directory: {:?}", e);
return;
}
}
// Send the list of files to the client
let file_list_json = json!(file_list).to_string();
write.send(Message::Text(file_list_json)).await.expect("Failed to send file list");
// Wait for the client to request a file
if let Some(msg) = read.next().await {
let msg = msg.expect("Failed to read message");
if let Message::Text(file_name) = msg {
let file_path = Path::new(&dir_path).join(file_name);
let mut file = File::open(file_path).await.expect("Failed to open file");
let mut buffer = Vec::new();
file.read_to_end(&mut buffer).await.expect("Failed to read file");
// Send the requested file to the client
write.send(Message::Binary(buffer)).await.expect("Failed to send file");
}
}
});
}
}