summaryrefslogtreecommitdiffstats
path: root/usr/bin/autowifi
blob: 6b9a090c0b9f7adabc5b4f273f0eac13f01022df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/bin/sh
cd $(dirname $(readlink -f $0))


interface=${interface:-wlan0}
root=${root:-../../}
crackdir=$root/usr/lib/autowifi/plugins
wifi_keys=$root/etc/autowifi/wifi_keys
wifi_log=$root/var/log/autowifi.log
painmode=${painmode:-}

# exists() run_hooks()
. $root/usr/lib/autowifi/lib/core


. $root/usr/lib/autowifi/lib/network

# start_wpa_supplicant()
. $root/usr/lib/autowifi/lib/wpa_supplicant


connect(){
    #mac ssid encryption key

    run_hooks interface pre 
    run_hooks profile pre 

    connect_wifi "$@" 

    ip_start dhcp 

    if check_gateway && check_internet; then
        echo yay internet >&2

        run_hooks interface post
        run_hooks profile post

        write_profile "$@"
        return 0
    fi
    return 1
}
write_profile(){
    bandw=$(check_bandwidth)
    ( cat $wifi_keys | grep -v '^#' | grep -v "|$1|" ; echo "$2|$1|$bandw|$4" ) | ( echo "#SSID|MAC|BANDWIDTH|KEY";sort )| uniq > "${wifi_keys}2"
    mv "${wifi_keys}2" "$wifi_keys"
}


find_count_of_ssid(){
    c=0
    for i in `seq 1 $WIFI_COUNT`; do
        eval SSID=\${ESSID_${i}}
        if [ "$SSID" = "$1" ]; then
            c+=1
            echo "$i"
        fi
    done
    if [ $c -eq 0 ];then
        exit 1
    fi
    exit 0
}


connect_to_network_by_ssid(){
    find_count_of_ssid "$1" | (while read i
    do
        loop_over_cracks "$i" && exit 0
    done;exit 1)
    if [ $? -eq 0 ]; then
        exit 0
    fi
    echo "no network found :("
    exit 1
}

connect_with_pw(){
    find_count_of_ssid "$1" | (while read i
    do
        KEY="$2"
        eval connect \"\${MAC_${i}}\" \"\${ESSID_${i}}\" \${ENCRYPTION_${i}} \"\${KEY}\"
        if [ $? -eq 0 ]; then
            exit 0
        fi
    done;exit 1)
}
wifi_init(){
    wpa_supplicant_is_usable || start_wpa_supplicant /tmp/autowifi.wpa_supplicant
    wifi_scan > /tmp/${interface}.scan
    . /tmp/${interface}.scan
}
loop_over_networks(){

    wifi_init
    for i in `seq 1 $WIFI_COUNT`; do
        loop_over_cracks "$i"
        if [ $? -eq 0 ]; then
            return 0
        fi
    done
}

loop_over_cracks(){
    i=$1
    KEY=''
    for crack in $(find $crackdir -type f | sort -u); do
        KEY="$(eval root=$root painmode=$painmode \$crack \"\${ESSID_${i}}\" \"\${MAC_${i}}\" \${FREQ_${i}} \${ENCRYPTION_${i}})"
        if [ $? -eq 0 ]; then
            eval connect \"\${MAC_${i}}\" \"\${ESSID_${i}}\" \${ENCRYPTION_${i}} \"\${KEY}\"
            if [ $? -eq 0 ]; then
                return 0
            fi
        fi
    done
    return 1
}
loop_cracks_over_networks(){
    wifi_init
    for crack in $(find $crackdir -type f | sort -u); do
        for i in `seq 1 $WIFI_COUNT`; do
            KEY="$(eval root=$root \$crack \"\${ESSID_${i}}\" \"\${MAC_${i}}\" \${FREQ_${i}} \${ENCRYPTION_${i}})"
            if [ $? -eq 0 ]; then
                eval connect \"\${MAC_${i}}\" \"\${ESSID_${i}}\" \${ENCRYPTION_${i}} \"\${KEY}\"
                if [ $? -eq 0 ]; then
                    return 0
                fi
            fi
        done
    done

}

#scan_all(){
#    for i in `seq 1 $WIFI_COUNT`; do
#        loop_over_cracks $i
#    done
#}
#
#scan_unscanned(){
#    find_unscanned_networks | (while read i
#    do
#        loop_over_cracks $i
#    done)
#}

#find_unscanned_networks(){
#    #TODO broken
#    for i in `seq 1 $WIFI_COUNT`; do
#        eval SSID=\${ESSID_${i}}
#        eval MAC=\${MAC_${i}}
#        cat $wifi_stats 2>/dev/null | (while IFS='|' read SSID MAC BANDW KEY; do
#            if [ "$1" = "$SSID" -a "$2" = "$MAC" ]; then
#                continue
#            fi
#        done; echo $i)
#    done
#    exit 0
#}

wifi_init
if [ -n "$2" ]; then
    echo connecting to $1 with pw $2
    connect_with_pw "$1" "$2"
elif [ -n "$1" ]; then
    echo connecting to $1
    connect_to_network_by_ssid "$1"
else
    echo looping network now 
    check_internet || loop_cracks_over_networks

    while sleep 10; do
        if ! check_gateway; then
            loop_cracks_over_networks
        fi
    done
fi