Skip to content

Commit

Permalink
style: 🎨 rearrange to separate functionalities
Browse files Browse the repository at this point in the history
also update README
  • Loading branch information
louonezime committed Sep 25, 2023
1 parent 24b91de commit a1c9e1c
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 92 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ Virtual No Ads is a project aimed at providing a more streamlined and pleasant b

2) **Adblock software:** Virtual No Ads also provides a software component that can be installed on devices directly. This software functions as a traditional adblocker, filtering out ads as they are loaded on websites. The software is designed to be lightweight and easy to use, and can be customized to allow or block specific types of ads.

While Pi-hole offers an implementation using a Raspberry Pi that blocks ads, our project goes a step further by implementing the WireGuard protocol on top of it. While blocking ads, we also provide the security features of WireGuard, creating a simplified desktop application to enable or disable the VPN.

## How does it work?

[Explain how this project is working]
As we connect a Raspberry Pi to Wi-Fi, it will intercept packets using the WireGuard protocol while filtering these packets to remove or sort ads; eventually emitting a Wi-Fi signal. Users can connect to this Wi-Fi network with any device, ensuring a secure network (thanks to WireGuard) and ad-free/ filtered content.

## Getting Started

Expand Down
91 changes: 91 additions & 0 deletions src/block_ads.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
use std::fs::File;
use std::io;
use std::io::BufRead;
use std::net::IpAddr;
use std::collections::HashSet;

use pnet::datalink::{self, Channel};
use pnet::packet::Packet;
use pnet::packet::ethernet::{EthernetPacket, EtherTypes};
use pnet::packet::ipv4::Ipv4Packet;
use pnet::packet::tcp::TcpPacket;
use pnet::packet::ip::IpNextHeaderProtocols;

use dns_lookup::lookup_addr;

pub fn parse_adsfile(arguments: &Vec<String>, domains_list: &mut HashSet<String>) -> Result<(), ()>
{
for i in 2..arguments.len() {
let file = File::open(&arguments[i]);

let file = match file {
Ok(file) => file,
Err(error) => {
eprintln!("Error while opening ads_domains file: {error}");
return Err(());
}
};

let lines = io::BufReader::new(file).lines();

for line in lines {
match line {
Ok(content) => {
domains_list.insert(content);
}
Err(error) => {
eprintln!("Error while reading content of ads_domain file: {}", error);
return Err(());
}
}
}
}
Ok(())
}

pub fn catch_packets(interface_name: &str, blacklist: HashSet<String>)
{
let interfaces = datalink::interfaces();

let interface = interfaces
.into_iter()
.find(|iface| iface.name == interface_name)
.expect("Interface not found");

let (_tx, mut rx) = match datalink::channel(&interface, Default::default()) {
Ok(Channel::Ethernet(tx, rx)) => (tx, rx),
Ok(_) => panic!("Unknown channel type"),
Err(e) => panic!("Error opening channel: {}", e),
};

loop {
let packet = rx.next().unwrap();
let ethernet = EthernetPacket::new(packet).unwrap();

if ethernet.get_ethertype() == EtherTypes::Ipv4 {
let ipv4 = Ipv4Packet::new(ethernet.payload()).unwrap();

if ipv4.get_next_level_protocol() == IpNextHeaderProtocols::Tcp {
let tcp = TcpPacket::new(ipv4.payload()).unwrap();
let src_ipv4 = ipv4.get_source();
let src_ip = IpAddr::V4(src_ipv4);
let dst_ip = ipv4.get_destination();
let src_port = tcp.get_source();
let dst_port = tcp.get_destination();
let domain_name = lookup_addr(&src_ip).unwrap_or_else(|_| String::from("Unknown"));

if src_ipv4.is_loopback() || src_ipv4.is_link_local() {
continue;
}
if blacklist.contains(&domain_name) {
continue;
}
println!("Source IP: {}", src_ipv4);
println!("Destination IP: {}", dst_ip);
println!("Source Port: {}", src_port);
println!("Destination Port: {}", dst_port);
println!("Domain Name: {}", domain_name);
}
}
}
}
94 changes: 3 additions & 91 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
use std::fs::File;
use std::io;
use std::io::BufRead;
use std::process;
use std::net::IpAddr;
use std::collections::HashSet;
use std::env;

use pnet::datalink::{self, Channel};
use pnet::packet::Packet;
use pnet::packet::ethernet::{EthernetPacket, EtherTypes};
use pnet::packet::ipv4::Ipv4Packet;
use pnet::packet::tcp::TcpPacket;
use pnet::packet::ip::IpNextHeaderProtocols;

use dns_lookup::lookup_addr;
pub mod block_ads;

fn main()
{
Expand All @@ -25,87 +14,10 @@ fn main()
process::exit(84)
}

match parse_adsfile(&args, &mut blacklist_set) {
match block_ads::parse_adsfile(&args, &mut blacklist_set) {
Ok(_) => {},
Err(_) => process::exit(84),
};

catch_packets(&args[1], blacklist_set);
}

fn parse_adsfile(arguments: &Vec<String>, domains_list: &mut HashSet<String>) -> Result<(), ()>
{
for i in 2..arguments.len() {
let file = File::open(&arguments[i]);

let file = match file {
Ok(file) => file,
Err(error) => {
eprintln!("Error while opening ads_domains file: {error}");
return Err(());
}
};

let lines = io::BufReader::new(file).lines();

for line in lines {
match line {
Ok(content) => {
domains_list.insert(content);
}
Err(error) => {
eprintln!("Error while reading content of ads_domain file: {}", error);
return Err(());
}
}
}
}
Ok(())
}

fn catch_packets(interface_name: &str, blacklist: HashSet<String>)
{
let interfaces = datalink::interfaces();

let interface = interfaces
.into_iter()
.find(|iface| iface.name == interface_name)
.expect("Interface not found");

let (_tx, mut rx) = match datalink::channel(&interface, Default::default()) {
Ok(Channel::Ethernet(tx, rx)) => (tx, rx),
Ok(_) => panic!("Unknown channel type"),
Err(e) => panic!("Error opening channel: {}", e),
};

loop {
let packet = rx.next().unwrap();
let ethernet = EthernetPacket::new(packet).unwrap();

if ethernet.get_ethertype() == EtherTypes::Ipv4 {
let ipv4 = Ipv4Packet::new(ethernet.payload()).unwrap();

if ipv4.get_next_level_protocol() == IpNextHeaderProtocols::Tcp {
let tcp = TcpPacket::new(ipv4.payload()).unwrap();
let src_ipv4 = ipv4.get_source();
let src_ip = IpAddr::V4(src_ipv4);
let dst_ip = ipv4.get_destination();
let src_port = tcp.get_source();
let dst_port = tcp.get_destination();
let domain_name = lookup_addr(&src_ip).unwrap_or_else(|_| String::from("Unknown"));

if src_ipv4.is_loopback() || src_ipv4.is_link_local() {
continue;
}
if blacklist.contains(&domain_name) {
continue;
}
println!("Source IP: {}", src_ipv4);
println!("Destination IP: {}", dst_ip);
println!("Source Port: {}", src_port);
println!("Destination Port: {}", dst_port);
println!("Domain Name: {}", domain_name);
}
}
}
block_ads::catch_packets(&args[1], blacklist_set);
}

0 comments on commit a1c9e1c

Please sign in to comment.