-
Notifications
You must be signed in to change notification settings - Fork 2
/
LDrop.go
498 lines (457 loc) · 14.1 KB
/
LDrop.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
package main
import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"flag"
"fmt"
"github.com/dustin/go-humanize"
_ "github.com/kamilkabir9/LDrop/statik" // NOTE: Replace with the absolute import path
"github.com/mdp/qrterminal"
"github.com/rakyll/statik/fs"
"github.com/skratchdot/open-golang/open"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
)
const (
SuccessStatus = "Ok"
FailedStatus = "Err"
)
var uploadFolder string
type onlySuffixFilter struct {
set bool
suffix []string
}
var oSF onlySuffixFilter
var verbose func(string)
func (filter *onlySuffixFilter) Set(value string) error {
filter.set = true
filter.suffix = strings.Split(value, ",")
return nil
}
func (filter *onlySuffixFilter) filterFile(fileName string) bool {
if filter.set {
for _, v := range filter.suffix {
if strings.HasSuffix(fileName, v) {
return false
}
}
return true
}
return false
}
func (filter *onlySuffixFilter) String() string {
result := "\n"
for i, v := range filter.suffix {
result += fmt.Sprintf("%v:%v\n", i, v)
}
return result
}
//------------------------------------
type ignorePrefixFilter struct {
set bool
preffix []string
}
var iPF ignorePrefixFilter
func (filter *ignorePrefixFilter) Set(value string) error {
filter.set = true
filter.preffix = strings.Split(value, ",")
return nil
}
func (filter *ignorePrefixFilter) filterFile(fileName string) bool {
if filter.set {
for _, v := range filter.preffix {
if strings.HasPrefix(fileName, v) {
return true
}
}
return false
}
return false
}
func (filter *ignorePrefixFilter) String() string {
result := "\n"
for i, v := range filter.preffix {
result += fmt.Sprintf("%v:%v\n", i, v)
}
return result
}
//--------------------------
type ignoreSuffixFilter struct {
set bool
suffix []string
}
var iSF ignoreSuffixFilter
func (filter *ignoreSuffixFilter) Set(value string) error {
filter.set = true
filter.suffix = strings.Split(value, ",")
return nil
}
func (filter *ignoreSuffixFilter) filterFile(fileName string) bool {
if filter.set {
for _, v := range filter.suffix {
if strings.HasSuffix(fileName, v) {
return true
}
}
return false
}
return false
}
func (filter *ignoreSuffixFilter) String() string {
result := "\n"
for i, v := range filter.suffix {
result += fmt.Sprintf("%v:%v\n", i, v)
}
return result
}
func filterFile(fileName string) bool {
if iSF.filterFile(fileName) || iPF.filterFile(fileName) || (ignoreHiddenFilesFlag && strings.HasPrefix(fileName, ".")) || oSF.filterFile(fileName) {
return true
}
return false
}
var statikFS http.FileSystem
var ignoreHiddenFoldersFlag bool
var ignoreHiddenFilesFlag bool
var verboseFlag bool
var err error
var secretFlag string
var secretFlagMD5 string
func checkSecret(secretEncoded string) bool {
if secretEncoded == secretFlagMD5 {
return true
}
verbose(fmt.Sprintf("Secret Failed %v=!%v !!!!!!", secretEncoded, secretFlagMD5))
return false
}
func main() {
log.SetFlags(log.Lshortfile)
flag.StringVar(&secretFlag, "secret", "007Jb", "Pass secret code.")
flag.Var(&iSF, "ignoreSuffix", "Pass file SUFFIX to exclude Example:\".png,.mp4\"")
flag.Var(&iPF, "ignorePreffix", "Pass file PREFFIX to exclude Example:\"PIC-,MOV-\"")
flag.Var(&oSF, "onlySuffix", "Pass file SUFFIX to only to include")
flag.BoolVar(&ignoreHiddenFoldersFlag, "ignoreHiddenFolders", false, "Pass True to ignore hidden Files")
flag.BoolVar(&ignoreHiddenFilesFlag, "ignoreHiddenFiles", false, "Pass True to ignore hidden Folders")
flag.BoolVar(&verboseFlag, "v", false, "Pass true to print verbose info")
flag.Parse()
if verboseFlag {
verbose = func(s string) {
fmt.Println(s)
}
} else {
verbose = func(s string) {
}
}
hasher := md5.New()
hasher.Write([]byte(secretFlag))
secretFlagMD5 = hex.EncodeToString(hasher.Sum(nil))
args := flag.Args()
if len(args) == 0 {
wd, err := os.Getwd()
if err != nil {
log.Panicln(err)
}
uploadFolder, err = filepath.Abs(wd)
if err != nil {
log.Panicln(err)
}
} else {
uploadFolder, err = filepath.Abs(args[0])
if err != nil {
log.Panicln(err)
}
}
statikFS, err = fs.New()
if err != nil {
log.Fatal(err)
}
http.HandleFunc("/viewFile/", viewFileHandler)
http.Handle("/", http.FileServer(statikFS))
http.HandleFunc("/upload", upLoadHandler) //api
http.HandleFunc("/getLastFile", getLastFileHandler) //api
http.HandleFunc("/getAllFiles", getAllFilesHandler) //api
http.HandleFunc("/getFile/", getFileHandler) //api
http.HandleFunc("/downLoadFile/", serveThisFileHandler) //api
//Adapted from https://stackoverflow.com/questions/43424787/how-to-use-next-available-port-in-http-listenandserve
listener, err := net.Listen("tcp", ":0")
if err != nil {
panic(err)
}
port := strconv.Itoa(listener.Addr().(*net.TCPAddr).Port)
fmt.Printf("Strarting Server...\n########################\nFolder:%v\nSecret:%v\nIP address: %v:%v\nverbose:%v\nFiltering rules\n------------------------\nIgnoring Files with Suffix(-ignoreSuffix):%v\nIgnoring Files with Preffix(-ignorePreffix):%v\nShowing Files ony with Suffix(-onlySuffix):%v\nHide Hidden Files(-ignoreHiddenFiles):%v\nHide Hidden Folders(-ignoreHiddenFolders):%v\n------------------------\n########################\n", uploadFolder, secretFlag, GetOutboundIP(), port, verboseFlag, iSF.String(), iPF.String(), oSF.String(), ignoreHiddenFilesFlag, ignoreHiddenFoldersFlag)
fmt.Println("Scan QRCode to get IP address")
qrterminal.GenerateHalfBlock(fmt.Sprintf("http://%v:%v", GetOutboundIP(), port), qrterminal.L, os.Stdout)
open.Start(fmt.Sprintf("http://%v:%v", GetOutboundIP(), port))
err = http.Serve(listener, nil)
if err != nil {
log.Println("ERR : ", err)
}
}
//Adapted from https://stackoverflow.com/questions/23558425/how-do-i-get-the-local-ip-address-in-go
func GetOutboundIP() string {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
log.Fatal(err)
}
defer conn.Close()
localAddr := conn.LocalAddr().(*net.UDPAddr)
return localAddr.IP.String()
}
func staticHandler(w http.ResponseWriter, r *http.Request) {
//log.Println("getting : " + r.URL.Path[1:])
http.ServeFile(w, r, r.URL.Path[1:])
}
func UploadStatusJson(status string, desc string) string {
type resultAsjson struct {
Status string
Description string
}
var resultJson = resultAsjson{status, desc}
result, err := json.Marshal(resultJson)
if err != nil {
log.Println("ERR : ", err)
return fmt.Sprintf("{\"Status\":%v,\"Description\":%v}", FailedStatus, err)
}
return string(result)
}
func upLoadHandler(w http.ResponseWriter, r *http.Request) {
verbose(fmt.Sprintln("Downloading File....."))
file, fileHeader, err := r.FormFile("fileUpload")
if err != nil {
log.Println(err)
result := UploadStatusJson(FailedStatus, fmt.Sprint(err))
fmt.Fprint(w, result)
return
}
if !checkSecret(r.Header.Get("secret")) {
result := UploadStatusJson(FailedStatus, fmt.Sprintf("Error Uploading file %v. Secret mismatch !!!", fileHeader.Filename))
fmt.Fprint(w, result)
return
}
if _, err := os.Stat(uploadFolder); os.IsNotExist(err) {
os.Mkdir(uploadFolder, 0777)
}
if err != nil {
log.Println(err)
result := UploadStatusJson(FailedStatus, fmt.Sprint(err))
fmt.Fprint(w, result)
return
}
fileBytes, err := ioutil.ReadAll(file)
if err != nil {
log.Println(err)
result := UploadStatusJson(FailedStatus, fmt.Sprint(err))
fmt.Fprint(w, result)
return
}
uniqfileName := getUniqFileName(fileHeader.Filename)
of, err := os.Create(uniqfileName)
if err != nil {
log.Println("ERR : ", err)
result := UploadStatusJson(FailedStatus, fmt.Sprint(err))
fmt.Fprint(w, result)
return
}
of.Write(fileBytes)
verbose(fmt.Sprintf("File: %v saved at location: %v\n", fileHeader.Filename, uniqfileName))
result := UploadStatusJson(SuccessStatus, fmt.Sprintf("Uploaded file %v", fileHeader.Filename))
fmt.Fprint(w, result)
verbose(fmt.Sprintln("Downloaded File : " + fileHeader.Filename))
}
//func getUniqFileName check if file with same file name exists .if yes then creates a new file name
func getUniqFileName(filename string) string {
uploadFileName := filename
uploadFileName = filepath.Join(uploadFolder, uploadFileName)
exists := true
count := 0
for exists {
count += 1
if _, err := os.Stat(uploadFileName); os.IsNotExist(err) {
exists = false
} else {
//file.png -> file-1.png
uploadFileName = strings.Replace(uploadFileName, path.Ext(uploadFileName), "-"+strconv.Itoa(count)+path.Ext(uploadFileName), 1)
verbose(fmt.Sprint("made uniq!!!!!!!!!!!!"))
}
}
return uploadFileName
}
func getLastFileHandler(w http.ResponseWriter, r *http.Request) {
if !checkSecret(r.Header.Get("secret")) {
verbose("Got wrong secret from client")
result := UploadStatusJson(FailedStatus, fmt.Sprintf("Error getting last file. Secret mismatch !!!"))
fmt.Fprint(w, result)
return
}
fileList := getAllFiles()
lastFile := fileList[0]
for _, file := range fileList {
if lastFile.Info.ModTime.Before(file.Info.ModTime) {
lastFile = file
}
}
verbose(fmt.Sprintln("Last file:", lastFile.Name))
//Adapted from https://stackoverflow.com/questions/31638447/how-to-server-a-file-from-a-handler-in-golang
w.Header().Set("Content-Description", "File Transfer")
w.Header().Set("Content-Transfer-Encoding", "binary")
w.Header().Set("Content-Disposition", "attachment; filename="+lastFile.Info.Name)
w.Header().Set("Content-Type", "application/octet-stream")
http.ServeFile(w, r, path.Join(uploadFolder, lastFile.Name))
}
type osFileInfo struct {
Name string
Size int64
Mode os.FileMode
ModTime time.Time
IsDir bool
}
type fileInfo struct {
Name string
ModTime string
Size string
Info osFileInfo
}
var wg sync.WaitGroup
var mx sync.Mutex
func getAllFiles() []fileInfo {
var fileNamesWithTime = new([]fileInfo)
wg.Add(1)
getAllFilesConcurrent(uploadFolder, fileNamesWithTime)
wg.Wait()
verbose(fmt.Sprint("completed reading root Folder"))
verbose(fmt.Sprint("Total nuber of Files: ", len(*fileNamesWithTime)))
return *fileNamesWithTime
}
func getAllFilesConcurrent(Dir string, fileNamesWithTime *[]fileInfo) {
verbose(fmt.Sprintln("Reading Dir: ", Dir))
fileList, err := ioutil.ReadDir(Dir)
if err != nil {
log.Panicln("ERR : ", err)
}
for _, file := range fileList {
if !file.IsDir() {
var fileNameKey string
if filterFile(file.Name()) {
continue
}
fileNameKey = filepath.Join(Dir, file.Name())
fileNameKey = strings.Replace(fileNameKey, uploadFolder+string(os.PathSeparator), "", 1)
mx.Lock()
*fileNamesWithTime = append(*fileNamesWithTime, fileInfo{fileNameKey, file.ModTime().Format(time.ANSIC), humanize.Bytes(uint64(file.Size())), osFileInfo{file.Name(), file.Size(), file.Mode(), file.ModTime(), file.IsDir()}})
mx.Unlock()
} else {
if !(ignoreHiddenFoldersFlag && strings.HasPrefix(file.Name(), ".")) {
wg.Add(1)
go getAllFilesConcurrent(filepath.Join(Dir, file.Name()), fileNamesWithTime)
} else if !ignoreHiddenFoldersFlag {
wg.Add(1)
go getAllFilesConcurrent(filepath.Join(Dir, file.Name()), fileNamesWithTime)
}
}
}
wg.Done()
}
func getAllFilesHandler(w http.ResponseWriter, r *http.Request) {
verbose(fmt.Sprint("getting All files.."))
if !checkSecret(r.Header.Get("secret")) {
verbose("Got wrong secret from client")
result := UploadStatusJson(FailedStatus, "secret mismatch")
fmt.Fprintln(w, result)
return
}
var fileNamesWithTime = getAllFiles()
fileNamesJson, err := json.Marshal(fileNamesWithTime)
if err != nil {
log.Println("ERR : ", err)
result := UploadStatusJson(FailedStatus, fmt.Sprint(err))
fmt.Fprintln(w, result)
return
}
result := UploadStatusJson(SuccessStatus, string(fileNamesJson))
fmt.Fprintln(w, result)
return
}
func getFileHandler(w http.ResponseWriter, r *http.Request) {
u, err := url.ParseQuery(r.URL.RawQuery)
if err != nil {
panic(err)
}
fileName := u["fileName"][0]
if fileName == "" {
verbose("Error getting file name !!!")
fmt.Fprint(w, fmt.Sprintf("Error getting file name from URL !!!"))
return
}
fileName, err = url.QueryUnescape(fileName)
if err != nil {
log.Println(err)
}
secretRcvd := u["secret"][0]
if secretRcvd == "" {
verbose("Error getting file name !!!")
fmt.Fprint(w, fmt.Sprintf("Error getting secret URL !!!"))
return
}
if !checkSecret(secretRcvd) {
verbose("Got wrong secret from client")
fmt.Fprint(w, fmt.Sprintf("Error getting file:%v. Secret mismatch !!!", fileName))
return
}
verbose(fmt.Sprintln("getting File : ", fileName))
http.ServeFile(w, r, path.Join(uploadFolder, fileName))
//http.ServeContent(w, r, path.Join(uploadFolder, fileName))
}
func serveThisFileHandler(w http.ResponseWriter, r *http.Request) {
u, err := url.ParseQuery(r.URL.RawQuery)
if err != nil {
panic(err)
}
fileName := u["fileName"][0]
if fileName == "" {
verbose("Error getting file name !!!")
fmt.Fprint(w, fmt.Sprintf("Error getting file name from URL !!!"))
return
}
fileName, err = url.QueryUnescape(fileName)
if err != nil {
log.Println(err)
}
secretRcvd := u["secret"][0]
if secretRcvd == "" {
verbose("Error getting file name !!!")
fmt.Fprint(w, fmt.Sprintf("Error getting secret URL !!!"))
return
}
if !checkSecret(secretRcvd) {
verbose("Got wrong secret from client")
result := UploadStatusJson(FailedStatus, fmt.Sprintf("Error getting file:%v. Secret mismatch !!!", fileName))
fmt.Fprint(w, result)
return
}
verbose(fmt.Sprintln("serving File : ", fileName))
//Adapted from https://stackoverflow.com/questions/31638447/how-to-server-a-file-from-a-handler-in-golang
w.Header().Set("Content-Description", "File Transfer")
w.Header().Set("Content-Transfer-Encoding", "binary")
//Adapted from https://stackoverflow.com/questions/13578428/duplicate-headers-received-from-server/25844041#25844041
w.Header().Set("Content-Disposition", "attachment; filename="+"\""+fileName+"\"")
w.Header().Set("Content-Type", "application/octet-stream")
http.ServeFile(w, r, path.Join(uploadFolder, fileName))
}
func viewFileHandler(w http.ResponseWriter, r *http.Request) {
f, err := statikFS.Open("/viewFile.html")
if err != nil {
log.Println(err)
}
http.ServeContent(w, r, "viewFile.html", time.Now(), f)
}