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

Feature request: lifecycle hooks for sslocal

Open dvsekhvalnov opened this issue 2 years ago • 3 comments
trafficstars

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.

dvsekhvalnov avatar Aug 18 '23 17:08 dvsekhvalnov

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):

https://github.com/shadowsocks/shadowsocks-rust/blob/a8955d2b2f74155f2771daa5919196c9d159dc0c/crates/shadowsocks/src/net/tcp.rs#L48-L65

  • 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.

zonyitoo avatar Aug 20 '23 04:08 zonyitoo

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.

dvsekhvalnov avatar Aug 22 '23 05:08 dvsekhvalnov

mari12231

sal47out avatar Sep 02 '23 14:09 sal47out