Merge pull request #11353 from kvuorine/fwknop-fixes
[feed/packages.git] / net / ddns-scripts / files / usr / lib / ddns / update_cnkuai_cn.sh
1 # inside url we need domain, username and password
2 [ -z "$domain" ] && write_log 14 "Service section not configured correctly! Missing 'domain'"
3 [ -z "$username" ] && write_log 14 "Service section not configured correctly! Missing 'username'"
4 [ -z "$password" ] && write_log 14 "Service section not configured correctly! Missing 'password'"
5
6 local urlCp='http://cp.cnkuai.cn/'
7 local urlLogin='http://cp.cnkuai.cn/userlogin.asp'
8 local urlCaptcha='http://cp.cnkuai.cn/inc/image.asp'
9 local urlDnsA='http://cp.cnkuai.cn/dns_a.asp'
10 local urlDnsAAAA='http://cp.cnkuai.cn/dns_ipv6.asp'
11 local urlDnsSave='http://cp.cnkuai.cn/dns_save.asp'
12
13 getPixel(){
14 local filename=$1
15 local x=$(($2*3))
16 local y=$(($3*3))
17 local width=48
18
19 hexdump -s "$((x+width*y))" -n 3 -e '3/1 "%02X"' "$filename"
20 }
21
22 captchaChar(){
23 local filename=$1
24 local xoffset=$2
25
26 if [ "$(getPixel "$filename" $((xoffset+2)) 5)" = '000000' ]; then
27 echo '1'
28 elif [ "$(getPixel "$filename" $((xoffset+5)) 7)" = '000000' ]; then
29 echo '2'
30 elif [ "$(getPixel "$filename" $((xoffset+4)) 3)" = '000000' ]; then
31 echo '4'
32 elif [ "$(getPixel "$filename" $((xoffset+6)) 4)" = '000000' ]; then
33 echo '7'
34 elif [ "$(getPixel "$filename" $((xoffset+5)) 8)" = '000000' ]; then
35 echo '8'
36 elif [ "$(getPixel "$filename" $((xoffset+6)) 8)" = '000000' ]; then
37 echo '9'
38 elif [ "$(getPixel "$filename" $((xoffset+5)) 6)" = '000000' ]; then
39 echo '3'
40 elif [ "$(getPixel "$filename" $((xoffset+0)) 4)" = '000000' ]; then
41 echo '5'
42 elif [ "$(getPixel "$filename" $((xoffset+1)) 5)" = '000000' ]; then
43 echo '6'
44 else
45 echo '0'
46 fi
47 }
48
49 captcha(){
50 local str
51 str=$(captchaChar "$1" 9)
52 str=$str$(captchaChar "$1" 18)
53 str=$str$(captchaChar "$1" 26)
54 str=$str$(captchaChar "$1" 35)
55 echo "$str"
56 }
57
58 #clean
59 rm /tmp/cnkuai.*
60 #login to cnkuai dns cp
61 curl -c '/tmp/cnkuai.cookiejar' "$urlCaptcha" | gif2rgb > /tmp/cnkuai.rgb || return 1
62 yzm=$(captcha "/tmp/cnkuai.rgb")
63 curl -b '/tmp/cnkuai.cookiejar' -c '/tmp/cnkuai.cookiejar' -H "Content-Type: application/x-www-form-urlencoded" -H "Referer: $urlCp" -d "userid=$URL_USER&password=$URL_PASS&yzm=$yzm&B1=%C8%B7%C8%CF%B5%C7%C2%BD&lx=0&userlx=3" -X POST "$urlLogin" > /dev/null || return 1
64
65 if [ "$use_ipv6" -eq 0 ]; then
66 curl -b '/tmp/cnkuai.cookiejar' -c '/tmp/cnkuai.cookiejar' "$urlDnsA" > /tmp/cnkuai.html || return 1
67 else
68 curl -b '/tmp/cnkuai.cookiejar' -c '/tmp/cnkuai.cookiejar' "$urlDnsAAAA" > /tmp/cnkuai.html || return 1
69 fi
70 local domainline
71 domainline=$(awk "/<td>$domain<\/td>/{ print NR; exit }" /tmp/cnkuai.html)
72 local domainid
73 domainid=$(awk "NR==$((domainline+3))" /tmp/cnkuai.html | sed 's/^.*name=\x27domainid\x27 value="//g' | sed 's/".*$//g')
74 local dnslistid
75 dnslistid=$(awk "NR==$((domainline+3))" /tmp/cnkuai.html | sed 's/^.*name=\x27dnslistid\x27 value="//g' | sed 's/".*$//g')
76
77 local data
78
79 if [ "$use_ipv6" -eq 0 ]; then
80 data="T2=$__IP&T3=120&act=dns_a_edit&domainid=$domainid&dnslistid=$dnslistid&B1=%D0%DE%B8%C4"
81 else
82 data="T2=$__IP&T3=120&act=dns_ipv6_edit&domainid=$domainid&dnslistid=$dnslistid&B1=%D0%DE%B8%C4"
83 fi
84 curl -b '/tmp/cnkuai.cookiejar' -c '/tmp/cnkuai.cookiejar' -H "Content-Type: application/x-www-form-urlencoded" -H "Referer: $urlDnsA" -d "$data" -X POST "$urlDnsSave" > /dev/null || return 1
85
86 return 0