-
Notifications
You must be signed in to change notification settings - Fork 0
/
sslayer
executable file
·167 lines (136 loc) · 3.94 KB
/
sslayer
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/bin/sh
set -o errexit
set -o nounset
set -o pipefail
CA_ROOT_DIR="$HOME/Library/Application Support/mkcert"
fatal() {
echo "ERROR: $*"
exit 1
}
log() {
echo "INFO: $*"
}
mark_last_setup() {
mkdir -p "$HOME/Library/Application Support/sslayer"
date '+%s' > "$HOME/Library/Application Support/sslayer/.last_setup"
}
configure_dnsmasq() {
brew_prefix=$(brew --prefix)
mkdir -p "$brew_prefix/etc"
mkdir -p "$brew_prefix/etc/dnsmasq.d/"
cat << EOF > "$brew_prefix/etc/dnsmasq.conf"
conf-dir=$brew_prefix/etc/dnsmasq.d/,*.conf
EOF
cat << EOF > "$brew_prefix/etc/dnsmasq.d/goodeggs-dev.conf"
address=/goodeggs.dev/127.0.0.1
address=/goodeggs.test/127.0.0.1
EOF
}
configure_dns_resolver() {
sudo mkdir -p /etc/resolver
sudo tee /etc/resolver/goodeggs.dev > /dev/null <<EOF
nameserver 127.0.0.1
EOF
sudo tee /etc/resolver/goodeggs.test > /dev/null << EOF
nameserver 127.0.0.1
EOF
}
generate_ssl_certificate() {
mkdir -p "$CA_ROOT_DIR"
docker run \
--rm \
--name mkcert \
--mount "type=bind,src=$CA_ROOT_DIR,dst=/root/.local/share/mkcert" \
goodeggs/mkcert \
'*.goodeggs.test'
sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain "$CA_ROOT_DIR/rootCA.pem"
}
migrate_from_legacy_sslayer() {
# Stop legacy sslayer
if [ "$(brew services list | grep sslayer | awk '{print $2}')" = "started" ]; then
log "Stopping legacy sslayer..."
sudo brew services stop sslayer
fi
}
#
# Commands
#
setup() {
# Check to ensure the user is logged in (this will succeed if the user is already logged in)
docker login
# If upgrading from sslayer v1, make sure to stop it
migrate_from_legacy_sslayer
# Generate a new root certificate if one does not yet exist
if [ ! -f "$CA_ROOT_DIR/rootCA.pem" ]; then
generate_ssl_certificate
fi
# Configure OS X to send all requests to *.goodeggs.test to localhost
configure_dnsmasq
configure_dns_resolver
log "Finished setting up sslayer. Run \`sslayer start\` to start sslayer."
mark_last_setup
}
start() {
if ! pgrep dnsmasq > /dev/null; then
sudo brew services start dnsmasq
fi
# If sslayer is already running, bail.
if [ "$(docker ps --all --filter "name=^sslayer$" --filter "status=running" --format "{{.ID}}")" != "" ]; then
fatal "sslayer is already running."
fi
# If the container already exists, start it.
if [ "$(docker ps --all --filter "name=^sslayer$" --filter "status=exited" --format "{{.ID}}")" != "" ]; then
log "Starting existing sslayer container."
docker start sslayer > /dev/null
log "Started sslayer."
return
fi
# If no container exists, create one.
log "Creating a sslayer container."
# https://github.com/moby/moby/pull/40007
extra_args=""
if [ -n "${SSLAYER_DOCKER_MACHINE:-}" ]; then
extra_args+=" --add-host 'host.docker.internal:$(docker-machine ip)'"
fi
docker run -d \
--name sslayer \
--restart unless-stopped \
--mount "type=bind,src=$CA_ROOT_DIR,dst=/usr/local/share/ca-certificates" \
--mount "type=bind,src=$CA_ROOT_DIR/certs,dst=/etc/nginx/certs" \
-p 80:80 \
-p 443:443 \
$extra_args \
goodeggs/sslayer:osx > /dev/null
log "Started sslayer."
}
stop() {
sudo brew services stop dnsmasq > /dev/null 2>&1 || true
docker stop sslayer > /dev/null
}
# Don't use sudo in this command, it gets run in Homebrew's post_install hook which does not permit
# sudo.
update() {
if [ ! -f "$HOME/Library/Application Support/sslayer/.last_setup" ]; then
log "Cannot update sslayer before running setup."
return
fi
log "Stopping sslayer..."
if [ "$(docker ps --all --filter "name=^sslayer$" --format "{{.ID}}" | wc -l)" -gt 0 ]; then
docker rm -f sslayer
fi
docker pull goodeggs/sslayer:osx
start
}
#
# Main
#
main() {
case "${1-}" in
setup) setup "${2-}" ;;
start) start "${2-}" ;;
stop) stop "${2-}" ;;
update) update "${2-}" ;;
*) fatal "usage: slayer [setup | server | stop | update]" ;;
esac
}
main "$@"