准备开始解析一下获取到的 file list
This commit is contained in:
parent
9632ff96e7
commit
710d419df3
1
.gitignore
vendored
1
.gitignore
vendored
@ -14,3 +14,4 @@ Cargo.lock
|
||||
*.pdb
|
||||
|
||||
.idea/
|
||||
config.toml
|
||||
|
@ -12,11 +12,13 @@ default = []
|
||||
|
||||
[dependencies]
|
||||
|
||||
reqwest = { version = "0.11.23", features = ["json"] }
|
||||
axum = "0.7.4"
|
||||
tokio = { version = "1.35.1", features = ["full"] }
|
||||
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
toml = "0.8.8"
|
||||
|
||||
md-5 = "0.10.6"
|
||||
sha1 = "0.10.6"
|
||||
|
126
src/cluster.rs
Normal file
126
src/cluster.rs
Normal file
@ -0,0 +1,126 @@
|
||||
use crate::config::{ClusterByoc, Config};
|
||||
use crate::PROTOCOL_VERSION;
|
||||
|
||||
use reqwest::{Client, StatusCode};
|
||||
|
||||
pub struct Cluster {
|
||||
pub config: Config,
|
||||
}
|
||||
|
||||
impl Cluster {
|
||||
/// this.ua = `openbmclapi-cluster/${version}`
|
||||
/// this.got = got.extend({
|
||||
/// prefixUrl: this.prefixUrl,
|
||||
/// username: this.clusterId,
|
||||
/// password: this.clusterSecret,
|
||||
/// headers: {
|
||||
/// 'user-agent': this.ua,
|
||||
/// },
|
||||
/// responseType: 'buffer',
|
||||
/// timeout: ms('1m'),
|
||||
/// })
|
||||
/// public async getFileList(): Promise<IFileList> {
|
||||
/// const FileListSchema = avsc.Type.forSchema({
|
||||
/// type: 'array',
|
||||
/// items: {
|
||||
/// type: 'record',
|
||||
/// fields: [
|
||||
/// {name: 'path', type: 'string'},
|
||||
/// {name: 'hash', type: 'string'},
|
||||
/// {name: 'size', type: 'long'},
|
||||
/// ],
|
||||
/// } as schema.RecordType,
|
||||
/// })
|
||||
/// const res = await this.got.get('openbmclapi/files', {
|
||||
/// responseType: 'buffer',
|
||||
/// cache: this.requestCache,
|
||||
/// })
|
||||
/// const decompressed = await decompress(res.body)
|
||||
/// return {
|
||||
/// files: FileListSchema.fromBuffer(Buffer.from(decompressed)),
|
||||
/// }
|
||||
/// }
|
||||
pub async fn get_file_list(&self) {
|
||||
// server: https://openbmclapi.bangbang93.com
|
||||
// path: /openbmclapi/files
|
||||
let url = format!(
|
||||
"{}://{}/openbmclapi/files",
|
||||
self.config.cluster_byoc.to_string(),
|
||||
self.config.center_url.clone()
|
||||
);
|
||||
let ua = format!("openbmclapi-cluster/{}", PROTOCOL_VERSION);
|
||||
let password = self.config.cluster_secret.clone();
|
||||
let username = self.config.cluster_id.to_string();
|
||||
let client = Client::builder().user_agent(ua).build().unwrap();
|
||||
let res = client
|
||||
.get(url)
|
||||
.basic_auth(username, Some(password))
|
||||
.timeout(std::time::Duration::from_secs(60))
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
match res.status() {
|
||||
StatusCode::OK => {
|
||||
let body = res.bytes().await.unwrap();
|
||||
println!("get file list body! len: {}", body.len());
|
||||
let cur = std::io::Cursor::new(body);
|
||||
let data = zstd::decode_all(cur);
|
||||
match data {
|
||||
Ok(data) => {
|
||||
println!("file list decompressed! len: {}", data.len());
|
||||
}
|
||||
Err(e) => {
|
||||
panic!("file list decompress faild e:{:?}", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
panic!("error status: {:?}", res.status());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
use serde::Deserialize;
|
||||
#[derive(Deserialize)]
|
||||
struct TestConfig {
|
||||
pub cluster_port: u32,
|
||||
pub cluster_id: String,
|
||||
pub cluster_secret: String,
|
||||
}
|
||||
|
||||
fn gen_config() -> Config {
|
||||
let center_url = "openbmclapi.bangbang93.com".to_string();
|
||||
// 读取 config.toml 获得
|
||||
let raw_config = std::fs::read_to_string("config.toml").unwrap();
|
||||
let test_conf: TestConfig = toml::from_str(raw_config.as_str()).unwrap();
|
||||
|
||||
let cluster_byoc = ClusterByoc::https;
|
||||
let no_demaon = false;
|
||||
let disable_access_log = false;
|
||||
let force_noopen = false;
|
||||
let enable_nginx = false;
|
||||
Config::new(
|
||||
center_url,
|
||||
test_conf.cluster_port,
|
||||
test_conf.cluster_id,
|
||||
test_conf.cluster_secret,
|
||||
cluster_byoc,
|
||||
no_demaon,
|
||||
disable_access_log,
|
||||
force_noopen,
|
||||
enable_nginx,
|
||||
)
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_get_file_list() {
|
||||
let config = gen_config();
|
||||
let cluster = Cluster { config };
|
||||
cluster.get_file_list().await;
|
||||
}
|
||||
}
|
@ -1,14 +1,23 @@
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum ClusterByoc {
|
||||
http,
|
||||
https,
|
||||
}
|
||||
|
||||
impl ClusterByoc {
|
||||
pub fn to_string(&self) -> String {
|
||||
match self {
|
||||
ClusterByoc::http => "http".to_string(),
|
||||
ClusterByoc::https => "https".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct Config {
|
||||
pub center_url: String,
|
||||
pub cluster_port: u32,
|
||||
pub cluster_id: u32,
|
||||
pub cluster_id: String,
|
||||
pub cluster_secret: String,
|
||||
/// CLUSTER_BYOC
|
||||
pub cluster_byoc: ClusterByoc,
|
||||
@ -22,18 +31,40 @@ pub struct Config {
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn new(
|
||||
center_url: String,
|
||||
cluster_port: u32,
|
||||
cluster_id: String,
|
||||
cluster_secret: String,
|
||||
cluster_byoc: ClusterByoc,
|
||||
no_demaon: bool,
|
||||
disable_access_log: bool,
|
||||
force_noopen: bool,
|
||||
enable_nginx: bool,
|
||||
) -> Self {
|
||||
Self {
|
||||
center_url,
|
||||
cluster_port,
|
||||
cluster_id,
|
||||
cluster_secret,
|
||||
cluster_byoc,
|
||||
no_demaon,
|
||||
disable_access_log,
|
||||
force_noopen,
|
||||
enable_nginx,
|
||||
}
|
||||
}
|
||||
pub fn new_from_env() -> Self {
|
||||
let center_url =
|
||||
std::env::var("CENTER_URL").unwrap_or("openbmclapi.bangbang93.com".to_string());
|
||||
let cluster_port = std::env::var("CLUSTER_PORT")
|
||||
.unwrap_or_else(|_| "8080".to_string())
|
||||
.unwrap_or("8080".to_string())
|
||||
.parse::<u32>()
|
||||
.expect("CLUSTER_PORT must be a number");
|
||||
let cluster_id = std::env::var("CLUSTER_ID")
|
||||
.unwrap_or_else(|_| "1".to_string())
|
||||
.parse::<u32>()
|
||||
.expect("CLUSTER_ID must be a number");
|
||||
let cluster_id = std::env::var("CLUSTER_ID").unwrap_or("1".to_string());
|
||||
let cluster_secret = std::env::var("CLUSTER_SECRET").expect("CLUSTER_SECRET must be set");
|
||||
let cluster_byoc = match std::env::var("CLUSTER_BYOC")
|
||||
.unwrap_or_else(|_| "http".to_string())
|
||||
.unwrap_or("http".to_string())
|
||||
.as_str()
|
||||
{
|
||||
"http" => ClusterByoc::http,
|
||||
@ -41,22 +72,23 @@ impl Config {
|
||||
_ => panic!("CLUSTER_BYOC must be http or https"),
|
||||
};
|
||||
let no_demaon = std::env::var("NO_DAEMON")
|
||||
.unwrap_or_else(|_| "false".to_string())
|
||||
.unwrap_or("false".to_string())
|
||||
.parse::<bool>()
|
||||
.expect("NO_DAEMON must be true or false");
|
||||
let disable_access_log = std::env::var("DISABLE_ACCESS_LOG")
|
||||
.unwrap_or_else(|_| "false".to_string())
|
||||
.unwrap_or("false".to_string())
|
||||
.parse::<bool>()
|
||||
.expect("DISABLE_ACCESS_LOG must be true or false");
|
||||
let force_noopen = std::env::var("FORCE_NOOPEN")
|
||||
.unwrap_or_else(|_| "false".to_string())
|
||||
.unwrap_or("false".to_string())
|
||||
.parse::<bool>()
|
||||
.expect("FORCE_NOOPEN must be true or false");
|
||||
let enable_nginx = std::env::var("ENABLE_NGINX")
|
||||
.unwrap_or_else(|_| "false".to_string())
|
||||
.unwrap_or("false".to_string())
|
||||
.parse::<bool>()
|
||||
.expect("ENABLE_NGINX must be true or false");
|
||||
Self {
|
||||
center_url,
|
||||
cluster_port,
|
||||
cluster_id,
|
||||
cluster_secret,
|
||||
|
@ -1,5 +1,9 @@
|
||||
mod cluster;
|
||||
mod config;
|
||||
mod log;
|
||||
mod serve;
|
||||
mod utils;
|
||||
|
||||
pub const PROTOCOL_VERSION: &str = "1.6.11";
|
||||
|
||||
fn main() {}
|
||||
|
14
src/serve.rs
14
src/serve.rs
@ -1,4 +1,9 @@
|
||||
use axum::{extract::Path, http::header::HeaderMap, http::StatusCode, response::Response};
|
||||
use axum::{
|
||||
extract::Path,
|
||||
http::header::HeaderMap,
|
||||
http::StatusCode,
|
||||
response::{IntoResponse, Response},
|
||||
};
|
||||
|
||||
/// ```ts
|
||||
/// import express from 'express'
|
||||
@ -22,7 +27,7 @@ use axum::{extract::Path, http::header::HeaderMap, http::StatusCode, response::R
|
||||
///
|
||||
/// export default MeasureRoute
|
||||
/// ```
|
||||
async fn measure(header: HeaderMap, Path(size): Path<u32>) -> Response<Vec<u8>> {
|
||||
pub async fn measure(header: HeaderMap, Path(size): Path<u32>) -> Response<Vec<u8>> {
|
||||
let mut data: Vec<u8> = Vec::new();
|
||||
match header.get("x-openbmclapi-secret") {
|
||||
Some(secret) => {
|
||||
@ -54,3 +59,8 @@ async fn measure(header: HeaderMap, Path(size): Path<u32>) -> Response<Vec<u8>>
|
||||
.unwrap(),
|
||||
}
|
||||
}
|
||||
|
||||
/// 返回文件的请求函数
|
||||
pub async fn get_file() -> impl IntoResponse {
|
||||
todo!();
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
use md5::{Digest, Md5};
|
||||
use sha1::{Digest as Sha1Digest, Sha1};
|
||||
use sha1::Sha1;
|
||||
use std::path::PathBuf;
|
||||
|
||||
/// import {join} from 'path'
|
||||
|
Loading…
Reference in New Issue
Block a user