-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.go
145 lines (123 loc) · 3.33 KB
/
proxy.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"io"
"bytes"
"flag"
"encoding/base64"
)
type UserRequest struct {
Url string `json:"url"`
Method string `json:"method"`
Headers map[string]string `json:"headers"`
Body string `json:"body"`
}
type Reader struct {
io.Reader
io.Closer
}
var (
debug bool
auth string
)
func (r *Reader) Close() error {
return nil
}
func passthrough(request *http.Request, writer http.ResponseWriter, content io.ReadCloser) error {
userReq := UserRequest{}
decoder := json.NewDecoder(content)
err := decoder.Decode(&userReq)
if err != nil {
return err
}
//send http request
httpReq, err := generateRequest(&userReq)
if err != nil {
return err
}
response, err := http.DefaultClient.Do(httpReq)
if err != nil {
return err
} else if response.Body != nil {
defer response.Body.Close()
}
//copy headers
headers := writer.Header()
for k, v := range response.Header {
headers.Add(k, v[0])
}
//do the dance
corsDance(request, writer)
writer.WriteHeader(response.StatusCode)
//write body
var buff [512]byte
io.CopyBuffer(writer, response.Body, buff[:])
//hooray!
return nil
}
func generateRequest(userReq *UserRequest) (*http.Request, error) {
//convert body to reader
reqBody := &Reader{Reader:bytes.NewReader([]byte(userReq.Body))}
//convert header map into string array
headers := make(map[string][]string)
for k, v := range userReq.Headers {
headers[k] = []string{v}
}
//add auth header
if len(auth) > 0 {
headers["Authorization"] = []string{auth}
}
//generate request
request, err := http.NewRequest(userReq.Method, userReq.Url, reqBody)
if err != nil {
return nil, err
}
request.Header = headers
if debug {
str := fmt.Sprintf("%v: %v\n", userReq.Method, userReq.Url)
for k, v := range headers {
str += fmt.Sprintf("\t%v: %v\n", k, v[0])
}
fmt.Print(str)
}
return request, nil
}
func corsDance(request *http.Request, writer http.ResponseWriter) {
writer.Header().Set("Access-Control-Allow-Origin", "*")
writer.Header().Set("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
writer.Header().Set("Access-Control-Request-Method", request.Header.Get("Access-Control-Request-Method"))
}
func main() {
debug_flag := flag.Bool("debug", false, "Enable debug output")
address := flag.String("address", "0.0.0.0", "The address the server will bind to")
port := flag.String("port", "8080", "The port the server will bind to")
auth_flag := flag.String("auth", "", "Enable Basic Auth on every request in user:pass form")
flag.Parse()
debug = *debug_flag
if *auth_flag != "" {
auth = "Basic " + base64.StdEncoding.EncodeToString([]byte(*auth_flag))
}
http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
var err error
if request.Method == "POST" {
err = passthrough(request, writer, request.Body)
} else if request.Method == "OPTIONS" {
corsDance(request, writer)
writer.WriteHeader(200)
} else {
writer.WriteHeader(400)
writer.Write([]byte(`Invalid request method`))
return
}
if err != nil {
corsDance(request, writer)
writer.WriteHeader(500)
writer.Write([]byte(err.Error()))
fmt.Printf("Error! '%v'\n", err)
}
})
err := http.ListenAndServe((*address)+":"+(*port), nil)
fmt.Printf("%v\n", err)
}