From f59462125e6bd875734cb753bd82049b9decb661 Mon Sep 17 00:00:00 2001 From: super1207 <1875159423@qq.com> Date: Sun, 1 Oct 2023 19:01:20 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=85=81=E8=AE=B8=E6=97=A0=E6=9D=83?= =?UTF-8?q?=E9=99=90=E8=AE=BF=E9=97=AE=E8=87=AA=E5=AE=9A=E4=B9=89=E7=BD=91?= =?UTF-8?q?=E7=BB=9Capi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/readme.md | 5 +++ src/httpevent/mod.rs | 11 +++++- src/httpserver/mod.rs | 78 +++++++++++++++++++++++++++++++++++------ src/redlang/exfun.rs | 39 --------------------- src/redlang/webexfun.rs | 4 +++ 5 files changed, 86 insertions(+), 51 deletions(-) diff --git a/doc/readme.md b/doc/readme.md index c1d4a0b..71b991d 100644 --- a/doc/readme.md +++ b/doc/readme.md @@ -2068,6 +2068,11 @@ QQ相关的信息,会复制到错误事件中;在错误事件中再次发生 返回`POST`、`GET`等。 +### 网络-权限 + +【网络-权限】 + +返回`可写`、`只读`、或空文本。 ## 包管理说明(未完全完成) 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 = vec![]; - buf.append(code.as_bytes().to_vec().as_mut()); - buf.push(b'\r'); - buf.push(b'\n'); - buf.push(u8::MAX); - buf.push(b'\r'); - buf.push(b'\n'); - buf.append(input.as_bytes().to_vec().as_mut()); - s.unwrap().write_all(&buf)?; - } - let output = p.wait_with_output()?; - let out = String::from_utf8_lossy(&output.stdout).to_string(); - let err = String::from_utf8_lossy(&output.stderr).to_string(); - if err != "" { - cq_add_log_w(&format!("TCC中的警告或错误:{}",err)).unwrap(); - } - cq_add_log_w(&format!("TCC中的输出:`{}`",out)).unwrap(); - Ok(Some(out)) - }); } 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; From e2cbc8314c6e9d60f2780a378468e952444a2f46 Mon Sep 17 00:00:00 2001 From: super1207 <1875159423@qq.com> Date: Tue, 3 Oct 2023 13:46:22 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E3=80=90=E6=AD=A3=E5=88=99=E6=9B=BF=E6=8D=A2=E3=80=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/readme.md | 19 +++++++++++++++++++ res/version.txt | 2 +- src/redlang/exfun.rs | 8 ++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/doc/readme.md b/doc/readme.md index 71b991d..de9ea46 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]` + + ### 文本查找 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/redlang/exfun.rs b/src/redlang/exfun.rs index b7c11d3..4d04019 100644 --- a/src/redlang/exfun.rs +++ b/src/redlang/exfun.rs @@ -2369,6 +2369,14 @@ def red_out(sw): } 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)) + }); }