Skip to content
This repository was archived by the owner on Dec 24, 2024. It is now read-only.

Commit bec16c2

Browse files
clean up (#72)
1 parent fee8d1c commit bec16c2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+1406
-1439
lines changed

Cargo.lock

+184-64
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+14-19
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ homepage = "https://github.com/andrewgazelka/SwarmBot"
1414
[dependencies]
1515

1616
# parsing arguments
17-
clap = { version = "3.0.0-rc.5", features = ["derive"] }
17+
clap = { version = "4.0.32", features = ["derive"] }
1818

1919
# reading from csv
2020
csv = "1.1"
@@ -34,15 +34,11 @@ tokio = { version = "1.24", features = [
3434
"parking_lot",
3535
] }
3636

37-
# async trait
38-
async-trait = "0.1"
39-
4037
# dns
41-
trust-dns-resolver = "0.21.0-alpha.4"
38+
trust-dns-resolver = "0.22"
4239

4340
# encryption
44-
aes = "0.7"
45-
cfb8 = "0.7"
41+
aes = "0.7.5"
4642

4743
# zlib
4844
#flate2 = {version = "1.0"}
@@ -61,7 +57,7 @@ reqwest = { version = "0.11", features = ["json", "socks"] }
6157

6258
# for minecraft auth RSA response
6359
rsa-der = "0.3"
64-
rsa = "0.6"
60+
rsa = "0.7"
6561

6662
# used for RSA and random generation
6763
rand = "0.8"
@@ -70,7 +66,7 @@ rand = "0.8"
7066
num-bigint = "0.4"
7167

7268
# mojang hash
73-
sha1 = "0.6"
69+
sha1 = "0.10"
7470

7571
# json parsing (particularly for mojang api)
7672
serde_json = "1.0"
@@ -79,7 +75,6 @@ serde_json = "1.0"
7975
itertools = "0.10"
8076

8177
# for data storage
82-
bincode = "2.0.0-alpha.2"
8378

8479
# chat parsing
8580
regex = "1.5"
@@ -108,17 +103,17 @@ swarm-bot-packets = { path = "packets", version = "0.2.0" }
108103
# so we don't have to use v-tables
109104
enum_dispatch = "0.3"
110105

111-
# for errors
112-
thiserror = "1.0"
113-
114106
# websockets
115-
tokio-tungstenite = "0.17"
107+
tokio-tungstenite = "0.18"
116108

117109
futures = "0.3"
118-
#futures-util = "0.3"
119110

120111
interfaces = { path = "interfaces" }
121-
112+
anyhow = "1.0.68"
113+
bincode = "2.0.0-alpha.2"
114+
hex-literal = "0.3.4"
115+
cfb8 = "0.7"
116+
tokio-stream = "0.1.11"
122117

123118
[dev-dependencies]
124119
assert_matches = "1.5"
@@ -134,6 +129,6 @@ opt-level = 1
134129

135130

136131
[profile.release]
137-
debug = true
138-
#lto = "fat"
139-
#codegen-units = 1
132+
# debug = true
133+
lto = "fat"
134+
codegen-units = 1

interfaces/src/lib.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#![feature(once_cell)]
22

3-
use crate::types::{BlockLocation, Selection2D};
43
use futures::{SinkExt, StreamExt};
54
use serde::{Deserialize, Serialize};
65
use tokio::net::{TcpListener, TcpStream, ToSocketAddrs};
76
use tungstenite::Message;
87

8+
use crate::types::{BlockLocation, Selection2D};
9+
910
pub mod types;
1011

1112
type Id = u64;
@@ -98,6 +99,7 @@ impl Comm {
9899
let (send_tx, mut send_rx) = tokio::sync::mpsc::unbounded_channel();
99100
let mut ws = tokio_tungstenite::accept_async(stream).await?;
100101
tokio::spawn(async move {
102+
let mut msg_to_send = None;
101103
tokio::select! {
102104
val = ws.next() => {
103105
if let Some(Ok(msg)) = val {
@@ -109,11 +111,15 @@ impl Comm {
109111
val = send_rx.recv() => {
110112
if let Some(cmd) = val {
111113
if let Ok(msg) = outgoing(cmd) {
112-
let _ = ws.send(msg);
114+
msg_to_send = Some(msg);
113115
}
114116
}
115117
}
116118
}
119+
120+
if let Some(msg) = msg_to_send {
121+
let _ = ws.send(msg).await;
122+
}
117123
});
118124

119125
Ok(Self {
@@ -133,6 +139,7 @@ impl Comm {
133139
let (stream, _) = server.accept().await?;
134140
let mut ws = tokio_tungstenite::accept_async(stream).await?;
135141

142+
let mut msg_to_send = None;
136143
tokio::select! {
137144
val = ws.next() => {
138145
if let Some(Ok(msg)) = val {
@@ -144,12 +151,16 @@ impl Comm {
144151
val = send_rx.recv() => {
145152
if let Some(cmd) = val {
146153
if let Ok(msg) = outgoing(cmd) {
147-
let _ = ws.send(msg);
154+
msg_to_send = Some(msg)
148155
}
149156
}
150157
}
151158
}
152159

160+
if let Some(msg) = msg_to_send {
161+
let _ = ws.send(msg).await;
162+
}
163+
153164
Ok(())
154165
}
155166
.await;
@@ -176,7 +187,6 @@ mod tests {
176187
}),
177188
};
178189

179-
let res = serde_json::to_string(&command).unwrap();
180-
println!("res {:?}", res);
190+
serde_json::to_string(&command).unwrap();
181191
}
182192
}

interfaces/src/types.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
use colored::Colorize;
21
use std::{
32
f32::consts::PI,
43
fmt::{Debug, Display, Formatter},
54
ops::{Add, AddAssign, Index, Mul, MulAssign, Neg, Sub},
65
};
76

7+
use colored::Colorize;
88
use itertools::Itertools;
99
use lazy_static::lazy_static;
1010
use regex::{Captures, Regex};
1111
use serde::{Deserialize, Serialize};
12-
1312
use swarm_bot_packets::{
1413
read::{ByteReadable, ByteReader},
1514
write::{ByteWritable, ByteWriter},

packets/packets_macro/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use proc_macro::TokenStream;
2-
32
use quote::quote;
43
use syn::{
54
parse::{Parse, ParseStream},

rustfmt.toml

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# https://rust-lang.github.io/rustfmt/?version=v1.4.36
2-
unstable_features = true
3-
reorder_imports = true
2+
format_code_in_doc_comments = true
3+
format_macro_bodies = true
4+
format_macro_matchers = true
5+
group_imports = "StdExternalCrate"
6+
imports_granularity = "Crate"
47
normalize_comments = true
58
normalize_doc_attributes = true
6-
imports_granularity = "Crate"
7-
format_macro_matchers = true
8-
format_macro_bodies = true
9+
reorder_imports = true
10+
unstable_features = true
911
wrap_comments = true
10-
format_code_in_doc_comments = true

src/bootstrap/csv.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1+
//! module for modifying CSVs
2+
13
use std::fs::File;
24

35
use serde::de::DeserializeOwned;
46

5-
use crate::{
6-
bootstrap::{CSVUser, Proxy},
7-
error::Res,
8-
};
7+
use crate::bootstrap::{CSVUser, Proxy};
98

10-
fn read_csv<T: DeserializeOwned>(file: File) -> Res<Vec<T>> {
9+
/// read CSV file into `Vec` of the desired type
10+
fn read_csv<T: DeserializeOwned>(file: File) -> anyhow::Result<Vec<T>> {
1111
csv::ReaderBuilder::new()
1212
.delimiter(b':')
1313
.has_headers(false)
@@ -20,10 +20,12 @@ fn read_csv<T: DeserializeOwned>(file: File) -> Res<Vec<T>> {
2020
.collect()
2121
}
2222

23-
pub fn read_users(file: File) -> Res<Vec<CSVUser>> {
23+
/// reade users from a CSV file
24+
pub fn read_users(file: File) -> anyhow::Result<Vec<CSVUser>> {
2425
read_csv(file)
2526
}
2627

27-
pub fn read_proxies(file: File) -> Res<Vec<Proxy>> {
28+
/// read proxies from a CSV file
29+
pub fn read_proxies(file: File) -> anyhow::Result<Vec<Proxy>> {
2830
read_csv(file)
2931
}

src/bootstrap/dns.rs

+17-13
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
1+
//! Module for interacting with DNS
2+
use anyhow::Context;
13
use trust_dns_resolver::{
24
config::{ResolverConfig, ResolverOpts},
3-
error::ResolveError,
45
AsyncResolver,
56
};
67

78
use crate::bootstrap::Address;
89

910
/// performs a DNS lookup on host
10-
async fn dns_lookup(host: &str) -> Result<Address, ResolveError> {
11-
let resolver =
12-
AsyncResolver::tokio(ResolverConfig::default(), ResolverOpts::default()).unwrap();
11+
async fn dns_lookup(host: &str) -> anyhow::Result<Address> {
12+
let resolver = AsyncResolver::tokio(ResolverConfig::default(), ResolverOpts::default())
13+
.context("could not create async lookup")?;
1314

14-
println!("performing srv lookup");
15-
resolver
15+
let res = resolver
1616
.srv_lookup(format!("_minecraft._tcp.{host}"))
1717
.await
18-
.map(|res| {
19-
let srv = res.iter().next().unwrap();
20-
Address {
21-
host: srv.target().to_utf8(),
22-
port: srv.port(),
23-
}
24-
})
18+
.context("could not perform SRV lookup")?;
19+
20+
let srv = res
21+
.iter()
22+
.next()
23+
.context("there are no elements in SRV lookup")?;
24+
25+
Ok(Address {
26+
host: srv.target().to_utf8(),
27+
port: srv.port(),
28+
})
2529
}
2630

2731
/// Normalizes the address. Some servers like 2b2t have a separate address for

src/bootstrap/mod.rs

+44-36
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! used for boostrapping the code
2+
use anyhow::Context;
13
use serde::Deserialize;
24
use tokio::{
35
net::{
@@ -22,7 +24,9 @@ pub mod storage;
2224
/// A server address
2325
#[derive(Clone, Debug)]
2426
pub struct Address {
27+
/// the hostname
2528
pub host: String,
29+
/// the port
2630
pub port: u16,
2731
}
2832

@@ -51,51 +55,55 @@ pub struct BotConnection {
5155
pub write: OwnedWriteHalf,
5256
}
5357

58+
async fn obtain_connection(user: BotDataLoader, address: Address) -> anyhow::Result<BotConnection> {
59+
let BotDataLoader {
60+
proxy,
61+
user,
62+
mojang,
63+
} = user;
64+
65+
let target = String::from(&address);
66+
67+
let conn = if let Some(proxy) = proxy {
68+
let address = proxy.address();
69+
let conn = Socks5Stream::connect_with_password(
70+
proxy.address().as_str(),
71+
target.as_str(),
72+
&proxy.user,
73+
&proxy.pass,
74+
)
75+
.await
76+
.with_context(|| format!("could not create to socks {address}"))?;
77+
78+
conn.into_inner()
79+
} else {
80+
TcpStream::connect(target.as_str()).await.unwrap()
81+
};
82+
83+
let (read, write) = conn.into_split();
84+
Ok(BotConnection {
85+
user,
86+
server_address: address,
87+
mojang,
88+
read,
89+
write,
90+
})
91+
}
92+
5493
impl BotConnection {
55-
/// Generates connections given BotData and an address
94+
/// Generates connections given [`BotData`] and an address
5695
pub fn stream(
5796
server_address: Address,
58-
mut users: tokio::sync::mpsc::Receiver<BotDataLoader>,
59-
) -> Receiver<BotConnection> {
97+
mut users: Receiver<BotDataLoader>,
98+
) -> Receiver<anyhow::Result<Self>> {
6099
let (tx, rx) = tokio::sync::mpsc::channel(1);
61100
tokio::task::spawn_local(async move {
62101
while let Some(user) = users.recv().await {
63102
let tx = tx.clone();
64103
let address = server_address.clone();
65104
tokio::task::spawn_local(async move {
66-
let BotDataLoader {
67-
proxy,
68-
user,
69-
mojang,
70-
} = user;
71-
72-
let target = String::from(&address);
73-
74-
let conn = match proxy {
75-
Some(proxy) => {
76-
let conn = Socks5Stream::connect_with_password(
77-
proxy.address().as_str(),
78-
target.as_str(),
79-
&proxy.user,
80-
&proxy.pass,
81-
)
82-
.await
83-
.unwrap();
84-
conn.into_inner()
85-
}
86-
None => TcpStream::connect(target.as_str()).await.unwrap(),
87-
};
88-
89-
let (read, write) = conn.into_split();
90-
tx.send(BotConnection {
91-
user,
92-
server_address: address,
93-
mojang,
94-
read,
95-
write,
96-
})
97-
.await
98-
.unwrap();
105+
let connection = obtain_connection(user, address).await;
106+
tx.send(connection).await.unwrap();
99107
});
100108
}
101109
});

0 commit comments

Comments
 (0)