Skip to content

Commit

Permalink
fix conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
super1207 committed Oct 15, 2023
2 parents e3f05f7 + e2cbc83 commit af5fc9e
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 13 deletions.
24 changes: 24 additions & 0 deletions doc/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,25 @@ jsonpath的规则参见[jsonpath-rust](https://github.com/besok/jsonpath-rust)

返回替换结果


### 正则替换


【正则替换@<font color="red">文本</font>@<font color="red">正则表达式</font>@<font color="red">新文本</font>】

返回替换结果

例如:
```
【正则替换
@(met)1875159423(met)
@\\(met\\)(?P<qq>(\\d+)|(all))\\(met\\)
@[CQ:at,qq=$qq]
```
将返回`[CQ:at,qq=1875159423]`


### 文本查找


Expand Down Expand Up @@ -2068,6 +2087,11 @@ QQ相关的信息,会复制到错误事件中;在错误事件中再次发生

返回`POST``GET`等。

### 网络-权限

【网络-权限】

返回`可写``只读`、或空文本。


## 包管理说明(未完全完成)
Expand Down
2 changes: 1 addition & 1 deletion res/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.65
0.0.66
11 changes: 10 additions & 1 deletion src/httpevent/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn get_params_from_uri(uri:&hyper::Uri) -> BTreeMap<String,String> {
ret_map
}

pub fn do_http_event(mut req:hyper::Request<hyper::Body>) -> Result<hyper::Response<hyper::Body>, Box<dyn std::error::Error>> {
pub fn do_http_event(mut req:hyper::Request<hyper::Body>,can_write:bool,can_read:bool) -> Result<hyper::Response<hyper::Body>, Box<dyn std::error::Error>> {
// 获取pkg_name和pkg_key
let url_path = req.uri().path();
let true_url = url_path.get(5..).unwrap();
Expand Down Expand Up @@ -88,13 +88,22 @@ pub fn do_http_event(mut req:hyper::Request<hyper::Body>) -> Result<hyper::Respo
}
}
});
let web_access;
if can_write {
web_access = "可写";
} else if can_read {
web_access = "只读";
} else {
web_access = "";
}
for i in 0..script_json.as_array().ok_or("script.json文件不是数组格式")?.len(){
let (keyword,cffs,code,ppfs,name,pkg_name) = get_script_info(&script_json[i])?;
let mut rl = RedLang::new();
if cffs == "网络触发" && pkg_name == pkg_name_t && crate::cqevent::is_key_match(&mut rl,&ppfs,keyword,&msg)? {
rl.set_coremap("网络-访问方法", &method)?;
rl.set_coremap("网络-访问参数", &rl.build_obj(req_params))?;
rl.set_coremap("网络-访问头", &rl.build_obj(req_headers))?;
rl.set_coremap("网络-权限",web_access)?;
rl.req_tx = Some(body_tx1);
rl.req_rx = Some(body_rx2);
rl.pkg_name = pkg_name.to_owned();
Expand Down
78 changes: 67 additions & 11 deletions src/httpserver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ pub fn add_ws_log(log_msg:String) {
});
}

