Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ plugins {

alias(libs.plugins.arturbosch.detekt) apply false

id("org.mozilla.rust-android-gradle.rust-android") apply false
alias(libs.plugins.rust.android.gradle) apply false

alias(libs.plugins.ksp) apply false
alias(libs.plugins.hilt) apply false
Expand Down
26 changes: 18 additions & 8 deletions file/src/main/kotlin/dev/clombardo/dnsnet/file/FileHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -131,21 +131,31 @@ object FileHelper {
* @param path A content:// URI, a https:// http:// URL, or a local path
* @return A read-only file descriptor or null if it cannot be opened.
*/
fun getDetachedReadOnlyFd(context: Context, path: String): Int? {
var descriptor: Int? = null
try {
descriptor = if (path.startsWith("content://")) {
context.contentResolver.openFileDescriptor(path.toUri(), "r")!!.detachFd()
fun getDetachedFd(
context: Context,
path: String,
mode: Int = ParcelFileDescriptor.MODE_READ_ONLY
): Int? {
val modeString = when (mode) {
ParcelFileDescriptor.MODE_WRITE_ONLY -> "w"
ParcelFileDescriptor.MODE_READ_WRITE -> "rw"
else -> "r"
}

return try {
if (path.startsWith("content://")) {
context.contentResolver.openFileDescriptor(path.toUri(), modeString)!!.detachFd()
} else if (isDownloadable(path)) {
val itemFile = getLocalFileForRemoteUrl(context, path) ?: return null
ParcelFileDescriptor.open(itemFile, ParcelFileDescriptor.MODE_READ_ONLY).detachFd()
ParcelFileDescriptor.open(itemFile, mode).detachFd()
} else {
val file = File(path)
ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY).detachFd()
file.createNewFile()
ParcelFileDescriptor.open(file, mode).detachFd()
}
} catch (e: Exception) {
logError("getDetachedReadOnlyFd: $path", e)
null
}
return descriptor
}
}
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,4 @@ arturbosch-detekt = { id = "io.gitlab.arturbosch.detekt", version.ref = "detekt"
hilt = { id = "com.google.dagger.hilt.android", version.ref = "hilt" }
ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
gradle-play-publisher = { id = "com.github.triplet.play", version.ref = "playPublisher" }
rust-android-gradle = { id = "org.mozilla.rust-android-gradle.rust-android" }
4 changes: 4 additions & 0 deletions resources/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,8 @@
<string name="base64_license_link" translatable="false">https://github.com/marshallpierce/rust-base64/tree/v0.22.1</string>
<string name="roboto_flex" translatable="false">Roboto Flex</string>
<string name="roboto_flex_link" translatable="false">https://github.com/googlefonts/roboto-flex/blob/3.200/OFL.txt</string>
<string name="bincode" translatable="false">bincode</string>
<string name="bincode_link" translatable="false">https://git.sr.ht/~stygianentity/bincode/tree/trunk/item/LICENSE.md</string>
<string name="lru" translatable="false">LRU</string>
<string name="lru_link" translatable="false">https://github.com/jeromefroe/lru-rs/blob/0.16.2/LICENSE</string>
</resources>
2 changes: 1 addition & 1 deletion service/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import com.nishtahir.CargoBuildTask
plugins {
alias(libs.plugins.android.library)
alias(libs.plugins.kotlin.android)
id("org.mozilla.rust-android-gradle.rust-android")
alias(libs.plugins.rust.android.gradle)
alias(libs.plugins.kotlinx.atomicfu)
alias(libs.plugins.ksp)
alias(libs.plugins.hilt)
Expand Down
66 changes: 63 additions & 3 deletions service/libnet/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions service/libnet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ path = "uniffi-bindgen.rs"

[dependencies]
base64 = "0.22.1"
bincode = "2.0.1"
etherparse = "0.19.0"
getrandom = "0.3.4"
libc = "0.2.177"
log = "0.4.28"
lru = "0.16.2"
mio = { version="1.1.0", features=["net","os-poll","os-ext"] }
quiche = "0.24.6"
simple-dns = "0.11.0"
Expand Down
5 changes: 4 additions & 1 deletion service/libnet/src/backend/doh3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use mio::{Interest, Poll, Token, event::Source, net::UdpSocket};
use quiche::h3::NameValue;
use quiche::{SendInfo, h3::Header};

use crate::cache::DnsCache;
use crate::validation::{NativeDnsServer, NativeDnsServerType};
use crate::{Vpn, VpnCallback};

Expand Down Expand Up @@ -472,6 +473,7 @@ impl DnsBackend for DoH3Backend {
fn process_events(
&mut self,
vpn: &mut Vpn,
dns_cache: Arc<DnsCache>,
_events: Vec<&mio::event::Event>,
) -> Result<Vec<Box<dyn Source>>, DnsBackendError> {
let mut sources_to_remove = Vec::<Box<dyn Source>>::new();
Expand Down Expand Up @@ -525,7 +527,7 @@ impl DnsBackend for DoH3Backend {
// There are no more UDP packets to read, so end the read
// loop.
if error.kind() == std::io::ErrorKind::WouldBlock {
debug!("process_events: recv() would block");
trace!("process_events: recv() would block");
break 'read;
}

Expand Down Expand Up @@ -725,6 +727,7 @@ impl DnsBackend for DoH3Backend {
match connection.sent_request_streams.get(&stream_id) {
Some(request) => {
vpn.handle_dns_response(
Some(dns_cache.clone()),
&request.request_packet,
&self.input_buffer[..read],
);
Expand Down
5 changes: 3 additions & 2 deletions service/libnet/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
pub mod doh3;
pub mod standard;

use std::time::Duration;
use std::{sync::Arc, time::Duration};

use mio::{Poll, event::Source};

use crate::{Vpn, VpnCallback};
use crate::{Vpn, VpnCallback, cache::DnsCache};

#[derive(Debug)]
pub enum DnsBackendError {
Expand Down Expand Up @@ -47,6 +47,7 @@ pub trait DnsBackend {
fn process_events(
&mut self,
ad_vpn: &mut Vpn,
dns_cache: Arc<DnsCache>,
events: Vec<&mio::event::Event>,
) -> Result<Vec<Box<dyn Source>>, DnsBackendError>;
}
4 changes: 4 additions & 0 deletions service/libnet/src/backend/standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@

use std::collections::VecDeque;
use std::os::fd::AsRawFd;
use std::sync::Arc;
use std::{
net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6},
time::Duration,
};

use mio::{Interest, Poll, Token, event::Source, net::UdpSocket};

use crate::cache::DnsCache;
use crate::get_epoch;
use crate::{Vpn, VpnCallback, backend::DnsBackendError};

Expand Down Expand Up @@ -231,6 +233,7 @@ impl DnsBackend for StandardDnsBackend {
fn process_events(
&mut self,
vpn: &mut Vpn,
dns_cache: Arc<DnsCache>,
events: Vec<&mio::event::Event>,
) -> Result<Vec<Box<dyn Source>>, DnsBackendError> {
let mut sources_to_remove = Vec::<Box<dyn Source>>::new();
Expand All @@ -247,6 +250,7 @@ impl DnsBackend for StandardDnsBackend {
match wosp.socket.recv(&mut self.response_packet.as_mut_slice()) {
Ok(size) => {
vpn.handle_dns_response(
Some(dns_cache.clone()),
&wosp.packet,
&mut self.response_packet[..size],
);
Expand Down
Loading
Loading