1
0
mirror of https://github.com/schollz/cowyo.git synced 2023-08-10 21:13:00 +03:00

Updated - websocket switches with SSL

Automatic detection of IP address (for local only)
Added binary building (not ready)
This commit is contained in:
Zack Scholl 2016-02-15 16:59:32 -05:00
parent 59cd27fca2
commit f934edc757
8 changed files with 47 additions and 7 deletions

View File

@ -28,4 +28,14 @@ install:
/etc/init.d/awwkoala.init restart
rm -rf jinstall
binaries:
rm -rf binaries
rm -f awwkoala
mkdir binaries
env GOOS=linux GOARCH=amd64 go build -o awwkoala -v *.go
zip -9 -r awwkoala-linux-amd64.zip awwkoala static/* templates/*
rm -f awwkoala
.PHONY: install
.PHONY: binaries

16
main.go
View File

@ -31,6 +31,7 @@ var RuntimeArgs struct {
ServerKey string
SourcePath string
AdminKey string
Socket string
}
var VersionNum string
@ -45,10 +46,10 @@ func main() {
flag.StringVar(&RuntimeArgs.ServerKey, "key", "", "location of ssl key")
flag.StringVar(&RuntimeArgs.WikiName, "w", "AwwKoala", "custom name for wiki")
flag.CommandLine.Usage = func() {
fmt.Println(`AwwKoala: A Websocket Wiki and Kind Of A List Application
fmt.Println(`AwwKoala (version ` + VersionNum + `): A Websocket Wiki and Kind Of A List Application
run this to start the server and then visit localhost at the port you specify
(see parameters).
Example: 'awwkoala localhost'
Example: 'awwkoala yourserver.com'
Example: 'awwkoala -p :8080 localhost:8080'
Example: 'awwkoala -db /var/lib/awwkoala/db.bolt localhost:8003'
Example: 'awwkoala -p :8080 -crt ssl/server.crt -key ssl/server.key localhost:8080'
@ -58,7 +59,7 @@ Options:`)
flag.Parse()
RuntimeArgs.ExternalIP = flag.Arg(0)
if RuntimeArgs.ExternalIP == "" {
log.Fatal("You need to specify the external IP address")
RuntimeArgs.ExternalIP = GetLocalIP() + RuntimeArgs.Port
}
RuntimeArgs.SourcePath = path.Dir(executableFile)
Open(RuntimeArgs.DatabaseLocation)
@ -95,9 +96,16 @@ Options:`)
r.DELETE("/listitem", deleteListItem)
r.DELETE("/deletepage", deletePage)
if RuntimeArgs.ServerCRT != "" && RuntimeArgs.ServerKey != "" {
RuntimeArgs.Socket = "wss"
fmt.Println("--------------------------")
fmt.Println("AwwKoala is up and running on https://" + RuntimeArgs.ExternalIP)
fmt.Println("--------------------------")
r.RunTLS(RuntimeArgs.Port, RuntimeArgs.ServerCRT, RuntimeArgs.ServerKey)
} else {
log.Println("No crt/key found, running non-https")
RuntimeArgs.Socket = "ws"
fmt.Println("--------------------------")
fmt.Println("AwwKoala is up and running on http://" + RuntimeArgs.ExternalIP)
fmt.Println("--------------------------")
r.Run(RuntimeArgs.Port)
}
}

View File

@ -48,6 +48,7 @@ func editNote(c *gin.Context) {
"NumRows": numRows,
"Versions": versions,
"TotalTime": totalTime,
"SocketType": RuntimeArgs.Socket,
})
} else {
c.HTML(http.StatusOK, "index.tmpl", gin.H{
@ -58,6 +59,7 @@ func editNote(c *gin.Context) {
"NumRows": numRows,
"Versions": versions,
"TotalTime": totalTime,
"SocketType": RuntimeArgs.Socket,
"NoEdit": true,
})
}

View File

@ -44,7 +44,7 @@ $(document).ready(function() {
}
// websockets
url = 'ws://'+external_ip+'/ws';
url = socketType + '://'+external_ip+'/ws';
c = new WebSocket(url);
send = function(data){

View File

@ -14,6 +14,7 @@
<script>
external_ip = '{{ .ExternalIP }}'
title_name = '{{ .Title }}'
socketType = '{{ .SocketType }}'
</script>

View File

@ -28,7 +28,7 @@
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/">{{ .WikiName }}</a>
<a class="navbar-brand" href="/">{{ .Title }}</a>
</div>
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">

View File

@ -28,7 +28,7 @@ a.deleteable {
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/">{{ .WikiName }}</a>
<a class="navbar-brand" href="/">{{ .Title }}</a>
</div>
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">

View File

@ -5,6 +5,7 @@ import (
"io/ioutil"
"log"
"math/rand"
"net"
"path"
"sort"
"strings"
@ -216,3 +217,21 @@ func RandStringBytesMaskImprSrc(n int) string {
return string(b)
}
// GetLocalIP returns the local ip address
func GetLocalIP() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
return ""
}
bestIP := ""
for _, address := range addrs {
// check the address type and if it is not a loopback the display it
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil && (strings.Contains(ipnet.IP.String(), "192.168.1") || strings.Contains(ipnet.IP.String(), "192.168")) {
return ipnet.IP.String()
}
}
}
return bestIP
}