Skip to content

Commit

Permalink
[+] support 'ip:host'
Browse files Browse the repository at this point in the history
  • Loading branch information
lebe-dev committed Feb 9, 2022
1 parent 4dfa04d commit 4aa1db7
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/apache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ pub mod apache {
}

fn get_apache_vhost_port_regex() -> Regex {
return Regex::new("(?:^|^[^#]+)<VirtualHost[\\s\t]+.*:(\\d+)>").unwrap();
return Regex::new("(?:^|^[^#]+)<VirtualHost[\\s\t]+.*:(?P<port>\\d+)>").unwrap();
}
}
2 changes: 1 addition & 1 deletion src/nginx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ pub mod nginx {
}

pub fn get_nginx_vhost_port_regex() -> Regex {
return Regex::new("^[\\s\t]*listen[\\s\t]+(\\d+)[\\s\t]*[ssl\\s|http2\\s]*;").unwrap();
return Regex::new("^[\\s\t]*listen[\\s\t]+(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}:)?(?P<port>\\d+)[\\s\t]*[ssl\\s|http2\\s]*;.*$").unwrap();
}
}
42 changes: 42 additions & 0 deletions src/nginx_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,48 @@ pub mod nginx_tests {
use crate::test_utils::test_utils::assert_vhost_in_vec;
use crate::webserver::webserver::get_virtual_hosts_from_file;

#[test]
fn support_ip_and_port() {
let section_start_regex = get_nginx_vhost_section_start_regex();
let redirect_with_301_regex = get_nginx_redirect_with_301_regex();
let port_search_regex = get_nginx_vhost_port_regex();
let domain_search_regex = get_domain_search_regex_for_nginx_vhost();

let vhost_file_path = Path::new("tests/nginx-vhosts/listen.conf");

match get_virtual_hosts_from_file(
vhost_file_path,
section_start_regex,
redirect_with_301_regex,
port_search_regex,
domain_search_regex,
) {
Ok(vhosts) => {
println!("{:?}", vhosts);
assert_eq!(vhosts.len(), 2);

let expected_domain1 = "qweqwe.ru";

let expected_vhost1 = VirtualHost {
domain: expected_domain1.to_string(),
port: 2345
};

assert_eq!(expected_vhost1.to_string(), vhosts.first().unwrap().to_string());

let expected_domain2 = "www.megatron2000.ru";

let expected_vhost2 = VirtualHost {
domain: expected_domain2.to_string(),
port: 443
};

assert_eq!(expected_vhost2.to_string(), vhosts.last().unwrap().to_string());
},
Err(_) => panic!("vhosts vec was expected")
}
}

#[test]
fn support_ssl_and_http2() {
let section_start_regex = get_nginx_vhost_section_start_regex();
Expand Down
12 changes: 11 additions & 1 deletion src/webserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ pub mod webserver {

if port.is_none() && port_search_pattern.is_match(&row) {
trace!("port wasn't detected yet, port pattern has been matched");
let vhost_port_str = get_first_group_match_as_string(
let vhost_port_str = find_group_with_port_value(
&row, &port_search_pattern
);

Expand Down Expand Up @@ -181,6 +181,16 @@ pub mod webserver {
String::from(&groups[1])
}

fn find_group_with_port_value(row: &str, pattern: &Regex) -> String {
let mut port = String::new();

for caps in pattern.captures_iter(&row) {
port = format!("{}", &caps["port"]);
}

return port
}

fn get_virtual_host(domain: Option<String>, port: Option<i32>) -> VirtualHost {
let domain_name = domain.unwrap();
VirtualHost {
Expand Down
28 changes: 28 additions & 0 deletions tests/nginx-vhosts/listen.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
server {
listen 89.23.23.23:2345 ssl http2;
server_name qweqwe.ru;
# return 301 https://www.megatron2000.ru$request_uri;
}

server {
listen 93.23.23.23:443;
server_name www.megatron2000.ru;
charset utf-8;
client_max_body_size 128M;
ssl_certificate ssl/www.megatron2000.ru.crt;
ssl_certificate_key ssl/www.megatron2000.ru.key;
ssl_dhparam ssl/dhparam.pem;
ssl_protocols TLSv1 TLSv1.2;
set $fpm_socket unix:/var/run/php/robot-www.megatron2000.ru-7.1.sock;
set $root_path /home/robot/www.megatron2000.ru/public_html/public;
disable_symlinks if_not_owner from=$root_path;
root $root_path;
index index.php;
gzip on;
gzip_comp_level 5;
gzip_disable "msie6";
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;
include vhosts-includes/robot/www.megatron2000.ru/*.conf;
access_log /home/robot/www.megatron2000.ru/logs/access.log main;
error_log /home/robot/www.megatron2000.ru/logs/error.log;
}

0 comments on commit 4aa1db7

Please sign in to comment.