mwarning / zerotier-openwrt

A OpenWrt package for ZeroTier One - Pull requests are welcome!
669 stars 140 forks source link

zerotier: add support for allowDefault and others #119

Closed generalinterest closed 1 month ago

generalinterest commented 1 month ago

Hi,

This is the the code I added to /etc/init.d/zerotier. it’s just adding variables, performing a config_get for them.
The clever bit is in add_join, where it simply writes these var’s to the network’s local.conf if the vars are present. This is safe because any preexisting local.conf is already deleted!

mwarning commented 1 month ago

Sorry, I di not have much time. The code looks like there should be a more elegant approach.

generalinterest commented 1 month ago

Hi @mwarning

How about this, where add_join calls a new function add_join_local. This is named to indicate the parameters come from the /etc/config/zerotier "join" statement and destined for it's local.conf file.

The add_join_local takes two parameters, the full string of the join statement, and the second for the string to find and retrieve it's value.

The first parameter on the join is the network id, so we skip that and take all remaining fields. Then the remaining string is split on a comma delimiter into separate lines. It is then easy to find the string we are looking for and retrieve it's value as everything after the = sign.

It's then just a matter of appending that to the network.local.conf file where zerotier-one will pick it up.

What do you think?

        add_join_local() {

                # split the parameters into individual lines for easier parsing.
                # we just need the value of what the specific parameter equels to.

                local params=$( echo "$1" | cut -d ',' -f 2-)

                local allow_val=$( echo "$params" | tr ',' '\n' | grep -w "$2" | sed 's/^.*=//' )
                if [ $allow_val ] && [ -n allow_val ]; then
                        echo "$2=$allow_val" >> $path/networks.d/$ZTnetwork.local.conf
                fi
        }

        add_join() {
                # an (empty) config file will cause ZT to join a network

                local ZTnetwork=$(echo "$1" | cut -d ',' -f 1 )

                touch $path/networks.d/$ZTnetwork.conf

                add_join_local $1 "allowManaged"
                add_join_local $1 "allowGlobal"
                add_join_local $1 "allowDefault"
                add_join_local $1 "allowDNS"
        }

All the best Brian

ogarcia commented 1 month ago

@mwarning since we have merged #120 I think we can close this PR. :wink: