-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
111 lines (97 loc) · 2.68 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
// upload service
// Copyright (C) 2014 [email protected]
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"crypto/subtle"
"errors"
"fmt"
"html/template"
"io"
"net/http"
"os"
"path/filepath"
"time"
)
type Page struct {
Status template.HTML
}
var t *template.Template
func checkSecrets(username, password string) (err error) {
var usernameFromFile, passwordFromFile string
var n int
file, err := os.Open("secrets")
if err != nil {
return err
}
for {
n, _ = fmt.Fscanf(file, "%s\t%s", &usernameFromFile, &passwordFromFile)
if username == usernameFromFile &&
subtle.ConstantTimeCompare([]byte(password), []byte(passwordFromFile)) == 1 {
return nil
}
if n == 0 {
break
}
}
err = errors.New("secrets: wrong username or password " + username)
return err
}
func index(w http.ResponseWriter, r *http.Request) {
//create|open log file
f, err := os.OpenFile("log.txt", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
}
p := Page{}
defer func() {
t.ExecuteTemplate(w, "index.html", p)
}()
if r.Method == "POST" {
err := checkSecrets(r.FormValue("username"), r.FormValue("password"))
defer func() {
p.Status = template.HTML(err.Error())
//write to log file
fmt.Fprintln(f, time.Now().Format("Mon, 02 Jan 2006 15:04 ")+err.Error())
}()
if err != nil {
return
}
file, handler, err := r.FormFile("file")
if err != nil {
return
}
out, err := os.Create("files/" + handler.Filename)
if err != nil {
return
}
bytes, err := io.Copy(out, file)
if err != nil {
return
}
//seems to be a good ideea to generate download link into an error message
//to treat this and previous possible errors in one defer code
err = errors.New(fmt.Sprintf("<a href=/files/%s>%s</a> %db transferred by user %s",
handler.Filename, handler.Filename, bytes, r.FormValue("username")))
}
}
func main() {
os.Chdir(filepath.Dir(os.Args[0]))
t, _ = template.ParseFiles("index.html")
//web server
http.HandleFunc("/", index)
//file server
http.Handle("/files/", http.FileServer(http.Dir("./")))
http.ListenAndServe(":8080", nil)
}