Root/package/6in4/files/6in4.hotplug

1#!/bin/sh
2
3if [ "$ACTION" = ifup ]; then
4    . /etc/functions.sh
5
6    include /lib/network
7    scan_interfaces
8
9    update_tunnel() {
10        local cfg="$1"
11
12        local proto
13        config_get proto "$cfg" proto
14        [ "$proto" = 6in4 ] || return 0
15
16        local wandev
17        config_get wandev "$cfg" wan_device "$(find_6in4_wanif)"
18        [ "$wandev" = "$DEVICE" ] || return 0
19
20        local wanip=$(find_6in4_wanip "$wandev")
21
22        [ -n "$wanip" ] && {
23            lsmod | grep -q ^sit || {
24                logger -t 6in4-update "Tunneling driver not loaded yet, deferring action"
25                exit 0
26            }
27
28            local tunnelid
29            config_get tunnelid "$cfg" tunnelid
30
31            local username
32            config_get username "$cfg" username
33
34            local password
35            config_get password "$cfg" password
36
37            uci_set_state network "$cfg" ipaddr "$wanip"
38
39            [ -n "$tunnelid" ] && [ -n "$username" ] && [ -n "$password" ] && {
40                [ "${#password}" == 32 -a -z "${password//[a-fA-F0-9]/}" ] || {
41                    password="$(echo -n "$password" | md5sum)"; password="${password%% *}"
42                }
43
44                (
45                    local url="http://ipv4.tunnelbroker.net/ipv4_end.php?ip=AUTO&apikey=$username&pass=$password&tid=$tunnelid"
46                    local try=0
47                    local max=3
48
49                    while [ $((++try)) -le $max ]; do
50                        wget -qO/dev/null "$url" 2>/dev/null && {
51                            logger -t 6in4-update "Updated tunnel #$tunnelid endpoint to $wanip"
52                            ifup "$cfg"
53                            break
54                        } || {
55                            logger -t 6in4-update "Try $try/$max failed, retrying"
56                            sleep 1
57                        }
58                    done
59                )&
60            } || {
61                logger -t 6in4-update "Re-establishing tunnel due to change on $INTERFACE ($DEVICE)"
62                ifup "$cfg" &
63            }
64        }
65    }
66
67    config_foreach update_tunnel interface
68fi
69

Archive Download this file



interactive