LoginSignup
0
0

ash と Caddy サーバで ActivityPub のインスタンス(15) unfollow.sh の修正

Posted at

unfollow.sh の修正

(10) で unfollow.sh を作ったが、これは自分が送ったフォローリクエストの json をカレントディレクトリに置いてのものだった。一々 post ディレクトリからそれを探し取り出してカレントディレクトリに置くのも面倒なので、フォローリクエストを承認したファイルが置いてある ./receive/accept_done のそのファイルの、 json の object を取り出す。
元々の unfollow.sh の

read -p "What request do you Undo?: " file_for_undo
echo


# grep でカレントディレクトリから undo したいリクエストの "ファイルの中身(json)" を取得
follow_json=$(cat "$file_for_undo")

URL=$(curl https://$to_instance/.well-known/webfinger?resource=acct:$to@$to_instance | jq -r '.links[] | select (.rel == "self")'.href | tr -d "\"")
INBOX=$(curl -H "Accept: application/activity+json" $URL | jq .inbox | tr -d "\"")
REQUEST_TARGET=$(echo  "/"$(sed -E 's/^.*(https):\/\/([^/]+).([^/]+)/\3/g' <<< $INBOX))

# ./receive/accept_done から自分のフォローを承認したファイルを探し、そこから自分のフォローリクエストの json を取り出す 
follow_json=$(grep -rh "$URL" ./receive/accept_done | grep "{" | jq -c .object | tail -n 1)

に変える。さらに下の

URL=$(curl https://$to_instance/.well-known/webfinger?resource=acct:$to@$to_instance | jq -r '.links[] | select (.rel == "self")'.href | tr -d "\"")
INBOX=$(curl -H "Accept: application/activity+json" $URL | jq .inbox | tr -d "\"")
REQUEST_TARGET=$(echo  "/"$(sed -E 's/^.*(https):\/\/([^/]+).([^/]+)/\3/g' <<< $INBOX))

は消しておく。フォロー/アンフォローを繰り返した場合、同じアクターから複数の Accept ファイルがあるはずだから、最後のだけからフォローリクエストを取り出すため tail -n 1 を付ける。
また、

grep -v $URL followings > >(VAR=$(cat) && echo "$VAR" > followings)

を追加している。following ファイルからアンフォローしたアクターを消すためだ。
これも一応、全文を記しておく。

unfollow.sh
#!/bin/bash


read -p "@myself@my_instance: " MY
MY_arry=(${MY//@/ })
myself=${MY_arry[0]}
my_instance=${MY_arry[1]}
echo

read -p "@to@to_instance: " TO
TO_arry=(${TO//@/ })
to=${TO_arry[0]}
to_instance=${TO_arry[1]}
echo


URL=$(curl https://$to_instance/.well-known/webfinger?resource=acct:$to@$to_instance | jq -r '.links[] | select (.rel == "self")'.href | tr -d "\"")
INBOX=$(curl -H "Accept: application/activity+json" $URL | jq .inbox | tr -d "\"")
REQUEST_TARGET=$(echo  "/"$(sed -E 's/^.*(https):\/\/([^/]+).([^/]+)/\3/g' <<< $INBOX))

# ./receive/accept_done から自分のフォローを承認したファイルを探し、そこから自分のフォローリクエストの json を取り出す 
follow_json=$(grep -rh "$URL" ./receive/accept_done | grep "{" | jq -c .object | tail -n 1)


unixtime=$(date "+%s")
date_now=$(date -uR  | head -c -6 | sed -e 's/$/GMT/')

sed -e "s/{id}/$unixtime/g" ./json_templates/unfollow.json > live
sed -i "s/{myself}/$myself/g" live
sed -i "s/{my_instance}/$my_instance/g" live
sed -i "s|{follow-json}|$follow_json|g" live

body=$(cat live | jq -c .)
digest_val=$(echo -n "$body" | sha256sum | xxd -r -p | base64)

signature_headers="(request-target): post ${REQUEST_TARGET}\ndate: ${date_now}\nhost: ${to_instance}\ndigest: SHA-256=${digest_val}"
echo -e "$signature_headers" > 4_sig_headers # 後に見返すためにファイルに
sig_val=$(echo -en $signature_headers | openssl dgst -binary -sign ./personal/private -sha256 | openssl enc -A -base64)
signature="Signature: keyId=\"https://${my_instance}/users/${myself}#main-key\",algorithm=\"rsa-sha256\",headers=\"(request-target) date host digest\",signature=\"$sig_val\""

curl -v -H "Date: $date_now" -H "Digest: SHA-256=$digest_val" -H "$signature" $INBOX -d "$body"

grep -v $URL followings > >(VAR=$(cat) && echo "$VAR" > followings)

echo $body > ./post/$unixtime

参考記事

bashでファイルを処理して結果を同じファイルに上書きする

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0