-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·147 lines (125 loc) · 5.27 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·147 lines (125 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/sh
# Install moshpit-proxy, and set this computer up to open Moshpit sites.
#
# curl -fsSL https://raw.githubusercontent.com/profullstack/moshpit-proxy/main/install.sh | sh
#
# Installs to your home directory and needs no root for the code itself. The
# setup step asks once before changing anything, and on macOS will ask for your
# password because the system store needs it.
#
# If piping a script from the internet into a shell makes you uneasy, good — read
# it first:
#
# curl -fsSL .../install.sh -o install.sh && less install.sh && sh install.sh
#
set -eu
REPO="profullstack/moshpit-proxy"
REF="${MOSHPIT_REF:-main}"
PREFIX="${MOSHPIT_PREFIX:-${XDG_DATA_HOME:-$HOME/.local/share}/moshpit-proxy}"
BINDIR="${MOSHPIT_BIN:-$HOME/.local/bin}"
ACTION="install"
RUN_TRUST=1
ASSUME_YES=""
RED=''; BOLD=''; DIM=''; OFF=''
if [ -t 2 ]; then RED=$(printf '\033[31m'); BOLD=$(printf '\033[1m'); DIM=$(printf '\033[2m'); OFF=$(printf '\033[0m'); fi
say() { printf '%s\n' "$*" >&2; }
step() { printf '%s==>%s %s\n' "$BOLD" "$OFF" "$*" >&2; }
warn() { printf '%swarning:%s %s\n' "$RED" "$OFF" "$*" >&2; }
die() { printf '%serror:%s %s\n' "$RED" "$OFF" "$*" >&2; exit 1; }
have() { command -v "$1" >/dev/null 2>&1; }
while [ $# -gt 0 ]; do
case "$1" in
--uninstall) ACTION="uninstall" ;;
--no-trust) RUN_TRUST=0 ;;
--yes|-y) ASSUME_YES="--yes" ;;
--prefix) PREFIX="${2:?--prefix needs a path}"; shift ;;
--bin) BINDIR="${2:?--bin needs a path}"; shift ;;
--ref) REF="${2:?--ref needs a git ref}"; shift ;;
-h|--help)
cat >&2 <<EOF
usage: install.sh [options]
--uninstall remove the install, and undo the browser setup
--no-trust install the code only, skip the browser setup
--yes do not ask before the browser setup
--prefix <dir> where the code goes (default: $PREFIX)
--bin <dir> where the shims go (default: $BINDIR)
--ref <tag|branch|sha> what to install (default: main)
environment: MOSHPIT_PREFIX, MOSHPIT_BIN, MOSHPIT_REF
EOF
exit 0 ;;
*) die "unknown option: $1 (try --help)" ;;
esac
shift
done
if [ "$ACTION" = "uninstall" ]; then
if [ -x "$PREFIX/bin/moshpit-trust.ts" ] && have node; then
step "undoing the browser setup"
node "$PREFIX/bin/moshpit-trust.ts" --uninstall || warn "could not undo the browser setup"
fi
step "removing $PREFIX"
rm -rf "$PREFIX"
for shim in moshpit-proxy moshpit-pin moshpit-trust; do rm -f "$BINDIR/$shim"; done
say "Removed."
exit 0
fi
# ---------------------------------------------------------------- runtime
have node || die "node is required (v24 or newer)"
# Checked by capability rather than by version string, for the same reason
# moshpit-transport does it: a Node built without the pieces we need passes a
# version check and then fails at the first connection. Dynamic SNI and TLS 1.3
# are the two this proxy cannot work without.
node -e '
const tls = require("node:tls");
const [maj] = process.versions.node.split(".").map(Number);
if (maj < 24) { console.error("node " + process.versions.node + " is too old"); process.exit(1); }
if (typeof tls.createSecureContext !== "function") { console.error("node:tls is incomplete"); process.exit(1); }
' || die "this node cannot run the proxy — install Node 24 or newer"
have openssl || die "openssl is required (the local key is generated with it)"
# ---------------------------------------------------------------- fetch
step "installing $REPO@$REF to $PREFIX"
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT INT TERM
url="https://codeload.github.com/$REPO/tar.gz/$REF"
if have curl; then
curl -fsSL "$url" -o "$tmp/src.tgz" || die "download failed: $url"
elif have wget; then
wget -qO "$tmp/src.tgz" "$url" || die "download failed: $url"
else
die "need curl or wget"
fi
mkdir -p "$tmp/src"
tar -xzf "$tmp/src.tgz" -C "$tmp/src" --strip-components=1 || die "could not unpack the download"
rm -rf "$PREFIX"
mkdir -p "$(dirname "$PREFIX")"
mv "$tmp/src" "$PREFIX"
# ---------------------------------------------------------------- shims
mkdir -p "$BINDIR"
for shim in moshpit-proxy moshpit-pin moshpit-trust; do
[ -f "$PREFIX/bin/$shim.ts" ] || continue
cat > "$BINDIR/$shim" <<EOF
#!/bin/sh
exec node "$PREFIX/bin/$shim.ts" "\$@"
EOF
chmod +x "$BINDIR/$shim"
done
case ":$PATH:" in
*":$BINDIR:"*) ;;
*) warn "$BINDIR is not on your PATH — add it to use \`moshpit-proxy\` by name" ;;
esac
# ---------------------------------------------------------------- setup
if [ "$RUN_TRUST" = "1" ]; then
step "setting up your browsers"
# Straight to the terminal, not through this script's stdin: when the whole
# installer arrived via `curl | sh`, stdin is the script itself and a prompt
# would read the rest of the file as the answer.
if [ -n "$ASSUME_YES" ] || [ ! -r /dev/tty ]; then
node "$PREFIX/bin/moshpit-trust.ts" ${ASSUME_YES:+--yes} || warn "browser setup did not finish — run \`moshpit-trust\` yourself"
else
node "$PREFIX/bin/moshpit-trust.ts" < /dev/tty || warn "browser setup did not finish — run \`moshpit-trust\` yourself"
fi
fi
say ""
say "${BOLD}Installed.${OFF}"
say " ${DIM}start the proxy:${OFF} moshpit-proxy"
say " ${DIM}redo the setup:${OFF} moshpit-trust"
say " ${DIM}undo everything:${OFF} install.sh --uninstall"