feat: updated config parser to new scheme
This commit is contained in:
parent
5502f21605
commit
07f9ceb12f
2 changed files with 169 additions and 29 deletions
|
|
@ -1,6 +1,6 @@
|
|||
use crate::config::Config;
|
||||
use maxminddb::{Reader, geoip2};
|
||||
use serde::Deserialize;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::net::IpAddr;
|
||||
|
||||
// For now only MMDB because i cant found .proto schemes of
|
||||
|
|
@ -9,34 +9,61 @@ use std::net::IpAddr;
|
|||
|
||||
/// Interface enum for `dst_addr` info
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
|
||||
#[serde(try_from = "String", into = "String")]
|
||||
pub enum RouteType {
|
||||
/// GeoSite type, like `category-ads-all`
|
||||
GeoSite(String),
|
||||
/// Result with GeoCode like "RU"
|
||||
GeoIp(String),
|
||||
Clearnet,
|
||||
// String because enum will used as interface in result of `route_packet`.
|
||||
}
|
||||
|
||||
impl TryFrom<String> for RouteType {
|
||||
type Error = String;
|
||||
|
||||
fn try_from(value: String) -> Result<Self, Self::Error> {
|
||||
if value == "clearnet" {
|
||||
return Ok(Self::Clearnet);
|
||||
}
|
||||
match value.split_once(':') {
|
||||
Some(("geosite", name)) if !name.is_empty() => Ok(Self::GeoSite(name.to_owned())),
|
||||
Some(("geoip", name)) if !name.is_empty() => Ok(Self::GeoIp(name.to_owned())),
|
||||
_ => Err(format!("invalid rule target: {value}")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<RouteType> for String {
|
||||
fn from(target: RouteType) -> Self {
|
||||
match target {
|
||||
RouteType::GeoSite(name) => format!("geosite:{name}"),
|
||||
RouteType::GeoIp(name) => format!("geoip:{name}"),
|
||||
RouteType::Clearnet => "clearnet".to_owned(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Routing actions
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum RouteAction {
|
||||
#[serde(alias = "block")]
|
||||
Block,
|
||||
#[serde(alias = "proxy")]
|
||||
Proxy,
|
||||
#[serde(alias = "direct")]
|
||||
Direct,
|
||||
}
|
||||
|
||||
pub type Rules = Vec<Rule>;
|
||||
|
||||
/// Type for deserializing the routing rules like:
|
||||
#[derive(serde::Deserialize)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
|
||||
pub struct Rule {
|
||||
pub target: RouteType,
|
||||
pub action: RouteAction,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub proxy_name: Option<String>,
|
||||
}
|
||||
|
||||
pub struct GeoIpService {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue