This commit is contained in:
ge 2023-04-25 22:19:13 +03:00
commit d11e0c71eb
4 changed files with 337 additions and 0 deletions

66
README.md Normal file
View File

@ -0,0 +1,66 @@
# PMGR
pmgr is wrapper utility for systemd to manage user services for prox
ies. See example for SSH SOCKS-proxy below.
# Installation
Just place pmgr into your PATH.
```
install -m755 pmgr ~/.local/bin/pmgr
```
# Usage example
For SSH SOCKS-proxy create template user unit ~/.config/sys
temd/user/ssh-proxy@.service with contents:
```ini
[Unit]
Description=SSH tunnel to %I
After=network.target
[Service]
Environment="LOCAL_ADDR=localhost"
EnvironmentFile=%h/.config/proxy-manager/ssh-proxy@%i
ExecStart=/usr/bin/ssh -NT \
-o ServerAliveInterval=60 \
-o ExitOnForwardFailure=yes \
-D ${LOCAL_ADDR}:${LOCAL_PORT} ${TARGET}
RestartSec=5
Restart=always
[Install]
WantedBy=default.target
```
Create environment file ~/.config/pmgr/environment/ssh-proxy@myserver
with contents:
```
TARGET=user@myserver
LOCAL_ADDR=127.0.0.1
LOCAL_PORT=1080
```
In there 'mysserver' may be a domain name, SSH Hostname (set in
~/.ssh/config) or IP address of remote server.
Now you can enable your proxy with systemctl:
```
systemctl --user start ssh-proxy@myserver
```
With pmgr:
```
pmgr -e ssh-proxy@myserver
```
Proxy usage:
```
curl --proxy socks5h://localhost:1080 eth0.me
```

24
UNLICENSE Normal file
View File

@ -0,0 +1,24 @@
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>

121
pmgr Executable file
View File

@ -0,0 +1,121 @@
#!/bin/sh
# pmgr -- proxy manager
PMGR_VERSION=0.0.1
PMGR_WORKDIR="${PMGR_WORKDIR:-"$HOME"/.config/pmgr}"
PMGR_ENV_DIR="${PMGR_ENV_DIR:-"$PMGR_WORKDIR"/environment}"
mkdir -p "$PMGR_WORKDIR"
mkdir -p "$PMGR_ENV_DIR"
usage()
{
printf \
'pmgr -- proxy manager based on systemd user units. See pmgr(1)
Usage: pmgr [options] [<arguments>]
Options:
-l list proxies
-e enable (start) proxy
-d disable (stop) proxy
-r restart proxy
-s show proxy status
-j view proxy logs
-o print PMGR_ENV_DIR
-R run systemctl --user daemon-reload
-a add to autostart
-A remove from autostart
-h print this help message and exit
-v print version and exit
Environment:
PMGR_WORKDIR [default: ~/.config/pmgr]
PMGR_ENV_DIR [default: $PMGR_WORKDIR/environment]
' | sed 's/^ //g'
}
list_proxies()
{
{
printf '\e[1mHOST PROTO FILE STATUS AUTOSTART\033[0m\n'
for file in $(find "$PMGR_ENV_DIR" -type f); do
unit="${file##*/}"
if systemctl --user is-active "$unit" >/dev/null 2>&1; then
active='\033[32mactive\033[0m'
else
active=inactive
fi
printf '%s %s %s %b %s\n' \
"${unit#*@}" \
"${unit%@*}" \
"$(echo "$file" | sed "s%$HOME%~%")" \
"$active" \
"$(systemctl --user is-enabled "$unit")"
done
} | column -t
}
[ $# -eq 0 ] && { usage; exit 0; }
while getopts le:d:r:s:j:oRa:A:hv opt; do
case "$opt" in
l)
list_proxies
exit "$?"
;;
e)
for unit in $(find "$PMGR_ENV_DIR" -type f -printf '%f\n'); do
if systemctl --user is-active "$unit" >/dev/null 2>&1; then
systemctl --user stop "$unit"
fi
done
systemctl --user start "$OPTARG"
exit "$?"
;;
d)
systemctl --user stop "$OPTARG"
exit "$?"
;;
r)
systemctl --user restart "$OPTARG"
exit "$?"
;;
s)
systemctl --user status "$OPTARG"
exit "$?"
;;
j)
journalctl --user --unit "$OPTARG"
exit "$?"
;;
o)
echo "$PMGR_ENV_DIR"
exit 0
;;
R)
systemctl --user daemon-reload
exit "$?"
;;
a)
systemctl --user enable "$OPTARG"
exit "$?"
;;
A)
systemctl --user disable "$OPTARG"
exit "$?"
;;
h)
usage
exit 0
;;
v)
printf 'v%s\n' "$PMGR_VERSION"
exit 0
;;
*)
:
esac
done

