-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path00-main.go
executable file
·362 lines (316 loc) · 10.7 KB
/
00-main.go
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
The MIT License (MIT)
Copyright (c) 2016 aerth
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// cosgo is an easy to use contact form server for any purpose
package main
import (
"flag"
"fmt"
"log"
"math/rand"
"net"
"net/http"
"net/http/fcgi"
_ "net/http/pprof"
"os"
"os/signal"
"strconv"
"syscall"
"time"
"github.com/aerth/mbox"
"github.com/aerth/seconf"
"github.com/gorilla/csrf"
)
var logo = `
_
___ ___ ___ __ _ ___ | |
/ __/ _ \/ __|/ _ |/ _ \| |
| (_| (_) \__ \ (_| | (_) |_|
\___\___/|___/\__, |\___/(_)
|___/
`
/*
https://github.com/aerth/cosgo
*/
var (
version = "0.9.3" // Use Makefile for precise version
err error
hitcounter int
inboxcount int
timeboot = time.Now()
cwd = os.Getenv("PWD")
// flags (all 20 of them)
portnum = flag.Int("port", 8080, "Server: `port` to listen on\n\t")
bind = flag.String("bind", "0.0.0.0", "Server: `interface` to bind to\n\t")
sslport = flag.String("tls", "0.0.0.0:443", "Server: `interface:port` to bind (SSL/TLS)o\n\t")
debug = flag.Bool("debug", false, "Logging: More verbose.\n")
sitename = flag.String("title", "Contact Form", "Config: Site name. Available as a template variable.\n")
configcreate = flag.Bool("new", false, "Config: Create.\n")
configlocation = flag.String("config", ".cosgorc", "Config: Location.\n")
quiet = flag.Bool("quiet", false, "Logging: Less output. See -nolog\n")
nolog = flag.Bool("nolog", false, "Logging: Logs to /dev/null")
fastcgi = flag.Bool("fastcgi", false, "Use fastcgi (for with nginx etc)")
secure = flag.Bool("secure", false, "HTTPS only.")
logfile = flag.String("log", "stdout", "Logging: Use a log `file` instead of stdout\n\tExample: cosgo -log cosgo.log -debug\n")
cookie = flag.String("cookie", "cosgo", "Custom cookie+form field name\n")
gpg = flag.String("gpg", "", "GPG: Path to ascii-armored `public-key` to encrypt mbox\n)")
sendgridKey = flag.String("sg", "", "Sendgrid: Sendgrid API `key` (disables mbox)\n")
dest = flag.String("to", "", "Email: Your email `address` (-sg flag required)\n")
mboxfile = flag.String("mbox", "cosgo.mbox", "Email: Custom mbox file `name`\n\tExample: cosgo -mbox custom.mbox\n\t")
refreshTime = flag.Duration("refresh", time.Hour, "How often to change the POST URL Key")
path2key = flag.String("key", "", "Path to SSL Key")
path2cert = flag.String("cert", "", "Path to SSL Cert")
)
func setup() *Cosgo {
log.SetPrefix("cosgo>")
cosgo := new(Cosgo)
cosgo.r = nil
cosgo.boot = time.Now()
cosgo.rw.Lock()
t1 := time.Now()
kee := generateURLKey(40)
cosgo.URLKey = kee
if *debug && !*quiet {
log.Printf("Generated URL Key %q in %v", cosgo.URLKey, time.Now().Sub(t1))
}
cosgo.rw.Unlock()
// Seconf encoded configuration file (recommended)
if seconf.Exists(*configlocation) {
config, errar := seconf.ReadJSON(*configlocation)
if errar != nil {
log.Println(errar)
log.Fatalf("Bad config. Please remove the %q file and try again.", *configlocation)
os.Exit(1)
}
if config.Fields["bind"] == nil || config.Fields["gpg"] == nil || config.Fields["port"] == nil || config.Fields["name"] == nil || config.Fields["cookie-key"] == nil {
log.Fatalf("Bad config. Please remove the %q file and try again.", *configlocation)
}
cosgo.Bind = config.Fields["bind"].(string)
*sitename = config.Fields["name"].(string)
cosgo.Port = config.Fields["port"].(string)
*gpg = config.Fields["gpg"].(string)
cosgo.antiCSRFkey = []byte(config.Fields["cookie-key"].(string))
} else if *configcreate { // Config does not exist, user is asking to make one.
fmt.Println("Welcome to Cosgo!")
seconf.LockJSON(*configlocation, "", map[string]string{
"bind": "Bind to what address? \n0.0.0.0 for all, 127.0.0.1 for local/tor only",
"port": "Which port to listen on?",
"cookie-code": "32/64 bit cookie key. Good to make this random. Important not to change across reboots.",
"name": "What is your site called? Available as a template variable",
"gpg": "Location of GPG public key. Enter \"none\" to disable",
})
os.Exit(0)
} else {
// No configuration detected
}
// Version information
if !*quiet {
fmt.Println(logo)
fmt.Printf("\n\tcosgo v" + version + "\n")
fmt.Printf("\tCopyright 2016 aerth\n\tSource code at https://github.com/aerth/cosgo\n")
fmt.Println(os.Args)
}
cosgo.initialize()
if !*quiet {
log.Println("booted:", timeboot)
log.Println("working dir:", cwd)
log.Println("css/js/img dir:", cosgo.staticDir)
log.Println("cosgo templates dir:", cosgo.templatesDir)
}
// Disable mbox completely if using sendgrid
if *sendgridKey != "" {
*mboxfile = os.DevNull
}
mbox.Destination = cosgo.Destination
mbox.Open(*mboxfile)
// Set destination email for mbox and sendgrid
if *dest != "" {
cosgo.Destination = *dest
}
go fortuneInit() // Spin fortunes
return cosgo
}
func main() {
// Create the server, load mbox and fortunes and run initialize
cosgo := setup()
// Set all the needed /url paths
e := cosgo.route(cwd)
if e != nil {
log.Fatalln(e)
}
// Needs to be compiled with build tag 'debug' to be redefined, and -debug CLI flag to be activated
if *debug {
cosgo.debug()
}
cosgo.Bind = *bind
cosgo.Port = strconv.Itoa(*portnum)
log.Println("Refreshing every", *refreshTime)
go func() {
time.Sleep(100 * time.Millisecond)
log.Println("Listening on", cosgo.Bind+":"+cosgo.Port)
}()
// Try to bind
listener, binderr := net.Listen("tcp", cosgo.Bind+":"+cosgo.Port)
if binderr != nil {
log.Println(binderr)
os.Exit(1)
}
if cosgo.antiCSRFkey == nil {
cosgo.antiCSRFkey = anticsrfGen()
}
if *path2cert != *path2key {
go cosgo.ServeSSL()
}
// Is nolog enabled?
if *nolog {
*logfile = os.DevNull
}
// stdout or a filename
openLogFile()
// Start Serving
// Here we either use fastcgi or normal http server, using csrf and mux.
// with custom csrf error handler and 10 minute cookie.
if !*fastcgi {
go func() {
if listener != nil {
go http.Serve(listener,
csrf.Protect(cosgo.antiCSRFkey,
csrf.HttpOnly(true),
csrf.FieldName(*cookie),
csrf.CookieName(*cookie),
csrf.Secure(*secure), csrf.MaxAge(600), csrf.ErrorHandler(http.HandlerFunc(csrfErrorHandler)))(cosgo.r))
} else {
log.Fatalln("nil listener")
}
}()
} else {
go func() {
if listener != nil {
go fcgi.Serve(listener,
csrf.Protect(cosgo.antiCSRFkey,
csrf.HttpOnly(true),
csrf.FieldName(*cookie),
csrf.CookieName(*cookie),
csrf.Secure(*secure), csrf.MaxAge(600), csrf.ErrorHandler(http.HandlerFunc(csrfErrorHandler)))(cosgo.r))
} else {
log.Fatalln("nil listener")
}
}()
}
for {
select {
// Fire up the cosgo engine
case <-time.After(*refreshTime):
cosgo.rw.Lock()
if *debug && !*quiet {
log.Println("Info: Generating Random 40 URL Key...")
}
t1 := time.Now()
// set a random URL key (40 char length).
kee := generateURLKey(40)
cosgo.URLKey = kee
if *debug && !*quiet {
log.Printf("Generated URL Key %q in %v", cosgo.URLKey, time.Now().Sub(t1))
}
cosgo.rw.Unlock()
// every X minutes change the URL key (default 42 minutes)
// break tests uncomment next line
//*refreshTime = time.Nanosecond
if !*quiet {
log.Printf("Uptime: %s (%s)", time.Since(timeboot), humanize(time.Since(timeboot)))
log.Printf("Hits: %v", hitcounter)
log.Printf("Messages: %v", inboxcount)
if *debug {
log.Printf("Port: %v", cosgo.Port)
}
if *path2cert != "" {
log.Println("TLS: ON")
}
}
// loop
}
}
}
func init() {
// Key Generator
rand.Seed(time.Now().UnixNano())
// Hopefully a clean exit if we get a sig
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, syscall.SIGINT, syscall.SIGHUP, syscall.SIGKILL)
go func() {
select {
case signal := <-interrupt:
fmt.Println()
log.Printf("Got %s signal, Goodbye!", signal)
log.Println("Boot time:", humanize(time.Since(timeboot)))
log.Println("Messages saved:", inboxcount)
log.Println("Hits:", hitcounter)
os.Exit(0)
}
}()
flag.Parse()
}
func anticsrfGen() []byte {
log.Println("Generating antiCSRFkey based on \"today\"")
/*
CSRF key is important, but doesn't need to change often.
And I don't expect users to set their own.
This method changes at most once a day, and makes sure each cosgo instance's key is unique.
*/
today := strconv.Itoa(int(time.Now().Truncate(24 * time.Hour).Unix()))
here, _ := os.Getwd()
// Add working directory
if len(here) < 23 { // short cwd
today += here + here
} else { // long cwd
today += here[len(here)-23 : len(here)-1]
}
// Increase length
for {
if len([]byte(today)) < 64 { // short cwd, add today
today += strconv.Itoa(int(time.Now().Truncate(24 * time.Hour).Unix()))
} else {
break
}
}
sixtyfour := []byte(today[0:64])
log.Printf("CSRF: %q", string(sixtyfour))
return sixtyfour
// 64 bit
// Example: CSRF: 1476230400/tmp/tstcosgo/tmp/tstcosgo1476230400147623040014762304
// Or: CSRF: 1476230400hub.com/aerth/cosgo/bi14762304001476230400147623040014
// In /home: CSRF: 1476230400/home/ftp/home/ftp147623040014762304001476230400147623
// All are 64 in length, reasonably secure, and unique. And none will change before tomorrow.
}
// ServeSSL serves cosgo on port 443 with attached key+cert
func (c *Cosgo) ServeSSL() {
go func() {
time.Sleep(100 * time.Millisecond)
log.Println("Cosgo: Serving TLS on", *sslport)
}()
log.Fatalln(http.ListenAndServeTLS(*sslport, *path2cert, *path2key,
csrf.Protect(c.antiCSRFkey,
csrf.HttpOnly(true),
csrf.FieldName(*cookie),
csrf.CookieName(*cookie),
csrf.Secure(true),
csrf.MaxAge(600),
csrf.ErrorHandler(http.HandlerFunc(csrfErrorHandler)),
)(c.r)))
}