-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmotion_webmonitor.go
78 lines (74 loc) · 2.02 KB
/
motion_webmonitor.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
package main
import (
"crypto/rand"
"crypto/tls"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/autotls"
"github.com/gin-gonic/gin"
"golang.org/x/crypto/acme/autocert"
"log"
"motion_webmonitor/configread"
"motion_webmonitor/routes"
"net/http"
"os"
"path"
"runtime"
"strconv"
)
func main() {
if len(os.Args) > 1 {
configread.ParseConfigFile(os.Args[1])
} else {
configread.CheckConfig()
}
runtime.GOMAXPROCS(runtime.NumCPU())
gin.SetMode(gin.ReleaseMode)
r := gin.Default()
r.LoadHTMLGlob(path.Join(configread.ViewsDir, "*"))
r.Static("/images", path.Join(configread.PublicDir, "images"))
r.Static("/script", path.Join(configread.PublicDir, "script"))
r.Static("/style", path.Join(configread.PublicDir, "style"))
r.Static("/fonts", path.Join(configread.PublicDir, "fonts"))
sessionKey, err := generateSessionKey(64)
if err != nil {
log.Fatal("Can't generate session key.")
}
store := cookie.NewStore(sessionKey)
store.Options(sessions.Options{
MaxAge: 3600,
Secure: configread.TLSMode,
HttpOnly: true,
Path: "/",
SameSite: http.SameSiteStrictMode,
})
r.Use(sessions.Sessions("motion_webmonitor_session", store))
routes.IndexRoute(r)
routes.AuthRoute(r)
routes.DisconnectRoute(r)
routes.CameraRoute(r)
routes.SurvRoute(r)
routes.SavedFilesRouter(r)
routes.FileViewRoute(r)
routes.CleanFilesRouter(r)
routes.StartStopMotionRoute(r)
if configread.TLSMode {
cfg := tls.Config{
MinVersion: tls.VersionTLS12,
CurvePreferences: []tls.CurveID{tls.CurveP521, tls.CurveP384, tls.CurveP256},
PreferServerCipherSuites: true,
}
m := autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(configread.ServerDomains...),
}
log.Fatal(autotls.RunWithManagerAndTLSConfig(r, &m, cfg))
} else {
r.Run(":" + strconv.Itoa(int(configread.NotSecureModePort)))
}
}
func generateSessionKey(size uint) ([]byte, error) {
key := make([]byte, size)
_, err := rand.Read(key)
return key, err
}