@@ -8,7 +8,7 @@ function path_inject() {
8
8
(( $DEBUG )) && echo ;
9
9
}
10
10
11
- function root_login () {
11
+ function root_login() {
12
12
local timeout=1800;
13
13
local keyActive=` ssh-add -l | grep ' administrator.dsa' | wc -l` ;
14
14
@@ -40,6 +40,111 @@ function root_login () {
40
40
41
41
}
42
42
43
+ # Figure out how to checksum
44
+ type shasum & > /dev/null
45
+ if [[ $? -eq 0 ]]; then
46
+ CHECKSUM_PROG=shasum
47
+ else
48
+ type sha1sum & > /dev/null
49
+ if [[ $? -eq 0 ]]; then
50
+ CHECKSUM_PROG=sha1sum
51
+ else
52
+ CHECKSUM_PROG=md5sum
53
+ fi
54
+ fi
55
+ # CACHE UUID
56
+ [ -z " $CACHE_UUID " ] && export CACHE_UUID=$( head -1 /dev/urandom | $CHECKSUM_PROG | awk ' {print $1}' ) ;
57
+ # Cache cleaning
58
+ find $HOME /.cache -type f -mtime +1 -exec rm -f {} \; & > /dev/null
59
+ find $HOME /.cache -type d -empty -exec rmdir -f {} \; & > /dev/null
60
+
61
+ function cache_reset() {
62
+ if [ -d " $HOME /.cache/$CACHE_UUID " ]; then
63
+ find " $HOME /.cache/$CACHE_UUID " -maxdepth 1 -type f -exec rm -f {} \;
64
+ fi
65
+ }
66
+
67
+ function cache_protected() {
68
+ # Variables
69
+ local check_name=$1
70
+ local check=$2
71
+ local timeout=3600
72
+
73
+ # Setup cache directory
74
+ [ ! -d " $HOME /.cache" ] && mkdir " $HOME /.cache" ;
75
+
76
+ # Setup a UUID for this session
77
+ [ ! -d " $HOME /.cache/$CACHE_UUID " ] && mkdir " $HOME /.cache/$CACHE_UUID " ;
78
+
79
+ # Only do this if cache is stale
80
+ cachefile=" $HOME /.cache/$CACHE_UUID /$check_name "
81
+ if [ -e " $cachefile " ]; then
82
+ cachetime=$( stat -r $cachefile | awk ' {print $9}' ) ;
83
+ now=$( date +%s) ;
84
+ oldest=$(( $now - $timeout ))
85
+ cachevalue=$( cat $cachefile ) ;
86
+ if [[ $cachetime -gt $oldest ]]; then
87
+ echo $cachevalue ;
88
+ return ;
89
+ fi
90
+ fi
91
+
92
+ # Run the check, get the value
93
+ (( $DEBUG )) && echo " + cache_protected() is running '$check '" ;
94
+ value=$( $check 2> /dev/null) ;
95
+ rc=$? ;
96
+
97
+ # store the and return
98
+ echo " $rc $value " > $cachefile ;
99
+ echo " $rc $value " ;
100
+ }
101
+
102
+ function ip_is_in() {
103
+ local file=$1
104
+
105
+ result=$( cache_protected " get_external_ip" " curl -XGET http://icanhazip.com/" )
106
+ ip=$( echo $result | cut -d' ' -f2)
107
+
108
+ grep $ip $file & > /dev/null
109
+ rc=$?
110
+
111
+ echo $rc ;
112
+ }
113
+
114
+ function check_proxy_wrapper() {
115
+ # Set a proxy in the environment variable MY_PROXY
116
+ program=" $1 "
117
+ args=(" $@ " )
118
+ unset args[0]
119
+
120
+ result=$( ip_is_in $HOME /.work_proxies) ;
121
+ if [ " $result " == " 0" ]; then
122
+ (( $DEBUG )) && echo " + setting proxies to $MY_PROXY " ;
123
+ export HTTP_PROXY=" http://$MY_PROXY /"
124
+ export http_proxy=" http://$MY_PROXY /"
125
+ export https_proxy=" https://$MY_PROXY /"
126
+ else
127
+ (( $DEBUG )) && echo " + removing proxies" ;
128
+ unset HTTP_PROXY
129
+ unset http_proxy
130
+ unset https_proxy
131
+ fi
132
+
133
+ command $program " ${args[@]} " ;
134
+ }
135
+
136
+ function ssh_check_proxy() {
137
+ result=$( ip_is_in $HOME /.work_proxies) ;
138
+ if [ " $result " == " 0" ]; then
139
+ export SSH_PROXY=" int"
140
+ else
141
+ export SSH_PROXY=" ext"
142
+ fi
143
+ (( $DEBUG )) && echo " + ssh is using $SSH_PROXY proxies" ;
144
+
145
+ command ssh $@
146
+ }
147
+
43
148
function contents() {
44
149
if [ -f " $1 " ] && [ -r " $1 " ]; then
45
150
file_lines=` wc -l $1 | awk ' {print $1}' ` ;
0 commit comments