diff options
| author | Matthieu <matthieu@developershouse.xyz> | 2021-09-17 13:14:24 +0400 |
|---|---|---|
| committer | Matthieu <matthieu@developershouse.xyz> | 2021-09-17 13:14:24 +0400 |
| commit | ba0170ea15d6ebc1c9049f02adabd1ee4df6de64 (patch) | |
| tree | e6b49157eacf443a3878683d297544cfbd20bd0f /webhook | |
| parent | 91a2855e3d0af5199d284d32cc2f6b8280df9d10 (diff) | |
webhook base implementation
Diffstat (limited to 'webhook')
| -rw-r--r-- | webhook/Cargo.toml | 1 | ||||
| -rw-r--r-- | webhook/cargo/BUILD.bazel | 9 | ||||
| -rw-r--r-- | webhook/config/default.yaml | 2 | ||||
| -rw-r--r-- | webhook/src/handle.rs | 18 | ||||
| -rw-r--r-- | webhook/src/main.rs | 2 |
5 files changed, 25 insertions, 7 deletions
diff --git a/webhook/Cargo.toml b/webhook/Cargo.toml index fb3da51..f2c278e 100644 --- a/webhook/Cargo.toml +++ b/webhook/Cargo.toml @@ -14,6 +14,7 @@ hex = "0.4.3" pretty_env_logger = "0.4" serde_json = { version = "1.0" } common = { path = "../common/rust" } +nats = "0.15.2" [[bin]] name = "webhook" diff --git a/webhook/cargo/BUILD.bazel b/webhook/cargo/BUILD.bazel index cb1c9cd..e698ae3 100644 --- a/webhook/cargo/BUILD.bazel +++ b/webhook/cargo/BUILD.bazel @@ -58,6 +58,15 @@ alias( ) alias( + name = "nats", + actual = "@raze__nats__0_15_2//:nats", + tags = [ + "cargo-raze", + "manual", + ], +) + +alias( name = "pretty_env_logger", actual = "@raze__pretty_env_logger__0_4_0//:pretty_env_logger", tags = [ diff --git a/webhook/config/default.yaml b/webhook/config/default.yaml index 80cf77a..770d67f 100644 --- a/webhook/config/default.yaml +++ b/webhook/config/default.yaml @@ -10,5 +10,5 @@ webhook: port: 8000
discord:
client_id: 738817757650485300
- public_key: "475bed67e20fb1e2d9b9007607f08d7b25b5a0aa936ef2d4ddaf7d592c993860"
+ public_key: "2dc5873bb9e603dbf70e6abc217768b9d7dfacef9e74ddaefcca6a9aed41f30d"
diff --git a/webhook/src/handle.rs b/webhook/src/handle.rs index b60889e..b87d9a5 100644 --- a/webhook/src/handle.rs +++ b/webhook/src/handle.rs @@ -11,6 +11,7 @@ use std::str::from_utf8; use std::task::{Context, Poll};
use serde::{Deserialize, Serialize};
use crate::config::Config;
+use nats::{Connection, connect};
pub fn validate_signature(b64_public_key: &str, data: &Vec<u8>, b64_signature: &str) -> bool {
// First, we need to check if the signature & private key is valid base64.
@@ -56,6 +57,7 @@ fn get_signature(headers: &HeaderMap) -> Option<(&str, &str)> { pub struct HandlerService {
pub config: Config,
+ pub nats: Box<Connection>
}
#[derive(Debug, Serialize, Deserialize)]
@@ -76,6 +78,7 @@ impl Service<Request<Body>> for HandlerService { fn call(&mut self, req: Request<Body>) -> Self::Future {
if req.method() == Method::POST {
let public_key = self.config.discord.public_key.clone();
+ let nats = self.nats.clone();
return Box::pin(async move {
let headers = req.headers().clone();
if let Some((signature, timestamp)) = get_signature(&headers) {
@@ -83,18 +86,19 @@ impl Service<Request<Body>> for HandlerService { let contatenated_data = [timestamp.as_bytes().to_vec(), data.to_vec()].concat();
if validate_signature(public_key.as_str(), &contatenated_data, signature) {
- let data: Value = serde_json::from_str(from_utf8(&data).unwrap()).unwrap();
- let t = data.get("type").unwrap().as_i64().unwrap();
+ let d: Value = serde_json::from_str(from_utf8(&data).unwrap()).unwrap();
+ let t = d.get("type").unwrap().as_i64().unwrap();
if t == 1 {
- info!("success!");
-
return Ok(Response::builder().header("Content-Type", "application/json").body(serde_json::to_string(&Ping {
t: 1
}).unwrap().into()).unwrap());
-
} else {
- Ok(Response::builder().status(StatusCode::UNAUTHORIZED).body("invalid operation".into()).unwrap())
+ info!("Handled event");
+ nats.publish(&format!("nova.dispatch.interaction_raw"), data).unwrap();
+ return Ok(Response::builder().header("Content-Type", "application/json").body(serde_json::to_string(&Ping {
+ t: 5
+ }).unwrap().into()).unwrap());
}
} else {
Ok(Response::builder().status(StatusCode::UNAUTHORIZED).body("signature verification failed".into()).unwrap())
@@ -116,6 +120,7 @@ impl Service<Request<Body>> for HandlerService { pub struct MakeSvc {
pub settings: Config,
+ pub nats: Box<Connection>,
}
impl<T> Service<T> for MakeSvc {
@@ -130,6 +135,7 @@ impl<T> Service<T> for MakeSvc { fn call(&mut self, _: T) -> Self::Future {
future::ready(Ok(HandlerService {
config: self.settings.clone(),
+ nats: self.nats.clone(),
}))
}
}
diff --git a/webhook/src/main.rs b/webhook/src/main.rs index 3125373..b9fefc5 100644 --- a/webhook/src/main.rs +++ b/webhook/src/main.rs @@ -22,8 +22,10 @@ async fn main() { "Starting server on {}:{}",
settings.config.server.address, settings.config.server.port
);
+ let nats = Box::new(nats::connect("localhost").unwrap());
let server = Server::bind(&addr).serve(MakeSvc {
settings: settings.config.clone(),
+ nats
});
if let Err(e) = server.await {
|
