-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
174 lines (162 loc) · 4.53 KB
/
main.go
File metadata and controls
174 lines (162 loc) · 4.53 KB
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
168
169
170
171
172
173
174
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
//
package main
import (
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"os/signal"
"regexp"
"text/template"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
)
const SEP = string(os.PathSeparator)
var (
httpPort = flag.String("http", "80", "http service address")
httpsPort = flag.String("https", "443", "https service address")
hostname = flag.String("host", "localhost", "domain or host name")
dbpath = flag.String("dbpath", "database", "database path")
certFile = flag.String("cert", "cert.pem", "SSL certificate file")
keyFile = flag.String("key", "key.pem", "SSL key file")
public = flag.String("public", "public", "public web directory")
clientTempl *template.Template
)
// isTLS checks for TLS and returns true if handshake is complete or false if not.
func isTLS(r *http.Request) bool {
if r.TLS != nil && r.TLS.HandshakeComplete {
return true
}
return false
}
// getArgs splits a slice of bytes into a slice of string arguments.
// Anything in '', "", or `` are consider a single argument (including spaces).
func getArgs(b []byte) (s []string) {
re := regexp.MustCompile("`([\\S\\s]*)`|('([\\S \\t\\r]*)'|\"([\\S ]*)\"|\\S+)")
args := re.FindAllSubmatch(b, -1)
for _, val := range args {
s = append(s, string(val[0]))
}
return
}
// serveWs serves the websocket and starts the listener on successful connection.
func serveWs(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, "Method not allowed", 405)
return
}
if r.Header.Get("Origin") != "https://"+r.Host {
http.Error(w, "Origin not allowed", 403)
return
}
ws, err := websocket.Upgrade(w, r, nil, 1024, 1024)
if _, ok := err.(websocket.HandshakeError); ok {
http.Error(w, "Not a websocket handshake", 400)
return
} else if err != nil {
log.Println(err)
return
}
defer ws.Close()
var c = client{ws: ws, address: ws.RemoteAddr().String(),
user: user{Name: guestName()}, command: &sysCommands}
log.Println(c.address, r.URL, "connected")
c.innerHTML("#status-box", "<b>"+c.user.Name+"</b>")
e := c.listener()
if e != nil && e != io.EOF {
log.Println(e)
}
c.user.logout()
log.Println(c.address, "disconnected")
}
// serveClient is the handler that serves the client html on initial connection.
func serveClient(w http.ResponseWriter, r *http.Request) {
log.Println(r.RemoteAddr, r.Referer(), r.URL, "connecting")
if r.URL.Path != "/" {
http.Error(w, "Not found", 404)
return
}
if r.TLS == nil {
log.Println("redirecting")
getAddr := func() string {
if *httpsPort != ":443" {
return "https://" + *hostname + ":" + *httpsPort
} else {
return "https://" + *hostname
}
}
http.Redirect(w, r, getAddr(), 301)
return
}
if r.Method != "GET" {
http.Error(w, "Method nod allowed", 405)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
type data struct {
SockUrl, Status string
}
sockUrl := "wss://" + *hostname + ":" + *httpsPort + "/ws"
clientTempl.Execute(w, data{SockUrl: sockUrl})
}
// pathExists returns true if the path exists or false if it doesn't.
func pathExists(path string) bool {
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
return false
}
panic(err)
}
return true
}
func init() {
flag.Parse()
dirs := map[string]os.FileMode{*public: 0755, *dbpath: 0700}
for path, perm := range dirs {
if pathExists(path) {
err := os.Chmod(path, perm)
if err != nil {
log.Fatal(err)
}
} else {
err := os.Mkdir(path, perm)
if err != nil {
log.Fatal(err)
}
}
}
clientTempl = template.Must(template.ParseFiles(*public + SEP + "client.html"))
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/", serveClient)
r.HandleFunc("/ws", serveWs)
https := ":" + *httpsPort
http.Handle("/", r)
http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir(*public))))
loadUserDB()
go func() {
// cert.pem is ssl.crt + *server.ca.pem
fmt.Println("Listening at " + "https://" + *hostname + https)
err := http.ListenAndServeTLS(https, *certFile, *keyFile, nil)
if err != nil {
log.Fatal("ListenAndServeTLS:", err)
}
}()
go func() {
err := http.ListenAndServe(":"+*httpPort, nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill)
s := <-c
fmt.Printf("Caught %s signal. Shutting down.\n", s)
closeUserDB()
}