-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #126 from weibocom/dev
Dev
- Loading branch information
Showing
48 changed files
with
1,737 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
## defaults for _all_ profiles | ||
[default] | ||
address = "0.0.0.0" | ||
port = 8000 | ||
workers = 8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
[package] | ||
name = "api" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
context = { path = "../context" } | ||
metrics = { path = "../metrics" } | ||
|
||
log = "0.4.14" | ||
tokio = {version = "1.17.0", features = ["fs"]} | ||
|
||
trust-dns-resolver = "0.20.0" | ||
|
||
# rocket for restful api | ||
rocket = { version = "0.5.0-rc.2", features = ["json"] } | ||
lazy_static = "1.4.0" | ||
|
||
# for serde json | ||
serde = { version = "1.0.126", features = ["derive"] } | ||
serde_derive = "1.0.126" | ||
serde_json = "1.0.65" | ||
|
||
redis = { version = "0.17.0", features = ["tokio-comp"] } | ||
memcache = "*" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
mod meta; | ||
pub mod props; | ||
mod protocol; | ||
|
||
use std::{collections::HashSet, time::Duration}; | ||
|
||
use trust_dns_resolver::{AsyncResolver, TokioConnection, TokioConnectionProvider, TokioHandle}; | ||
type Resolver = AsyncResolver<TokioConnection, TokioConnectionProvider>; | ||
|
||
use rocket::{Build, Rocket}; | ||
|
||
#[macro_use] | ||
extern crate rocket; | ||
|
||
#[macro_use] | ||
extern crate lazy_static; | ||
|
||
use metrics::Path; | ||
|
||
const API_PATH: &str = "api"; | ||
|
||
// 整合所有routers | ||
pub fn routes() -> Rocket<Build> { | ||
let mut rocket = rocket::build(); | ||
|
||
// 元数据相关routes | ||
rocket = meta::routes(rocket); | ||
|
||
// 各种协议 cmd相关routes | ||
protocol::routes(rocket) | ||
} | ||
|
||
// 定期刷新白名单域名 | ||
pub async fn start_whitelist_refresh(host: String) { | ||
let resolver: Resolver = | ||
AsyncResolver::from_system_conf(TokioHandle).expect("crate api dns resolver"); | ||
|
||
// 每10分钟刷新一次 | ||
let mut tick = tokio::time::interval(Duration::from_secs(10 * 60)); | ||
loop { | ||
tick.tick().await; | ||
|
||
let mut whitelist = HashSet::with_capacity(2); | ||
match resolver.lookup_ip(host.clone()).await { | ||
Ok(ips) => { | ||
for ip in ips.iter() { | ||
whitelist.insert(ip.to_string()); | ||
} | ||
} | ||
Err(err) => { | ||
log::warn!("api - parse whitelist host {} failed: {:?}", host, err); | ||
} | ||
} | ||
if whitelist.len() > 0 { | ||
// 合法域名时,同时将localhost加入,支持本地访问 | ||
whitelist.insert("127.0.0.1".to_string()); | ||
props::update_whitelist(whitelist); | ||
} | ||
} | ||
} | ||
|
||
// 统计 | ||
fn qps_incr(name: &'static str) { | ||
let mut opts = Path::new(vec![API_PATH]).qps(name); | ||
opts += 1; | ||
} | ||
|
||
// 校验client,并统计接口qps, 当前只检查ip白名单 | ||
fn verify_client(client_ip: &String, api_name: &'static str) -> bool { | ||
// 统计qps | ||
qps_incr(api_name); | ||
|
||
// 检查白名单 | ||
if props::is_in_whitelist(client_ip) { | ||
return true; | ||
} | ||
log::info!("api - found illegal user: {}", client_ip); | ||
false | ||
} |
Oops, something went wrong.