async fn deal_api(request: hyper::Request<hyper::Body>,can_write:bool) -> Result<hyper::Response<hyper::Body>, Box<dyn std::error::Error + Send + Sync>> {
async fn deal_api(request: hyper::Request<hyper::Body>,can_write:bool,can_read:bool) -> Result<hyper::Response<hyper::Body>, Box<dyn std::error::Error + Send + Sync>> {
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!({
Expand All @@ -49,6 +53,10 @@ async fn deal_api(request: hyper::Request<hyper::Body>,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!({
Expand All @@ -66,6 +74,10 @@ async fn deal_api(request: hyper::Request<hyper::Body>,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!({
Expand Down Expand Up @@ -150,6 +162,10 @@ async fn deal_api(request: hyper::Request<hyper::Body>,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()
Expand All @@ -172,7 +188,7 @@ async fn deal_api(request: hyper::Request<hyper::Body>,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() {
Expand Down Expand Up @@ -327,6 +343,21 @@ fn http_auth(request: &hyper::Request<hyper::Body>) -> Result<i32, Box<dyn std::
return Err(RedLang::make_err("password invaild"));
}


fn is_users_api(url_path:&str) -> bool {
if url_path.starts_with("/user") {
return true;
}
return false;
}

fn rout_to_login() -> hyper::Response<hyper::Body> {
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<hyper::Body>) -> Result<hyper::Response<hyper::Body>, Box<dyn std::error::Error + Send + Sync>> {

let url_path = request.uri().path();
Expand All @@ -335,22 +366,22 @@ async fn connect_handle(request: hyper::Request<hyper::Body>) -> Result<hyper::R
if url_path == "/login.html" {
return deal_file(request).await;
}

// 登录API不进行身份验证
if url_path == "/login" {
return deal_api(request,false).await;
return deal_api(request,false,false).await;
}

// 身份验证
// 获取身份信息
let can_write;
let can_read;
{
let http_auth_rst = http_auth(&request);
if http_auth_rst.is_err() {
// 认证失败,跳转登录页面
cq_add_log_w(&format!("{}",http_auth_rst.err().unwrap())).unwrap();
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 Ok(res);
can_read = false;
can_write = false;
}else {
can_read = true;
let auth_code = http_auth_rst.unwrap();
if auth_code == 2 {
can_write = true;
Expand All @@ -360,6 +391,13 @@ async fn connect_handle(request: hyper::Request<hyper::Body>) -> Result<hyper::R
}
}

// 认证失败,且不是用户API,跳转登录页面
if !can_read {
if !is_users_api(url_path) {
return Ok(rout_to_login());
}
}

// 升级ws协议
if hyper_tungstenite::is_upgrade_request(&request) {
if url_path == "/watch_log" {
Expand Down Expand Up @@ -421,12 +459,22 @@ async fn connect_handle(request: hyper::Request<hyper::Body>) -> Result<hyper::R
}else {
// 处理HTTP协议
if url_path == "/" {

// 没有读权限不允许访问主页
if !can_read {
return Ok(rout_to_login());
}
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("/index.html"));
return Ok(res);
}
if url_path == "/readme.html" {

// 没有读权限不允许访问帮助
if !can_read {
return Ok(rout_to_login());
}
let app_dir = cq_get_app_directory1().unwrap();
let path = PathBuf::from(&app_dir);
let path = path.join("webui");
Expand All @@ -441,6 +489,10 @@ async fn connect_handle(request: hyper::Request<hyper::Body>) -> Result<hyper::R
res.headers_mut().insert("Content-Type", HeaderValue::from_static("text/html; charset=utf-8"));
return Ok(res);
}else if url_path == "/favicon.ico" {
// 没有读权限不允许访问图标
if !can_read {
return Ok(rout_to_login());
}
let app_dir = cq_get_app_directory1().unwrap();
let path = PathBuf::from(&app_dir);
let path = path.join("webui");
Expand All @@ -451,8 +503,12 @@ async fn connect_handle(request: hyper::Request<hyper::Body>) -> Result<hyper::R
res.headers_mut().insert("Content-Type", HeaderValue::from_static("image/x-icon"));
return Ok(res);
} else if !url_path.contains(".") || url_path.starts_with("/user") {
return deal_api(request,can_write).await;
return deal_api(request,can_write,can_read).await;
} else {
// 没有读权限不允许访问文件
if !can_read {
return Ok(rout_to_login());
}
return deal_file(request).await;
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/redlang/exfun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2369,6 +2369,7 @@ def red_out(sw):
}
Ok(Some("".to_string()))
});

add_fun(vec!["内存使用"],|_self_t,_params|{
let s = <sysinfo::System as sysinfo::SystemExt>::new_all();
if let Some(process) = sysinfo::SystemExt::process(&s, sysinfo::Pid::from(std::process::id() as usize)) {
Expand All @@ -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 = <sysinfo::System as sysinfo::SystemExt>::new_all();
std::thread::sleep(<sysinfo::System as sysinfo::SystemExt>::MINIMUM_CPU_UPDATE_INTERVAL);
Expand Down
4 changes: 4 additions & 0 deletions src/redlang/webexfun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit af5fc9e

Please sign in to comment.