-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
143 lines (119 loc) · 4.05 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
package main
import (
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"strings"
)
func main() {
overrides := GetUpstreamOverrides()
listenAddress := os.Getenv("LISTEN_ADDRESS")
gd := NewGoogleDrive(GoogleDriveConfig{
RefreshToken: os.Getenv("GD_REFRESH_TOKEN"),
DefaultRootID: os.Getenv("GD_DEFAULT_ROOT_ID"),
ClientID: os.Getenv("GD_CLIENT_ID"),
ClientSecret: os.Getenv("GD_CLIENT_SECRET"),
}, false)
metrics := NewMetrics()
http.HandleFunc("/_health", func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s %s\n", r.RemoteAddr, r.Method, r.URL)
fmt.Fprint(w, "ok")
})
http.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s %s\n", r.RemoteAddr, r.Method, r.URL)
metrics.ServeHTTP(w, r)
})
http.HandleFunc("/", metrics.WithMetrics(func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s %s\n", r.RemoteAddr, r.Method, r.URL)
u, _ := url.Parse(r.URL.String())
// Parse the upstream tag and the path
upstreamTag, path := GetUpstreamTag(u.Path)
if upstreamTag == "" {
upstreamTag = fmt.Sprintf("gd:%s", gd.Config.DefaultRootID)
}
// Disallow directory listing
if strings.HasSuffix(path, "/") {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
defaultRootId := gd.Config.DefaultRootID
if strings.HasPrefix(upstreamTag, "gd:") {
defaultRootId = upstreamTag[3:]
}
fileName := path[strings.LastIndex(path, "/")+1:]
var fileResponse *http.Response
var err error
httpClient := &http.Client{}
// Check first if the file is available from the upstream
if override, ok := overrides[upstreamTag]; ok {
upstreamUrl := override.UpstreamUrl + path
log.Printf("Using upstream override URL %s\n", upstreamUrl)
// Check whether to redirect or proxy (Sec-Fetch-Site header)
if override.MustProxy || r.Header.Get("Sec-Fetch-Mode") == "cors" {
// Cross-site request, proxy
u, err := url.Parse(upstreamUrl)
if err != nil {
log.Printf("Error parsing upstream URL %s: %s\n", upstreamUrl, err.Error())
http.Error(w, "Not found", http.StatusNotFound)
return
}
req := &http.Request{
Method: http.MethodGet,
URL: u,
}
if r.Header.Get("Range") != "" {
req.Header = make(http.Header)
req.Header.Set("Range", r.Header.Get("Range"))
}
fileResponse, err = httpClient.Do(req)
if err != nil {
log.Printf("Error fetching upstream URL %s: %s\n", upstreamUrl, err.Error())
http.Error(w, "Not found", http.StatusNotFound)
return
}
} else {
// Not a cross-site request, redirect
http.Redirect(w, r, upstreamUrl, http.StatusFound)
return
}
}
// Fetch file
if fileResponse == nil {
fileResponse, err = gd.DownloadByPath(path, r.Header.Get("Range"), defaultRootId)
if err != nil || fileResponse == nil {
http.Error(w, "Not found", http.StatusNotFound)
return
}
}
defer fileResponse.Body.Close()
// Modify headers
fileResponse.Header.Del("X-GUploader-UploadID")
fileResponse.Header.Del("X-Goog-Hash")
fileResponse.Header.Del("X-Amz-Request-Id")
fileResponse.Header.Del("X-Amz-Expiration")
fileResponse.Header.Del("X-HW")
fileResponse.Header.Set("Content-Disposition", `inline; filename="`+url.QueryEscape(fileName)+`"`)
fileResponse.Header.Set("Access-Control-Allow-Origin", "*")
fileResponse.Header.Set("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS")
fileResponse.Header.Set("Access-Control-Allow-Headers", "Range")
if strings.HasSuffix(fileName, ".vtt") {
fileResponse.Header.Set("Content-Type", "text/vtt")
}
if fileResponse.StatusCode >= 200 && fileResponse.StatusCode < 300 {
fileResponse.Header.Set("Cache-Control", "public, max-age=604800, immutable")
}
// Write headers to client
for k, v := range fileResponse.Header {
w.Header().Set(k, v[0])
}
// Write status code to client
w.WriteHeader(fileResponse.StatusCode)
// Write response to client
io.Copy(w, fileResponse.Body)
}))
log.Println("HTTP webserver running. Access it at", listenAddress)
log.Fatal(http.ListenAndServe(listenAddress, nil))
}