diff --git a/doc/readme.md b/doc/readme.md index 5f012c3..2f66ec2 100644 --- a/doc/readme.md +++ b/doc/readme.md @@ -871,6 +871,25 @@ jsonpath的规则参见[jsonpath-rust](https://github.com/besok/jsonpath-rust) 返回替换结果 + +### 正则替换 + + +【正则替换@文本@正则表达式@新文本】 + +返回替换结果 + +例如: +``` +【正则替换 +@(met)1875159423(met) +@\\(met\\)(?P(\\d+)|(all))\\(met\\) +@[CQ:at,qq=$qq] +】 +``` +将返回`[CQ:at,qq=1875159423]` + + ### 文本查找 @@ -2068,6 +2087,11 @@ QQ相关的信息,会复制到错误事件中;在错误事件中再次发生 返回`POST`、`GET`等。 +### 网络-权限 + +【网络-权限】 + +返回`可写`、`只读`、或空文本。 ## 包管理说明(未完全完成) diff --git a/res/version.txt b/res/version.txt index b0fea7d..9990aec 100644 --- a/res/version.txt +++ b/res/version.txt @@ -1 +1 @@ -0.0.65 \ No newline at end of file +0.0.66 \ No newline at end of file diff --git a/src/httpevent/mod.rs b/src/httpevent/mod.rs index 25f75b0..f966da8 100644 --- a/src/httpevent/mod.rs +++ b/src/httpevent/mod.rs @@ -52,7 +52,7 @@ fn get_params_from_uri(uri:&hyper::Uri) -> BTreeMap { ret_map } -pub fn do_http_event(mut req:hyper::Request) -> Result, Box> { +pub fn do_http_event(mut req:hyper::Request,can_write:bool,can_read:bool) -> Result, Box> { // 获取pkg_name和pkg_key let url_path = req.uri().path(); let true_url = url_path.get(5..).unwrap(); @@ -88,6 +88,14 @@ pub fn do_http_event(mut req:hyper::Request) -> Result) -> Result,can_write:bool) -> Result, Box> { +async fn deal_api(request: hyper::Request,can_write:bool,can_read:bool) -> Result, Box> { let url_path = request.uri().path(); if url_path == "/get_code" { + if !can_read { + let res = hyper::Response::new(hyper::Body::from("api not found")); + return Ok(res); + } match crate::read_code_cache() { Ok(code) => { let ret = json!({ @@ -49,6 +53,10 @@ async fn deal_api(request: hyper::Request,can_write:bool) -> Result }, } }else if url_path == "/get_all_pkg_name" { + if !can_read { + let res = hyper::Response::new(hyper::Body::from("api not found")); + return Ok(res); + } match crate::get_all_pkg_name_by_cache() { Ok(code) => { let ret = json!({ @@ -66,6 +74,10 @@ async fn deal_api(request: hyper::Request,can_write:bool) -> Result }, } }else if url_path == "/get_config" { + if !can_read { + let res = hyper::Response::new(hyper::Body::from("api not found")); + return Ok(res); + } match crate::read_config() { Ok(code) => { let ret = json!({ @@ -150,6 +162,10 @@ async fn deal_api(request: hyper::Request,can_write:bool) -> Result cq_add_log_w("收到退出指令,正在退出").unwrap(); crate::wait_for_quit(); }else if url_path == "/get_version" { + if !can_read { + let res = hyper::Response::new(hyper::Body::from("api not found")); + return Ok(res); + } let ret = json!({ "retcode":0, "data":crate::get_version() @@ -172,7 +188,7 @@ async fn deal_api(request: hyper::Request,can_write:bool) -> Result else if url_path.starts_with("/user") { let (tx, rx) = tokio::sync::oneshot::channel(); tokio::task::spawn_blocking(move || { - let ret = do_http_event(request); + let ret = do_http_event(request,can_write,can_read); if ret.is_ok() { let rst = tx.send(ret.unwrap()); if rst.is_err() { @@ -327,6 +343,21 @@ fn http_auth(request: &hyper::Request) -> Result bool { + if url_path.starts_with("/user") { + return true; + } + return false; +} + +fn rout_to_login() -> hyper::Response { + let mut res = hyper::Response::new(hyper::Body::from(vec![])); + *res.status_mut() = hyper::StatusCode::MOVED_PERMANENTLY; + res.headers_mut().insert("Location", HeaderValue::from_static("/login.html")); + return res; +} + async fn connect_handle(request: hyper::Request) -> Result, Box> { let url_path = request.uri().path(); @@ -335,22 +366,22 @@ async fn connect_handle(request: hyper::Request) -> Result) -> Result) -> Result) -> Result) -> Result::new_all(); if let Some(process) = sysinfo::SystemExt::process(&s, sysinfo::Pid::from(std::process::id() as usize)) { @@ -2378,6 +2379,15 @@ def red_out(sw): return Ok(Some("".to_string())); }); + add_fun(vec!["正则替换"],|self_t,params|{ + let text = self_t.get_param(params, 0)?; + let re = self_t.get_param(params, 1)?; + let out_text = self_t.get_param(params, 2)?; + let re_obj = fancy_regex::Regex::new(&re)?; + let out = re_obj.replace_all(&text, out_text).to_string(); + Ok(Some(out)) + }); + add_fun(vec!["CPU使用"],|_self_t,_params|{ let mut s = ::new_all(); std::thread::sleep(::MINIMUM_CPU_UPDATE_INTERVAL); diff --git a/src/redlang/webexfun.rs b/src/redlang/webexfun.rs index 4785548..705186a 100644 --- a/src/redlang/webexfun.rs +++ b/src/redlang/webexfun.rs @@ -54,6 +54,10 @@ pub fn init_web_ex_fun_map() { let ret = self_t.get_coremap("网络-访问头")?; return Ok(Some(ret.to_owned())); }); + add_fun(vec!["网络-权限"],|self_t,_params|{ + let ret = self_t.get_coremap("网络-权限")?; + return Ok(Some(ret.to_owned())); + }); add_fun(vec!["网络-访问体"],|self_t,_params|{ if self_t.req_tx.is_none() || self_t.req_rx.is_none() { self_t.req_rx = None;