Skip to content

Commit

Permalink
feat(config): add switch for every proxy
Browse files Browse the repository at this point in the history
use config param —— enable
  • Loading branch information
lighkLife committed May 31, 2022
1 parent b7e8b29 commit ab38e5c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
22 changes: 15 additions & 7 deletions src/bin/rs-proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use clap::Parser;
use std::fs::File;
use std::io::{Read, Write};
use std::path::Path;
use anyhow::{ Result};
use std::rc::Rc;
use anyhow::{Context, Result};

use log::LevelFilter;
use rs_proxy::{ProxyService, RsProxyArgs, RsProxyConfig};
Expand All @@ -20,27 +21,34 @@ listen = 21883
target = "127.0.0.1:1883"
"#;

fn main() {
fn main() -> Result<()> {
env_logger::builder().filter_level(LevelFilter::Debug).init();

let args: RsProxyArgs = RsProxyArgs::parse();
let config_value = read_config(args.config).unwrap();
let config_value = read_config(args.config)
.context("read config file failed.")?;
debug!("rs-proxy config: \n{}", config_value);
info!("rs-proxy starting... ");
let rs_proxy_config: RsProxyConfig = toml::from_str(&config_value).unwrap();
let rs_proxy_config: RsProxyConfig = toml::from_str(&config_value)
.context("invalid config file.")?;
let mut handles = Vec::new();
match rs_proxy_config.proxy {
Some(proxy_vec) => {
for proxy in proxy_vec {
let service = ProxyService::new(proxy.name, proxy.listen, proxy.target).unwrap();
handles.push(service.run());
let proxy_rf = Rc::new(proxy);
if proxy_rf.enable != Some(false) {
let service = ProxyService::new(proxy_rf.clone())
.context(format!("invalid value in {:?}", proxy_rf))?;
handles.push(service.run());
}
}
}
None => info!("rs-proxy exit with empty config.")
};
for handle in handles {
handle.join().expect("exit.");
let _ = handle.join().expect("exit.");
}
Ok(())
}

fn read_config(config_path: Option<String>) -> Result<String> {
Expand Down
3 changes: 2 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ pub struct RsProxyConfig
}

#[derive(Deserialize)]
#[derive(Debug)]
#[derive(Debug, )]
pub struct ProxyConfig {
pub enable: Option<bool>,
pub name: String,
pub listen: u16,
pub target: String,
Expand Down
10 changes: 6 additions & 4 deletions src/proxy_service.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use std::net::{Ipv4Addr, Shutdown, SocketAddrV4, TcpListener, TcpStream};
use std::str::FromStr;
use std::{io, thread};
use std::rc::Rc;
use std::sync::Arc;
use std::thread::JoinHandle;

use anyhow::{Result};
use thread::spawn;
use log::{error, info};
use crate::ProxyConfig;

pub struct ProxyService {
name: Arc<String>,
Expand All @@ -16,11 +18,11 @@ pub struct ProxyService {


impl ProxyService {
pub fn new(name: String, listen: u16, target: String) -> Result<ProxyService> {
let listen_socket = SocketAddrV4::new(Ipv4Addr::from_str("0.0.0.0")?, listen);
let target_socket = SocketAddrV4::from_str(target.as_str())?;
pub fn new(config: Rc<ProxyConfig>) -> Result<ProxyService> {
let listen_socket = SocketAddrV4::new(Ipv4Addr::from_str("0.0.0.0")?, config.listen);
let target_socket = SocketAddrV4::from_str(config.target.as_str())?;
Ok(ProxyService {
name: Arc::new(name),
name: Arc::new(config.name.clone()),
listen: Arc::new(listen_socket),
target: Arc::new(target_socket),
})
Expand Down

0 comments on commit ab38e5c

Please sign in to comment.