Skip to content

Commit

Permalink
Update log_x.rs and main.rs files
Browse files Browse the repository at this point in the history
  • Loading branch information
arloor committed Apr 29, 2024
1 parent dfec9e5 commit e2eb749
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 22 deletions.
72 changes: 72 additions & 0 deletions a/log/nat.log_rCURRENT.
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
2024-04-29 14:12:53 [INFO] [src/log_x.rs:42] log is output to /repo/nftables-nat-rust/a/log/nat.log
2024-04-29 14:12:53 [INFO] [src/main.rs:24] kernel ip_forward config enabled!

2024-04-29 14:12:53 [INFO] [src/main.rs:67] nftables脚本如下:
#!/usr/sbin/nft -f

add table ip nat
delete table ip nat
add table ip nat
add chain nat PREROUTING { type nat hook prerouting priority -100 ; }
add chain nat POSTROUTING { type nat hook postrouting priority 100 ; }

# 测试1
# Single { src_port: 10000, dst_port: 443, dst_domain: "baidu.com", protocol: Tcp }
add rule ip nat PREROUTING tcp dport 10000 counter dnat to 110.242.68.66:443
#add rule ip nat PREROUTING udp dport 10000 counter dnat to 110.242.68.66:443
add rule ip nat POSTROUTING ip daddr 110.242.68.66 tcp dport 443 counter snat to 10.0.4.15
#add rule ip nat POSTROUTING ip daddr 110.242.68.66 udp dport 443 counter snat to 10.0.4.15

# 测试2
# Range { port_start: 1000, port_end: 2000, dst_domain: "baidu.com", protocol: All }
add rule ip nat PREROUTING tcp dport 1000-2000 counter dnat to 110.242.68.66:1000-2000
add rule ip nat PREROUTING udp dport 1000-2000 counter dnat to 110.242.68.66:1000-2000
add rule ip nat POSTROUTING ip daddr 110.242.68.66 tcp dport 1000-2000 counter snat to 10.0.4.15
add rule ip nat POSTROUTING ip daddr 110.242.68.66 udp dport 1000-2000 counter snat to 10.0.4.15

#测试3
# Single { src_port: 2222, dst_port: 22, dst_domain: "localhost", protocol: All }
add rule ip nat PREROUTING tcp dport 2222 redirect to :22
add rule ip nat PREROUTING udp dport 2222 redirect to :22


2024-04-29 14:12:53 [INFO] [src/main.rs:80] 执行/usr/sbin/nft -f /etc/nftables/nat-diy.nft
执行结果: exit status: 0
2024-04-29 14:12:53 [INFO] [src/main.rs:90] WAIT:等待配置或目标IP发生改变....

2024-04-29 14:13:43 [INFO] [src/log_x.rs:42] log is output to /repo/nftables-nat-rust/a/log/nat.log
2024-04-29 14:13:43 [INFO] [src/main.rs:24] kernel ip_forward config enabled!

2024-04-29 14:13:43 [INFO] [src/main.rs:67] nftables脚本如下:
#!/usr/sbin/nft -f

add table ip nat
delete table ip nat
add table ip nat
add chain nat PREROUTING { type nat hook prerouting priority -100 ; }
add chain nat POSTROUTING { type nat hook postrouting priority 100 ; }

# 测试1
# Single { src_port: 10000, dst_port: 443, dst_domain: "baidu.com", protocol: Tcp }
add rule ip nat PREROUTING tcp dport 10000 counter dnat to 110.242.68.66:443
#add rule ip nat PREROUTING udp dport 10000 counter dnat to 110.242.68.66:443
add rule ip nat POSTROUTING ip daddr 110.242.68.66 tcp dport 443 counter snat to 10.0.4.15
#add rule ip nat POSTROUTING ip daddr 110.242.68.66 udp dport 443 counter snat to 10.0.4.15

# 测试2
# Range { port_start: 1000, port_end: 2000, dst_domain: "baidu.com", protocol: All }
add rule ip nat PREROUTING tcp dport 1000-2000 counter dnat to 39.156.66.10:1000-2000
add rule ip nat PREROUTING udp dport 1000-2000 counter dnat to 39.156.66.10:1000-2000
add rule ip nat POSTROUTING ip daddr 39.156.66.10 tcp dport 1000-2000 counter snat to 10.0.4.15
add rule ip nat POSTROUTING ip daddr 39.156.66.10 udp dport 1000-2000 counter snat to 10.0.4.15

