From 8d4d68c840f97112dc9cbe737e2e7117e48ea628 Mon Sep 17 00:00:00 2001
From: Zack Scholl <zack.scholl@gmail.com>
Date: Sat, 19 Mar 2016 11:31:47 -0400
Subject: [PATCH] Updated and renamed

---
 Makefile                                      |  58 +++++-----
 install/cowyo.init                            | 101 ++++++++++++++++++
 install/{awwkoala.init => cowyo.init~}        |   0
 install/cowyo.nginx                           |  41 +++++++
 install/{awwkoala.nginx => cowyo.nginx~}      |   0
 letsencrypt/Makefile                          |  28 ++---
 letsencrypt/Makefile~                         |  31 ++++++
 letsencrypt/README.md                         |   4 +-
 letsencrypt/README.md~                        |  17 +++
 letsencrypt/cowyo.init                        | 101 ++++++++++++++++++
 letsencrypt/{awwkoala.init => cowyo.init~}    |   0
 .../{awwkoala.ssl.nginx => cowyo.ssl.nginx}   |   0
 letsencrypt/cowyo.ssl.nginx~                  |  50 +++++++++
 13 files changed, 386 insertions(+), 45 deletions(-)
 create mode 100755 install/cowyo.init
 rename install/{awwkoala.init => cowyo.init~} (100%)
 create mode 100755 install/cowyo.nginx
 rename install/{awwkoala.nginx => cowyo.nginx~} (100%)
 create mode 100644 letsencrypt/Makefile~
 create mode 100644 letsencrypt/README.md~
 create mode 100644 letsencrypt/cowyo.init
 rename letsencrypt/{awwkoala.init => cowyo.init~} (100%)
 rename letsencrypt/{awwkoala.ssl.nginx => cowyo.ssl.nginx} (100%)
 create mode 100644 letsencrypt/cowyo.ssl.nginx~

diff --git a/Makefile b/Makefile
index 1f9408c..f4ca73c 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-ADDRESS = awwkoala.com
+ADDRESS = cowyo.com
 PORT = 8003
 
 CUR_DIR = $(shell bash -c 'pwd')
@@ -10,42 +10,42 @@ make:
 install:
 	rm -rf jinstall
 	mkdir jinstall
-	cp install/awwkoala.nginx jinstall/awwkoala.nginx
-	sed -i 's/PORT/$(PORT)/g'  jinstall/awwkoala.nginx
-	sed -i 's/ADDRESS/$(ADDRESS)/g'  jinstall/awwkoala.nginx
-	sed -i 's^CUR_DIR^$(CUR_DIR)^g'  jinstall/awwkoala.nginx
-	cp install/awwkoala.init jinstall/awwkoala.init
-	sed -i 's/EXT_ADDRESS/$(ADDRESS)/g'  jinstall/awwkoala.init
-	sed -i 's^CUR_DIR^$(CUR_DIR)^g'  jinstall/awwkoala.init
-	sed -i 's^USERCUR^$(USERCUR)^g'  jinstall/awwkoala.init
-	sed -i 's^PORT^$(PORT)^g'  jinstall/awwkoala.init
-	cp jinstall/awwkoala.init /etc/init.d/awwkoala.init
-	chmod +x /etc/init.d/awwkoala.init
-	cp jinstall/awwkoala.nginx /etc/nginx/sites-available/awwkoala.nginx
-	ln -fs /etc/nginx/sites-available/awwkoala.nginx /etc/nginx/sites-enabled/awwkoala.nginx
+	cp install/cowyo.nginx jinstall/cowyo.nginx
+	sed -i 's/PORT/$(PORT)/g'  jinstall/cowyo.nginx
+	sed -i 's/ADDRESS/$(ADDRESS)/g'  jinstall/cowyo.nginx
+	sed -i 's^CUR_DIR^$(CUR_DIR)^g'  jinstall/cowyo.nginx
+	cp install/cowyo.init jinstall/cowyo.init
+	sed -i 's/EXT_ADDRESS/$(ADDRESS)/g'  jinstall/cowyo.init
+	sed -i 's^CUR_DIR^$(CUR_DIR)^g'  jinstall/cowyo.init
+	sed -i 's^USERCUR^$(USERCUR)^g'  jinstall/cowyo.init
+	sed -i 's^PORT^$(PORT)^g'  jinstall/cowyo.init
+	cp jinstall/cowyo.init /etc/init.d/cowyo.init
+	chmod +x /etc/init.d/cowyo.init
+	cp jinstall/cowyo.nginx /etc/nginx/sites-available/cowyo.nginx
+	ln -fs /etc/nginx/sites-available/cowyo.nginx /etc/nginx/sites-enabled/cowyo.nginx
 	/etc/init.d/nginx reload
 	/etc/init.d/nginx restart
