initial commit :p

This commit is contained in:
raphy 2024-12-16 02:58:27 +01:00
commit ce65897102
Signed by: Wintermute
GPG Key ID: D85876B31F4275CF
11 changed files with 471 additions and 0 deletions

14
.cargo/config.toml Normal file
View File

@ -0,0 +1,14 @@
[target.xtensa-esp32-none-elf]
runner = "espflash flash --monitor"
[env]
ESP_LOG = "TRACE"
ESP_LOGLEVEL = "TRACE"
[build]
rustflags = ["-C", "link-arg=-nostartfiles"]
target = "xtensa-esp32-none-elf"
[unstable]
build-std = ["alloc", "core"]

17
.gitignore vendored Normal file
View File

@ -0,0 +1,17 @@
# Generated by Cargo
# will have compiled files and executables
debug/
target/
# These are backup files generated by rustfmt
**/*.rs.bk
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
# RustRover
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

17
.lazy.lua Normal file
View File

@ -0,0 +1,17 @@
return {
"williamboman/mason-lspconfig.nvim",
config = function()
local nvim_lsp = require("lspconfig")
nvim_lsp.rust_analyzer.setup({
settings = {
["rust-analyzer"] = {
cargo = {
allTargets = false,
target = "xtensa-esp32-none-elf",
},
},
},
})
end,
}

73
Cargo.toml Normal file
View File

@ -0,0 +1,73 @@
[package]
name = "evil_wifi"
version = "0.1.0"
edition = "2021"
[[bin]]
name = "evil_wifi"
[dependencies]
esp-backtrace = { version = "0.14.2", features = [
"esp32",
"exception-handler",
"panic-handler",
"println",
] }
esp-hal = { version = "0.22.0", features = ["esp32"] }
esp-println = { version = "0.12.0", features = ["esp32", "log"] }
log = { version = "0.4.21" }
esp-alloc = { version = "0.5.0" }
embedded-io = "0.6.1"
embedded-io-async = "0.6.1"
embassy-net = { version = "0.4.0", features = [
"tcp",
"udp",
"dhcpv4",
"medium-ethernet",
] }
esp-wifi = { version = "0.11.0", default-features = false, features = [
"esp32",
"utils",
"wifi",
"esp-alloc",
"log",
] }
heapless = { version = "0.8.0", default-features = false }
smoltcp = { version = "0.11.0", default-features = false, features = [
"medium-ethernet",
"proto-dhcpv4",
"proto-igmp",
"proto-ipv4",
"socket-dhcpv4",
"socket-icmp",
"socket-raw",
"socket-tcp",
"socket-udp",
] }
embassy-executor = { version = "0.6.0", features = ["nightly"] }
embassy-time = { version = "0.3.1", features = ["generic-queue-8"] }
esp-hal-embassy = { version = "0.5.0", features = ["esp32"] }
static_cell = { version = "2.1.0", features = ["nightly"] }
edge-nal = "0.3.0"
edge-dhcp = "0.3.0"
edge-mdns = "0.3.1"
edge-nal-embassy = "0.3.0"
edge-captive = "0.3.0"
ufmt = "0.2.0"
[profile.dev]
# Rust debug is too slow.
# For debug builds always builds with some optimization
opt-level = "s"
[profile.release]
codegen-units = 1 # LLVM can perform better optimizations using a single thread
debug = 2
debug-assertions = false
incremental = false
lto = 'fat'
opt-level = 's'
overflow-checks = false

3
build.rs Normal file
View File

@ -0,0 +1,3 @@
fn main() {
println!("cargo:rustc-link-arg-bins=-Tlinkall.x");
}

2
rust-toolchain.toml Normal file
View File

@ -0,0 +1,2 @@
[toolchain]
channel = "esp"

34
src/captive_portal.rs Normal file
View File

@ -0,0 +1,34 @@
use embassy_net::Stack;
use esp_wifi::wifi::{WifiApDevice, WifiDevice};
#[embassy_executor::task]
pub async fn captive_task(stack: &'static Stack<WifiDevice<'static, WifiApDevice>>) -> ! {
use edge_captive::io::run;
use edge_nal_embassy::{Udp, UdpBuffers};
use core::net::{Ipv4Addr, SocketAddrV4};
let mut tx_buf = [0; 1500];
let mut rx_buf = [0; 1500];
let buffers = UdpBuffers::<2, 1024, 1024, 10>::new();
let unbound_socket = Udp::new(stack, &buffers);
log::info!("Running Captive Portal DNS on UDP port 53...");
loop {
match run(
&unbound_socket,
core::net::SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, 53)),
&mut tx_buf,
&mut rx_buf,
core::net::Ipv4Addr::new(192, 168, 2, 1),
core::time::Duration::from_secs(60),
)
.await
{
Ok(()) => (),
Err(e) => log::warn!("{e:?}"),
}
}
}

54
src/dhcp.rs Normal file
View File

@ -0,0 +1,54 @@
use core::str::FromStr;
use embassy_net::Stack;
use embassy_time::{Duration, Timer};
use esp_wifi::wifi::{WifiApDevice, WifiDevice};
#[embassy_executor::task]
pub async fn run_dhcp(
stack: &'static Stack<WifiDevice<'static, WifiApDevice>>,
gw_ip_addr: &'static str,
) {
use core::net::{Ipv4Addr, SocketAddrV4};
use edge_dhcp::{
io::{self, DEFAULT_SERVER_PORT},
server::{Server, ServerOptions},
};
use edge_nal::UdpBind;
use edge_nal_embassy::{Udp, UdpBuffers};
let ip = Ipv4Addr::from_str(gw_ip_addr).expect("dhcp task failed to parse gw ip");
let mut buf = [0u8; 1500];
let mut gw_buf = [Ipv4Addr::UNSPECIFIED];
let buffers = UdpBuffers::<3, 1024, 1024, 10>::new();
let unbound_socket = Udp::new(stack, &buffers);
let mut bound_socket = unbound_socket
.bind(core::net::SocketAddr::V4(SocketAddrV4::new(
Ipv4Addr::UNSPECIFIED,
DEFAULT_SERVER_PORT,
)))
.await
.unwrap();
let mut opts = ServerOptions::new(ip, Some(&mut gw_buf));
let dns = [Ipv4Addr::from_str(&gw_ip_addr).unwrap()];
opts.dns = &dns;
loop {
_ = io::server::run(
&mut Server::<64>::new(ip),
&opts,
&mut bound_socket,
&mut buf,
)
.await
.inspect_err(|e| log::warn!("DHCP server error: {e:?}"));
Timer::after(Duration::from_millis(500)).await;
}
}

99
src/http.rs Normal file
View File

@ -0,0 +1,99 @@
use embassy_net::{tcp::TcpSocket, IpListenEndpoint, Stack};
use embassy_time::{with_timeout, Duration, Timer};
use esp_wifi::wifi::{WifiApDevice, WifiDevice};
#[embassy_executor::task]
pub async fn run_http(stack: &'static Stack<WifiDevice<'static, WifiApDevice>>) {
let mut rx_buffer = [0; 1536];
let mut tx_buffer = [0; 1536];
let mut connected_clients: u32 = 0;
// I would like a nicer solution for this
let mut buf: heapless::String<256> = heapless::String::new();
let mut socket = TcpSocket::new(&stack, &mut rx_buffer, &mut tx_buffer);
socket.set_timeout(Some(embassy_time::Duration::from_secs(1)));
'connection: loop {
log::info!("Wait for connection on HTTP");
let r = socket
.accept(IpListenEndpoint {
addr: None,
port: 80,
})
.await;
connected_clients += 1;
if let Err(e) = r {
log::error!("connect error: {:?}", e);
continue;
}
let mut buffer = [0u8; 1024];
let mut pos = 0;
'read: loop {
log::info!("Waiting for new data!");
buf.clear();
match with_timeout(Duration::from_secs(1), socket.read(&mut buffer)).await {
Ok(Ok(0)) => {
log::info!("read EOF");
break 'read;
}
Ok(Ok(len)) => {
let to_print =
unsafe { core::str::from_utf8_unchecked(&buffer[..(pos + len)]) };
if to_print.contains("\r\n\r\n") {
log::info!("Got HTML data probably: {}", to_print);
log::info!("HTML data over");
break 'read;
}
pos += len;
}
Ok(Err(e)) => {
log::info!("read error: {:?}", e);
continue 'connection;
}
Err(e) => {
log::info!(
"Timeout: {e:?} socket state: {:?}, aborting socket and fuck it",
socket.state()
);
socket.abort();
continue 'connection;
}
};
}
log::info!("im outta reading phase");
{
use core::fmt::Write;
match write!(buf, "HTTP/1.0 200 OK\r\n\r\n<html><body style=\"font-size: 45px; text-align: center;\"><p style=\"paddin:200px\">Fra ma che cazzo ti connetti... coglione... \nSi sono connessi {} coglioni fin ora. Bravo!!</p></body></html>\r\n", connected_clients) {
Ok(()) => log::debug!("Formatted message succesfully"),
Err(e) => log::error!("{}", e),
};
}
log::debug!("Our turn to write");
let r = {
use embedded_io_async::Write;
socket.write_all(&buf.as_bytes()).await
};
if let Err(e) = r {
log::error!("write error: {:?}", e);
}
log::debug!("Just wrote a bunch of stuff");
let r = socket.flush().await;
if let Err(e) = r {
log::error!("flush error: {:?}", e);
// log::info!("wrote {} bytes:{}", buf.len(), buf)
}
log::debug!("Just flushed the stuff");
Timer::after(Duration::from_millis(300)).await;
socket.close();
Timer::after(Duration::from_millis(300)).await;
socket.abort();
}
}

2
src/lib.rs Normal file
View File

@ -0,0 +1,2 @@
#![no_std]
#![feature(impl_trait_in_assoc_type)]

156
src/main.rs Normal file
View File

@ -0,0 +1,156 @@
#![no_std]
#![no_main]
#![feature(impl_trait_in_assoc_type)]
mod captive_portal;
mod dhcp;
mod http;
use core::str::FromStr;
use captive_portal::captive_task;
use dhcp::run_dhcp;
use embassy_executor::Spawner;
use embassy_net::{Ipv4Address, Ipv4Cidr, Stack, StackResources, StaticConfigV4};
use embassy_time::{Duration, Timer};
use esp_alloc as _;
use esp_backtrace as _;
use esp_hal::{prelude::*, rng::Rng, timer::timg::TimerGroup};
use esp_wifi::{
init,
wifi::{
AccessPointConfiguration, Configuration, WifiApDevice, WifiController, WifiDevice,
WifiEvent, WifiState,
},
EspWifiController,
};
// When you are okay with using a nightly compiler it's better to use https://docs.rs/static_cell/2.1.0/static_cell/macro.make_static.html
macro_rules! mk_static {
($t:ty,$val:expr) => {{
static STATIC_CELL: static_cell::StaticCell<$t> = static_cell::StaticCell::new();
#[deny(unused_attributes)]
let x = STATIC_CELL.uninit().write(($val));
x
}};
}
const GW_IP_ADDR_ENV: &'static str = "192.168.2.1";
#[esp_hal_embassy::main]
async fn main(spawner: Spawner) -> ! {
// esp_println::logger::init_logger_from_env();
esp_println::logger::init_logger(log::LevelFilter::Trace);
log::info!("Loggin enabled, max level: {:?}", log::max_level());
log::error!("Error!");
log::warn!("Warn!");
log::info!("Info!");
log::debug!("Debug!");
log::trace!("Trace!");
let mut config = esp_hal::Config::default();
config.cpu_clock = CpuClock::max();
let peripherals = esp_hal::init(config);
esp_alloc::heap_allocator!(78 * 1024);
let timg0 = TimerGroup::new(peripherals.TIMG0);
let mut rng = Rng::new(peripherals.RNG);
let init = &*mk_static!(
EspWifiController<'static>,
init(timg0.timer0, rng.clone(), peripherals.RADIO_CLK).unwrap()
);
let wifi = peripherals.WIFI;
let (wifi_interface, controller) =
esp_wifi::wifi::new_with_mode(&init, wifi, WifiApDevice).unwrap();
let timg1 = TimerGroup::new(peripherals.TIMG1);
esp_hal_embassy::init(timg1.timer0);
let gw_ip_addr_str = GW_IP_ADDR_ENV;
let gw_ip_addr = Ipv4Address::from_str(gw_ip_addr_str).expect("failed to parse gateway ip");
let mut dns_servers: heapless::Vec<_, 3> = heapless::Vec::new();
dns_servers
.push(Ipv4Address::from_str(GW_IP_ADDR_ENV).unwrap())
.unwrap();
log::info!("Configured dns: {:?}", dns_servers);
let config = embassy_net::Config::ipv4_static(StaticConfigV4 {
address: Ipv4Cidr::new(gw_ip_addr, 24),
gateway: Some(gw_ip_addr),
dns_servers,
});
let seed = (rng.random() as u64) << 32 | rng.random() as u64;
// Init network stack
let stack = &*mk_static!(
Stack<WifiDevice<'_, WifiApDevice>>,
Stack::new(
wifi_interface,
config,
mk_static!(StackResources<4>, StackResources::<4>::new()),
seed
)
);
spawner.spawn(connection(controller)).ok();
spawner.spawn(net_task(&stack)).ok();
spawner.spawn(run_dhcp(&stack, gw_ip_addr_str)).ok();
spawner.spawn(captive_task(&stack)).ok();
spawner.spawn(http::run_http(&stack)).ok();
loop {
if stack.is_link_up() {
break;
}
Timer::after(Duration::from_millis(500)).await;
}
while !stack.is_config_up() {
Timer::after(Duration::from_millis(100)).await
}
stack
.config_v4()
.inspect(|c| log::info!("ipv4 config: {c:?}"));
loop {
Timer::after(Duration::from_secs(10)).await;
log::info!("All going well :)");
}
}
#[embassy_executor::task]
async fn connection(mut controller: WifiController<'static>) {
log::info!("start connection task");
log::info!("Device capabilities: {:?}", controller.capabilities());
loop {
match esp_wifi::wifi::wifi_state() {
WifiState::ApStarted => {
// wait until we're no longer connected
controller.wait_for_event(WifiEvent::ApStop).await;
Timer::after(Duration::from_millis(5000)).await
}
_ => {}
}
if !matches!(controller.is_started(), Ok(true)) {
let client_config = Configuration::AccessPoint(AccessPointConfiguration {
ssid: "NON CONNETTERTI DIO CANE".try_into().unwrap(),
..Default::default()
});
controller.set_configuration(&client_config).unwrap();
log::info!("Starting wifi");
controller.start_async().await.unwrap();
log::info!("Wifi started!");
}
}
}
#[embassy_executor::task]
async fn net_task(stack: &'static Stack<WifiDevice<'static, WifiApDevice>>) -> ! {
stack.run().await
}