Skip to content

Commit

Permalink
feat(core): Prepare return the decision to http server
Browse files Browse the repository at this point in the history
Signed-off-by: KunoiSayami <[email protected]>
  • Loading branch information
KunoiSayami committed Dec 14, 2021
1 parent e960517 commit 675b807
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "github-webhook-notification"
version = "2.0.0"
version = "2.1.0-alpha"
edition = "2021"

[dependencies]
Expand Down
4 changes: 2 additions & 2 deletions SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

| Version | Supported |
|---------|--------------------|
| 2.0.x | :white_check_mark: |
| < 2.0 | :x: |
| 2.1.x | :white_check_mark: |
| < 2.1 | :x: |

## Reporting a Vulnerability

Expand Down
28 changes: 21 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
** along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

use crate::configure::Repository;
use crate::configure::{Config, Repository};
use crate::datastructures::{DisplayableEvent, GitHubPingEvent, GitHubPushEvent, Response};
use actix_web::http::Method;
use actix_web::web::Data;
Expand All @@ -42,11 +42,12 @@ const SERVER_VERSION: &str = env!("CARGO_PKG_VERSION");
#[derive(Debug)]
enum Command {
Terminate,
#[deprecated(since = "2.1.0", note = "You should use Bundle instead send GitHub Data directly, this field will removed in next version.")]
Data(Box<dyn DisplayableEvent>),
Bundle((Vec<i64>, String)),
}

struct ExtraData {
secrets: String,
bot_tx: mpsc::Sender<Command>,
}

Expand Down Expand Up @@ -102,7 +103,17 @@ async fn process_send_message(
}
}
}
Command::Bundle((receiver, text)) => {
for send_to in receiver {
let mut payload = bot.send_message(send_to, &text);
payload.disable_web_page_preview = Option::from(true);
if let Err(e) = payload.send().await {
error!("Got error in send message {:?}", e);
}
}
},
Command::Terminate => break,

}
}
debug!("Send message daemon exiting...");
Expand All @@ -116,6 +127,7 @@ fn check_0(s: &str) -> bool {
async fn route_post(
request: HttpRequest,
mut payload: web::Payload,
configure: web::Data<Config>,
data: web::Data<Arc<Mutex<ExtraData>>>,
) -> actix_web::Result<HttpResponse> {
let mut body = web::BytesMut::new();
Expand All @@ -130,9 +142,10 @@ async fn route_post(
}

let sender = data.lock().await;
if !sender.secrets.is_empty() {
let secrets =configure.server().secrets();
if !secrets.is_empty() {
type HmacSha256 = Hmac<Sha256>;
let mut h = HmacSha256::new_from_slice(sender.secrets.as_bytes()).unwrap();
let mut h = HmacSha256::new_from_slice(secrets.as_bytes()).unwrap();
h.update(&*body);
let result = h.finalize();
let sha256val = format!("sha256={:x}", result.into_bytes()).to_lowercase();
Expand Down Expand Up @@ -192,7 +205,6 @@ async fn async_main<P: AsRef<Path>>(path: P) -> anyhow::Result<()> {
crate::datastructures::AuthorizationGuard::from(config.server().token());

let extra_data = Arc::new(Mutex::new(ExtraData {
secrets: config.server().secrets().clone(),
bot_tx: bot_tx.clone(),
}));
let msg_sender = tokio::spawn(process_send_message(
Expand All @@ -203,7 +215,8 @@ async fn async_main<P: AsRef<Path>>(path: P) -> anyhow::Result<()> {
bot_rx,
));

info!("Bind address: {}", config.server().bind());
let bind = config.server().bind().clone();
info!("Bind address: {}", bind);

let server = tokio::spawn(
HttpServer::new(move || {
Expand All @@ -212,6 +225,7 @@ async fn async_main<P: AsRef<Path>>(path: P) -> anyhow::Result<()> {
.service(
web::scope("/")
.guard(authorization_guard.to_owned())
.app_data(Data::new(config.clone()))
.app_data(Data::new(extra_data.clone()))
.route("", web::method(Method::POST).to(route_post)),
)
Expand All @@ -221,7 +235,7 @@ async fn async_main<P: AsRef<Path>>(path: P) -> anyhow::Result<()> {
))
.route("/", web::to(HttpResponse::Forbidden))
})
.bind(config.server().bind())?
.bind(bind)?
.run(),
);

Expand Down

0 comments on commit 675b807

Please sign in to comment.