-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
129 lines (111 loc) · 2.84 KB
/
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
package main
import (
"log"
"math/rand"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/gorilla/context"
"github.com/hoisie/mustache"
)
type Db struct {
Hash string `json:"hash"`
Files []File `json:"files"`
}
type File struct {
Name string `json:"name"`
Id string `json:"id"`
Uploaded int64 `json:"uploaded"`
Size int64 `json:"size"`
}
var (
RootPath string
AssetsPath string
)
func main() {
rand.Seed(time.Now().UnixNano())
if len(os.Args) != 3 {
log.Fatal("Usage: f1le <port> <root path>")
}
// Setup global variables
_, sourcePath, _, _ := runtime.Caller(0)
AssetsPath = filepath.Join(filepath.Dir(sourcePath), "assets")
RootPath = os.Args[2]
// Load database
if err := LoadDb(); err != nil {
log.Fatal(err)
}
// Setup the server
handlers := map[string]http.HandlerFunc{
"/delete/": HandleDelete,
"/get/": HandleDownload,
"/files": HandleFiles,
"/login": HandleLogin,
"/logout": HandleLogout,
"/upload": HandleUpload,
"/last": HandleLast,
"/view/": HandleView,
"/": HandleRoot,
}
for path, f := range handlers {
http.Handle(path, context.ClearHandler(f))
}
log.Print("Attempting to listen on http://localhost:" + os.Args[1])
if err := http.ListenAndServe(":"+os.Args[1], nil); err != nil {
log.Fatal(err)
}
}
func HandleLogin(w http.ResponseWriter, r *http.Request) {
template := map[string]interface{}{"failed": false}
if r.Method == "POST" {
password := r.PostFormValue("password")
DbLock.RLock()
authed := (HashPassword(password) == Database.Hash)
DbLock.RUnlock()
if authed {
s, _ := Store.Get(r, "sessid")
s.Values["authenticated"] = true
s.Save(r, w)
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
return
}
log.Print("Failed login attempt.")
template["failed"] = true
}
log.Print("Serving login page.")
templatePath := filepath.Join(AssetsPath, "login.mustache")
body := mustache.RenderFile(templatePath, template)
w.Header().Set("Content-Type", "text/html")
w.Write([]byte(body))
}
func HandleLogout(w http.ResponseWriter, r *http.Request) {
s, _ := Store.Get(r, "sessid")
s.Values["authenticated"] = false
s.Save(r, w)
http.Redirect(w, r, "/login", http.StatusTemporaryRedirect)
}
func HandleRoot(w http.ResponseWriter, r *http.Request) {
// This handler will be called for all static files.
if r.URL.Path != "/" {
cleanPath := strings.Replace(r.URL.Path, "..", "", -1)
log.Print("Static file: ", cleanPath)
http.ServeFile(w, r, filepath.Join(AssetsPath, cleanPath))
return
}
if !IsAuthenticated(w, r) {
http.Redirect(w, r, "/login", http.StatusTemporaryRedirect)
return
}
http.ServeFile(w, r, filepath.Join(AssetsPath, "index.html"))
}
func ValidateId(id string) bool {
for _, ch := range id {
if (ch < 'a' || ch > 'z') && (ch < '0' || ch > '9') {
return false
}
}
return true
}