shadowsocks-rust icon indicating copy to clipboard operation
shadowsocks-rust copied to clipboard

Allow user to pass SocketProtectFn for android

Open zhangsan946 opened this issue 5 months ago • 1 comments
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.

zhangsan946 avatar Jun 17 '25 13:06 zhangsan946

@zonyitoo, Please check this new PR.

zhangsan946 avatar Jun 17 '25 13:06 zhangsan946

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.

zonyitoo avatar Jun 18 '25 02:06 zonyitoo