Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: lifecycle hooks for sslocal #1275

Open
dvsekhvalnov opened this issue Aug 18, 2023 · 3 comments
Open

Feature request: lifecycle hooks for sslocal #1275

dvsekhvalnov opened this issue Aug 18, 2023 · 3 comments

Comments

@dvsekhvalnov
Copy link

Hey guys, may i request small feature. In a vein of openvpn or wireguard, will it be possible to have:

PreStart, PostStart, PreExit, PostExit hooks (may be others) for sslocal ?
Just arbitrary shell commands to execute before/after establishing/terminating connection to remote ssserver.

Will make routing tables setup so much simpler for local-tun feature.

Will greatly appreciate.

@zonyitoo
Copy link
Collaborator

I could understand your situation.

PreStart, PostStart, PreExit, PostExit hooks could be done easily by wrapping sslocal with a shell script, so it doesn't need any modification of sslocal itself.

Just arbitrary shell commands to execute before/after establishing/terminating connection to remote ssserver.

Hmm, this would be a lot more complicated. Just a quick thought:

  • For pre-connect and post-connect hook, it could be added right here (for TCP, UDP has similar APIs):

/// Connects shadowsocks server
pub async fn connect_server_with_opts(
context: &Context,
addr: &ServerAddr,
opts: &ConnectOpts,
) -> io::Result<TcpStream> {
let stream = match *addr {
ServerAddr::SocketAddr(ref addr) => SysTcpStream::connect(*addr, opts).await?,
ServerAddr::DomainName(ref domain, port) => {
lookup_then_connect!(context, domain, port, |addr| {
SysTcpStream::connect(addr, opts).await
})?
.1
}
};
Ok(TcpStream(stream))
}

  • For pre-close, it could be put in the Drop trait (destructor) implementation of TcpStream and UdpSocket, but Drop trait doesn't support async fn, so it can only be a blocking call.
  • For post-close, hmm.. nothing we can do currently, because the socket is close()d in other libraries.

What would be the best way to implement hooks?

  • Commands? I don't think that would be a good way. Executing a command requires creating a new process.
  • Unix Domain Socket? It would require users to implement a simple UDS server and parse the communication protocol.
  • TCP/UDP socket? Same as UDS.
  • ...

It looks very complicated. I don't know if there is any other more elegant ways.

@dvsekhvalnov
Copy link
Author

dvsekhvalnov commented Aug 22, 2023

PreStart, PostStart, PreExit, PostExit hooks could be done easily by wrapping sslocal with a shell script, so it doesn't need any modification of sslocal itself.

well, kind of easily. You typically would like to set gateway rule before sslocal start (to let it connect). Then routing all traffic to TUN after it starts (otherwise you missing TUN interface yet). So it takes some gymnastics to run sslocal in background, propagiting kill signals, e.t.c. Not a rocket science but can be simpler :)

I can't advise on how to do it best with codebase, haven't really dig it. But i think spawning shell process will be most flexible.

I also don't think you should care much whether it's blocking or not, because it's for local client only - only affects user who doing it. If you do something slow - that's fine, it affects only you, you can change it back or whatever.

@sal47out
Copy link

sal47out commented Sep 2, 2023

mari12231

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

No branches or pull requests

3 participants