-	/etc/init.d/awwkoala.init restart
+	/etc/init.d/cowyo.init restart
 	rm -rf jinstall
 
 binaries:
 	rm -rf binaries
-	rm -f awwkoala
+	rm -f cowyo
 	mkdir binaries
-	env GOOS=linux GOARCH=amd64 go build -o awwkoala -v *.go
-	zip -9 -r awwkoala-linux-64bit.zip awwkoala static/* templates/*
-	rm -f awwkoala
-	env GOOS=windows GOARCH=amd64 go build -o awwkoala.exe -v *.go
-	zip -9 -r awwkoala-windows-64bit.zip awwkoala.exe static/* templates/*
-	rm -f awwkoala.exe
-	env GOOS=linux GOARCH=arm go build -o awwkoala -v *.go
-	zip -9 -r awwkoala-raspberrypi.zip awwkoala static/* templates/*
-	rm -f awwkoala
-	env GOOS=darwin GOARCH=amd64 go build -o awwkoala -v *.go
-	zip -9 -r awwkoala-macosx-64bit.zip awwkoala static/* templates/*
-	rm -f awwkoala
+	env GOOS=linux GOARCH=amd64 go build -o cowyo -v *.go
+	zip -9 -r cowyo-linux-64bit.zip cowyo static/* templates/*
+	rm -f cowyo
+	env GOOS=windows GOARCH=amd64 go build -o cowyo.exe -v *.go
+	zip -9 -r cowyo-windows-64bit.zip cowyo.exe static/* templates/*
+	rm -f cowyo.exe
+	env GOOS=linux GOARCH=arm go build -o cowyo -v *.go
+	zip -9 -r cowyo-raspberrypi.zip cowyo static/* templates/*
+	rm -f cowyo
+	env GOOS=darwin GOARCH=amd64 go build -o cowyo -v *.go
+	zip -9 -r cowyo-macosx-64bit.zip cowyo static/* templates/*
+	rm -f cowyo
 	mv *.zip binaries/
 
 
 .PHONY: install
-.PHONY: binaries
\ No newline at end of file
+.PHONY: binaries
diff --git a/install/cowyo.init b/install/cowyo.init
new file mode 100755
index 0000000..ec14215
--- /dev/null
+++ b/install/cowyo.init
@@ -0,0 +1,101 @@
+#!/bin/sh
+
+### BEGIN INIT INFO
+# Provides:          awwkoala
+# Required-Start:    $remote_fs $syslog
+# Required-Stop:     $remote_fs $syslog
+# Default-Start:     2 3 4 5
+# Default-Stop:      0 1 6
+# Short-Description: Start daemon at boot time
+# Description:       Enable service provided by daemon.
+### END INIT INFO
+
+# This should be placed in /etc/init.d directory
+# start with
+# sudo /etc/init.d/awwkoala start
+# stop with
+# sudo /etc/init.d/awwkoala start
+
+name="cowyo"
+dir="CUR_DIR"
+user="USERCUR"
+cmd="./$name -p :PORT EXT_ADDRESS"
+
+pid_file="/var/run/$name.pid"
+stdout_log="/var/log/$name.log"
+stderr_log="/var/log/$name.err"
+
+get_pid() {
+    cat "$pid_file"
+}
+
+is_running() {
+    [ -f "$pid_file" ] && ps `get_pid` > /dev/null 2>&1
+}
+
+case "$1" in
+    start)
+    if is_running; then
+        echo "Already started"
+    else
+        echo "Starting $name"
+        cd "$dir"
+        sudo -u "$user" $cmd >> "$stdout_log" 2>> "$stderr_log" &
+        echo $! > "$pid_file"
+        if ! is_running; then
+            echo "Unable to start, see $stdout_log and $stderr_log"
+            exit 1
+        fi
+    fi
+    ;;
+    stop)
+    if is_running; then
+        echo -n "Stopping $name.."
+        kill `get_pid`
+        for i in {1..10}
+        do
+            if ! is_running; then
+                break
+            fi
+
+            echo -n "."
+            sleep 1
+        done
+        echo
+
+        if is_running; then
+            echo "Not stopped; may still be shutting down or shutdown may have failed"
+            exit 1
+        else
+            echo "Stopped"
+            if [ -f "$pid_file" ]; then
+                rm "$pid_file"
+            fi
+        fi
+    else
+        echo "Not running"
+    fi
+    ;;
+    restart)
+    $0 stop
+    if is_running; then
+        echo "Unable to stop, will not attempt to start"
+        exit 1
+    fi
+    $0 start
+    ;;
+    status)
+    if is_running; then
+        echo "Running"
+    else
+        echo "Stopped"
+        exit 1
+    fi
+    ;;
+    *)
+    echo "Usage: $0 {start|stop|restart|status}"
+    exit 1
+    ;;
+esac
+
+exit 0
diff --git a/install/awwkoala.init b/install/cowyo.init~
similarity index 100%
rename from install/awwkoala.init
rename to install/cowyo.init~
diff --git a/install/cowyo.nginx b/install/cowyo.nginx
new file mode 100755
index 0000000..0e9afd8
--- /dev/null
+++ b/install/cowyo.nginx
@@ -0,0 +1,41 @@
+server {
+	# SERVER BLOCK FOR cowyo
+	listen   80; ## listen for ipv4; this line is default and implied
+
+	access_log /etc/nginx/logs/access-cowyo.log;
+	error_log /etc/nginx/logs/error-cowyo.log info;
+	root CUR_DIR;
+	server_name ADDRESS;
+
+	# Media: images, icons, video, audio, HTC
+	location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
+		expires 1M;
+		access_log off;
+		add_header Cache-Control "public";
+	}
+
+	# CSS and Javascript
+	location ~* \.(?:css|js)$ {
+		expires 1y;
+		access_log off;
+		add_header Cache-Control "public";
+	}
+
+	location ^~ /static  {
+		try_files $uri $uri/ =404;
+	}
+
+	location ~ ^/ {
+		proxy_set_header X-Real-IP $remote_addr;
+		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+		proxy_set_header Host $http_host;
+		proxy_set_header X-NginX-Proxy true;
+
+		proxy_pass http://127.0.0.1:PORT;
+		proxy_redirect off;
+
+		proxy_http_version 1.1;
+		proxy_set_header Upgrade $http_upgrade;
+		proxy_set_header Connection "upgrade";
+	}
+}
diff --git a/install/awwkoala.nginx b/install/cowyo.nginx~
similarity index 100%
rename from install/awwkoala.nginx
rename to install/cowyo.nginx~
diff --git a/letsencrypt/Makefile b/letsencrypt/Makefile
index f433c04..31a35b1 100644
--- a/letsencrypt/Makefile
+++ b/letsencrypt/Makefile
@@ -10,22 +10,22 @@ make:
 install:
 	rm -rf jinstall
 	mkdir jinstall
-	cp awwkoala.ssl.nginx awwkoala.ssl.nginx.temp
-	sed -i 's/PORT/$(PORT)/g'  awwkoala.ssl.nginx.temp
-	sed -i 's/ADDRESS/$(ADDRESS)/g'  awwkoala.ssl.nginx.temp
-	sed -i 's^CUR_DIR^$(CUR_DIR)^g'  awwkoala.ssl.nginx.temp
-	cp awwkoala.init awwkoala.init.temp
-	sed -i 's/EXT_ADDRESS/$(ADDRESS)/g'   awwkoala.init.temp
-	sed -i 's^CUR_DIR^$(CUR_DIR)^g'  awwkoala.init.temp
-	sed -i 's^USERCUR^$(USERCUR)^g'  awwkoala.init.temp
-	sed -i 's^PORT^$(PORT)^g'  awwkoala.init.temp
-	cp awwkoala.init.temp /etc/init.d/awwkoala.init
-	chmod +x /etc/init.d/awwkoala.init
-	cp awwkoala.ssl.nginx.temp /etc/nginx/sites-available/awwkoala.nginx
-	ln -fs /etc/nginx/sites-available/awwkoala.nginx /etc/nginx/sites-enabled/awwkoala.nginx
+	cp cowyo.ssl.nginx cowyo.ssl.nginx.temp
+	sed -i 's/PORT/$(PORT)/g'  cowyo.ssl.nginx.temp
+	sed -i 's/ADDRESS/$(ADDRESS)/g'  cowyo.ssl.nginx.temp
+	sed -i 's^CUR_DIR^$(CUR_DIR)^g'  cowyo.ssl.nginx.temp
+	cp cowyo.init cowyo.init.temp
+	sed -i 's/EXT_ADDRESS/$(ADDRESS)/g'   cowyo.init.temp
+	sed -i 's^CUR_DIR^$(CUR_DIR)^g'  cowyo.init.temp
+	sed -i 's^USERCUR^$(USERCUR)^g'  cowyo.init.temp
+	sed -i 's^PORT^$(PORT)^g'  cowyo.init.temp
+	cp cowyo.init.temp /etc/init.d/cowyo.init
+	chmod +x /etc/init.d/cowyo.init
+	cp cowyo.ssl.nginx.temp /etc/nginx/sites-available/cowyo.nginx
+	ln -fs /etc/nginx/sites-available/cowyo.nginx /etc/nginx/sites-enabled/cowyo.nginx
 	/etc/init.d/nginx reload
 	/etc/init.d/nginx restart
-	/etc/init.d/awwkoala.init restart
+	/etc/init.d/cowyo.init restart
 	rm -rf *.temp
 
 .PHONY: install
diff --git a/letsencrypt/Makefile~ b/letsencrypt/Makefile~
new file mode 100644
index 0000000..f433c04
--- /dev/null
+++ b/letsencrypt/Makefile~
@@ -0,0 +1,31 @@
+ADDRESS = yourserver.com
+PORT = 8003
+
+CUR_DIR = $(shell bash -c 'pwd')
+USERCUR = $(shell bash -c 'whoami')
+
+make:
+	go build
+
+install:
+	rm -rf jinstall
+	mkdir jinstall
+	cp awwkoala.ssl.nginx awwkoala.ssl.nginx.temp
+	sed -i 's/PORT/$(PORT)/g'  awwkoala.ssl.nginx.temp
+	sed -i 's/ADDRESS/$(ADDRESS)/g'  awwkoala.ssl.nginx.temp
+	sed -i 's^CUR_DIR^$(CUR_DIR)^g'  awwkoala.ssl.nginx.temp
+	cp awwkoala.init awwkoala.init.temp
+	sed -i 's/EXT_ADDRESS/$(ADDRESS)/g'   awwkoala.init.temp
+	sed -i 's^CUR_DIR^$(CUR_DIR)^g'  awwkoala.init.temp
+	sed -i 's^USERCUR^$(USERCUR)^g'  awwkoala.init.temp
+	sed -i 's^PORT^$(PORT)^g'  awwkoala.init.temp
+	cp awwkoala.init.temp /etc/init.d/awwkoala.init
+	chmod +x /etc/init.d/awwkoala.init
+	cp awwkoala.ssl.nginx.temp /etc/nginx/sites-available/awwkoala.nginx
+	ln -fs /etc/nginx/sites-available/awwkoala.nginx /etc/nginx/sites-enabled/awwkoala.nginx
+	/etc/init.d/nginx reload
+	/etc/init.d/nginx restart
+	/etc/init.d/awwkoala.init restart
+	rm -rf *.temp
+
+.PHONY: install
diff --git a/letsencrypt/README.md b/letsencrypt/README.md
index c8657dc..aad6151 100644
--- a/letsencrypt/README.md
+++ b/letsencrypt/README.md
@@ -10,8 +10,8 @@ sudo ./letsencrypt-auto certonly --standalone --email youremail@somewhere.com -d
 sudo service nginx start
 ```
 
-Then startup `awwkoala` with
+Then startup `cowyo` with
 
 ```bash
-sudo ./awwkoala -p :8001 -key /etc/letsencrypt/live/yourserver.com/privkey.pem -crt /etc/letsencrypt/live/yourserver.com/cert.pem yourserver.com
+sudo ./cowyo -p :8001 -key /etc/letsencrypt/live/yourserver.com/privkey.pem -crt /etc/letsencrypt/live/yourserver.com/cert.pem yourserver.com
 ```
diff --git a/letsencrypt/README.md~ b/letsencrypt/README.md~
new file mode 100644
index 0000000..c8657dc
--- /dev/null
+++ b/letsencrypt/README.md~
@@ -0,0 +1,17 @@
+First install the NGINX block in this directory. (There is an experimental Makefile that will do this, just try `sudo make install`.
+
+To use letsencrypt follow these steps:
+
+```
+git clone https://github.com/letsencrypt/letsencrypt
+cd letsencrypt
+sudo service nginx stop
+sudo ./letsencrypt-auto certonly --standalone --email youremail@somewhere.com -d yourserver.com
+sudo service nginx start
+```
+
+Then startup `awwkoala` with
+
+```bash
+sudo ./awwkoala -p :8001 -key /etc/letsencrypt/live/yourserver.com/privkey.pem -crt /etc/letsencrypt/live/yourserver.com/cert.pem yourserver.com
+```
diff --git a/letsencrypt/cowyo.init b/letsencrypt/cowyo.init
new file mode 100644
index 0000000..14f403d
--- /dev/null
+++ b/letsencrypt/cowyo.init
@@ -0,0 +1,101 @@
+#!/bin/sh
+
+### BEGIN INIT INFO
+# Provides:          cowyo
+# Required-Start:    $remote_fs $syslog
+# Required-Stop:     $remote_fs $syslog
+# Default-Start:     2 3 4 5
+# Default-Stop:      0 1 6
+# Short-Description: Start daemon at boot time
+# Description:       Enable service provided by daemon.
+### END INIT INFO
+
+# This should be placed in /etc/init.d directory
+# start with
+# sudo /etc/init.d/cowyo start
+# stop with
+# sudo /etc/init.d/cowyo start
+
+dir="CUR_DIR"
+user="USERCUR"
+cmd="./cowyo -p :PORT -key /etc/letsencrypt/live/EXT_ADDRESS/privkey.pem -crt /etc/letsencrypt/live/EXT_ADDRESS/cert.pem yourserver.com./cowyo EXT_ADDRESS"
+
+name="cowyo"
+pid_file="/var/run/$name.pid"
+stdout_log="/var/log/$name.log"
+stderr_log="/var/log/$name.err"
+
+get_pid() {
+    cat "$pid_file"
+}
+
+is_running() {
+    [ -f "$pid_file" ] && ps `get_pid` > /dev/null 2>&1
+}
+
+case "$1" in
+    start)
+    if is_running; then
+        echo "Already started"
+    else
+        echo "Starting $name"
+        cd "$dir"
+        sudo -u "$user" $cmd >> "$stdout_log" 2>> "$stderr_log" &
+        echo $! > "$pid_file"
+        if ! is_running; then
+            echo "Unable to start, see $stdout_log and $stderr_log"
+            exit 1
+        fi
+    fi
+    ;;
+    stop)
+    if is_running; then
+        echo -n "Stopping $name.."
+        kill `get_pid`
+        for i in {1..10}
+        do
+            if ! is_running; then
+                break
+            fi
+
+            echo -n "."
+            sleep 1
+        done
+        echo
+
+        if is_running; then
+            echo "Not stopped; may still be shutting down or shutdown may have failed"
+            exit 1
+        else
+            echo "Stopped"
+            if [ -f "$pid_file" ]; then
+                rm "$pid_file"
+            fi
+        fi
+    else
+        echo "Not running"
+    fi
+    ;;
+    restart)
+    $0 stop
+    if is_running; then
+        echo "Unable to stop, will not attempt to start"
+        exit 1
+    fi
+    $0 start
+    ;;
+    status)
+    if is_running; then
+        echo "Running"
+    else
+        echo "Stopped"
+        exit 1
+    fi
+    ;;
+    *)
+    echo "Usage: $0 {start|stop|restart|status}"
+    exit 1
+    ;;
+esac
+
+exit 0
diff --git a/letsencrypt/awwkoala.init b/letsencrypt/cowyo.init~
similarity index 100%
rename from letsencrypt/awwkoala.init
rename to letsencrypt/cowyo.init~
diff --git a/letsencrypt/awwkoala.ssl.nginx b/letsencrypt/cowyo.ssl.nginx
similarity index 100%
rename from letsencrypt/awwkoala.ssl.nginx
rename to letsencrypt/cowyo.ssl.nginx
diff --git a/letsencrypt/cowyo.ssl.nginx~ b/letsencrypt/cowyo.ssl.nginx~
new file mode 100644
index 0000000..ca59024
--- /dev/null
+++ b/letsencrypt/cowyo.ssl.nginx~
@@ -0,0 +1,50 @@
+server {
+    listen      80;
+    server_name ADDRESS;
+    rewrite     ^   https://$server_name$request_uri? permanent;
+}
+
+server {
+  # SERVER BLOCK FOR ADDRESS
+  listen   443 ssl;
+  ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
+  ssl_certificate         /etc/letsencrypt/live/ADDRESS/fullchain.pem; 
+  ssl_certificate_key     /etc/letsencrypt/live/ADDRESS/privkey.pem; 
+
+	access_log /etc/nginx/logs/access-ADDRESS.log;
+	error_log /etc/nginx/logs/error-ADDRESS.log info;
+	root CUR_DIR;
+	server_name ADDRESS;
+
+	# Media: images, icons, video, audio, HTC
+	location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
+		expires 1M;
+		access_log off;
+		add_header Cache-Control "public";
+	}
+
+	# CSS and Javascript
+	location ~* \.(?:css|js)$ {
+		expires 1y;
+		access_log off;
+		add_header Cache-Control "public";
+	}
+
+	location ^~ /static  {
+		try_files $uri $uri/ =404;
+	}
+
+	location ~ ^/ {
+		proxy_set_header X-Real-IP $remote_addr;
+		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+		proxy_set_header Host $http_host;
+		proxy_set_header X-NginX-Proxy true;
+
+		proxy_pass https://127.0.0.1:PORT;
+		proxy_redirect off;
+
+		proxy_http_version 1.1;
+		proxy_set_header Upgrade $http_upgrade;
+		proxy_set_header Connection "upgrade";
+	}
+}