-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdropsite.go
More file actions
214 lines (193 loc) · 6.96 KB
/
dropsite.go
File metadata and controls
214 lines (193 loc) · 6.96 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
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
package main
import (
"crypto/md5"
"flag"
"fmt"
"html/template"
"io"
"log"
"net/http"
"os"
"path"
"strconv"
"strings"
"sync"
"time"
// "html"
)
var (
tmpl = template.Must(template.ParseFiles("drop_form.html"))
dropDir string
agents agentdata = agentdata{Data: make(map[string]*agent)}
)
type FormData struct {
SID string
}
type agent struct {
Seen time.Time
SigExpire time.Time
SigCnt int
}
type agentdata struct {
Data map[string]*agent
Mux sync.Mutex
}
// Mux usage--
// NetPrefixes.Mux.Lock()
// NetPrefixes.Data[prefix] = struct{}{}
// NetPrefixes.Mux.Unlock()
func genToken() string {
crutime := time.Now().Unix()
h := md5.New()
io.WriteString(h, strconv.FormatInt(crutime, 10))
token := fmt.Sprintf("%x", h.Sum(nil))
return token
}
func genHash(f string) string {
data, err := os.Open(f)
if err != nil {
log.Fatal(err)
}
defer data.Close()
h := md5.New()
if _, err := io.Copy(h, data); err != nil {
log.Fatal(err)
}
return fmt.Sprintf("%x", h.Sum(nil))
}
//func manageDrops(t <-chan time.Time)() {
// range over t to perform routine checks on dir contents and file inventory
//for now := range t {
// status := fmt.Sprintf("Ran manageDrops function body at: %v", now)
//log.Print(status)
//}
//}
func fileServerWithLogging(fs http.FileSystem) http.Handler {
fsh := http.FileServer(fs)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
client := strings.Split(r.RemoteAddr, ":")[0]
// track all clients
if _, ok := agents.Data[client]; !ok {
agents.Data[client] = &agent{
Seen: time.Now(),
SigCnt: 0,
SigExpire: time.Now().Add(300 * time.Second),
}
} else {
if time.Now().After(agents.Data[client].SigExpire) {
log.Println("Expired signal session with ", client)
agents.Data[client].SigCnt = 0
agents.Data[client].SigExpire = time.Now().Add(300 * time.Second)
}
}
switch r.URL.Path {
case "/drop":
switch r.Method {
case "GET":
data := FormData{SID: genToken()}
err := tmpl.Execute(w, data)
if err != nil {
log.Print(err)
}
log.Printf("%s accessed the drop form", client)
case "POST":
// take an upload as form-data
r.ParseMultipartForm(32 << 20)
// Access the drops key which is a list of uploaded files
fhs := r.MultipartForm.File["drops"]
log.Printf("Recieving file drop from %s", client)
for _, fh := range fhs {
// open a file handle from tmp or cache
f, err := fh.Open()
if err != nil {
log.Print(err)
}
defer f.Close()
// open a file handle for the destination file
out, err := os.OpenFile(dropDir+"/"+fh.Filename, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
log.Print(err)
}
defer out.Close()
// copy the reader to the writer
io.Copy(out, f)
log.Printf("%s dropped file %s", client, fh.Filename)
}
http.Redirect(w, r, "/", http.StatusAccepted)
}
case "/d":
switch r.Method {
case "POST":
// take an upload as form-data
r.ParseMultipartForm(32 << 20)
// Access the drops key which is a list of uploaded files
fhs := r.MultipartForm.File["d"]
log.Printf("Recieving file drop from %s", client)
for _, fh := range fhs {
// open a file handle from tmp or cache
f, err := fh.Open()
if err != nil {
log.Print(err)
}
defer f.Close()
// open a file handle for the destination file
out, err := os.OpenFile(dropDir+"/"+fh.Filename, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
log.Print(err)
}
defer out.Close()
// copy the reader to the writer
io.Copy(out, f)
log.Printf("%s dropped file %s", client, fh.Filename)
}
w.WriteHeader(http.StatusAccepted)
}
case "/signal":
// they haven't asked enough
if agents.Data[client].SigCnt < 4 {
agents.Data[client].SigCnt++
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("Unauthorized"))
} else if agents.Data[client].SigCnt == 4 {
// givem the signal
w.WriteHeader(http.StatusAccepted)
w.Write([]byte("listdir"))
} else {
// Something went wrong, restart signal tracking
agents.Data[client].SigCnt = 0
agents.Data[client].SigExpire = time.Now().Add(300 * time.Second)
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("Unauthorized"))
}
case "/":
fsh.ServeHTTP(w, r)
log.Printf("%s accessed the dropsite file server", client)
default:
if _, err := os.Stat(dropDir + path.Clean(r.URL.Path)); err != nil {
if os.IsNotExist(err) {
log.Printf("%s requested non-existent resource %s", client, path.Clean(r.URL.Path))
http.Redirect(w, r, "/", http.StatusNotFound)
}
} else {
http.ServeFile(w, r, dropDir+path.Clean(r.URL.Path))
log.Printf("%s retrieved file %s", client, path.Clean(r.URL.Path))
}
}
})
}
func main() {
// t := time.Tick(time.Minute / 2)
// go manageDrops(t)
flag.StringVar(&dropDir, "dir", "/var/dropsite", "Directory to store files.")
cert_pem := flag.String("cert", "cert.pem", "Server TLS certificate.")
key_pem := flag.String("key", "key.pem", "Server TLS certificate key.")
httpPort := flag.String("http_port", "8880", "Port for HTTP dropsite.")
httpsPort := flag.String("https_port", "8443", "Port for HTTPS dropsite.")
flag.Parse()
log.Printf("Running dropsite on ports %s and %s. Drop directory %s", *httpPort, *httpsPort, dropDir)
go http.ListenAndServe(":"+*httpPort, fileServerWithLogging(http.Dir(dropDir)))
werr := http.ListenAndServeTLS(":"+*httpsPort, *cert_pem, *key_pem, fileServerWithLogging(http.Dir(dropDir)))
if werr != nil {
log.Fatal(werr)
}
}