This commit is contained in:
ntr 2019-09-12 14:50:54 +10:00
parent 937fdae92e
commit b89b9a549a
8 changed files with 150 additions and 135 deletions

View File

@ -3,6 +3,14 @@ name = "mnml"
version = "1.4.1"
authors = ["ntr <ntr@smokestack.io>"]
[[bin]]
name = "main"
path = "bin/main.rs"
[[bin]]
name = "svt"
path = "bin/svt.rs"
[dependencies]
serde = "1"
serde_derive = "1"
@ -33,10 +41,13 @@ router = "0.6"
mount = "0.4"
cookie = "0.12"
crossbeam-channel = "0.3"
ws = "0.8"
ws = { version = "0.8", features = ["ssl"] }
lettre = "0.9"
lettre_email = "0.9"
stripe-rust = "0.10"
# stripe-rust = { path = "/home/ntr/code/stripe-rs" }
reqwest = "0.9"
url = "1"

96
server/bin/svt.rs Normal file
View File

@ -0,0 +1,96 @@
extern crate reqwest;
extern crate rand;
extern crate ws;
extern crate mnml;
extern crate serde_cbor;
use std::thread;
use serde_cbor::{from_slice, to_vec};
use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};
use reqwest::header;
use std::iter;
use ws::{connect, CloseCode, Message, Handler, Sender, Result, Request, Response};
use mnml::rpc::{RpcRequest};
use mnml::{setup_logger};
struct Bot {
out: Sender,
token: String,
}
impl Handler for Bot {
fn build_request(&mut self, url: &url::Url) -> Result<Request> {
let mut req = Request::from_url(url)?;
let token = format!("x-auth-token={:}", self.token);
println!("{:?}", token);
req.headers_mut().push(("Cookie".to_string(), Vec::from(token)));
Ok(req)
}
fn on_open(&mut self, _: ws::Handshake) -> ws::Result<()> {
println!("websocket connected");
let pvp_q = to_vec(&RpcRequest::InstanceQueue {}).unwrap();
self.out.send(Message::Binary(pvp_q)).unwrap();
Ok(())
}
}
fn main() {
setup_logger().unwrap();
let mut clients = vec![];
for i in 0..1000 {
clients.push(thread::Builder::new().name(i.to_string()).spawn(|| {
let mut rng = thread_rng();
let name: String = iter::repeat(()).map(|()| rng.sample(Alphanumeric)).take(12).collect();
let password: String = iter::repeat(()).map(|()| rng.sample(Alphanumeric)).take(12).collect();
let mut headers = header::HeaderMap::new();
headers.insert(header::CONTENT_TYPE, header::HeaderValue::from_static("application/json"));
let client = reqwest::Client::builder()
.cookie_store(true)
.default_headers(headers)
.build()
.unwrap();
let register_body = format!("{{ \"name\": {:?}, \"password\": {:?}, \"code\": \"grep842\" }}", name, password);
println!("{:?}", register_body);
let account = client.post("https://sixtysix.pro/api/account/register")
.body(register_body)
.send()
.unwrap();
println!("{:?}", account.headers().get("set-cookie"));
let ws = thread::Builder::new().name(name).spawn(move || {
connect("wss://sixtysix.pro/api/ws", |out| {
let token_cookie = account.cookies()
.find(|c| c.name() == "x-auth-token")
.unwrap();
Bot {
out: out,
token: token_cookie.value().to_string(),
}
}).unwrap()
}).unwrap();
ws.join().unwrap();
}).unwrap());
}
for client in clients {
let _ = client.join();
}
}

View File

