blooemu 0.1.19

the best library for OS API's manipulation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
#[cfg(any(target_os = "linux", target_os = "macos"))]
use pnet::datalink;
use serde_json::Value;
#[cfg(target_os = "windows")]
use std::ffi::CStr;
use std::fs;
use std::io::{self, ErrorKind, Read, Write};
use std::net::{IpAddr, SocketAddr, TcpListener, TcpStream, ToSocketAddrs};
use std::path::Path;
#[cfg(target_os = "windows")]
use std::ptr::null_mut;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::thread;
use std::time::Duration;
#[cfg(target_os = "windows")]
use winapi::shared::minwindef::ULONG;
#[cfg(target_os = "windows")]
use winapi::shared::winerror::ERROR_BUFFER_OVERFLOW;
#[cfg(target_os = "windows")]
use winapi::um::iphlpapi::GetAdaptersAddresses;
#[cfg(target_os = "windows")]
use winapi::um::iptypes::IP_ADAPTER_ADDRESSES;


pub fn get_mac_address() -> Option<String> {
    let net = Path::new("/sys/class/net");
    let entry = fs::read_dir(net).ok()?;

    // Find the interface file with the MAC address
    let iface = entry
        .filter_map(|p| p.ok())
        .map(|p| p.path().file_name().expect("Error").to_os_string())
        .filter_map(|s| s.into_string().ok())
        .find(|iface| {
            let iface_path = net.join(iface).join("address");
            fs::metadata(iface_path).is_ok()
        });

    // Read the MAC address from the file
    let macaddr = match iface {
        Some(iface) => {
            let iface_path = net.join(&iface).join("address");
            let mut f = fs::File::open(iface_path).ok()?;
            let mut macaddr = String::new();
            f.read_to_string(&mut macaddr).ok()?;
            Some(macaddr.trim().to_string())
        }
        None => None,
    };

    macaddr
}

#[cfg(target_os = "windows")]
pub fn get_interface_name() -> Option<String> {
    let adapters = get_network_interfaces();
    adapters.first().map(|iface| iface.name.clone())
}

#[cfg(any(target_os = "linux", target_os = "macos"))]
pub fn get_interface_name() -> Option<String> {
    let interfaces = get_network_interfaces();
    interfaces.first().map(|iface| iface.name.clone())
}

pub fn connect_socket<A: ToSocketAddrs>(address: A) -> io::Result<TcpStream> {
    let stream = TcpStream::connect(address)?;
    println!("Successfully connected to {}", stream.peer_addr()?);
    Ok(stream)
}

pub fn listen_socket(addr: &str) -> io::Result<()> {
    let listener = TcpListener::bind(addr)?;
    println!("Listening for connections on {}", addr);

    // Waiting for incoming connections
    for stream in listener.incoming() {
        match stream {
            Ok(stream) => {
                println!("New connection from: {}", stream.peer_addr()?);

                // Handling the connection in a separate thread
                thread::spawn(move || {
                    if let Err(e) = handle_connection(stream) {
                        eprintln!("Error handling connection: {}", e);
                    }
                });
            }
            Err(e) => {
                eprintln!("Failed to accept connection: {}", e);
            }
        }
    }
    Ok(())
}

fn handle_connection(mut stream: TcpStream) -> io::Result<()> {
    let mut buffer = [0; 1024];
    let bytes_read = stream.read(&mut buffer)?;

    if bytes_read == 0 {
        return Err(io::Error::new(
            ErrorKind::ConnectionAborted,
            "Connection closed",
        ));
    }

    println!(
        "Received data: {}",
        String::from_utf8_lossy(&buffer[..bytes_read])
    );

    stream.write_all(b"HTTP/1.1 200 OK\r\n\r\n")?;
    stream.flush()?;
    Ok(())
}

pub fn resolve_hostname(hostname: &str) -> Result<IpAddr, String> {
    let addrs = (hostname, 0).to_socket_addrs(); // Port 0, since it is not important when resolving the hostname
    match addrs {
        Ok(mut addresses) => {
            if let Some(socket_addr) = addresses.next() {
                Ok(socket_addr.ip())
            } else {
                Err(format!("No IP addresses found for hostname: {}", hostname))
            }
        }
        Err(e) => Err(format!("Failed to resolve hostname {}: {}", hostname, e)),
    }
}

#[derive(Debug)]
pub struct NetworkInterface {
    pub name: String,
    pub ip_addresses: Vec<String>,
}

