-
Notifications
You must be signed in to change notification settings - Fork 2
/
iimatey
executable file
·93 lines (90 loc) · 3.53 KB
/
iimatey
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
#!/usr/bin/env bash
# set -xe # Enable for debug
# Use IIMATEY_DIR if set, or default to $HOME/.config/iimatey
export IIMATEY_DIR=${IIMATEY_DIR:-$HOME/.config/iimatey}
mkdir -p $IIMATEY_DIR
# Logfiles collect both stderr and stdout from ttyd and tunnel
export TTYD_LOGFILE=$IIMATEY_DIR/ttyd.log
export TUNNEL_LOGFILE=$IIMATEY_DIR/tunnel.log
# Possibly presist this...
# https://www.man7.org/linux/man-pages/man8/wg.8.html#COMMANDS
# wg genkey :: Generates a random private key in base64 and prints it to standard output.
# You could save this to persist your url.
# By default, for safety let's generate it on the fly each time
# Use IIMATEY_WIREGUARD_KEY if set, or default to running `wg keygen` for a new one each time
#export TUNNEL_WIREGUARD_KEY=${IIMATEY_WIREGUARD_KEY:-$(wg genkey)}
export TUNNEL_WIREGUARD_KEYFILE=${IIMATEY_DIR}/tunnel-key
# Use IIMATEY_API_URL if set, or default to https://try.cloudnative.coop
export TUNNEL_API_URL=${IIMATEY_API_URL:-https://try.sharing.io}
# We install to /usr/local/bin... but on OSX it's not in the path by default
export PATH=/usr/local/bin:$PATH
# Usage ttydmux [status|start|stop] # Default is status
ACTION=${1:-status}
case $ACTION in
status)
# List information about tmux
if tmux has-session -t ii 2>/dev/null 1>/dev/null; then
tmux list-sessions
tmux has-session -t ii
tmux list-windows -t ii
else
echo "No tmux sessions"
fi
# Display how to connect to ttyd
if [[ -e $IIMATEY_DIR/ttyd.pid ]]; then
ps -p $(cat $IIMATEY_DIR/ttyd.pid) 2>&1 >/dev/null &&
echo Connect to ttyd locally via http://localhost:12345
fi
# Display how to connect to tunnel
if [[ -e $IIMATEY_DIR/tunnel.pid ]]; then
ps -p $(cat $IIMATEY_DIR/tunnel.pid) 2>&1 >/dev/null &&
grep "You can now connect" $IIMATEY_DIR/tunnel.log
fi
# Display how to connect to tmux directly
tmux has-session -t ii 2>/dev/null 1>/dev/null &&
echo Connect to tmux locally via: &&
echo tmux at
echo 'USAGE: iimatey [status|start|stop|connect]'
;;
start)
# TODO: nicer logic here to detect available port and start
# ttyd support -p 0 (random port), but have not reliable way to grab it
#export TTYD_PORT=${TTYD_PORT:12346}
ttyd --writable -p 12345 tmux at 2>&1 >$TTYD_LOGFILE &
# ttyd -p 12345 tmux at 2>&1 >$TTYD_LOGFILE &
echo $! >$IIMATEY_DIR/ttyd.pid
echo ttyd logs are available in $TTYD_LOGFILE
tunnel localhost:12345 2>&1 >$TUNNEL_LOGFILE &
echo $! >$IIMATEY_DIR/tunnel.pid
echo tunnel logs are available in $TUNNEL_LOGFILE
sleep 1 # Would be good to just wait till URL is available
export IIMATEY_URL=$(grep "You can now connect" $IIMATEY_DIR/tunnel.log | sed 'sX.*\(https://.*\)X\1X')
# echo Connect to tmux locally via: &&\
# echo tmux at
# if $( tmux has-session -t ii ); then
if [ $(tmux ls -t ii 2>/dev/null | wc -l) -eq 0 ]; then
tmux new -e IIMATEY=true -s ii -P -F "$IIMATEY_URL" -n matey
echo $! >$IIMATEY_DIR/tmux.pid
else
tmux at
fi
;;
stop)
# we won't stop tmux... let's leave it
if [[ -e $IIMATEY_DIR/ttyd.pid ]]; then
kill $(cat $IIMATEY_DIR/ttyd.pid) && rm $IIMATEY_DIR/ttyd.pid
fi
if [[ -e $IIMATEY_DIR/tunnel.pid ]]; then
kill $(cat $IIMATEY_DIR/tunnel.pid) && rm $IIMATEY_DIR/tunnel.pid
fi
;;
connect)
# Create new, or connect to old (doesn't start ttyd or tunnel)
if $(tmux has-session -t ii); then
tmux at
else
tmux new -e IIMATEY=true -s ii
echo $! >$IIMATEY_DIR/tmux.pid
fi
;;
esac