@ -50,7 +50,7 @@ mod payments;
mod pg;
mod player;
mod events;
mod rpc;
pub mod rpc;
mod skill;
mod spec;
mod util;
@ -70,7 +70,7 @@ struct JsonLog {
msg: String,
}
fn setup_logger() -> Result<(), fern::InitError> {
pub fn setup_logger() -> Result<(), fern::InitError> {
let colors_line = ColoredLevelConfig::new()
.error(Color::Red)
.warn(Color::Yellow)

View File

@ -1,7 +0,0 @@
extern crate mnml;
use mnml::start;
fn main() {
start()
}

View File

@ -14,7 +14,7 @@ use cookie::Cookie;
use stripe::{Client as StripeClient, Subscription};
use crossbeam_channel::{unbounded, Sender as CbSender};
use ws::{listen, CloseCode, Message, Handler, Request, Response};
use ws::{Builder, listen, CloseCode, Message, Handler, Request, Response, Settings, Sender as WsSender};
use account::{Account};
use account;
@ -66,7 +66,7 @@ pub enum RpcMessage {
}
#[derive(Debug,Clone,Serialize,Deserialize)]
enum RpcRequest {
pub enum RpcRequest {
Ping {},
ItemInfo {},
DevResolve { a: Uuid, b: Uuid, skill: Skill },
@ -359,39 +359,46 @@ impl Handler for Connection {
pub fn start(pool: PgPool, events_tx: CbSender<Event>, stripe: StripeClient) {
let mut rng = thread_rng();
listen("127.0.0.1:40055", move |out| {
Builder::new()
.with_settings(Settings {
max_connections: 10_000,
..Settings::default()
})
.build(move |out: WsSender| {
// we give the tx half to the connection object
// which in turn passes a clone to the events system
// the rx half goes into a thread where it waits for messages
// that need to be delivered to the client
// both the ws message handler and the events thread must use
// this channel to send messages
let (tx, rx) = unbounded::<RpcMessage>();
// we give the tx half to the connection object
// which in turn passes a clone to the events system
// the rx half goes into a thread where it waits for messages
// that need to be delivered to the client
// both the ws message handler and the events thread must use
// this channel to send messages
let (tx, rx) = unbounded::<RpcMessage>();
spawn(move || {
loop {
match rx.recv() {
Ok(n) => {
let response = to_vec(&n).unwrap();
out.send(Message::Binary(response)).unwrap();
}
// we done
Err(_e) => {
break;
},
};
}
});
spawn(move || {
loop {
match rx.recv() {
Ok(n) => {
let response = to_vec(&n).unwrap();
out.send(Message::Binary(response)).unwrap();
}
// we done
Err(_e) => {
break;
},
};
Connection {
id: rng.gen::<usize>(),
account: None,
ws: tx,
pool: pool.clone(),
stripe: stripe.clone(),
events: events_tx.clone(),
}
});
Connection {
id: rng.gen::<usize>(),
account: None,
ws: tx,
pool: pool.clone(),
stripe: stripe.clone(),
events: events_tx.clone(),
}
}).unwrap();
})
.unwrap()
.listen("127.0.0.1:40055")
.unwrap();
}

4
svt/.gitignore vendored
View File

@ -1,4 +0,0 @@
target/
Cargo.lock
log/
.env

View File

@ -1,13 +0,0 @@
[package]
name = "mnml_svt"
version = "0.1.0"
authors = ["ntr <ntr@mnml.gg>"]
edition = "2018"
[dependencies]
ws = "0.8"
reqwest = "0.9"
rand = "0.6"
url = "1"
cookie = "0.12"

View File

@ -1,75 +0,0 @@
extern crate reqwest;
extern crate rand;
extern crate ws;
use std::thread;
use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};
use reqwest::header;
use std::iter;
use ws::{connect, CloseCode, Message, Handler, Sender, Result, Request, Response};
struct Bot {
out: Sender,
token: String,
}
impl Handler for Bot {
fn build_request(&mut self, url: &url::Url) -> Result<Request> {
let mut req = Request::from_url(url)?;
let headers = req.headers_mut();
headers.push(("x-auth-token".to_string(), Vec::from(self.token.clone())));
Ok(req)
}
fn on_open(&mut self, _: ws::Handshake) -> ws::Result<()> {
println!("websocket connected");
Ok(())
}
}
fn main() {
let mut rng = thread_rng();
let name: String = iter::repeat(()).map(|()| rng.sample(Alphanumeric)).take(12).collect();
let password: String = iter::repeat(()).map(|()| rng.sample(Alphanumeric)).take(12).collect();
let mut headers = header::HeaderMap::new();
headers.insert(header::CONTENT_TYPE, header::HeaderValue::from_static("application/json"));
let client = reqwest::Client::builder()
.cookie_store(true)
.default_headers(headers)
.build()
.unwrap();
let register_body = format!("{{ \"name\": {:?}, \"password\": {:?}, \"code\": \"grep842\" }}", name, password);
println!("{:?}", register_body);
let account = client.post("http://localhost/api/account/register")
.body(register_body)
.send()
.unwrap();
println!("{:?}", account.headers().get("set-cookie"));
// Client thread
let client = thread::Builder::new().name(name).spawn(move || {
connect("ws://localhost/api/ws", |out| {
let token_cookie = account.cookies()
.find(|c| c.name() == "x-auth-token")
.unwrap();
Bot {
out: out,
token: token_cookie.value().to_string(),
}
}).unwrap()
}).unwrap();
client.join().unwrap()
}