#[cfg(target_os = "windows")]
pub fn get_network_interfaces() -> Vec<NetworkInterface> {
    let mut adapters: Vec<NetworkInterface> = Vec::new();
    let mut buf_len: ULONG = 0;

    unsafe {
        // First call attempt to get the required buffer size
        let ret = GetAdaptersAddresses(0, 0, null_mut(), null_mut(), &mut buf_len);
        if ret != ERROR_BUFFER_OVERFLOW {
            return adapters;
        }

        // Allocating a buffer for adapters
        let mut buf: Vec<u8> = vec![0; buf_len as usize];
        let adapter_addresses = buf.as_mut_ptr() as *mut IP_ADAPTER_ADDRESSES;

        // Second call attempt to get adapter information
        if GetAdaptersAddresses(0, 0, null_mut(), adapter_addresses, &mut buf_len) == 0 {
            let mut adapter = adapter_addresses;
            while !adapter.is_null() {
                let adapter_ref = &*adapter;

                let name = CStr::from_ptr(adapter_ref.AdapterName)
                    .to_string_lossy()
                    .into_owned();
                let mut ips = Vec::new();

                // Second call attempt to get adapter information
                let mut address = adapter_ref.FirstUnicastAddress;
                while !address.is_null() {
                    let addr = &*address;
                    let sockaddr = addr.Address.lpSockaddr;
                    if !sockaddr.is_null() {
                        let ip_address = format!("{:?}", sockaddr);
                        ips.push(ip_address);
                    }
                    address = addr.Next;
                }

                // Passing loopback interfaces by IP addresses
                if !ips.iter().any(|ip| ip.starts_with("127.") || ip == "::1") {
                    adapters.push(NetworkInterface {
                        name,
                        ip_addresses: ips,
                    });
                }

                adapter = adapter_ref.Next;
            }
        }
    }

    adapters
}

#[cfg(any(target_os = "linux", target_os = "macos"))]
pub fn get_network_interfaces() -> Vec<NetworkInterface> {
    let mut interfaces = Vec::new();
    let all_interfaces = datalink::interfaces();

    for iface in all_interfaces {
        let name = iface.name.clone();
        let mut ips = Vec::new();

        for ip in iface.ips {
            ips.push(ip.ip().to_string());
        }

        // Passing loopback interfaces by IP addresses
        if !ips.iter().any(|ip| ip.starts_with("127.") || ip == "::1") {
            interfaces.push(NetworkInterface {
                name,
                ip_addresses: ips,
            });
        }
    }

    interfaces
}

#[cfg(target_os = "windows")]
pub fn get_hostname() -> io::Result<String> {
    use std::process::Command;

    let output = Command::new("hostname").output()?;
    let hostname = String::from_utf8_lossy(&output.stdout).trim().to_string();
    Ok(hostname)
}

#[cfg(target_os = "macos")]
pub fn get_hostname() -> io::Result<String> {
    use std::process::Command;

    let output = Command::new("hostname").output()?;
    let hostname = String::from_utf8_lossy(&output.stdout).trim().to_string();
    Ok(hostname)
}

#[cfg(target_os = "linux")]
pub fn get_hostname() -> io::Result<String> {
    use std::process::Command;

    let output = Command::new("hostname").output()?;
    let hostname = String::from_utf8_lossy(&output.stdout).trim().to_string();
    Ok(hostname)
}

pub fn send_data(address: &str, request: &str) -> Result<String, String> {
    // Establishing a connection to the server
    let mut stream = TcpStream::connect(address).map_err(|e| e.to_string())?;
    stream
        .set_read_timeout(Some(Duration::new(5, 0)))
        .map_err(|e| e.to_string())?;

    // Отправляем запрос
    stream
        .write_all(request.as_bytes())
        .map_err(|e| e.to_string())?;

    // Читаем ответ
    let mut response = String::new();
    stream
        .read_to_string(&mut response)
        .map_err(|e| e.to_string())?;

    Ok(response)
}

pub fn close_socket(addr: SocketAddr) -> Result<(), io::Error> {
    // Attempting to connect to a socket address
    let mut stream = TcpStream::connect(addr)?;

    // Sending an empty message to check if a socket is working
    let msg = b"";
    stream.write_all(msg)?;

    // Closing a socket
    stream.shutdown(std::net::Shutdown::Both)?;

    // Error checking
    if let Err(err) = stream.read(&mut [0; 1]) {
        // If the error is "Connection reset by peer", then the socket is closed
        if err.kind() == ErrorKind::ConnectionReset {
            Ok(())
        } else {
            Err(err)
        }
    } else {
        // Если чтение прошло успешно, сокет не закрыт
        Err(io::Error::new(
            ErrorKind::Other,
            "The socket is not closed or has not been connected",
        ))
    }
}

pub type RequestHandler = fn(&str, Option<Value>) -> String;