126
pmgr.1 Normal file
View File

@ -0,0 +1,126 @@
.\" vim: set filetype=groff:
.TH PMGR 1 "2023-04-25" "pmgr 0.0.1"
.SH NAME
pmgr - proxy manager based on systemd user units.
.SH SYNOPSYS
.SY pmrg
.OP \-ledrsjoRaAhv
[\fIservice\fR]
.YS
.SH DESCRIPTION
.PP
pmgr is wrapper utility for systemd to manage user services for proxies.
See example for SSH SOCKS-proxy in EXAMPLES section below.
.SH OPTIONS
.TP
.B \-l
list proxies
.TP
.B \-e \fIservice\fR
enable (start) proxy
.TP
.B \-d \fIservice\fR
disable (stop) proxy
.TP
.B \-r \fIservice\fR
restart proxy
.TP
.B \-s \fIservice\fR
show proxy status
.TP
.B \-j \fIservice\fR
view proxy logs
.TP
.B \-o
print PMGR_ENV_DIR
.TP
.B \-R
run systemctl --user daemon-reload
.TP
.B \-a \fIservice\fR
add to autostart
.TP
.B \-A \fIservice\fR
remove from autostart
.TP
.B \-h
print this help message and exit
.TP
.B \-v
print version and exit
.SH ENVIRONMENT
.TP
.B PMGR_WORKDIR
Working directory. Deafult is ~/.config/pmgr
.TP
.B PMGR_ENV_DIR
Directory for systemd environment files, depends on PMGR_WORKDIR by default.
Default is $PMGR_WORKDIR/environment
.SH EXAMPLES
.PP
For SSH SOCKS-proxy create template user unit
~/.config/systemd/user/ssh-proxy@.service with contents:
.PP
.in +4n
.EX
[Unit]
Description=SSH tunnel to %I
After=network.target
[Service]
Environment="LOCAL_ADDR=localhost"
EnvironmentFile=%h/.config/proxy-manager/ssh-proxy@%i
ExecStart=/usr/bin/ssh -NT \\
-o ServerAliveInterval=60 \\
-o ExitOnForwardFailure=yes \\
-D ${LOCAL_ADDR}:${LOCAL_PORT} ${TARGET}
RestartSec=5
Restart=always
[Install]
WantedBy=default.target
.EE
.in
.PP
Create environment file
~/.config/pmgr/environment/ssh-proxy@myserver with contents:
.PP
.in +4n
.EX
TARGET=user@myserver
LOCAL_ADDR=127.0.0.1
LOCAL_PORT=1080
.EE
.in
.PP
In there 'mysserver' may be a domain name, SSH Hostname (set in ~/.ssh/config)
or IP address of remote server.
Now you can enable your proxy with systemctl:
.PP
.in +4n
.EX
systemctl --user start ssh-proxy@myserver
.EE
.in
.PP
With pmgr:
.PP
.in +4n
.EX
pmgr -e ssh-proxy@myserver
.EE
.in
.SH SEE ALSO
.PP
.BR systemctl (1),
.BR journalctl (1),
.BR systemd.special (7),
.PP
https://wiki.archlinux.org/title/systemd/User