Skip to content

Commit

Permalink
Add static_root and fix escape
Browse files Browse the repository at this point in the history
Signed-off-by: owent <[email protected]>
  • Loading branch information
owent committed May 19, 2023
1 parent 3a41aa9 commit c1d9697
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 54 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CHANGELOG
============

v0.10.1
----------

1. 修复回包的espace问题
2. 增加 `static_root` 选项,以便用于设置静态文件

v0.10.0
----------

Expand Down
57 changes: 56 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 3 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ license = "MIT OR Apache-2.0"
name = "wxwork_robotd"
readme = "README.md"
repository = "https://github.com/owent/wxwork_robotd"
version = "0.10.0"
version = "0.10.1"

[[bin]]
name = "wxwork_robotd"
Expand All @@ -49,10 +49,8 @@ cbc = ">=0.1.2"
md-5 = ">=0.10.0"
cipher = { version = ">=0.4.0", features = ["alloc"] }
# https://github.com/RustCrypto

[dependencies.actix-web]
features = ["rustls"]
version = "^4.3.0"
actix-web = { version = "^4.3.0", features = ["rustls"] }
actix-files = { version = ">=0.6.2" }

[dependencies.awc]
features = ["rustls"]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
```javascript
{
"listen": ["0.0.0.0:12019", ":::12019"], // 监听列表,这里配置了ipv4和ipv6地址
"taskTimeout": 4000, // 超时时间4000ms,企业微信要求在5秒内回应,这里容忍1秒钟的网络延迟
"task_timeout": 4000, // 超时时间4000ms,企业微信要求在5秒内回应,这里容忍1秒钟的网络延迟
"workers": 8, // 工作线程数
"backlog": 256, // 建立连接的排队长度
"keep_alive": 5, // tcp保持连接的心跳间隔(秒) (版本: >=0.6.0)
Expand Down
1 change: 1 addition & 0 deletions etc/conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"max_connection_per_worker": 20480,
"max_concurrent_rate_per_worker": 256,
"payload_size_limit": 262144,
"static_root": ".",
"cmds": {
"default": {
"type": "echo",
Expand Down
16 changes: 15 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fs::{create_dir_all, OpenOptions};
use std::io::Write;
use std::net::ToSocketAddrs;
use std::path::Path;
use std::path::{Path, PathBuf};
use std::process;
use std::rc::Rc;
use std::str::FromStr;
Expand All @@ -25,6 +25,7 @@ pub struct AppConfigure {
pub max_connection_per_worker: usize,
pub max_concurrent_rate_per_worker: usize,
pub payload_size_limit: usize,
pub static_root: Option<PathBuf>,
}

#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -75,6 +76,7 @@ static mut APP_ENV_INFO_STORE: AppEnvironmentInfo = AppEnvironmentInfo {
max_connection_per_worker: 20480,
max_concurrent_rate_per_worker: 256,
payload_size_limit: 262144, // 256KB
static_root: None,
},
};

Expand Down Expand Up @@ -621,6 +623,18 @@ impl AppEnvironment {
}
}

if let Some(x) = kvs.get("static_root") {
if let Some(v) = x.as_str() {
if !v.is_empty() {
if let Ok(root_path) = PathBuf::from_str(v) {
unsafe {
APP_ENV_INFO_STORE.conf.static_root = Some(root_path);
}
}
}
}
}

{
let mut hosts = Vec::new();
if let Some(x) = kvs.get("listen") {
Expand Down
1 change: 1 addition & 0 deletions src/handles/robot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub async fn dispatch_robot_request(
if let Ok(x) = web::Query::<WxWorkRobotPostMessage>::from_query(req.query_string()) {
return dispatch_robot_message(app, Arc::new(project_name), x.into_inner(), body).await;
}

make_robot_error_response_future("parameter error.")
}

Expand Down
31 changes: 21 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ static GLOBAL: System = System;
// crates
#[macro_use]
extern crate clap;
extern crate actix_files;
extern crate actix_web;
extern crate awc;
extern crate futures;
Expand Down Expand Up @@ -37,7 +38,9 @@ extern crate tokio;

// import packages
// use std::sync::Arc;
use crate::actix_files::Files;
use actix_web::{middleware::Logger, web, App, HttpServer};

use std::io;
use std::net::TcpListener;
use std::time::Duration;
Expand Down Expand Up @@ -78,21 +81,29 @@ async fn main() -> io::Result<()> {
let reg_move_default = app_env;
let reg_move_robot = app_env;

app
let app = app
// ====== register for index ======
.service(
web::resource(app_env.prefix.to_string())
.app_data(web::PayloadConfig::default().limit(app_env.conf.payload_size_limit))
.to(move |req| handles::default::dispatch_default_index(reg_move_default, req)),
)
// ====== register for project ======
.service(
web::resource(format!("{}{{project}}/", app_env.prefix).as_str())
.app_data(web::PayloadConfig::default().limit(app_env.conf.payload_size_limit))
.to(move |req, body| {
handles::robot::dispatch_robot_request(reg_move_robot, req, body)
}),
)
);

// ====== register for static files ======
let app = if let Some(static_root) = app_env.conf.static_root.as_ref() {
app.service(Files::new("/", static_root).show_files_listing())
} else {
app
};

// ====== register for project ======
app.service(
web::resource(format!("{}{{project}}/", app_env.prefix).as_str())
.app_data(web::PayloadConfig::default().limit(app_env.conf.payload_size_limit))
.to(move |req, body| {
handles::robot::dispatch_robot_request(reg_move_robot, req, body)
}),
)
// app_env.setup(app)
});

Expand Down
52 changes: 16 additions & 36 deletions src/wxwork_robot/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,9 +595,7 @@ pub fn pack_text_message(msg: WxWorkMessageTextRsp) -> Result<String, String> {
.write_event(Event::Start(BytesStart::new("Content")))
.is_ok()
{
let _ = writer.write_event(Event::CData(BytesCData::new(
quick_xml::escape::escape(msg.content.as_str()),
)));
let _ = writer.write_event(Event::CData(BytesCData::new(msg.content)));
let _ = writer.write_event(Event::End(BytesEnd::new("Content")));
}

Expand All @@ -610,9 +608,7 @@ pub fn pack_text_message(msg: WxWorkMessageTextRsp) -> Result<String, String> {
.write_event(Event::Start(BytesStart::new("Item")))
.is_ok()
{
let _ = writer.write_event(Event::CData(BytesCData::new(
quick_xml::escape::escape(v.as_str()),
)));
let _ = writer.write_event(Event::CData(BytesCData::new(v)));
let _ = writer.write_event(Event::End(BytesEnd::new("Item")));
}
}
Expand All @@ -628,9 +624,7 @@ pub fn pack_text_message(msg: WxWorkMessageTextRsp) -> Result<String, String> {
.write_event(Event::Start(BytesStart::new("Item")))
.is_ok()
{
let _ = writer.write_event(Event::CData(BytesCData::new(
quick_xml::escape::escape(v.to_string().as_str()),
)));
let _ = writer.write_event(Event::CData(BytesCData::new(v.to_string())));
let _ = writer.write_event(Event::End(BytesEnd::new("Item")));
}
}
Expand Down Expand Up @@ -673,9 +667,7 @@ pub fn pack_markdown_message(msg: WxWorkMessageMarkdownRsp) -> Result<String, St
.is_ok()
{
// BytesText::from_escaped_str
let _ = writer.write_event(Event::CData(BytesCData::new(
quick_xml::escape::escape(msg.content.as_str()),
)));
let _ = writer.write_event(Event::CData(BytesCData::new(msg.content)));
let _ = writer.write_event(Event::End(BytesEnd::new("Content")));
}

Expand All @@ -702,9 +694,7 @@ pub fn pack_image_message(msg: WxWorkMessageImageRsp) -> Result<String, String>
.write_event(Event::Start(BytesStart::new("MsgType")))
.is_ok()
{
let _ = writer.write_event(Event::CData(BytesCData::new(quick_xml::escape::escape(
"image",
))));
let _ = writer.write_event(Event::CData(BytesCData::new("image")));
let _ = writer.write_event(Event::End(BytesEnd::new("MsgType")));
}

Expand All @@ -717,14 +707,12 @@ pub fn pack_image_message(msg: WxWorkMessageImageRsp) -> Result<String, String>
.is_ok()
{
// BytesText::from_escaped_str
let _ =
writer.write_event(Event::CData(BytesCData::new(quick_xml::escape::escape(
match base64::STANDARD.encode(&msg.content) {
Ok(x) => x,
Err(e) => e.message,
}
.as_str(),
))));
let _ = writer.write_event(Event::CData(BytesCData::new(
match base64::STANDARD.encode(&msg.content) {
Ok(x) => x,
Err(e) => e.message,
},
)));
let _ = writer.write_event(Event::End(BytesEnd::new("Base64")));
}

Expand All @@ -737,7 +725,7 @@ pub fn pack_image_message(msg: WxWorkMessageImageRsp) -> Result<String, String>
{
// BytesText::from_escaped_str
let _ = writer.write_event(Event::CData(BytesCData::new(
quick_xml::escape::escape(hex::encode(hasher.finalize().as_slice()).as_str()),
hex::encode(hasher.finalize().as_slice()).as_str(),
)));
let _ = writer.write_event(Event::End(BytesEnd::new("Md5")));
}
Expand Down Expand Up @@ -769,19 +757,15 @@ pub fn pack_message_response(
.write_event(Event::Start(BytesStart::new("Encrypt")))
.is_ok()
{
let _ = writer.write_event(Event::CData(BytesCData::new(quick_xml::escape::escape(
encrypt.as_str(),
))));
let _ = writer.write_event(Event::CData(BytesCData::new(encrypt.as_str())));
let _ = writer.write_event(Event::End(BytesEnd::new("Encrypt")));
}

if writer
.write_event(Event::Start(BytesStart::new("MsgSignature")))
.is_ok()
{
let _ = writer.write_event(Event::CData(BytesCData::new(quick_xml::escape::escape(
msg_signature.as_str(),
))));
let _ = writer.write_event(Event::CData(BytesCData::new(msg_signature.as_str())));
let _ = writer.write_event(Event::End(BytesEnd::new("MsgSignature")));
}

Expand All @@ -797,9 +781,7 @@ pub fn pack_message_response(
.write_event(Event::Start(BytesStart::new("Nonce")))
.is_ok()
{
let _ = writer.write_event(Event::CData(BytesCData::new(quick_xml::escape::escape(
nonce.as_str(),
))));
let _ = writer.write_event(Event::CData(BytesCData::new(nonce.as_str())));
let _ = writer.write_event(Event::End(BytesEnd::new("Nonce")));
}

Expand Down Expand Up @@ -830,9 +812,7 @@ pub fn get_robot_response_access_deny_content(msg: &str) -> String {
.write_event(Event::Start(BytesStart::new("message")))
.is_ok()
{
let _ = writer.write_event(Event::CData(BytesCData::new(quick_xml::escape::escape(
msg,
))));
let _ = writer.write_event(Event::CData(BytesCData::new(msg)));
let _ = writer.write_event(Event::End(BytesEnd::new("message")));
}

Expand Down

0 comments on commit c1d9697

Please sign in to comment.