rmfakecloud-proxy/scripts/installer.sh

259 lines
5.6 KiB
Bash
Raw Normal View History

2021-02-12 00:36:22 +01:00
#!/bin/bash
2021-02-12 01:56:32 +01:00
set -e
2024-09-26 20:54:33 +02:00
UNIT_NAME=rmfakecloud-proxy
BINARY=rmfakecloud-proxy
2021-02-12 00:36:22 +01:00
DESTINATION="/home/root/rmfakecloud"
# Create destination folder
function unpack(){
mkdir -p ${DESTINATION}
2021-02-12 01:56:32 +01:00
systemctl stop ${UNIT_NAME} || true
2021-02-12 00:36:22 +01:00
# Find __ARCHIVE__ maker, read archive content and decompress it
ARCHIVE=$(awk '/^__ARCHIVE__/ {print NR + 1; exit 0; }' "${0}")
2021-02-12 01:56:32 +01:00
tail -n+${ARCHIVE} "${0}" | gunzip > ${DESTINATION}/${BINARY}
chmod +x ${DESTINATION}/${BINARY}
2021-02-12 00:36:22 +01:00
}
# marks all as unsynced so that they are not deleted
function fixsync(){
grep sync ~/.local/share/remarkable/xochitl/*.metadata -l | xargs -r sed -i 's/synced\": true/synced\": false/'
2024-11-10 16:04:10 +01:00
}
2021-02-12 00:36:22 +01:00
function install_proxyservice(){
cloudurl=$1
echo "Setting cloud sync to: ${cloudurl}"
workdir=$DESTINATION
2021-02-12 01:56:32 +01:00
cat > /etc/systemd/system/${UNIT_NAME}.service <<EOF
2021-02-12 00:36:22 +01:00
[Unit]
2024-09-26 20:54:33 +02:00
Description=rmfakecloud reverse proxy
2021-02-12 00:36:22 +01:00
#StartLimitIntervalSec=600
#StartLimitBurst=4
After=home.mount
[Service]
Environment=HOME=/home/root
WorkingDirectory=$workdir
2021-09-26 23:50:53 +02:00
ExecStart=$workdir/${BINARY} -cert $workdir/proxy.bundle.crt -key $workdir/proxy.key ${cloudurl}
2021-02-12 00:36:22 +01:00
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
2021-02-12 01:56:32 +01:00
systemctl enable ${UNIT_NAME}
systemctl restart ${UNIT_NAME}
2021-02-12 00:36:22 +01:00
}
function uninstall(){
2021-02-12 01:56:32 +01:00
systemctl stop ${UNIT_NAME}
systemctl disable ${UNIT_NAME}
#rm proxy.key proxy.crt ca.crt ca.srl ca.key proxy.pubkey proxy.csr csr.conf proxy.cfg
2021-02-12 00:36:22 +01:00
rm /usr/local/share/ca-certificates/ca.crt
2021-02-12 01:56:32 +01:00
update-ca-certificates --fresh
2021-09-26 23:50:53 +02:00
rm /etc/systemd/system/${UNIT_NAME}.service
2021-02-12 00:36:22 +01:00
sed -i '/# rmfake_start/,/# rmfake_end/d' /etc/hosts
echo "Marking files as not synced to prevent data loss"
2021-09-26 23:50:53 +02:00
echo "Stopping xochitl..."
systemctl stop xochitl
2021-02-12 00:36:22 +01:00
fixsync
2021-02-12 01:56:32 +01:00
rm -fr $DESTINATION
2021-09-26 23:50:53 +02:00
echo "Restart xochitl for the changes to take effect"
2021-02-12 00:36:22 +01:00
}
function generate_certificates(){
# thanks to https://gist.github.com/Soarez/9688998
cat <<EOF > csr.conf
[ req ]
default_bits = 2048
default_keyfile = proxy.key
encrypt_key = no
default_md = sha256
prompt = no
utf8 = yes
distinguished_name = dn
req_extensions = ext
x509_extensions = caext
[ dn ]
C = AA
ST = QQ
L = JJ
O = the culture
CN = *.appspot.com
[ ext ]
subjectAltName=@san
basicConstraints=CA:FALSE
subjectKeyIdentifier = hash
[ caext ]
subjectAltName=@san
[ san ]
DNS.1 = *.appspot.com
2022-10-28 23:41:02 +02:00
DNS.2 = *.remarkable.com
DNS.3 = *.cloud.remarkable.com
DNS.4 = *.cloud.remarkable.engineering
DNS.5 = *.rmfakecloud.localhost
2024-11-10 16:04:10 +01:00
DNS.6 = *.apphost.com
2021-02-12 00:36:22 +01:00
EOF
# ca
2024-11-10 16:04:10 +01:00
if [ ! -f ca.crt ]; then
2021-09-26 23:50:53 +02:00
echo "Generating CA key and crt..."
2021-02-12 00:36:22 +01:00
openssl genrsa -out ca.key 2048
openssl req -new -sha256 -x509 -key ca.key -out ca.crt -days 3650 -subj /CN=rmfakecloud
2021-09-26 23:50:53 +02:00
rm -f proxy.key
rm -f proxy.pubkey
2021-02-12 00:36:22 +01:00
else
echo "CA exists"
fi
2024-11-10 16:04:10 +01:00
if [ ! -f proxy.key ]; then
2021-09-26 23:50:53 +02:00
echo "Generating private key..."
2021-02-12 00:36:22 +01:00
openssl genrsa -out proxy.key 2048
2021-09-26 23:50:53 +02:00
rm -f proxy.pubkey
2021-02-12 00:36:22 +01:00
else
echo "Private key exists"
fi
2024-11-10 16:04:10 +01:00
if [ ! -f proxy.pubkey ]; then
2021-09-26 23:50:53 +02:00
echo "Generating pub key..."
2021-02-12 00:36:22 +01:00
openssl rsa -in proxy.key -pubout -out proxy.pubkey
2021-09-26 23:50:53 +02:00
rm -f proxy.crt
2021-02-12 00:36:22 +01:00
else
echo "Pub key exists"
fi
2024-11-10 16:04:10 +01:00
if [ ! -f proxy.crt ]; then
2021-09-26 23:50:53 +02:00
echo "Generating csr and crt..."
2024-11-10 16:04:10 +01:00
openssl req -new -config ./csr.conf -key proxy.key -out proxy.csr
2021-02-12 00:36:22 +01:00
# Signing
openssl x509 -req -in proxy.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out proxy.crt -days 3650 -extfile csr.conf -extensions caext
2021-09-26 23:50:53 +02:00
cat proxy.crt ca.crt > proxy.bundle.crt
2021-02-12 00:36:22 +01:00
2021-09-26 23:50:53 +02:00
#echo "showing result"
2024-11-10 16:04:10 +01:00
#openssl x509 -in proxy.bundle.crt -text -noout
2021-02-12 00:36:22 +01:00
2021-09-26 23:50:53 +02:00
echo "Generation complete!"
2021-02-12 00:36:22 +01:00
else
echo "crt exists"
fi
}
2021-02-12 01:56:32 +01:00
2021-02-12 00:36:22 +01:00
function install_certificates(){
certdir="/usr/local/share/ca-certificates"
certname=$certdir/ca.crt
if [ -f $certname ]; then
echo "The cert has been already installed, it will be removed and reinstalled!!!"
rm $certname
update-ca-certificates --fresh
fi
mkdir -p $certdir
2021-02-12 01:56:32 +01:00
cp $DESTINATION/ca.crt $certdir/
2021-02-12 00:36:22 +01:00
update-ca-certificates --fresh
}
function patch_hosts(){
if ! grep rmfake_start /etc/hosts ; then
cat <<EOF >> /etc/hosts
# rmfake_start
127.0.0.1 hwr-production-dot-remarkable-production.appspot.com
127.0.0.1 service-manager-production-dot-remarkable-production.appspot.com
127.0.0.1 local.appspot.com
127.0.0.1 my.remarkable.com
2021-03-21 10:17:58 +01:00
127.0.0.1 ping.remarkable.com
2022-10-28 23:41:02 +02:00
127.0.0.1 internal.cloud.remarkable.com
2022-04-06 21:18:56 +02:00
127.0.0.1 backtrace-proxy.cloud.remarkable.engineering
2024-11-10 16:04:10 +01:00
127.0.0.1 local.apphost.com
2021-02-12 00:36:22 +01:00
# rmfake_end
EOF
fi
}
2021-02-12 01:56:32 +01:00
function getproxy(){
2021-09-26 23:50:53 +02:00
read -p "Enter your own cloud url [http(s)://somehost:port] >" url
2021-02-12 01:56:32 +01:00
echo $url
}
2021-02-12 00:36:22 +01:00
function doinstall(){
2021-09-26 23:50:53 +02:00
echo "Extracting embedded binary..."
2021-02-12 00:36:22 +01:00
unpack
2021-09-26 23:50:53 +02:00
pushd "${DESTINATION}"
2021-02-12 00:36:22 +01:00
generate_certificates
install_certificates
# install proxy
2021-09-26 23:50:53 +02:00
url=$1
if [ -z $url ]; then
url=$(getproxy)
fi
2021-02-12 01:56:32 +01:00
install_proxyservice $url
2021-09-26 23:50:53 +02:00
echo "Patching /etc/hosts"
2021-02-12 00:36:22 +01:00
patch_hosts
2021-09-26 23:50:53 +02:00
echo "Stoping xochitl.."
2021-02-12 00:36:22 +01:00
systemctl stop xochitl
2021-09-26 23:50:53 +02:00
echo "Fixing sync status..."
2021-02-12 00:36:22 +01:00
fixsync
2021-09-26 23:50:53 +02:00
echo "Starting xochitl..."
2021-02-12 00:36:22 +01:00
systemctl start xochitl
2021-09-26 23:50:53 +02:00
popd
2021-02-12 00:36:22 +01:00
}
case $1 in
"uninstall" )
uninstall
2021-09-26 23:50:53 +02:00
;;
2021-02-12 00:36:22 +01:00
"install" )
2021-09-26 23:50:53 +02:00
shift 1
doinstall $1
;;
2021-02-12 00:36:22 +01:00
2021-09-26 23:50:53 +02:00
"gencert" )
generate_certificates
;;
"setcloud" )
2021-02-12 00:36:22 +01:00
shift 1
url=$1
if [ $# -lt 1 ]; then
url=$(getproxy)
fi
2021-02-12 01:56:32 +01:00
install_proxyservice $url
2021-09-26 23:50:53 +02:00
;;
2021-02-12 00:36:22 +01:00
* )
2021-09-26 23:50:53 +02:00
2021-02-12 01:56:32 +01:00
cat <<EOF
2021-09-26 23:50:53 +02:00
rmFakeCloud reverse proxy installer
2021-02-12 01:56:32 +01:00
Usage:
2021-09-26 23:50:53 +02:00
install [cloudurl]
installs and asks for cloud url
2021-02-12 01:56:32 +01:00
uninstall
2021-09-26 23:50:53 +02:00
uninstall, removes everything
gencert
generate certificates
2021-02-12 01:56:32 +01:00
2021-09-26 23:50:53 +02:00
setcloud [cloudurl]
2021-02-12 01:56:32 +01:00
changes the cloud address to
EOF
2021-09-26 23:50:53 +02:00
;;
2021-02-12 00:36:22 +01:00
esac
exit 0
__ARCHIVE__