shadowsocks-rust
shadowsocks-rust copied to clipboard
Allow user to pass SocketProtectFn for android
trafficstars
Allow user to implement the Rust interface which calls the Java VPNService protect method on Android, and to be used to protect the socket fd right after the socket is created.
@zonyitoo, Please check this new PR.
This is not the design I have mentioned in the previous PR. What I wanted was:
trait SocketProtect {
fn protect(fd: RawFd) -> io::Result<()>;
}
struct SocketProtectFn<F> {
f: F
}
impl<F> SocketProtect for SocketProtectFn<F>
where F: Fn(RawFd) -> io::Result<()>
{
fn protect(&self, fd: RawFd) -> io::Result<()> {
self.f(fd)
}
}
pub fn make_socket_protect_fn(f: F) -> SocketProtectFn<F>
where F: Fn(RawFd) -> io::Result<()> + Send + Sync + Clone
{
SocketProtectFn { f }
}
struct ConnectOpts {
// ... other fields
#[cfg(target_os = "android")]
pub vpn_socket_protect_fn: Option<Arc<Box<dyn SocketProtect + Send + Sync>>>,
}
I would recommend Arc<Box<dyn SocketProtect>>. The function itself doesn't need to be clonable.