This repository has been archived by the owner on Jan 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
153 lines (129 loc) · 3.48 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"encoding/base64"
"io/ioutil"
"fmt"
"log"
"math"
"net/http"
"path"
"os"
"strings"
"time"
"unsafe"
)
type VersionInfo struct {
Major int8
Minor int8
Patch int8
Release string
}
const (
Domain string = "https://someurl.com"
FileRoute string = "/"
FormName string = "file"
MaxUploadSize int64 = 100
Port string = ":56749"
Secret string = "youshallnotpass"
StoragePath string = "./uploads"
UploadRoute string = "/upload"
)
var (
Version VersionInfo = VersionInfo{
Major: 1,
Minor: 0,
Patch: 2,
Release: "stable",
}
Whitelist []string = []string{}
)
func getIP(r *http.Request) string {
address := r.Header.Get("CF-Connecting-IP")
if address == "" {
address = r.Header.Get("True-Client-IP")
}
if address == "" {
address = r.Header.Get("X-Real-IP")
}
if address == "" {
address = r.Header.Get("X-Forwarded-For")
}
if address == "" {
address = r.RemoteAddr
}
return address
}
func genName(str string) string {
timeNano := time.Now().UnixNano()
return base64.RawURLEncoding.EncodeToString((*[8]byte)(unsafe.Pointer(&timeNano))[:]) + path.Ext(str)
}
func isWhitelistedIP(r *http.Request) bool {
for _, ip := range Whitelist {
if ip == getIP(r) {
return true
}
}
return false
}
func uploadFile(w http.ResponseWriter, r *http.Request) {
if isWhitelistedIP(r) || len(Whitelist) == 0 {
providedSecret := r.Header.Get("Authorization")
if providedSecret != Secret {
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
log.Printf("%s: Attempted to upload file with incorrect secret: %s", getIP(r), providedSecret)
return
}
r.ParseMultipartForm(MaxUploadSize)
file, handler, err := r.FormFile(FormName)
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
log.Fatalf("Error when reading form file: %s", err.Error())
return
}
if float64(handler.Size) > (float64(MaxUploadSize) * math.Pow(1024, 2)) {
http.Error(w, http.StatusText(http.StatusRequestEntityTooLarge), http.StatusRequestEntityTooLarge)
log.Printf("%s: Attempted to upload file with size: %d", getIP(r), handler.Size)
return
}
defer file.Close()
fileBytes, err := ioutil.ReadAll(file)
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
log.Fatalf("Error when reading from uploaded file: %s", err.Error())
return
}
name := genName(handler.Filename)
_ = os.Mkdir(StoragePath, 0700)
err = ioutil.WriteFile(StoragePath + "/" + name, fileBytes, 0644)
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
log.Fatalf("Error when writing to new file: %s", err.Error())
return
}
url := Domain
if !strings.HasSuffix(Domain, "/") {
url += "/"
}
if FileRoute != "/" {
url += FileRoute
}
url += name
fmt.Fprint(w, url)
log.Printf("%s: Uploaded file %s", getIP(r), name)
return
}
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
log.Printf("%s: Attempted to upload file", getIP(r))
return
}
func main() {
log.Printf("Running goshare v%d.%d.%d-%s", Version.Major, Version.Minor, Version.Patch, Version.Release)
fs := http.FileServer(http.Dir(StoragePath))
if FileRoute == "/" {
http.Handle(FileRoute, fs)
} else {
http.Handle(FileRoute, http.StripPrefix(FileRoute, fs))
}
http.HandleFunc(UploadRoute, uploadFile)
http.ListenAndServe(Port, nil)
}