pub fn get_external_ip() -> io::Result<String> {
    // The URL of the service that returns the external IP address
    let address = "api.ipify.org:80";
    let request = "GET / HTTP/1.1\r\nHost: api.ipify.org\r\nConnection: close\r\n\r\n";

    // Connecting to the server
    let stream_result = TcpStream::connect(address.to_socket_addrs()?.next().unwrap());

    let mut stream = match stream_result {
        Ok(s) => s,
        Err(e) => {
            eprintln!("Connection error: {}", e);
            return Err(io::Error::new(
                ErrorKind::NotConnected,
                "No internet connection",
            ));
        }
    };

    // Sending an HTTP request
    if let Err(e) = stream.write_all(request.as_bytes()) {
        eprintln!("Error sending request: {}", e);
        return Err(e);
    }

    // Reading an answer
    let mut response = Vec::new();
    if let Err(e) = stream.read_to_end(&mut response) {
        eprintln!("Error when reading the answer: {}", e);
        return Err(e);
    }

    // Convert an answer to a string
    let response_str = String::from_utf8_lossy(&response);

    // Parsing an IP address from a response
    if let Some(ip_start) = response_str.find("\r\n\r\n") {
        let ip_address = &response_str[ip_start + 4..]; // Removing headings
        return Ok(ip_address.trim().to_string());
    }

    Err(io::Error::new(
        ErrorKind::Other,
        "Failed to parse IP address",
    ))
}

pub fn get_local_ip() -> io::Result<IpAddr> {
    let listener = TcpListener::bind("0.0.0.0:0")?;
    let local_addr = listener.local_addr()?;
    Ok(local_addr.ip())
}
pub fn is_network_available(addr: &str) -> bool {
    // Add the standard port 80 if it is not specified
    let socket_addr = if addr.contains(':') {
        match addr.parse::<SocketAddr>() {
            Ok(addr) => addr,
            Err(e) => {
                eprintln!("Address parsing error: {}", e);
                return false;
            }
        }
    } else {
        format!("{}:80", addr) // Adding Port 80
            .parse::<SocketAddr>()
            .expect("Address parsing error")
    };

    // Timed Out Connection Attempt
    TcpStream::connect_timeout(&socket_addr, Duration::from_secs(2)).is_ok()
}

pub fn create_socket(addr: &str, handler: RequestHandler) -> io::Result<()> {
    let listener = TcpListener::bind(addr)?;
    let local_addr = listener.local_addr()?;
    println!("Socket running on {}", local_addr);

    let running = Arc::new(AtomicBool::new(true));

    for stream in listener.incoming() {
        let running = Arc::clone(&running);
        match stream {
            Ok(mut stream) => {
                let peer_addr = stream.peer_addr()?;
                println!("Connection from {}", peer_addr);

                let running_clone = Arc::clone(&running);
                let handler_clone = handler;
                thread::spawn(move || {
                    if let Err(e) =
                        handle_client(&mut stream, &running_clone, peer_addr, handler_clone)
                    {
                        eprintln!("Client processing error {}: {}", peer_addr, e);
                    }
                });
            }
            Err(e) => {
                eprintln!("Connection error: {}", e);
            }
        }

        if !running.load(Ordering::Relaxed) {
            break;
        }
    }

    Ok(())
}

fn handle_client(
    stream: &mut TcpStream,
    _running: &Arc<AtomicBool>,
    peer_addr: SocketAddr,
    handler: RequestHandler,
) -> io::Result<()> {
    let mut buffer = [0; 1024];

    let bytes_read = stream.read(&mut buffer)?;
    if bytes_read == 0 {
        println!("Connection closed by the client {}", peer_addr);
        return Ok(());
    }

    let request = String::from_utf8_lossy(&buffer[..bytes_read]);
    println!("Request received from {}: {}", peer_addr, request);

    let (method, path, body) = parse_http_request(&request);

    let json_body: Option<Value> = if method == "POST" && !body.is_empty() {
        serde_json::from_str(&body).ok()
    } else {
        None
    };

    let response_body = handler(&path, json_body);
    let response = format!(
        "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\n\r\n{}",
        response_body.len(),
        response_body
    );

    stream.write_all(response.as_bytes())?;
    println!("Response successfully sent to client {}", peer_addr);

    stream.shutdown(std::net::Shutdown::Both)?;
    println!("The connection to the {} client is complete", peer_addr);

    Ok(())
}

fn parse_http_request(request: &str) -> (String, String, String) {
    let lines: Vec<&str> = request.split("\r\n").collect();

    if lines.is_empty() {
        return ("".to_string(), "".to_string(), "".to_string());
    }

    let first_line = lines[0];
    let parts: Vec<&str> = first_line.split_whitespace().collect();

    if parts.len() < 2 {
        return ("".to_string(), "".to_string(), "".to_string());
    }

    let method = parts[0].to_string();
    let path = parts[1].to_string();

    let mut body = String::new();
    let mut is_body = false;

    for line in lines {
        if line.is_empty() {
            is_body = true;
            continue;
        }
        if is_body {
            body.push_str(line);
        }
    }

    (method, path, body)
}