1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
|
use failure::Error;
extern crate libquassel;
extern crate tokio;
extern crate pretty_env_logger;
use libquassel::primitive::*;
use libquassel::message::*;
use libquassel::client::*;
use tokio::io::{AsyncRead, AsyncWrite};
use core::marker::Unpin;
use futures::SinkExt;
use std::future::Future;
use log::*;
// struct Client {
// stream: Frame<TcpStream, QuasselProtocol>,
// }
#[tokio::main]
async fn main() -> Result<(), Error> {
pretty_env_logger::init();
let username = std::env::args().nth(1).expect("no username given");
let password = std::env::args().nth(2).expect("no password given");
let mut client = Client::<tokio_tls::TlsStream<tokio::net::TcpStream>>::connect_tls(
"cocaine.farm",
4242,
true,
User {
name: username,
password,
},
).await.unwrap();
client.run().await;
Ok(())
}
// let funcs = Funcs {
// init: InitFuncs {
// client_init_ack,
// client_init_reject,
// client_login_ack,
// client_login_reject,
// session_init
// },
// message: MessageFuncs {
// sync_message,
// rpc_call,
// init_request,
// init_data,
// heart_beat,
// heart_beat_reply
// }
// };
// async fn client_init_ack<T: AsyncRead + AsyncWrite + Unpin, F: Future>(client: &mut Client<T, F>, item: VariantMap) {
// use libquassel::{HandshakeSerialize, HandshakeDeserialize};
//
// info!(target: "init", "Initialization successfull");
// info!(target: "login", "Starting Login");
// let login = ClientLogin {user: client.user.name.clone(), password: client.user.password.clone()};
// client.stream.send(login.serialize().unwrap()).await.unwrap();
// }
//
// async fn client_init_reject<T: AsyncRead + AsyncWrite + Unpin, F: Future>(client: Client<T, F>, item: VariantMap) {
// error!(target: "init", "Initialization failed: {:?}", item);
// }
//
// async fn client_login_ack<T: AsyncRead + AsyncWrite + Unpin, F: Future>(client: Client<T, F>, item: VariantMap) {
// info!(target: "login", "Login successfull");
// }
//
// async fn client_login_reject<T: AsyncRead + AsyncWrite + Unpin, F: Future>(client: Client<T, F>, item: VariantMap) {
// error!(target: "login", "Login failed: {:?}", item);
// }
//
// async fn session_init<T: AsyncRead + AsyncWrite + Unpin, F: Future>(client: &mut Client<T, F>, item: VariantMap) {
// info!(target: "login", "Session Initialization finished. Switching to Connected state");
// client.state = ClientState::Connected;
// }
//
// async fn sync_message<T: AsyncRead + AsyncWrite + Unpin, F: Future>(client: Client<T, F>, item: SyncMessage) { unimplemented!() }
// async fn rpc_call<T: AsyncRead + AsyncWrite + Unpin, F: Future>(client: Client<T, F>, item: RpcCall) { unimplemented!() }
// async fn init_request<T: AsyncRead + AsyncWrite + Unpin, F: Future>(client: Client<T, F>, item: InitRequest) { unimplemented!() }
// async fn init_data<T: AsyncRead + AsyncWrite + Unpin, F: Future>(client: Client<T, F>, item: InitData) { unimplemented!() }
// async fn heart_beat<T: AsyncRead + AsyncWrite + Unpin, F: Future>(client: Client<T, F>, item: HeartBeat) { unimplemented!() }
// async fn heart_beat_reply<T: AsyncRead + AsyncWrite + Unpin, F: Future>(client: Client<T, F>, item: HeartBeatReply) { unimplemented!() }
// //use std::io::BufWriter;
// use std::result::Result;
// use std::vec::Vec;
//
// use tokio::io::{AsyncRead, AsyncWrite};
// use core::marker::Unpin;
// use tokio::net::TcpStream;
// use tokio::prelude::*;
//
// use native_tls::TlsConnector;
//
// use tokio_tls;
// use tokio_tls::TlsStream;
// use tokio_util::codec::Framed;
// use futures_util::stream::StreamExt;
// use futures::SinkExt;
// use std::future::Future;
//
// use crate::frame::QuasselCodec;
//
// use failure::Error;
//
// use log::{trace, debug, info, error};
//
// use crate::message::*;
// use crate::primitive::*;
//
// extern crate log;
//
// pub struct Client<T>
// where
// T: 'static + AsyncRead + AsyncWrite + Unpin,
// {
// pub stream: Framed<T, QuasselCodec>,
// pub tls: bool,
// pub compression: bool,
// pub state: ClientState,
// pub user: User,
// }
//
// pub struct User {
// pub name: String,
// pub password: String,
// }
//
// pub enum ClientState {
// Handshake,
// Connected,
// }
//
// impl <T: AsyncRead + AsyncWrite + Unpin> Client<T> {
// pub async fn run(&mut self) {
// use crate::primitive::StringList;
// use crate::message::ClientInit;
// use crate::HandshakeSerialize;
//
// info!(target: "init", "Setting Features");
//
// let mut features = StringList::new();
// features.push("SynchronizedMarkerLine".to_string());
// features.push("Authenticators".to_string());
// features.push("ExtendedFeatures".to_string());
// features.push("BufferActivitySync".to_string());
// let client_init = ClientInit {
// client_version:String::from("Rust 0.0.0"),
// client_date: String::from("1579009211"),
// feature_list: features,
// client_features: 0x00008000,
// };
//
// self.stream.send(client_init.serialize().unwrap()).await.unwrap();
//
// // Start event loop
// while let Some(msg) = self.stream.next().await {
// let msg = msg.unwrap();
// match self.state {
// ClientState::Handshake => handle_login_message(self, &msg).await.unwrap(),
// ClientState::Connected => handle_message(self, &msg).await.unwrap(),
// }
// };
// }
//
// pub async fn connect(
// address: &'static str,
// port: u64,
// compression: bool,
// user: User,
// funcs: Funcs<TcpStream, impl Future>
// ) -> Result<Client<TcpStream, impl Future>, Error> {
// let mut stream = TcpStream::connect(format!("{}:{}", address, port)).await?;
//
// info!(target: "init", "Establishing Connection");
// let connack = init(&mut stream, false, compression).await?;
//
// debug!(target: "init", "{:?}", connack);
//
// let codec = QuasselCodec::builder()
// .compression(compression)
// .new_codec();
//
// let framed_stream = Framed::new(stream, codec);
//
// info!(target: "init", "Established Connection");
//
// return Ok(Client {
// stream: framed_stream,
// tls: false,
// compression,
// state: ClientState::Handshake,
// user,
// funcs,
// });
// }
//
// pub async fn connect_tls(
// address: &'static str,
// port: u64,
// compression: bool,
// user: User,
// funcs: Funcs<TlsStream<TcpStream>, impl Future>
// ) -> Result<Client<TlsStream<TcpStream>, impl Future>, Error> {
// let mut stream: TcpStream = TcpStream::connect(format!("{}:{}", address, port)).await?;
//
// info!(target: "init", "Establishing Connection");
// let connack = init(&mut stream, true, compression).await?;
//
// debug!(target: "init", "{:?}", connack);
//
// let codec = QuasselCodec::builder()
// .compression(compression)
// .new_codec();
//
// let tls_connector = tokio_tls::TlsConnector::from(TlsConnector::builder().build().unwrap());
//
// let tls_stream = tls_connector.connect(address, stream).await?;
//
// let framed_stream = Framed::new(tls_stream, codec);
//
// info!(target: "init", "Established Connection");
//
// return Ok(Client {
// stream: framed_stream,
// tls: true,
// compression,
// state: ClientState::Handshake,
// user,
// funcs,
// });
// }
//
// }
//
// pub async fn handle_login_message<T: AsyncRead + AsyncWrite + Unpin, F: Future>(client: &mut Client<T, F>, buf: &[u8]) -> Result<(), Error> {
// use crate::{HandshakeSerialize, HandshakeDeserialize};
// use crate::message::ClientLogin;
// use crate::primitive::{VariantMap, Variant};
//
// trace!(target: "message", "Received bytes: {:x?}", buf);
// let (_, res) = VariantMap::parse(buf)?;
// debug!(target: "init", "Received Messsage: {:#?}", res);
//
// let msgtype = match_variant!(&res["MsgType"], Variant::String);
// match msgtype.as_str() {
// "ClientInitAck" => {
// info!(target: "init", "Initialization successfull");
// info!(target: "login", "Starting Login");
// let login = ClientLogin {user: client.user.name.clone(), password: client.user.password.clone()};
// client.stream.send(login.serialize()?).await?;
// },
// "ClientInitReject" => {
// error!(target: "init", "Initialization failed: {:?}", res);
// },
// "ClientLoginAck" => {
// info!(target: "login", "Login successfull");
// },
// "SessionInit" => {
// info!(target: "login", "Session Initialization finished. Switching to Connected state");
// client.state = ClientState::Connected;
// }
// "ClientLoginReject" => {
// error!(target: "login", "Login failed: {:?}", res);
// },
// _ => {
// error!(target: "client", "Error: WrongMsgType: {:#?}", res);
// }
// }
//
// return Ok(());
// }
//
// pub async fn handle_message<T: AsyncRead + AsyncWrite + Unpin, F: Future>(client: &mut Client<T, F>, buf: &[u8]) -> Result<(), Error> {
// use crate::message::Message;
// use crate::primitive::VariantList;
// use crate::Deserialize;
// use crate::Serialize;
//
// trace!(target: "message", "Received bytes: {:x?}", buf);
// let (_, res) = Message::parse(buf)?;
// debug!(target: "init", "Received Messsage: {:#?}", res);
//
// match res {
// Message::SyncMessage(_) => {}
// Message::RpcCall(_) => {}
// Message::InitRequest(_) => {}
// Message::InitData(_) => {}
// Message::HeartBeat(_) => {}
// Message::HeartBeatReply(_) => {}
// }
//
// return Ok(());
// }
//
// pub struct Funcs<T, F>
// where
// T: 'static + AsyncRead + AsyncWrite + Unpin,
// F: std::future::Future,
// {
// pub init: InitFuncs<T, F>,
// pub message: MessageFuncs<T, F>,
// }
//
// pub struct InitFuncs<T, F>
// where
// T: 'static + AsyncRead + AsyncWrite + Unpin,
// F: std::future::Future,
// {
// pub client_init_ack: fn(&mut Client<T, F>, VariantMap) -> F,
// pub client_init_reject: fn(Client<T, F>, VariantMap) -> F,
// pub client_login_ack: fn(Client<T, F>, VariantMap) -> F,
// pub client_login_reject: fn(Client<T, F>, VariantMap) -> F,
// pub session_init: fn(&mut Client<T, F>, VariantMap) -> F,
// }
//
// pub struct MessageFuncs<T, F>
// where
// T: 'static + AsyncRead + AsyncWrite + Unpin,
// F: std::future::Future,
// {
// pub sync_message: fn(Client<T, F>, SyncMessage) -> F,
// pub rpc_call: fn(Client<T, F>, RpcCall) -> F,
// pub init_request: fn(Client<T, F>, InitRequest) -> F,
// pub init_data: fn(Client<T, F>, InitData) -> F,
// pub heart_beat: fn(Client<T, F>, HeartBeat) -> F,
// pub heart_beat_reply: fn(Client<T, F>, HeartBeatReply) -> F,
// }
|