#测试3
# Single { src_port: 2222, dst_port: 22, dst_domain: "localhost", protocol: All }
add rule ip nat PREROUTING tcp dport 2222 redirect to :22
add rule ip nat PREROUTING udp dport 2222 redirect to :22


2024-04-29 14:13:43 [INFO] [src/main.rs:80] 执行/usr/sbin/nft -f /etc/nftables/nat-diy.nft
执行结果: exit status: 0
2024-04-29 14:13:43 [INFO] [src/main.rs:90] WAIT:等待配置或目标IP发生改变....

57 changes: 38 additions & 19 deletions src/log_x.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
use flexi_logger::{Duplicate, Criterion, Naming, Cleanup, Logger, FileSpec, DeferredNow};
use log::Record;
use std::{fs, io, path::PathBuf};

pub fn init_log(log_dir: &str, log_file: &str) {
Logger::try_with_env_or_str("info").unwrap()
.log_to_file(FileSpec::default()
.directory(log_dir)
.basename(log_file)
.suffix(""))
use flexi_logger::{
Cleanup, Criterion, DeferredNow, Duplicate, FileSpec, FlexiLoggerError, Logger, LoggerHandle,
Naming,
};
use log::{info, Record};

pub fn init_log(log_dir: &str, log_file: &str) -> Result<LoggerHandle, FlexiLoggerError> {
// 转换成绝对路径
let log_dir_path = PathBuf::from(log_dir);
if !log_dir_path.exists() {
fs::create_dir_all(log_dir_path.clone())?;
}
let log_dir_path=log_dir_path.canonicalize()?;
let log_dir = log_dir_path.as_path().to_str().ok_or(io::Error::new(
io::ErrorKind::InvalidInput,
"error parse absolute path of log dir",
))?;
let logger = if cfg!(debug_assertions) {
Logger::try_with_env_or_str("debug")?
} else {
Logger::try_with_env_or_str("info")?
};
let log = logger
.log_to_file(
FileSpec::default()
.directory(log_dir)
.basename(log_file)
.suffix(""),
)
.duplicate_to_stdout(Duplicate::All)
.rotate(
Criterion::Size(10_000_000), // 例如, 每 10MB 切割
Expand All @@ -15,13 +37,10 @@ pub fn init_log(log_dir: &str, log_file: &str) {
)
.append()
.format(my_format)
.create_symlink(if log_dir.starts_with('/'){
format!("{}/{}", log_dir, log_file)
}else{
log_file.to_string()
})
.start()
.unwrap();
.create_symlink(format!("{}/{}", log_dir, log_file))
.start();
info!("log is output to {}/{}", log_dir, log_file);
log
}

fn my_format(
Expand All @@ -31,12 +50,12 @@ fn my_format(
) -> Result<(), std::io::Error> {
write!(
w,
"{} [{}] {}",
"{} [{}] [{}:{}] {}",
// now.format("%Y-%m-%d %H:%M:%S%.6f"),
now.format("%Y-%m-%d %H:%M:%S"),
record.level(),
// record.file().unwrap_or("<unnamed>"),
// record.line().unwrap_or(0),
record.file().unwrap_or("<unnamed>"),
record.line().unwrap_or(0),
&record.args()
)
}
}
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use std::{env, io};
const NFTABLES_ETC: &str = "/etc/nftables";
const IP_FORWARD: &str = "/proc/sys/net/ipv4/ip_forward";

fn main() {
log_x::init_log("log", "nat.log");
fn main() -> Result<(), Box<dyn std::error::Error>>{
log_x::init_log("a/log", "nat.log")?;

let _ = std::fs::create_dir_all(NFTABLES_ETC);
// 修改内核参数,开启端口转发
Expand All @@ -37,7 +37,7 @@ fn main() {
let conf = "nat.conf".to_string();
info!("{}{}", "使用方式:nat ", conf);
config::example(&conf);
return;
return Ok(());
} else {
conf += &args[1];
}
Expand Down

0 comments on commit e2eb749

Please sign in to comment.