svt
This commit is contained in:
parent
937fdae92e
commit
b89b9a549a
@ -3,6 +3,14 @@ name = "mnml"
|
|||||||
version = "1.4.1"
|
version = "1.4.1"
|
||||||
authors = ["ntr <ntr@smokestack.io>"]
|
authors = ["ntr <ntr@smokestack.io>"]
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "main"
|
||||||
|
path = "bin/main.rs"
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "svt"
|
||||||
|
path = "bin/svt.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
serde = "1"
|
serde = "1"
|
||||||
serde_derive = "1"
|
serde_derive = "1"
|
||||||
@ -33,10 +41,13 @@ router = "0.6"
|
|||||||
mount = "0.4"
|
mount = "0.4"
|
||||||
cookie = "0.12"
|
cookie = "0.12"
|
||||||
crossbeam-channel = "0.3"
|
crossbeam-channel = "0.3"
|
||||||
ws = "0.8"
|
ws = { version = "0.8", features = ["ssl"] }
|
||||||
|
|
||||||
lettre = "0.9"
|
lettre = "0.9"
|
||||||
lettre_email = "0.9"
|
lettre_email = "0.9"
|
||||||
|
|
||||||
stripe-rust = "0.10"
|
stripe-rust = "0.10"
|
||||||
# stripe-rust = { path = "/home/ntr/code/stripe-rs" }
|
# stripe-rust = { path = "/home/ntr/code/stripe-rs" }
|
||||||
|
|
||||||
|
reqwest = "0.9"
|
||||||
|
url = "1"
|
||||||
|
|||||||
96
server/bin/svt.rs
Normal file
96
server/bin/svt.rs
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -50,7 +50,7 @@ mod payments;
|
|||||||
mod pg;
|
mod pg;
|
||||||
mod player;
|
mod player;
|
||||||
mod events;
|
mod events;
|
||||||
mod rpc;
|
pub mod rpc;
|
||||||
mod skill;
|
mod skill;
|
||||||
mod spec;
|
mod spec;
|
||||||
mod util;
|
mod util;
|
||||||
@ -70,7 +70,7 @@ struct JsonLog {
|
|||||||
msg: String,
|
msg: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn setup_logger() -> Result<(), fern::InitError> {
|
pub fn setup_logger() -> Result<(), fern::InitError> {
|
||||||
let colors_line = ColoredLevelConfig::new()
|
let colors_line = ColoredLevelConfig::new()
|
||||||
.error(Color::Red)
|
.error(Color::Red)
|
||||||
.warn(Color::Yellow)
|
.warn(Color::Yellow)
|
||||||
|
|||||||
@ -1,7 +0,0 @@
|
|||||||
extern crate mnml;
|
|
||||||
|
|
||||||
use mnml::start;
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
start()
|
|
||||||
}
|
|
||||||
@ -14,7 +14,7 @@ use cookie::Cookie;
|
|||||||
use stripe::{Client as StripeClient, Subscription};
|
use stripe::{Client as StripeClient, Subscription};
|
||||||
|
|
||||||
use crossbeam_channel::{unbounded, Sender as CbSender};
|
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::{Account};
|
||||||
use account;
|
use account;
|
||||||
@ -66,7 +66,7 @@ pub enum RpcMessage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||||
enum RpcRequest {
|
pub enum RpcRequest {
|
||||||
Ping {},
|
Ping {},
|
||||||
ItemInfo {},
|
ItemInfo {},
|
||||||
DevResolve { a: Uuid, b: Uuid, skill: Skill },
|
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) {
|
pub fn start(pool: PgPool, events_tx: CbSender<Event>, stripe: StripeClient) {
|
||||||
let mut rng = thread_rng();
|
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
|
spawn(move || {
|
||||||
// which in turn passes a clone to the events system
|
loop {
|
||||||
// the rx half goes into a thread where it waits for messages
|
match rx.recv() {
|
||||||
// that need to be delivered to the client
|
Ok(n) => {
|
||||||
// both the ws message handler and the events thread must use
|
let response = to_vec(&n).unwrap();
|
||||||
// this channel to send messages
|
out.send(Message::Binary(response)).unwrap();
|
||||||
let (tx, rx) = unbounded::<RpcMessage>();
|
}
|
||||||
|
// we done
|
||||||
|
Err(_e) => {
|
||||||
|
break;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
spawn(move || {
|
Connection {
|
||||||
loop {
|
id: rng.gen::<usize>(),
|
||||||
match rx.recv() {
|
account: None,
|
||||||
Ok(n) => {
|
ws: tx,
|
||||||
let response = to_vec(&n).unwrap();
|
pool: pool.clone(),
|
||||||
out.send(Message::Binary(response)).unwrap();
|
stripe: stripe.clone(),
|
||||||
}
|
events: events_tx.clone(),
|
||||||
// we done
|
|
||||||
Err(_e) => {
|
|
||||||
break;
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
|
.unwrap()
|
||||||
Connection {
|
.listen("127.0.0.1:40055")
|
||||||
id: rng.gen::<usize>(),
|
.unwrap();
|
||||||
account: None,
|
|
||||||
ws: tx,
|
|
||||||
pool: pool.clone(),
|
|
||||||
stripe: stripe.clone(),
|
|
||||||
events: events_tx.clone(),
|
|
||||||
}
|
|
||||||
}).unwrap();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
4
svt/.gitignore
vendored
4
svt/.gitignore
vendored
@ -1,4 +0,0 @@
|
|||||||
target/
|
|
||||||
Cargo.lock
|
|
||||||
log/
|
|
||||||
.env
|
|
||||||
@ -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"
|
|
||||||
|
|
||||||
@ -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()
|
|
||||||
}
|
|
||||||
Loading…
x
Reference in New Issue
Block a user