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

Merge pull request #3 from tscholl2/master

flags+tls

Former-commit-id: 1a75015231dfb0fecd5a40239ffcadaaadcd4ae6 [formerly 4075865c155682705ca076b28d210a5fb60969f2] [formerly 8b53f0c8ff73a3d2c4bd61816fd8dda24cf0b85b [formerly a4b98dd1c1]]
Former-commit-id: 3403edc55069a15be83320f1e0b1ef6e74a2cd11 [formerly 0e7b6e71efd962b404e4d363a441dc164f3c41df]
Former-commit-id: e6d9624e2fc0be4b5d0811b699096bd31ed07d52
This commit is contained in:
Zack 2016-02-07 13:24:14 -05:00
commit 921e5a0ce5
4 changed files with 58 additions and 16 deletions

8
db.go
View File

@ -4,8 +4,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log" "log"
"path"
"runtime"
"time" "time"
"github.com/boltdb/bolt" "github.com/boltdb/bolt"
@ -15,12 +13,10 @@ var db *bolt.DB
var open bool var open bool
// Open to create the database and open // Open to create the database and open
func Open() error { func Open(filename string) error {
var err error var err error
_, filename, _, _ := runtime.Caller(0) // get full path of this file
dbfile := path.Join(path.Dir(filename), "data.db")
config := &bolt.Options{Timeout: 30 * time.Second} config := &bolt.Options{Timeout: 30 * time.Second}
db, err = bolt.Open(dbfile, 0600, config) db, err = bolt.Open(filename, 0600, config)
if err != nil { if err != nil {
fmt.Println("Opening BoltDB timed out") fmt.Println("Opening BoltDB timed out")
log.Fatal(err) log.Fatal(err)

54
main.go
View File

@ -1,26 +1,57 @@
package main package main
import ( import (
"flag"
"fmt" "fmt"
"log" "log"
"os" "path"
"runtime"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
var ExternalIP string // AllowedIPs is a white/black list of
var AllowedIPs string // IP addresses allowed to access cowyo
var AllowedIPs = map[string]bool{
"192.168.1.13": true,
"192.168.1.12": true,
"192.168.1.2": true,
}
func init() { // RuntimeArgs contains all runtime
AllowedIPs = "192.168.1.13,192.168.1.12,192.168.1.2" // arguments available
var RuntimeArgs struct {
ExternalIP string
Port string
DatabaseLocation string
ServerCRT string
ServerKey string
} }
func main() { func main() {
if len(os.Args) == 1 { _, executableFile, _, _ := runtime.Caller(0) // get full path of this file
databaseFile := path.Join(path.Dir(executableFile), "data.db")
flag.StringVar(&RuntimeArgs.Port, "p", ":12312", "port to bind")
flag.StringVar(&RuntimeArgs.DatabaseLocation, "db", databaseFile, "location of database file")
flag.StringVar(&RuntimeArgs.ServerCRT, "crt", "", "location of ssl crt")
flag.StringVar(&RuntimeArgs.ServerKey, "key", "", "location of ssl key")
flag.CommandLine.Usage = func() {
fmt.Println(`cowyo: a websocket notepad
run this to start the server and then visit localhost at the port you specify
(see parameters).
Example: 'cowyo localhost'
Example: 'cowyo -p :8080 localhost'
Example: 'cowyo -db /var/lib/cowyo/db.bolt localhost'
Example: 'cowyo -p :8080 -crt ssl/server.crt -key ssl/server.key localhost'
Options:`)
flag.CommandLine.PrintDefaults()
}
flag.Parse()
RuntimeArgs.ExternalIP = flag.Arg(0)
if RuntimeArgs.ExternalIP == "" {
log.Fatal("You need to specify the external IP address") log.Fatal("You need to specify the external IP address")
} }
ExternalIP = os.Args[1] Open(RuntimeArgs.DatabaseLocation)
Open()
defer Close() defer Close()
// Default page // Default page
@ -34,5 +65,10 @@ func main() {
r.GET("/:title", editNote) r.GET("/:title", editNote)
r.GET("/:title/*option", everythingElse) r.GET("/:title/*option", everythingElse)
r.DELETE("/listitem", deleteListItem) r.DELETE("/listitem", deleteListItem)
r.Run(":12312") if RuntimeArgs.ServerCRT != "" && RuntimeArgs.ServerKey != "" {
r.RunTLS(RuntimeArgs.Port, RuntimeArgs.ServerCRT, RuntimeArgs.ServerKey)
} else {
log.Println("No crt/key found, running non-https")
r.Run(RuntimeArgs.Port)
}
} }

View File

@ -27,7 +27,7 @@ func editNote(c *gin.Context) {
} else { } else {
c.HTML(http.StatusOK, "index.tmpl", gin.H{ c.HTML(http.StatusOK, "index.tmpl", gin.H{
"Title": title, "Title": title,
"ExternalIP": ExternalIP, "ExternalIP": RuntimeArgs.ExternalIP,
}) })
} }
} }

10
ssl/README.md Normal file
View File

@ -0,0 +1,10 @@
# To create sample keys:
```
openssl genrsa -out server.key 2048
openssl req -new -x509 -key server.key -days 3650 -nodes -out server.crt -keyout server.crt
```
## TODO
* check if ed25519 keys work