| #!/bin/sh |
| |
| # Copyright 2015-2025 Rivoreo |
| |
| # Permission is hereby granted, free of charge, to any person obtaining a copy |
| # of this software and associated documentation files (the "Software"), to |
| # deal in the Software without restriction, including without limitation the |
| # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| # sell copies of the Software, and to permit persons to whom the Software is |
| # furnished to do so, subject to the following conditions: |
| |
| # The above copyright notice and this permission notice shall be included in |
| # all copies or substantial portions of the Software. |
| |
| # 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 |
| # COPYRIGHT HOLDERS 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. |
| |
| |
| if [ $# != 2 ] || [ "$1" != -c ]; then |
| echo "Usage: <this-shell?> -c <wg-command-line>" 1>&2 |
| exit 255 |
| fi |
| |
| set -f |
| rncn_command="${2#rncn[ ]}" |
| [ "$rncn_command" = "$2" ] && exec wg $2 |
| set -- $rncn_command |
| set -e |
| case "$1" in |
| set-remote-port) |
| if [ $# != 4 ]; then |
| echo "Usage: set-remote-port <interface> <peer-pub-key> <new-port>" 1>&2 |
| exit 255 |
| fi |
| interface="$2" |
| public_key="$3" |
| new_port="$4" |
| wg show "$interface" endpoints | while read -r line; do |
| set -- $line |
| [ "$1" != "$public_key" ] && continue |
| addr="${2%:*}" |
| { |
| i=0 |
| while [ $i -lt 5 ]; do |
| sleep 1 |
| wg set "$interface" peer "$1" endpoint "$addr:$new_port" |
| i=$((i+1)) |
| done |
| } 0<> /dev/null 1>&0 2>&0 & |
| exec wg set "$interface" peer "$1" endpoint "$addr:$new_port" |
| done |
| exit |
| ;; |
| *) |
| echo e! 1>&2 |
| exit 255 |
| ;; |
| esac |