Message ID | 20200617214652.4933-1-rlanghammer@web.de |
---|---|
State | Superseded |
Headers | show |
diff --git a/src/packages/fff/fff-vpn-select/files/usr/sbin/vpn-select b/src/packages/fff/fff-vpn-select/files/usr/sbin/vpn-select index 30883f5..bf9c199 100755 --- a/src/packages/fff/fff-vpn-select/files/usr/sbin/vpn-select +++ b/src/packages/fff/fff-vpn-select/files/usr/sbin/vpn-select @@ -1,65 +1,36 @@ #!/bin/sh # Usage: vpn-select <path-to-hood-file> +# To add a new protocol, put a file with two functions to /etc/vpn-select.d/ . +# The function ${protocol}_config is called for every peer in hoodfile. +# The second function ${protocol}_start_stop is called once per installed protocol . /usr/share/libubox/jshn.sh hoodfile="$1" - -make_config() { - # remove old config - rm /tmp/fastd_fff_peers/* - - # prepare - Index=1 - json_load "$(cat "$hoodfile")" - json_select vpn - - # get fastd peers - while json_select "$Index" > /dev/null - do - json_get_var protocol protocol - if [ "$protocol" = "fastd" ]; then - # set up fastd - json_get_var servername name - filename="/etc/fastd/fff/peers/$servername" - echo "#name \"${servername}\";" > "$filename" - json_get_var key key - echo "key \"${key}\";" >> "$filename" - json_get_var address address - json_get_var port port - echo "remote \"${address}\" port ${port};" >> "$filename" - echo "" >> "$filename" - echo "float yes;" >> "$filename" - fi - json_select ".." # back to vpn - Index=$(( Index + 1 )) - done - json_select ".." # back to root -} - # Only do something if file is there and not empty; otherwise exit 1 -if [ -s "$hoodfile" ]; then - if [ ! -d /tmp/fastd_fff_peers ]; then - # first run after reboot - mkdir /tmp/fastd_fff_peers - make_config - # start fastd only if there are some peers - [ "$(ls /etc/fastd/fff/peers/* 2>/dev/null)" ] && /etc/init.d/fastd start - else - make_config - /etc/init.d/fastd reload +[ -s "$hoodfile" ] || exit 1 + +# source functions +for file in /etc/vpn-select.d/*; do + . "$file" +done + +# load hoodfile +json_load "$(cat "$hoodfile")" +json_select vpn + +# configure vpn +index=1 +while json_select "$index" > /dev/null ; do + json_get_var protocol protocol + "${protocol}_config" + json_select ".." # back to vpn + index=$(( index + 1 )) +done + +# start/restart/stop vpnservices +for protocol in $(grep "_start_stop()" /etc/vpn-select.d/* | cut -f1 -d\(); do + "${protocol}" +done - # fastd start/stop for various situations - pidfile="/tmp/run/fastd.fff.pid" - if [ "$(ls /etc/fastd/fff/peers/* 2>/dev/null)" ]; then - ([ -s "$pidfile" ] && [ -d "/proc/$(cat "$pidfile")" ]) || /etc/init.d/fastd start - else - ([ -s "$pidfile" ] && [ -d "/proc/$(cat "$pidfile")" ]) && /etc/init.d/fastd stop - fi - fi - exit 0 -else - echo "vpn-select: Hood file not found or empty!" - exit 1 -fi
Hallo Robert, gute Sache. Folgende Anmerkungen: On 17.06.20 23:46, Robert Langhammer wrote: > diff --git a/src/packages/fff/fff-vpn-select/files/usr/sbin/vpn-select b/src/packages/fff/fff-vpn-select/files/usr/sbin/vpn-select > index 30883f5..bf9c199 100755 > --- a/src/packages/fff/fff-vpn-select/files/usr/sbin/vpn-select > +++ b/src/packages/fff/fff-vpn-select/files/usr/sbin/vpn-select > @@ -1,65 +1,36 @@ > #!/bin/sh > > # Usage: vpn-select <path-to-hood-file> > +# To add a new protocol, put a file with two functions to /etc/vpn-select.d/ . > +# The function ${protocol}_config is called for every peer in hoodfile. > +# The second function ${protocol}_start_stop is called once per installed protocol Hier könnte man noch erwähnen, dass sämtliche alte Config beim Sourcen entfernt werden muss. Eigentlich gefällt mir das nicht so recht, eine ${protocol}_clear() Funktion wäre mir dafür glaube ich irgendwie lieber. > > . /usr/share/libubox/jshn.sh > > hoodfile="$1" > - > -make_config() { > - # remove old config > - rm /tmp/fastd_fff_peers/* > - > - # prepare > - Index=1 > - json_load "$(cat "$hoodfile")" > - json_select vpn > - > - # get fastd peers > - while json_select "$Index" > /dev/null > - do > - json_get_var protocol protocol > - if [ "$protocol" = "fastd" ]; then > - # set up fastd > - json_get_var servername name > - filename="/etc/fastd/fff/peers/$servername" > - echo "#name \"${servername}\";" > "$filename" > - json_get_var key key > - echo "key \"${key}\";" >> "$filename" > - json_get_var address address > - json_get_var port port > - echo "remote \"${address}\" port ${port};" >> "$filename" > - echo "" >> "$filename" > - echo "float yes;" >> "$filename" > - fi > - json_select ".." # back to vpn > - Index=$(( Index + 1 )) > - done > - json_select ".." # back to root > -} > - > # Only do something if file is there and not empty; otherwise exit 1 > -if [ -s "$hoodfile" ]; then > - if [ ! -d /tmp/fastd_fff_peers ]; then > - # first run after reboot > - mkdir /tmp/fastd_fff_peers > - make_config > - # start fastd only if there are some peers > - [ "$(ls /etc/fastd/fff/peers/* 2>/dev/null)" ] && /etc/init.d/fastd start > - else > - make_config > - /etc/init.d/fastd reload > +[ -s "$hoodfile" ] || exit 1 > + > +# source functions > +for file in /etc/vpn-select.d/*; do > + . "$file" > +done > + > +# load hoodfile > +json_load "$(cat "$hoodfile")" > +json_select vpn > + > +# configure vpn > +index=1 Stimmt das, hier mit Index 1 anzufangen? Ich konnte das dem jshn.sh auf die schnelle nicht entnehmen. > +while json_select "$index" > /dev/null ; do > + json_get_var protocol protocol > + "${protocol}_config" Was passiert, wenn ein VPN Protokoll nicht unterstützt wird (aka keine passende Funktion gesourced wurde)? Kann man das irgendwie abfangen und dann eine entsprechende Meldung ausgeben? Da das ganze für jeden Peer einmal aufgerufen wird, würde ich da zu tendieren es ${protocol}_addpeer() o.ä. zu nennen. Wenn wir neue Protokolle hinzufügen möchten, dann brauchen wir vermutlich einen Weg, Protokolle zu bevorzugen, da batman das nicht kann. Da müssen wir uns noch was einfallen lassen, das sollte aber ein lösbares Problem sein. Abgesehen davon gefällts mir, danke! :-) Schlussendlich bleibt für mich die Frage, ob das ganze überhaupt ein eigenes Paket wert ist. Aber das ist eine Frage für einen anderen Patch. Gruß Fabian
hi On 18.06.20 11:50, Fabian Bläse wrote: >> +while json_select "$index" > /dev/null ; do >> + json_get_var protocol protocol >> + "${protocol}_config" > Was passiert, wenn ein VPN Protokoll nicht unterstützt wird (aka keine passende Funktion gesourced wurde)? Spätens wenn wir 2 Protokolle unterstützen muss ein Weg her, was passieren soll wenn ein Router nur Protokoll X unterstützt die Hood aber nur Protokoll Y (soll ja schon mal vorgekommen sein, hab ich gehört, irgendwo ;)). Ist zu anfang vllt. noch kein Problem aber wie man letztens bemerkt hat kanns schnell eins werden. Gruß Christian
Hi, s. inline Am 18.06.20 um 11:50 schrieb Fabian Bläse: > Hallo Robert, > > gute Sache. Folgende Anmerkungen: > > On 17.06.20 23:46, Robert Langhammer wrote: >> diff --git a/src/packages/fff/fff-vpn-select/files/usr/sbin/vpn-select b/src/packages/fff/fff-vpn-select/files/usr/sbin/vpn-select >> index 30883f5..bf9c199 100755 >> --- a/src/packages/fff/fff-vpn-select/files/usr/sbin/vpn-select >> +++ b/src/packages/fff/fff-vpn-select/files/usr/sbin/vpn-select >> @@ -1,65 +1,36 @@ >> #!/bin/sh >> >> # Usage: vpn-select <path-to-hood-file> >> +# To add a new protocol, put a file with two functions to /etc/vpn-select.d/ . >> +# The function ${protocol}_config is called for every peer in hoodfile. >> +# The second function ${protocol}_start_stop is called once per installed protocol > Hier könnte man noch erwähnen, dass sämtliche alte Config beim Sourcen entfernt werden muss. Kommt auf das Protokoll an und wie man dann die config schreibt. Bei fastd in unserer Version ist das so. > Eigentlich gefällt mir das nicht so recht, eine ${protocol}_clear() Funktion wäre mir dafür glaube ich irgendwie lieber. Da bin ich mir noch nicht sicher wie man das am flexibelsten/schönsten mach. Soll ja auch nicht so kompliziert werden. Das configurehood reicht mir da schon jedesmal wenn man rein schaut. > >> +[ -s "$hoodfile" ] || exit 1 >> + >> +# source functions >> +for file in /etc/vpn-select.d/*; do >> + . "$file" >> +done >> + >> +# load hoodfile >> +json_load "$(cat "$hoodfile")" >> +json_select vpn >> + >> +# configure vpn >> +index=1 > Stimmt das, hier mit Index 1 anzufangen? Ich konnte das dem jshn.sh auf die schnelle nicht entnehmen. Ja, beginnt mit 1. > >> +while json_select "$index" > /dev/null ; do >> + json_get_var protocol protocol >> + "${protocol}_config" > Was passiert, wenn ein VPN Protokoll nicht unterstützt wird (aka keine passende Funktion gesourced wurde)? > Kann man das irgendwie abfangen und dann eine entsprechende Meldung ausgeben? Es kommt so eine kleine Fehlermeldung: /usr/sbin/vpn-select: line 27: testproto_config: not found fand ich ok. Kann man aber auch schöner machen. "vpn-protokoll testproto nicht unterstützt" oder so ähnlich. Eigentlich egal, das liest keiner. Und der Routeraufsteller draussen kann da eh nix machen. Ist Gatewaybetreiber-Sache. > Da das ganze für jeden Peer einmal aufgerufen wird, würde ich da zu tendieren es ${protocol}_addpeer() o.ä. zu nennen. das ist leicht :) > > Wenn wir neue Protokolle hinzufügen möchten, dann brauchen wir vermutlich einen Weg, Protokolle zu bevorzugen, da batman das nicht kann. > Da müssen wir uns noch was einfallen lassen, das sollte aber ein lösbares Problem sein. Da gehört auch Christians Anmerkung dazu. Das ist aber etwas konzeptionelles. Da müssen wir uns nochmal Gedanken machen. Mal sehen, was noch kommt, dann gibt es eine v2. Viele Grüße Robert > > Abgesehen davon gefällts mir, danke! :-) > Schlussendlich bleibt für mich die Frage, ob das ganze überhaupt ein eigenes Paket wert ist. Aber das ist eine Frage für einen anderen Patch. > > Gruß > Fabian >
vpn-select is an old relic and did not reflect the opportunities of our hoodfile. This rewrite makes vpn-select modular to easely add new vpn-protocols. The stuff dependent on the vpn-protocol is outsourced to files in /etc/vpn-select.d and comes in with the respective vpn package. Signed-off-by: Robert Langhammer <rlanghammer@web.de> --- .../fff-vpn-select/files/usr/sbin/vpn-select | 83 ++++++------------- 1 file changed, 27 insertions(+), 56 deletions(-) -- 2.20.1