如何使用 bash 脚本处理 Chrome Native Messaging API 调用?
我成功地用Python完成了这个任务这个例子
当然,我可以使用
bash
从 python 代码中调用 subprocess
,但是是否可以跳过 python 并直接处理 bash
中的消息?
有问题的部分是将 JSON 序列化消息读入变量。该消息使用 JSON、UTF-8 编码进行序列化,并通过 stdin 以本机字节顺序添加 32 位消息长度。
echo $*
仅输出:
chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/
还有类似的东西
read
echo $REPLY
不输出任何内容。没有 JSON 消息的迹象。 Python 为此使用
struct.unpack
。可以在bash
完成吗?
我建议不要使用 (bash) shell 脚本作为本机消息传递主机,因为 bash 的局限性太大,无法发挥作用。
不带任何参数的read
在终止前读取整行,而本机消息协议指定前四个字节指定以下消息的长度(按本机字节顺序)。
Bash 是处理二进制数据的糟糕工具。
read
命令的改进版本将指定 -n N
参数以在 N
字符(注意:不是字节)之后停止读取,并指定 -r
来删除某些处理。例如。下面的代码会将前四个字符存储在名为 var_prefix
的变量中:
IFS= read -rn 4 var_prefix
即使您假设这将前四个字节存储在变量中(事实并非如此!),那么您也必须将字节转换为整数。我是否已经提到过 bash 会自动删除所有 NUL 字节?这一特性使得 Bash 作为一个功能齐全的本机消息传递主机完全毫无价值。
您可以通过忽略前几个字节来解决此缺点,并在发现
{
字符(JSON 格式请求的开头)时开始解析结果。之后,您必须读取所有输入,直到找到输入的末尾。您需要一个 JSON 解析器,当遇到 JSON 字符串末尾时,该解析器会停止读取输入。祝你写得好运。
生成输出更容易,只需使用
echo -n
或 printf
。
这是一个最小的示例,假设输入以
}
结尾,读取它(不进行处理)并回复结果。虽然这个演示有效,但我强烈建议不要使用 bash,而是使用更丰富的(脚本)语言,例如 Python 或 C++。
#!/bin/bash
# Loop forever, to deal with chrome.runtime.connectNative
while IFS= read -r -n1 c; do
# Read the first message
# Assuming that the message ALWAYS ends with a },
# with no }s in the string. Adopt this piece of code if needed.
if [ "$c" != '}' ] ; then
continue
fi
message='{"message": "Hello world!"}'
# Calculate the byte size of the string.
# NOTE: This assumes that byte length is identical to the string length!
# Do not use multibyte (unicode) characters, escape them instead, e.g.
# message='"Some unicode character:\u1234"'
messagelen=${#message}
# Convert to an integer in native byte order.
# If you see an error message in Chrome's stdout with
# "Native Messaging host tried sending a message that is ... bytes long.",
# then just swap the order, i.e. messagelen1 <-> messagelen4 and
# messagelen2 <-> messagelen3
messagelen1=$(( ($messagelen ) & 0xFF ))
messagelen2=$(( ($messagelen >> 8) & 0xFF ))
messagelen3=$(( ($messagelen >> 16) & 0xFF ))
messagelen4=$(( ($messagelen >> 24) & 0xFF ))
# Print the message byte length followed by the actual message.
printf "$(printf '\\x%x\\x%x\\x%x\\x%x' \
$messagelen1 $messagelen2 $messagelen3 $messagelen4)%s" "$message"
done
消息的读取和写入可以在纯bash中完成,无需使用任何子shell。
棘手的部分是读取前 4 个字节,但这可以通过设置
LC_ALL=C
来完成,这将使 bash 将每个字节视为一个字符,并为 read
使用以下选项:
-n 1
一次读取一个字符(=字节)-r
防止反斜杠处理-d ''
让 read
在读取空字节后返回 0我们可以使用
printf %d "'C"
将字符转换为其数字表示形式。
获得长度后,我们可以使用
read -r -N "$len"
从输入中准确读取那么多字节。
LC_ALL=C IFS=
readmsg() {
# return non-zero if fewer than 4 bytes of input available
# or if message is shorter than length specified by first
# 4 bytes of input
# otherwise, set the variable $msg and return 0
local -i i n len=0
for ((i=0; i<4; i++)); do
read -r -d '' -n 1 || return
printf -v n %d "'$REPLY"
len+='n<<i*8'
done
read -r -N "$len" && ((${#REPLY}==len)) && msg=$REPLY
}
sendmsg() {
local x
printf -v x %08X "${#1}"
printf %b%s "\x${x:6:2}\x${x:4:2}\x${x:2:2}\x${x:0:2}" "$1"
}
while readmsg; do
# do_something "$msg"
response='{"echo": "foo"}'
sendmsg "$response"
done
这就是我最终在 Bash 中使用 GNU Coreutils
head
和 tail
(它们确实创建子进程)来执行此操作,以将输入从主机回显给客户端。
#!/bin/bash
# Bash Native Messaging host
set -x
set -o posix
getMessage() {
# https://lists.gnu.org/archive/html/help-bash/2023-06/msg00036.html
# length=$(busybox dd iflag=fullblock bs=4 count=1 | busybox od -An -td4)
# message=$(busybox dd iflag=fullblock bs=$((length)) count=1)
length=$(head -q -z --bytes=4 -| od -An -td4 -)
message=$(head -q -z --bytes=$((length)) -)
sendMessage "$message"
}
# https://stackoverflow.com/a/24777120
sendMessage() {
message="$*"
# Calculate the byte size of the string.
# NOTE: This assumes that byte length is identical to the string length!
# Do not use multibyte (unicode) characters, escape them instead, e.g.
# message='"Some unicode character:\u1234"'
messagelen=${#message}
# Convert to an integer in native byte order.
# If you see an error message in Chrome's stdout with
# "Native Messaging host tried sending a message that is ... bytes long.",
# then just swap the order, i.e. messagelen1 <-> messagelen4 and
# messagelen2 <-> messagelen3
messagelen1=$(((messagelen) & 0xFF))
messagelen2=$(((messagelen >> 8) & 0xFF))
messagelen3=$(((messagelen >> 16) & 0xFF))
messagelen4=$(((messagelen >> 24) & 0xFF))
# Print the message byte length followed by the actual message.
printf "$(printf '\\x%x\\x%x\\x%x\\x%x' \
$messagelen1 $messagelen2 $messagelen3 $messagelen4)%s" "$message"
}
main() {
while true; do
getMessage
done
}
main