made input's more safe

This commit is contained in:
deadvey 2025-01-12 23:20:30 +00:00
parent 88200eb354
commit 2aa4a82af9

View File

@ -3,6 +3,13 @@ use std::io::{stdin,stdout,Write};
use colored::Colorize; use colored::Colorize;
use regex::Regex; use regex::Regex;
struct Url {
protocol: String,
hostname: String,
port: u16,
path: String,
}
fn clear_screen() { fn clear_screen() {
Command::new("clear") Command::new("clear")
.status() .status()
@ -132,7 +139,7 @@ fn parse_markdown(page_content: String) -> (String, Vec<String>) {
return (parsed_page_content, links); return (parsed_page_content, links);
} }
fn fetch_page(host: &String, port: &String, path: &String) -> String { fn fetch_page(host: &String, port: u16, path: &String) -> String {
let full_url_formatted = format!("{}:{}/{}", host, port, path); let full_url_formatted = format!("{}:{}/{}", host, port, path);
// Call curl using Com, mand // Call curl using Com, mand
@ -152,9 +159,9 @@ fn fetch_page(host: &String, port: &String, path: &String) -> String {
} }
} }
fn render_page(host: String, port: String, path: String) -> Vec<String> { fn render_page(host: String, port: u16, path: String) -> Vec<String> {
clear_screen(); clear_screen();
let mut content = fetch_page(&host, &port, &path); let mut content = fetch_page(&host, port, &path);
let mut links = Vec::new(); let mut links = Vec::new();
let (screen_width, screen_height) = termion::terminal_size().unwrap(); let (screen_width, screen_height) = termion::terminal_size().unwrap();
@ -190,125 +197,142 @@ fn input() -> String{
return s; return s;
} }
fn parse_url(url: String, previous_host: &String) -> (String, String, String, String) { fn parse_url(user_input: String, previous_host: &String) -> Result<Url, u8> {
let mut host: String = previous_host.to_string(); let mut url = Url {
let mut port: String = "3477".to_string(); protocol: "internal".to_string(),
let mut path: String = "/".to_string(); hostname: "home".to_string(),
let mut protocol: String = "mttp".to_string(); port: 0,
path: "/".to_string(),
};
let mttp_regex = Regex::new(r"^mttp:\/\/(.*?)\/(.*?)$").unwrap(); // mttp://example.com/index.md let mttp_regex = Regex::new(r"^mttp:\/\/(.*?)\/(.*?)$").unwrap(); // mttp://example.com/index.md
let mttp_regex_no_path = Regex::new(r"^mttp:\/\/(.*?)$").unwrap(); // mttp://example.com let mttp_regex_no_path = Regex::new(r"^mttp:\/\/(.*?)$").unwrap(); // mttp://example.com
let accept_this_as_mttp = Regex::new(r"^(.*?)$").unwrap(); // example.com let accept_this_as_mttp = Regex::new(r"^(.*?)\.(.*?)$").unwrap(); // example.com
let no_protocol_but_path = Regex::new(r"^(.*?)/(.*?)$").unwrap(); // example.com/index.md let no_protocol_but_path = Regex::new(r"^(.*?)/(.*?)$").unwrap(); // example.com/index.md
let path_change = Regex::new(r"^/(.*?)$").unwrap(); // /index.md let path_change = Regex::new(r"^/(.*?)$").unwrap(); // /index.md
let http_regex = Regex::new(r"^http:\/\/(.*?)\/(.*?)$").unwrap(); // http://example.com/index.md let http_regex = Regex::new(r"^http:\/\/(.*?)\/(.*?)$").unwrap(); // http://example.com/index.md
let http_regex_no_path = Regex::new(r"^http:\/\/(.*?)$").unwrap(); // http://example.com let http_regex_no_path = Regex::new(r"^http:\/\/(.*?)$").unwrap(); // http://example.com
if let Some(caps) = mttp_regex.captures(&url) { if let Some(caps) = mttp_regex.captures(&user_input) {
host = caps[1].to_string(); url.hostname = caps[1].to_string();
port = "3477".to_string(); url.port = 3477;
path = caps[2].to_string(); url.path = caps[2].to_string();
protocol = "mttp".to_string(); url.protocol = "mttp".to_string();
Ok(url)
} }
else if let Some(caps) = mttp_regex_no_path.captures(&url) { else if let Some(caps) = mttp_regex_no_path.captures(&user_input) {
host = caps[1].to_string(); url.hostname = caps[1].to_string();
port = "3477".to_string(); url.port = 3477;
path = "/".to_string(); url.path = "/".to_string();
protocol = "mttp".to_string(); url.protocol = "mttp".to_string();
Ok(url)
} }
else if let Some(caps) = path_change.captures(&url) { else if let Some(caps) = path_change.captures(&user_input) {
host = previous_host.to_string(); url.hostname = previous_host.to_string();
port = "3477".to_string(); url.port = 3477;
path = caps[1].to_string(); url.path = caps[1].to_string();
protocol = "mttp".to_string(); url.protocol = "mttp".to_string();
Ok(url)
} }
else if let Some(caps) = no_protocol_but_path.captures(&url) { else if let Some(caps) = no_protocol_but_path.captures(&user_input) {
host = caps[1].to_string(); url.hostname = caps[1].to_string();
port = "3477".to_string(); url.port = 3477;
path = caps[2].to_string(); url.path = caps[2].to_string();
protocol = "mttp".to_string(); url.protocol = "mttp".to_string();
Ok(url)
} }
else if let Some(caps) = accept_this_as_mttp.captures(&url) { else if let Some(caps) = http_regex.captures(&user_input) {
host = caps[1].to_string(); url.hostname = caps[1].to_string();
port = "3477".to_string(); url.port = 80;
path = "/".to_string(); url.path = caps[2].to_string();
protocol = "mttp".to_string(); url.protocol = "http".to_string();
Ok(url)
} }
else if let Some(caps) = http_regex.captures(&url) { else if let Some(caps) = http_regex_no_path.captures(&user_input) {
host = caps[1].to_string(); url.hostname = caps[1].to_string();
port = "80".to_string(); url.port = 80;
path = caps[2].to_string(); url.path = "/".to_string();
protocol = "http".to_string(); url.protocol = "http".to_string();
Ok(url)
} }
else if let Some(caps) = http_regex_no_path.captures(&url) { else if let Some(caps) = accept_this_as_mttp.captures(&user_input) {
host = caps[1].to_string(); url.hostname = format!("{}{}{}",caps[1].to_string(),".",caps[2].to_string());
port = "80".to_string(); url.port = 3477;
path = "/".to_string(); url.path = "/".to_string();
protocol = "http".to_string(); url.protocol = "mttp".to_string();
Ok(url)
}
else {
Err(1)
} }
println!("{}://{}:{}/{}",protocol,host,port,path);
return (host, port, path, protocol);
} }
fn main() { fn main() {
clear_screen(); clear_screen();
println!("Enter a url: "); println!("Enter a url: ");
let url = input(); let user_input = input();
if url == "q" {
std::process::exit(0);
}
let (mut host, mut port, mut path, mut protocol) = parse_url(url, &"example.com".to_string()); // Must pass if user_input == "q" {
// 'previous std::process::exit(0);
// host' }
let mut load_page: bool = true; let mut load_page: bool = true;
let mut links: Vec<String> = Vec::new(); let mut history: Vec<Url> = Vec::new();
let mut links: Vec<String> = Vec::new();
'mainloop: loop { if let Ok(url) = parse_url(user_input, &"example.com".to_string()) {
if load_page { 'mainloop: loop {
links = Vec::new(); if load_page {
links = render_page(host.clone(), port.clone(), path.clone()); history.push(Url {
protocol: url.protocol.clone(),
hostname: url.hostname.clone(),
port: url.port.clone(),
path: url.path.clone(),
});
links = Vec::new();
links = render_page(url.hostname.clone(), url.port.clone(), url.path.clone());
println!("Enter reference number to follow, h for help, or q to quit "); println!("Enter reference number to follow, h for help, or q to quit ");
} }
load_page = true; load_page = false;
let link_to_follow = input(); let user_input = input();
if link_to_follow == "q" { if user_input == "q" {
break 'mainloop; break 'mainloop;
} }
else if link_to_follow == "r" { else if user_input == "r" {
continue; load_page = true;
} continue;
else if link_to_follow == "h" { }
println!("Source code: https://git.javalsai.dynv6.net/deadvey/markdown-webbrowser\nq: quit\nh: help\nr: reload\ni: visit root index of this host eg: root index of mttp://deadvey.com/blog/4.md is just deadvey.com\nb: go back in history\nox: print the hyprlink of reference x eg: o5 or o24 else if user_input == "h" {
"); println!("Source code: https://git.javalsai.dynv6.net/deadvey/markdown-webbrowser\nq: quit\nh: help\nr: reload\ni: visit root index of this host eg: root index of mttp://deadvey.com/blog/4.md is just deadvey.com\nb: go back in history\nox: print the hyprlink of reference x eg: o5 or o24
load_page = false; ");
} }
else if link_to_follow.chars().nth(0).unwrap() == 'o' { else if user_input.chars().nth(0).unwrap() == 'o' {
let number_str = &link_to_follow[1..]; let number_str = &user_input[1..];
if let Ok(number) = number_str.parse::<usize>() { if let Ok(number) = number_str.parse::<usize>() {
println!("{}", links[number]); println!("{}", links[number]);
} else { } else {
println!("error"); println!("error");
} }
load_page = false; }
} else if let Ok(number) = user_input.parse::<usize>() {
else { if number < links.len() {
if let Ok(number) = link_to_follow.parse::<usize>() { let url = parse_url(links[number].clone(), &url.hostname).expect("Error parsing URL");
if number < links.len() { load_page = true;
(host, port, path, protocol) = parse_url(links[number].clone(), &host); } else {
} else { println!("Invalid reference id");
println!("Invalid reference id"); }
load_page = false; }
} else if let Ok(url) = parse_url(user_input, &url.hostname) {
} else { load_page = true;
println!("Invalid input"); }
load_page = false; else {
} println!("Invalid input");
} }
} }
}
else {
println!("Invalid url");
}
} }