This repository was archived by the owner on Sep 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy paths3proxy.go
More file actions
156 lines (139 loc) · 3.88 KB
/
s3proxy.go
File metadata and controls
156 lines (139 loc) · 3.88 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
package main
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"flag"
"fmt"
"io"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
"sort"
"strings"
"time"
)
// Query parameters that should be used in signing the request.
var canonParams = map[string]bool{
"acl": true,
"delete": true,
"lifecycle": true,
"location": true,
"logging": true,
"notification": true,
"partnumber": true,
"policy": true,
"requestpayment": true,
"response-cache-control": true,
"response-content-disposition": true,
"response-content-encoding": true,
"response-content-language": true,
"response-content-type": true,
"response-expires": true,
"torrent": true,
"uploadid": true,
"uploads": true,
"versionid": true,
"versioning": true,
"versions": true,
"website": true,
}
func main() {
proxy := new(Proxy)
proxy.Director = proxy.Direct
server := http.Server{Handler: proxy}
// Parse command line flags.
flag.StringVar(&proxy.ID, "id", "", "access key")
flag.StringVar(&proxy.Key, "key", "", "secret key")
flag.StringVar(&server.Addr, "addr", "127.0.0.1:8080", "addr to bind to")
flag.BoolVar(&proxy.ReadOnly, "ro", false, "only allow GETs (read-only)")
flag.Usage = func() {
fmt.Fprintln(os.Stderr, "Usage: s3proxy [options] <url>")
flag.PrintDefaults()
}
flag.Parse()
// Parse the proxy target.
if flag.NArg() != 1 {
flag.Usage()
os.Exit(1)
}
proxy.SetURL(flag.Arg(0))
log.Fatalln(server.ListenAndServe())
}
// A Proxy is an http.Handler that proxies to a URL and signs requests.
type Proxy struct {
httputil.ReverseProxy
*url.URL
ID, Key string
ReadOnly bool
}
// SetURL sets the base URL, exiting if it is invalid.
func (p *Proxy) SetURL(u string) {
if p.URL, _ = url.Parse(flag.Arg(0)); p.URL == nil || p.URL.Scheme == "" {
log.Fatalf("bad URL: %q", flag.Arg(0))
}
p.URL.Path = strings.TrimSuffix(p.URL.Path, "/")
}
// ServeHTTP implements http.Handler.
func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if p.ReadOnly && r.Method != "GET" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
p.ReverseProxy.ServeHTTP(w, r)
}
// Direct the incoming request to the proxy target.
func (p *Proxy) Direct(req *http.Request) {
log.Println("Request:", req.Method, req.URL)
// Re-route the request.
req.URL.Scheme = p.Scheme
req.URL.Host = p.Host
req.URL.Path = p.Path + req.URL.Path
if req.Header["Date"] == nil {
req.Header.Set("Date", time.Now().Format(time.RFC1123Z))
}
// Extract the date if X-Amz-Date is unset.
date := ""
if req.Header["X-Amz-Date"] == nil {
date = req.Header.Get("Date")
}
// Sign the request.
hmac := hmac.New(sha1.New, []byte(p.Key))
io.WriteString(hmac, req.Method+"\n"+
req.Header.Get("Content-MD5")+"\n"+
req.Header.Get("Content-Type")+"\n"+
date+"\n"+
canonicalizedAmzHeaders(req.Header)+
canonicalizedResource(req.URL))
sig := base64.StdEncoding.EncodeToString(hmac.Sum(nil))
req.Header.Set("Authorization", "AWS "+p.ID+":"+sig)
}
func canonicalizedAmzHeaders(h http.Header) (s string) {
var keys []string
for k := range h {
if strings.HasPrefix(k, "X-Amz") {
keys = append(keys, strings.ToLower(k))
}
}
sort.Strings(keys)
for _, k := range keys {
s += k + ":" + strings.Join(h[k], ",") + "\n"
}
return
}
func canonicalizedResource(u *url.URL) string {
if q := canonicalizedQuery(u.Query()); q != "" {
return u.EscapedPath() + "?" + q
}
return u.EscapedPath()
}
func canonicalizedQuery(query url.Values) string {
for k := range query {
if !canonParams[strings.ToLower(k)] {
delete(query, k)
}
}
return query.Encode()
}