Skip to content

Commit

Permalink
chore: final cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nhtyy committed Jun 18, 2024
1 parent bf3f066 commit 8517f12
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 22 deletions.
14 changes: 4 additions & 10 deletions core/firewall/src/rate_limiting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl RateLimiting {
.filter_map(|(k, v)| {
// take the user rules if theyre are not global
if v.first().map_or(false, |policy| !policy.is_global) {
return Some((k, v.into_iter().map(IsGlobal::take).collect()));
return Some((k, v.into_iter().map(IsGlobal::inner).collect()));
}

None
Expand Down Expand Up @@ -212,7 +212,7 @@ impl RateLimiting {
// there is no policy for this ip, use the global policy
let mut policies: Vec<IsGlobal<RateLimitingPolicy>> = global
.iter()
.cloned()
.copied()
.map(|p| IsGlobal::yes(p.into()))
.collect();

Expand All @@ -230,13 +230,7 @@ impl RateLimiting {

impl Default for RateLimiting {
fn default() -> Self {
Self::WithGlobal {
global: vec![RateLimitingRule {
period: Period::Second,
max_requests: 10,
}],
per: HashMap::new(),
}
Self::None
}
}

Expand Down Expand Up @@ -335,7 +329,7 @@ impl<T> IsGlobal<T> {
}
}

fn take(self) -> T {
fn inner(self) -> T {
self.value
}
}
Expand Down
15 changes: 7 additions & 8 deletions core/firewall/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,29 +129,28 @@ where
mut self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Self::Output> {
let mut this = self.as_mut();

// If we haven't called the firewall yet, do so
if !this.called {
let pinned = Pin::new(&mut this.firewall_fut);
if !self.called {
let pinned = Pin::new(&mut self.firewall_fut);

match ready!(pinned.poll(cx)) {
Ok(_) => {},
Err(e) => return Poll::Ready(Ok(e.into())),
}

this.called = true;
self.called = true;
}

loop {
if let Some(fut) = this.svc_fut.as_mut() {
if let Some(fut) = self.svc_fut.as_mut() {
match Pin::new(fut).poll(cx) {
Poll::Ready(res) => return Poll::Ready(res),
Poll::Pending => return Poll::Pending,
}
}

let req = this.req.take().expect("req is none");
this.svc_fut = Some(this.svc.call(req));
let req = self.req.take().expect("req is none");
self.svc_fut = Some(self.svc.call(req));
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions core/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ static HMAC_SECRET: OnceLock<[u8; 32]> = OnceLock::new();
pub static HMAC_NONCE: AtomicUsize = AtomicUsize::new(0);
pub static HMAC_SALT: &[u8] = b"lightning-hmac-salt";

/// Tries to read the hmac secret from the given path or the default FS location
/// or it will generate one and write it to disk
/// Tries to read the hmac secret from the given path or the default location if empty
/// if the file exists it will read the secret from it otherwise
/// it will generate one and write it to disk in both cases
pub fn hmac_secret(secret_dir_path: Option<PathBuf>) -> anyhow::Result<&'static [u8; 32]> {
match HMAC_SECRET.get() {
Some(secret) => Ok(secret),
Expand Down
2 changes: 1 addition & 1 deletion core/rpc/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ fn verify_hmac(hmac: &str, ts: &str, nonce: &str) -> Result<(), BoxedError> {
}

// assume hmac secret is loaded by now
// we have verified that params are valid so lets creat the correct one
// we have verified that params are valid so lets create the correct one
let correct_hmac = create_hmac(super::hmac_secret(None)?, u64_ts, usize_nonce)?;

if hmac != correct_hmac {
Expand Down
2 changes: 1 addition & 1 deletion core/types/src/firewall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub enum RateLimitingConfig {
},
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub struct RateLimitingRule {
pub period: Period,
pub max_requests: u64,
Expand Down

0 comments on commit 8517f12

Please sign in to comment.