shadowsocks-rust
shadowsocks-rust copied to clipboard
Feature request: lifecycle hooks for sslocal
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.
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
Droptrait (destructor) implementation ofTcpStreamandUdpSocket, butDroptrait doesn't supportasync fn, so it can only be a blocking call. - For post-close, hmm.. nothing we can do currently, because the
socketisclose()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.
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.
mari12231