Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
修反向ws的鉴权与self_id网络头
Browse files Browse the repository at this point in the history
  • Loading branch information
super1207 committed Jul 6, 2023
1 parent 82ce7c2 commit d8029de
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ reverse_uri:反向http和反向websocket需要这个,若不需要反向http

反向ws

反向 http
反向 http (暂时不支持X-Signature)

## API

Expand Down
2 changes: 1 addition & 1 deletion src/kook_onebot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1189,7 +1189,7 @@ impl KookOnebot {
"retcode":0,
"data": {
"app_name":"kook-onebot",
"app_version":"0.0.5",
"app_version":"0.0.6",
"protocol_version":"v11"
},
"echo":echo
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async fn connect_handle(request: hyper::Request<hyper::Body>) -> Result<hyper::R
let is_pass = onebot_http::check_auth(&request).await?;

if is_pass == false {
log::error!("WS鉴权失败!");
log::error!("WS或HTTP鉴权失败!");
let mut res = hyper::Response::new(hyper::Body::from(vec![]));
*res.status_mut() = hyper::StatusCode::NOT_FOUND;
return Ok(res);
Expand Down Expand Up @@ -71,7 +71,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
.init();


log::warn!("欢迎使用KookOnebot by super1207!!! v0.0.5");
log::warn!("欢迎使用KookOnebot by super1207!!! v0.0.6");

log::warn!("开源地址:https://github.com/super1207/KookOneBot");

Expand Down
3 changes: 2 additions & 1 deletion src/onebot_http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub async fn check_auth(request: &hyper::Request<hyper::Body>) -> Result<bool, B
let mut is_pass = false;
let g_access_token = G_ACCESS_TOKEN.read().await.clone();
let headers_map = request.headers();
println!("headers_map:{headers_map:?}");
if !g_access_token.is_empty() {
{
let access_token:String;
Expand All @@ -91,7 +92,7 @@ pub async fn check_auth(request: &hyper::Request<hyper::Body>) -> Result<bool, B
else {
access_token = "".to_owned();
}
if access_token == "Bear ".to_owned() + &g_access_token {
if access_token == "Bearer ".to_owned() + &g_access_token {
is_pass = true;
}
}
Expand Down
16 changes: 13 additions & 3 deletions src/onebot_ws_rev.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::sync::{Arc, atomic::AtomicI64};
use std::{sync::{Arc, atomic::AtomicI64}, str::FromStr};

use futures_util::{StreamExt, SinkExt};
use hyper::http::{HeaderValue, HeaderName};

use crate::{G_REVERSE_URL, G_ONEBOT_RX, G_KOOK_TOKEN, G_SELF_ID};
use crate::{G_REVERSE_URL, G_ONEBOT_RX, G_KOOK_TOKEN, G_SELF_ID, G_ACCESS_TOKEN};


// 反向ws
Expand Down Expand Up @@ -117,7 +118,16 @@ async fn deal_ws2(url:&str,

async fn onebot_rev_ws(ws_url:String) {
loop {
let rst = tokio_tungstenite::connect_async(ws_url.clone()).await;
let mut request = tungstenite::client::IntoClientRequest::into_client_request(ws_url.clone()).unwrap();
// 反向ws鉴权
let g_access_token = G_ACCESS_TOKEN.read().await.clone();
if g_access_token != "" {
request.headers_mut().insert("Authorization", HeaderValue::from_str(&format!("Bearer {}",g_access_token)).unwrap());
}
let self_id = G_SELF_ID.read().await;
request.headers_mut().append(HeaderName::from_str("X-Self-ID").unwrap(), HeaderValue::from_str(&self_id.to_string()).unwrap());
request.headers_mut().append(HeaderName::from_str("X-Client-Role").unwrap(), HeaderValue::from_str("Universal").unwrap());
let rst = tokio_tungstenite::connect_async(request).await;
if rst.is_err() {
log::error!("连接到WS_REV:{ws_url} 失败");
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
Expand Down

0 comments on commit d8029de

Please sign in to comment.