Skip to content

Conversation

@zhangsan946
Copy link
Contributor

@zhangsan946 zhangsan946 commented Jun 17, 2025

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
Copy link
Contributor Author

@zonyitoo, Please check this new PR.

@zhangsan946 zhangsan946 changed the title Allow user to pass SocketProtector callback function for android Allow user to pass SocketProtectFn for android Jun 17, 2025
@zonyitoo
Copy link
Collaborator

zonyitoo commented Jun 18, 2025

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.

Signed-off-by: San Zhang <[email protected]>
Signed-off-by: San Zhang <[email protected]>
Signed-off-by: San Zhang <[email protected]>
Signed-off-by: San Zhang <[email protected]>
Signed-off-by: San Zhang <[email protected]>
Signed-off-by: San Zhang <[email protected]>
@zonyitoo zonyitoo merged commit 1fff8e6 into shadowsocks:master Jun 18, 2025
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants