最近在使用OpenWrt的DDNS发现,如果使用write_log来实现日志输出,中文不会显示出来,会出现格式错误。对dynamic_dns_functions.sh的源代码进行细读发现,write_log()函数会调用urlencode()函数,而该函数是不支持中文的。

对该函数改为支持中文的URL编码即可,将urlencode(){}改写代码如下:
注:dynamic_dns_functions.sh文件地址一般为 /usr/lib/ddns/

urlencode() {
local __ENC
[ $# -ne 2 ] && write_log 12 "Error calling 'urlencode()' - wrong number of parameters"
__ENC=$(echo "$2" | awk 'BEGIN {
        split ("1 2 3 4 5 6 7 8 9 A B C D E F", hextab, " ")
        hextab [0] = 0
        for (i=1; i<=255; ++i) {
            ord [ sprintf ("%c", i) "" ] = i + 0
        }
    }
    {
        encoded = ""
        for (i=1; i<=length($0); ++i) {
            c = substr ($0, i, 1)
            if ( c ~ /[a-zA-Z0-9.-]/ ) {
                encoded = encoded c             # safe character
            } else if ( c == " " ) {
                encoded = encoded "+"   # special handling
            } else {
                # unsafe character, encode it as a two-digit hex-number
                lo = ord [c] % 16
                hi = int (ord [c] / 16);
                encoded = encoded "%" hextab [hi] hextab [lo]
            }
        }
        print encoded
    }')
eval "$1=\"$__ENC\""
return 0
}

目前试过了多种方法,还是需要将urlencode()函数增加中文支持,直接更改write_log()函数还是会报错,应该是ddns某些内容需要进行url编码才能正确读取。

本文由 小TiD笔记 发布在小TiD笔记,转载此文请保持文章完整性,并请附上文章来源(小TiD笔记)及本页链接。

原文链接: https://www.tidnotes.ga/2020/06/ddns-writelog.html
扫描分享到社交APP
夜间模式