Skip to content

Commit 9fda946

Browse files
committed
refactor: add new section to test the new version
1 parent feba339 commit 9fda946

File tree

1 file changed

+20
-47
lines changed

1 file changed

+20
-47
lines changed

src/main.rs

Lines changed: 20 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
mod web_ip_checker;
2+
mod bind;
23

34
use std::fs::File;
4-
use std::hash::Hash;
5-
use std::io::{BufRead, BufReader, Read, Write};
5+
use std::io::{Read, Write};
66
use std::net::Ipv6Addr;
77
use regex::Regex;
8-
use ipnet::{Ipv4Net, Ipv6Net};
8+
use ipnet::{Ipv6Net};
99

1010
use std::str::FromStr;
1111

12-
enum RecordType {
13-
A(Ipv4Net),
14-
AAAA(Ipv6Net),
15-
CNAME(String),
16-
}
17-
struct BindRecord {
18-
hostname: String,
19-
record_type: String,
20-
record: RecordType,
21-
}
2212
fn main() {
23-
let filepath = "src/db.maximizzar.io";
24-
read(filepath);
13+
//new program
14+
let prefix_size: u8 = 56;
15+
let mut ipv6_addresses: Vec<Ipv6Net> = vec![];
2516

17+
bind::get_ipv6_addresses_from_config("src/db.maximizzar.io", &mut ipv6_addresses, &prefix_size);
18+
for ipv6_address in ipv6_addresses {
19+
println!("{}", ipv6_address);
20+
}
21+
22+
// old program
2623
let args: Vec<_> = std::env::args().collect();
2724

2825
if args.len() < 2 {
@@ -39,6 +36,8 @@ fn main() {
3936
let address_from_config = get_address_from_config(&config, &hostname, &prefix_size);
4037
let address_from_web = web_ip_checker::MyIp::get_ipv6_address(&prefix_size);
4138

39+
40+
4241
compare_prefixes(&address_from_config, &address_from_web);
4342
println!("prefixes differ from each other. Start to update config now!\n");
4443

@@ -47,35 +46,6 @@ fn main() {
4746
address_from_config.network().to_string(),
4847
address_from_web.network().to_string()
4948
);
50-
51-
}
52-
fn read(filepath: &'static str) {
53-
let bind_db_file = File::open(filepath)
54-
.expect(&*format!("Bind DB File under {} not found.", filepath));
55-
let reader = BufReader::new(bind_db_file);
56-
57-
for line in reader.lines() {
58-
let mut line_part = line.unwrap();
59-
60-
//for header in line_part.split_whitespace() {
61-
// print!("{}", &header);
62-
63-
// if header.contains(")") {
64-
// break;
65-
// }
66-
//}
67-
println!();
68-
69-
for lp in line_part.split_whitespace() {
70-
71-
if line_part.contains(" A ")
72-
|| line_part.contains(" AAAA ")
73-
|| line_part.contains(" CNAME ") {
74-
print!("{}", &lp);
75-
}
76-
77-
}
78-
}
7949
}
8050
fn compare_prefixes(address_from_config: &Ipv6Net, address_from_web: &Ipv6Net) {
8151
if address_from_config.network() == address_from_web.network() {
@@ -101,8 +71,9 @@ fn get_address_from_config(config: &String, hostname: &String, netmask: &u8) ->
10171
host_record = host_record.strip_prefix(&prefix).unwrap().to_string();
10272
return Ipv6Net::new(Ipv6Addr::from_str(host_record.as_str()).unwrap(), *netmask).unwrap();
10373
}
74+
10475
fn read_config(config_path: &String) -> String {
105-
let mut file = std::fs::File::open(config_path).expect("Can't open file!");
76+
let mut file = File::open(config_path).expect("Can't open file!");
10677
let mut contents = String::new();
10778
file.read_to_string(&mut contents).expect("Can't read file!");
10879

@@ -112,15 +83,17 @@ fn read_config(config_path: &String) -> String {
11283
fn update_config(config: &String, address_from_config: &Ipv6Net, address_from_web: &Ipv6Net) -> String {
11384
return config.replace(
11485
&address_from_config.network().to_string().strip_suffix("::").unwrap().to_string(),
115-
&address_from_web.network().to_string().strip_suffix("::").unwrap()
86+
&address_from_web.network().to_string().strip_suffix("::").unwrap(),
11687
);
11788
}
89+
11890
fn write_config(config_path: &String, config: String) {
119-
let mut file = std::fs::File::create(config_path)
91+
let mut file = File::create(config_path)
12092
.expect("Wasn't able to create file!");
12193

12294
let _ = file.write_all(config.as_ref()).expect("config couldn't be written");
12395
}
96+
12497
fn help() {
12598
println!("To function this tool needs some arguments:\n\
12699
- record-name = the name of a record that has the desired prefix\

0 commit comments

Comments
 (0)