Skip to content

Commit

Permalink
comm
Browse files Browse the repository at this point in the history
  • Loading branch information
tickbh committed Jan 24, 2024
1 parent 314c0d7 commit c8b19c2
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 23 deletions.
2 changes: 1 addition & 1 deletion config/reverse.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ try_paths = "{path}/ '/ro(\\w+)/(.*) {path} /ro$1/Cargo.toml' /root/README.md"
# allow_ip = "127.0.0.1"

[[http.server.location]]
rule = "/ws"
rule = "@ws"
is_ws = true
proxy_url = "http://ws"
headers = ["+ aaa bbb"]
Expand Down
15 changes: 14 additions & 1 deletion src/reverse/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use webparse::Url;
use wenmeng::RateLimitLayer;
use wenmeng::TimeoutLayer;

use super::LimitReq;
use super::{LimitReq, Matcher};

#[serde_as]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
Expand Down Expand Up @@ -65,6 +65,10 @@ pub struct CommonConfig {
pub domain: Option<String>,
#[serde_as(as = "Option<DisplayFromStr>")]
pub proxy_url: Option<Url>,

#[serde(default = "HashMap::new")]
#[serde_as(as = "HashMap<_, DisplayFromStr>")]
pub match_names: HashMap<String, Matcher>,
}

impl CommonConfig {
Expand Down Expand Up @@ -95,6 +99,8 @@ impl CommonConfig {

domain: None,
proxy_url: None,

match_names: HashMap::new(),
}
}

Expand Down Expand Up @@ -141,12 +147,19 @@ impl CommonConfig {
if self.deny_ip.is_none() {
self.deny_ip = parent.deny_ip.clone();
}

for p in &parent.match_names {
if !self.match_names.contains_key(p.0) {
self.match_names.insert(p.0.clone(), p.1.clone());
}
}
}

pub fn pre_deal(&mut self) {
if let Some(err) = &mut self.error_log {
err.as_error();
}

}

pub fn get_rate_limit(&self) -> Option<RateLimitLayer> {
Expand Down
6 changes: 5 additions & 1 deletion src/reverse/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ impl LocationConfig {

/// 当本地限制方法时,优先匹配方法,在进行路径的匹配
pub fn is_match_rule(&self, path: &String, req: &RecvRequest) -> bool {
self.rule.is_match_rule(path, req)
match self.rule.is_match_rule(path, req) {
Err(_) => false,
Ok(b) => b,
}

}

async fn deal_client(
Expand Down
63 changes: 43 additions & 20 deletions src/reverse/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
use std::{
collections::HashSet,
fmt::{self, Display},
str::FromStr,
str::FromStr, net::IpAddr,
};

use serde::{
Deserialize, Serialize,
};
use serde_with::{serde_as, DisplayFromStr};
use webparse::{Method, Scheme, WebError};
use wenmeng::RecvRequest;
use wenmeng::{RecvRequest, ProtResult, ProtError};

use crate::IpSets;

Expand Down Expand Up @@ -53,6 +53,15 @@ impl Matcher {
}
}

pub fn get_match_name(&self) -> Option<String> {
if let Some(p) = &self.path {
if p.starts_with("@") {
return Some(p.replace("@", ""));
}
}
None
}

pub fn get_path(&self) -> String {
if let Some(p) = &self.path {
if p.contains("*") {
Expand All @@ -69,33 +78,47 @@ impl Matcher {


/// 当本地限制方法时,优先匹配方法,在进行路径的匹配
pub fn is_match_rule(&self, path: &String, req: &RecvRequest) -> bool {
pub fn is_match_rule(&self, path: &String, req: &RecvRequest) -> ProtResult<bool> {
if let Some(p) = &self.path {
if path.find(&*p).is_none() {
return false;
return Ok(false);
}
}

if let Some(m) = &self.method {
if !m.0.contains(req.method()) {
return false;
return Ok(false);
}
}

if let Some(s) = &self.scheme {
if !s.0.contains(req.scheme()) {
return Ok(false);
}
}

if let Some(h) = &self.host {
match req.get_host() {
Some(host) if &host == h => {},
_ => return Ok(false),
}
}

if let Some(c) = &self.client_ip {
match req.headers().system_get("{client_ip}") {
Some(ip) => {
let ip = ip
.parse::<IpAddr>()
.map_err(|_| ProtError::Extension("client ip error"))?;
if !c.contains(&ip) {
return Ok(false)
}
},
None => return Ok(false),
}
}

true
// if self.method.is_some()
// && !self
// .method
// .as_ref()
// .unwrap()
// .eq_ignore_ascii_case(method.as_str())
// {
// return false;
// }
// if let Some(_) = path.find(&self.rule) {
// return true;
// } else {
// false
// }
Ok(true)
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/reverse/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ impl ServerConfig {
for l in &mut self.location {
l.comm.copy_from_parent(&self.comm);
l.comm.pre_deal();
if let Some(n) = l.rule.get_match_name() {
if l.comm.match_names.contains_key(&n) {
l.rule = l.comm.match_names[&n].clone();
} else {
log::error!("配置匹配名字@{},但未找到相应的配置", n);
println!("配置匹配名字@{},但未找到相应的配置", n);
}
}
l.up_name = Some(self.up_name.clone());
l.upstream.append(&mut self.upstream.clone());
l.headers.append(&mut self.headers.clone());
Expand Down

0 comments on commit c8b19c2

Please sign in to comment.