-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
injector/optional extractor/user group
- Loading branch information
Showing
21 changed files
with
536 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
pub mod combinator; | ||
pub mod implementation; | ||
|
||
use crate::SgRequest; | ||
|
||
pub trait UserGroup { | ||
fn is_match(&self, req: &SgRequest) -> bool; | ||
} | ||
|
||
pub trait UserGroupExt: UserGroup { | ||
fn and<B>(self, b: B) -> combinator::And<Self, B> | ||
where | ||
Self: Sized, | ||
{ | ||
combinator::And { a: self, b } | ||
} | ||
|
||
fn or<B>(self, b: B) -> combinator::Or<Self, B> | ||
where | ||
Self: Sized, | ||
{ | ||
combinator::Or { a: self, b } | ||
} | ||
|
||
fn not(self) -> combinator::Not<Self> | ||
where | ||
Self: Sized, | ||
{ | ||
combinator::Not { a: self } | ||
} | ||
|
||
fn boxed(self) -> Box<dyn UserGroup> | ||
where | ||
Self: Sized + 'static, | ||
{ | ||
Box::new(self) | ||
} | ||
} | ||
|
||
impl<T: UserGroup> UserGroupExt for T {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
use std::ops::{Deref, DerefMut}; | ||
|
||
use crate::SgRequest; | ||
|
||
use super::UserGroup; | ||
|
||
#[derive(Debug)] | ||
pub struct And<A, B> { | ||
pub a: A, | ||
pub b: B, | ||
} | ||
|
||
impl<A, B> UserGroup for And<A, B> | ||
where | ||
A: UserGroup, | ||
B: UserGroup, | ||
{ | ||
fn is_match(&self, req: &SgRequest) -> bool { | ||
self.a.is_match(req) && self.b.is_match(req) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct Not<A> { | ||
pub a: A, | ||
} | ||
|
||
impl<A: UserGroup> UserGroup for Not<A> { | ||
fn is_match(&self, req: &SgRequest) -> bool { | ||
!self.a.is_match(req) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct Or<A, B> { | ||
pub a: A, | ||
pub b: B, | ||
} | ||
|
||
impl<A, B> UserGroup for Or<A, B> | ||
where | ||
A: UserGroup, | ||
B: UserGroup, | ||
{ | ||
fn is_match(&self, req: &SgRequest) -> bool { | ||
self.a.is_match(req) || self.b.is_match(req) | ||
} | ||
} | ||
|
||
pub struct All { | ||
pub groups: Vec<Box<dyn UserGroup>>, | ||
} | ||
|
||
impl std::fmt::Debug for All { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
f.debug_struct("DynamicAll").finish() | ||
} | ||
} | ||
|
||
impl Deref for All { | ||
type Target = Vec<Box<dyn UserGroup>>; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.groups | ||
} | ||
} | ||
|
||
impl DerefMut for All { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
&mut self.groups | ||
} | ||
} | ||
|
||
impl UserGroup for All { | ||
fn is_match(&self, req: &SgRequest) -> bool { | ||
self.groups.iter().all(|g| g.is_match(req)) | ||
} | ||
} | ||
|
||
impl All { | ||
pub fn new(groups: Vec<Box<dyn UserGroup>>) -> Self { | ||
All { groups } | ||
} | ||
} | ||
|
||
pub struct Any { | ||
pub groups: Vec<Box<dyn UserGroup>>, | ||
} | ||
|
||
impl std::fmt::Debug for Any { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
f.debug_struct("DynamicAny").finish() | ||
} | ||
} | ||
|
||
impl Deref for Any { | ||
type Target = Vec<Box<dyn UserGroup>>; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.groups | ||
} | ||
} | ||
|
||
impl DerefMut for Any { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
&mut self.groups | ||
} | ||
} | ||
|
||
impl UserGroup for Any { | ||
fn is_match(&self, req: &SgRequest) -> bool { | ||
self.groups.iter().any(|g| g.is_match(req)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#[cfg(feature = "ipnet")] | ||
pub mod ipnet; |
16 changes: 16 additions & 0 deletions
16
crates/kernel/src/extension/user_group/implementation/ipnet.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use ipnet::IpNet; | ||
|
||
use crate::{ | ||
extension::{user_group::UserGroup, OriginalIpAddr}, | ||
SgRequestExt, | ||
}; | ||
|
||
impl UserGroup for IpNet { | ||
fn is_match(&self, req: &crate::SgRequest) -> bool { | ||
if let Some(OriginalIpAddr(ip)) = req.extract() { | ||
self.contains(&ip) | ||
} else { | ||
false | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,3 +21,6 @@ pub mod timeout; | |
|
||
/// Balancer layer | ||
pub mod balancer; | ||
|
||
/// Inject data into request | ||
pub mod injector; |
Oops, something went wrong.