/
proxy.go
150 lines (126 loc) · 3.11 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
146
147
148
149
150
package api
import (
"bytes"
"errors"
"github.com/gorilla/mux"
"github.com/mreiferson/go-httpclient"
"inspector/config"
"inspector/feeder"
"inspector/models"
"inspector/store"
"log"
"net/http"
"strconv"
"strings"
"sync"
"time"
)
var httpTransport = &httpclient.Transport{
ResponseHeaderTimeout: config.RequestTimeout,
}
var httpClient = http.Client{Transport: httpTransport}
func initProxyApi(r *mux.Router) {
http.Handle("/rproxy/", ApiHandler(handleProxy))
}
func createTargetUrl(path string, ep *models.Endpoint) string {
buff := bytes.NewBufferString(*ep.Protocol)
buff.WriteString("://")
buff.WriteString(*ep.Target)
if len(path) > 0 {
buff.WriteString("/")
buff.WriteString(path)
}
return buff.String()
}
var operationId uint = 0
var idMutex sync.Mutex
// Url will be /[project_name]/[path]
func handleProxy(w http.ResponseWriter, r *http.Request) *InspectorError {
requestURL := strings.Trim(r.URL.Path, "/")
tokens := strings.SplitN(requestURL, "/", 3)
log.Println(tokens)
if len(tokens) < 2 {
return &InspectorError{
Error: errors.New("Project not found."),
Code: 404}
}
if len(tokens) < 3 {
tokens = append(tokens, "")
}
reqInbound, err := models.NewRequest(r)
if err != nil {
log.Println("Error creating request: ", err)
return &InspectorError{
Error: err,
Code: 400}
}
projId, err := strconv.ParseUint(tokens[1], 10, strconv.IntSize)
if err != nil {
return &InspectorError{
Error: err}
}
log.Println("Project ID: ", projId)
project, err := store.AnonStore.ProjectById(uint(projId))
if err != nil {
return &InspectorError{
Error: err}
}
if project == nil {
return &InspectorError{
Error: errors.New("Project not found."),
Code: 404}
}
if project.Endpoint == nil {
return &InspectorError{
Error: errors.New("Project has no endpoint."),
Code: 400}
}
log.Println("Project: ", *project.Endpoint.Target)
idMutex.Lock()
operationId += 1
id := operationId
idMutex.Unlock()
operation := models.Operation{
Id: id,
ProjectId: new(uint),
Request: reqInbound}
*operation.ProjectId = project.Id
req, err := http.NewRequest(reqInbound.Method,
createTargetUrl(tokens[2], project.Endpoint),
bytes.NewReader(reqInbound.Body))
req.Header = reqInbound.Header
if err != nil {
log.Println("Error creating outbound request.", err)
return &InspectorError{
Error: err,
Code: 500}
}
feeder.FeedOperation(&operation)
operation.Request.Timestamp = time.Now().UTC().UnixNano()
resp, err := httpClient.Do(req)
if err != nil {
log.Println("Request failed: ", err)
return &InspectorError{
Error: err,
Code: 400}
}
respOutbound, err := models.NewResponse(resp)
if err != nil {
return &InspectorError{
Error: err,
}
}
operation.Response = respOutbound
operation.Response.Timestamp = time.Now().UTC().UnixNano()
for key, vals := range respOutbound.Header {
for _, val := range vals {
w.Header().Add(key, val)
}
}
w.Write([]byte(respOutbound.Body))
resp.Trailer.Write(w)
feeder.FeedOperation(&operation)
project.Endpoint.Operations.PushBack(&operation)
store.AnonStore.SaveProject(project)
return nil
}