Skip to content

Commit

Permalink
Rust agent v2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Adamanr committed Aug 14, 2022
0 parents commit f16feaa
Show file tree
Hide file tree
Showing 13 changed files with 244 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .idea/untitled.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "rust_agent"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[profile.dev]
panic = 'abort'


[profile.release]
opt-level="z"
strip = true
lto = true
codegen-units = 1
debug = 0


[dependencies]
reqwest = {version = "0.11.11", features = ["json"]}
serde_json = "1.0.81"
serde = {version = "1.0.137", features = ["derive"] }
tokio-tun = {version = "0.5.3"}
tokio = { version = "1.19.2", features = ["full"] }
Empty file added config/config.json
Empty file.
81 changes: 81 additions & 0 deletions src/assets/assets.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
pub mod assets{
use std::net::{IpAddr, Ipv4Addr};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio;
use tokio::net::{TcpStream};
use crate::assets::tun::tun::tun::create_tun;
use crate::common::netutil::netutil::netutil::get_ipv4;
use crate::Config;
use std::net::Ipv4Addr;
use std::os::unix::io::AsRawFd;
use tokio_tun::{Tun, TunBuilder};

pub async fn start_client(config: Config) {
let tuns = create_tun(&config);
let mut buf0 = [0u8; 1500];
let (mut reader, mut writer) = tokio::io::split(tuns);
loop {
let n = reader.read(&mut buf0).await.expect("Error read");
let b = &buf0[..n];
println!("reading {} bytes from tuns: {:?}", buf0.len(), b);
let ipv4 = IpAddr::V4(Ipv4Addr::new(b[0],b[1],b[2],b[3]));
println!("IPv4 {}",ipv4);
if !IpAddr::is_ipv4(&ipv4){
continue
}
let (src_ipv4,dst_ipv4) = get_ipv4();
if src_ipv4 == " " || dst_ipv4 == " " {
continue
}
let addr = config.server_addr;
println!("########8");
tokio::spawn(async move {
println!("#########9");
let mut buffer = [0u8; 1500];
loop {
let mut stream = TcpStream::connect(addr.as_str()).await.expect("Error stream");
println!("##########10");
let n = stream.read(&mut buffer).await.expect("Error read");
buffer.map(|e|{
print!("{}",e.to_string());
});
println!("\n###########11");
let b = &buffer[..n];
println!("{:?}",std::str::from_utf8(b));
println!("############12");
writer.write(&b).await.expect("");
}
}).await.expect("Error spawn");
break
}
}


pub fn create_tun(_config: &Config) -> Tun{
let tuns = TunBuilder::new()
.name("")
.tap(false)
.packet_info(false)
.mtu(1350)
.up()
.address(Ipv4Addr::new(0, 0, 0, 0))
.destination(Ipv4Addr::new(0, 0, 0, 0))
.broadcast(Ipv4Addr::BROADCAST)
.netmask(Ipv4Addr::new(255, 255, 255, 0))
.try_build()
.expect("Error tun");
println!(
"┌ name: {}\n├ fd: {} \n├ mtu: {}\n├ flags: {}\n├ address: {}\n├ destination: {}\n├ broadcast: {}\n└ netmask: {}",
tuns.name(),
tuns.as_raw_fd(),
tuns.mtu().unwrap(),
tuns.flags().unwrap(),
tuns.address().unwrap(),
tuns.destination().unwrap(),
tuns.broadcast().unwrap(),
tuns.netmask().unwrap(),
);
return tuns
}

}
1 change: 1 addition & 0 deletions src/assets/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod assets;
76 changes: 76 additions & 0 deletions src/common/common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
pub mod common{
use std::{fs};
use std::collections::HashMap;
use std::fs::{File};
use std::io::{Read};
use serde::{Deserialize,Serialize};
use std::path::{Path, PathBuf};

const CONFIG_DIR_NAME: &'static str = "config";

#[derive(Serialize, Deserialize, Debug)]
pub struct Config {
pub app_id: String,
pub get_host_uri: String,
pub cam_id: String,
pub local_addr: String,
pub server_addr: String,
pub cidr: String,
pub key: String,
pub obfuscate: bool,
}

impl Config{
pub fn init(self){
let json_config = serde_json::to_string(&self).unwrap();
println!("{}",json_config);
}

pub async fn get_cidr(&mut self) {
let mut body = HashMap::new();
body.insert("objectId",&self.cam_id);
let client = reqwest::Client::builder()
.build().expect("Error build client");
let resp = client.post(self.get_host_uri.as_str())
.json(&body)
.header("Content-Type","application/json")
.header("application/json",self.app_id.as_str())
.send()
.await
.expect("Error response");
if resp.content_length().unwrap() > 0 && resp.status().is_server_error() {
let t = resp.text().await.unwrap();
self.cidr = t + "/16";
}
}
}

pub fn get_ipv4() -> (String,String){
return (String::from("b"), String::from("a"))
}

pub async fn get_config() -> Config {
let mut data = String::new();
File::open(get_config_file_name())
.unwrap()
.read_to_string(&mut data)
.expect("Error read files json");
let f:serde_json::Value = serde_json::from_str(data.as_str()).unwrap();
let b = Config::deserialize(f).expect("Error deserialize");
return b
}

pub fn check_path(ex_path: &Path) {
let config_dir_path = Path::join(ex_path, CONFIG_DIR_NAME);
if config_dir_path.exists() != true {
fs::create_dir_all(config_dir_path).unwrap();
}
}

pub fn get_config_file_name() -> PathBuf {
let ex = Path::new("./");
check_path(ex);
let us = Path::join(CONFIG_DIR_NAME.as_ref(), "config.json");
us
}
}
1 change: 1 addition & 0 deletions src/common/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod common;
19 changes: 19 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
extern crate core;

mod assets;
mod common;

use std::thread::sleep;
use std::time::Duration;
use crate::assets::tcp::tcp_client::tcp_c::start_client;
use crate::common::config::config::config::{Config, get_config};

#[tokio::main]
async fn main() {
println!("---- PREPARE ----");
sleep(Duration::from_secs(1));
println!("---- START ----");
let f = get_config().await;
start_client(f).await;

}

0 comments on commit f16feaa

Please sign in to comment.