1
1
mod web_ip_checker;
2
+ mod bind;
2
3
3
4
use std:: fs:: File ;
4
- use std:: hash:: Hash ;
5
- use std:: io:: { BufRead , BufReader , Read , Write } ;
5
+ use std:: io:: { Read , Write } ;
6
6
use std:: net:: Ipv6Addr ;
7
7
use regex:: Regex ;
8
- use ipnet:: { Ipv4Net , Ipv6Net } ;
8
+ use ipnet:: { Ipv6Net } ;
9
9
10
10
use std:: str:: FromStr ;
11
11
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
- }
22
12
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 ! [ ] ;
25
16
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
26
23
let args: Vec < _ > = std:: env:: args ( ) . collect ( ) ;
27
24
28
25
if args. len ( ) < 2 {
@@ -39,6 +36,8 @@ fn main() {
39
36
let address_from_config = get_address_from_config ( & config, & hostname, & prefix_size) ;
40
37
let address_from_web = web_ip_checker:: MyIp :: get_ipv6_address ( & prefix_size) ;
41
38
39
+
40
+
42
41
compare_prefixes ( & address_from_config, & address_from_web) ;
43
42
println ! ( "prefixes differ from each other. Start to update config now!\n " ) ;
44
43
@@ -47,35 +46,6 @@ fn main() {
47
46
address_from_config. network( ) . to_string( ) ,
48
47
address_from_web. network( ) . to_string( )
49
48
) ;
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
- }
79
49
}
80
50
fn compare_prefixes ( address_from_config : & Ipv6Net , address_from_web : & Ipv6Net ) {
81
51
if address_from_config. network ( ) == address_from_web. network ( ) {
@@ -101,8 +71,9 @@ fn get_address_from_config(config: &String, hostname: &String, netmask: &u8) ->
101
71
host_record = host_record. strip_prefix ( & prefix) . unwrap ( ) . to_string ( ) ;
102
72
return Ipv6Net :: new ( Ipv6Addr :: from_str ( host_record. as_str ( ) ) . unwrap ( ) , * netmask) . unwrap ( ) ;
103
73
}
74
+
104
75
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!" ) ;
106
77
let mut contents = String :: new ( ) ;
107
78
file. read_to_string ( & mut contents) . expect ( "Can't read file!" ) ;
108
79
@@ -112,15 +83,17 @@ fn read_config(config_path: &String) -> String {
112
83
fn update_config ( config : & String , address_from_config : & Ipv6Net , address_from_web : & Ipv6Net ) -> String {
113
84
return config. replace (
114
85
& 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 ( ) ,
116
87
) ;
117
88
}
89
+
118
90
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)
120
92
. expect ( "Wasn't able to create file!" ) ;
121
93
122
94
let _ = file. write_all ( config. as_ref ( ) ) . expect ( "config couldn't be written" ) ;
123
95
}
96
+
124
97
fn help ( ) {
125
98
println ! ( "To function this tool needs some arguments:\n \
126
99
- record-name = the name of a record that has the desired prefix\
0 commit comments