-
Notifications
You must be signed in to change notification settings - Fork 2
/
cheeseshop.go
258 lines (243 loc) · 6.78 KB
/
cheeseshop.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
package main
import (
"crypto/md5"
"fmt"
"gopkg.in/yaml.v2"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"os/user"
"path/filepath"
"strings"
)
const (
version = "UNKNOWN"
listHead = "<html><head><title>Links for %s</title></head><body><h1>Links for %s</h1>"
listTail = "</body></html>"
listElement = "<a href='%s'>%s</a><br/>"
)
var defaultConfig = []string{"~/.cheeseshop.yml", "/etc/cheeseshop.yml"}
// Config contains configuration
type Config struct {
HTTP int
HTTPS int
Path string
Root string
Shop string
Cert string
Key string
Overwrite bool
Auth map[string]string
}
var config Config
func listRoot(w http.ResponseWriter, r *http.Request) {
log.Printf("Listing root %s", config.Root)
files, err := ioutil.ReadDir(config.Root)
if err != nil {
http.Error(w, fmt.Sprintf("Error listing root directory %s", config.Root), http.StatusInternalServerError)
return
}
w.Write([]byte(fmt.Sprintf(listHead, "root", "root")))
for _, file := range files {
if file.Mode().IsDir() {
w.Write([]byte(fmt.Sprintf(listElement, config.Path+file.Name(), file.Name())))
}
}
w.Write([]byte(listTail))
}
func redirect(url string, w http.ResponseWriter, r *http.Request) {
if r.TLS == nil {
url = "http://" + url
} else {
url = "https://" + url
}
log.Printf("Redirecting to %s", url)
http.Redirect(w, r, url, http.StatusFound)
}
func listDirectory(dir string, w http.ResponseWriter, r *http.Request) {
directory := filepath.Join(config.Root, dir)
if _, err := os.Stat(directory); os.IsNotExist(err) {
url := config.Shop + "/" + dir
redirect(url, w, r)
return
}
log.Printf("Listing directory %s", directory)
files, err := ioutil.ReadDir(directory)
if err != nil {
http.Error(w, fmt.Sprintf("Error listing directory %s", dir), http.StatusInternalServerError)
return
}
w.Write([]byte(fmt.Sprintf(listHead, dir, dir)))
for _, file := range files {
w.Write([]byte(fmt.Sprintf(listElement, config.Path+dir+"/"+file.Name(), file.Name())))
}
w.Write([]byte(listTail))
}
func servePackage(dir, file string, w http.ResponseWriter, r *http.Request) {
filename := filepath.Join(config.Root, dir, file)
if _, err := os.Stat(filename); os.IsNotExist(err) {
url := config.Shop + "/" + dir + "/" + file
redirect(url, w, r)
return
}
log.Printf("Serving file %s", filename)
http.ServeFile(w, r, filename)
}
func copyFile(w http.ResponseWriter, r *http.Request) {
if len(config.Auth) > 0 {
username, password, ok := r.BasicAuth()
sum := fmt.Sprintf("%x", md5.Sum([]byte(password)))
if !ok || config.Auth[username] != sum {
log.Printf("Unauthorized access from %s", username)
http.Error(w, "Not Authorized", http.StatusUnauthorized)
return
}
log.Printf("Granted access for user %s", username)
}
err := r.ParseMultipartForm(100000)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
m := r.MultipartForm
files := m.File["content"]
for _, file := range files {
name := file.Filename
pack := name[:strings.LastIndex(name, "-")]
dir := filepath.Join(config.Root, pack)
path := filepath.Join(config.Root, pack, name)
if _, err := os.Stat(path); err == nil && !config.Overwrite {
log.Printf("Failed attempt to overwrite package %s", name)
http.Error(w, fmt.Sprintf("Package %s already exists", name), http.StatusBadRequest)
return
}
if _, err := os.Stat(dir); os.IsNotExist(err) {
err = os.Mkdir(dir, 0777)
if err != nil {
log.Printf("Error creating directory for package %s", pack)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
log.Printf("Created directory for package %s", pack)
}
log.Printf("Writing file %s", name)
f, err := file.Open()
defer f.Close()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
dst, err := os.Create(path)
defer dst.Close()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if _, err := io.Copy(dst, f); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}
func handler(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
parts := strings.Split(r.URL.Path[len(config.Path):], "/")
if len(parts) > 2 {
http.Error(w, fmt.Sprintf("%s is not a valid path", r.URL.Path), http.StatusNotFound)
return
} else if len(parts) == 1 && parts[0] == "" {
listRoot(w, r)
} else if len(parts) == 1 {
listDirectory(parts[0], w, r)
} else {
servePackage(parts[0], parts[1], w, r)
}
} else if r.Method == "POST" {
copyFile(w, r)
}
}
func normalizeFile(file string) string {
if strings.HasPrefix(file, "~") {
usr, _ := user.Current()
dir := usr.HomeDir
file = filepath.Join(dir, file[1:])
}
absfile, err := filepath.Abs(file)
if err != nil {
log.Fatalf("Error getting absolute path for file %s", file)
}
return absfile
}
func loadConfig() {
var file = ""
if len(os.Args) > 1 {
file = os.Args[1]
} else {
for _, path := range defaultConfig {
path = normalizeFile(path)
if _, err := os.Stat(path); err == nil {
file = path
break
}
}
}
if file == "" {
log.Fatal("No configuration file found")
}
log.Printf("Loading %s configuration file", file)
source, err := ioutil.ReadFile(file)
if err != nil {
log.Fatalf("Error loading config file %s", file)
}
err = yaml.Unmarshal(source, &config)
if err != nil {
log.Fatalf("Error parsing config file %s: %s", file, err)
}
}
func checkConfig() {
config.Root = normalizeFile(config.Root)
info, err := os.Stat(config.Root)
if err != nil {
log.Fatalf("Root directory %s not found", config.Root)
}
if !info.Mode().IsDir() {
log.Fatalf("Root %s is not a directory", config.Root)
}
if !strings.HasPrefix(config.Path, "/") {
config.Path = "/" + config.Path
}
if !strings.HasSuffix(config.Path, "/") {
config.Path = config.Path + "/"
}
if config.HTTP > 65535 || config.HTTP < 0 {
log.Fatalf("Bad HTTP port number %d", config.HTTP)
}
if config.HTTPS > 65535 || config.HTTPS < 0 {
log.Fatalf("Bad HTTPS port number %d", config.HTTPS)
}
if config.HTTP == 0 && config.HTTPS == 0 {
log.Fatal("At least one of HTTP or HTTPS must be enabled")
}
}
func main() {
loadConfig()
checkConfig()
log.Printf("Starting CheeseShop (ports: %d & %d, path: %s, root: %s, shop: %s, cert: %s, key: %s, overwrite: %t)",
config.HTTP, config.HTTPS, config.Path, config.Root, config.Shop, config.Cert, config.Key, config.Overwrite)
http.HandleFunc(config.Path, handler)
if config.HTTP != 0 {
go func() {
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", config.HTTP), nil))
}()
}
if config.HTTPS != 0 {
go func() {
log.Fatal(http.ListenAndServeTLS(fmt.Sprintf(":%d", config.HTTPS),
normalizeFile(config.Cert), normalizeFile(config.Key), nil))
}()
}
wait := make(chan bool, 1)
<-wait
}