-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxyhttp.go
176 lines (160 loc) · 4.52 KB
/
proxyhttp.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
package main
import (
"bytes"
"database/sql"
"flag"
"fmt"
_ "github.com/lib/pq"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"sync/atomic"
)
var (
localPort = flag.Int("f", 12345, "The proxy server's port")
endpoint = flag.String("t", "http://wsf.cdyne.com/WeatherWS/Weather.asmx", "The host that the proxy server should forward requests to")
maxConnections = flag.Int("c", 25, "The maximum number of concurrent connection")
mock = flag.Int("m", 0, "Use mock response when needle is found")
verbose = flag.Int("v", 0, "Log response data to STDOUT when needle is found")
matches uint64 = 0
needles []string
mocks []string
)
const (
SERVER_PATH = "/proxy"
KILL_PATH = "/kill"
MATCH_FOUND_STR = "Found the needle"
PROXY_INIT_STR = "========= Proxy initializing ========="
NEEDLE_MOCK_STR = "Loading needles and mocks..."
PROXY_ACTIVE_STR = "------------ Proxy active ------------"
RECEIVED_MSG_STR = "<<<<<<< Started request proxy >>>>>>>>"
MOCK_MSG_STR = "Returning mock value"
PROXY_MSG_STR = "Proxy request: returning actual value"
DONE_STR = "<<<<<<<<<<<<<< Finished >>>>>>>>>>>>>>"
SHUTTING_DOWN_STR = "=========== Shutting down ============"
MOCKS_IN_USE_STR = "Using mock responses when applicable"
PORT_STR = "Port: "
MAX_CON_STR = "Max connections: "
ENDPOINT_STR = "Endpoint: "
NEEDLE_MOCK_SQL = "SELECT needle, mock FROM proxy.mapping"
DB_USER = "http"
DB_PASSWORD = "proxy"
DB_NAME = "httpproxy"
)
/**
* Parse input flags and start HTTP server on *fromPort
*/
func main() {
flag.Parse()
log.Println(PROXY_INIT_STR)
log.Println(PORT_STR, *localPort)
log.Println(MAX_CON_STR, *maxConnections)
log.Println(ENDPOINT_STR, *endpoint)
if *mock == 1 {
log.Println(MOCKS_IN_USE_STR)
}
getNeedlesAndMocks()
log.Println(PROXY_ACTIVE_STR)
http.HandleFunc(SERVER_PATH, ProxyServer)
http.HandleFunc(KILL_PATH, KillServer)
err := http.ListenAndServe(fmt.Sprintf(":%d", *localPort), nil)
handleError(err)
}
/**
* HTTP server for proxy
*/
func ProxyServer(outputStream http.ResponseWriter, request *http.Request) {
responseChannel := make(chan []byte, *maxConnections)
defer close(responseChannel)
go proxy(request, responseChannel)
outputStream.Write(<-responseChannel)
}
/**
* Actual proxy method
*/
func proxy(request *http.Request, responseChannel chan []byte) {
log.Println(RECEIVED_MSG_STR)
var responseBody []byte
requestBody, err := ioutil.ReadAll(request.Body)
handleError(err)
contentType := request.Header.Get("Content-type")
handleError(err)
request.Body.Close()
mockResponse := getMockOnMatch(requestBody)
if len(mockResponse) > 0 {
log.Println(MOCK_MSG_STR)
responseBody = []byte(mockResponse)
} else {
log.Println(PROXY_MSG_STR)
responseBody = getResponse(requestBody, contentType)
if *verbose == 1 {
log.Println(string(responseBody))
}
}
responseChannel <- responseBody
log.Println(DONE_STR)
}
/**
* Function to determine whether certain needle exists in request
*/
func getMockOnMatch(input []byte) (mockResponse string) {
for key, needle := range needles {
if strings.Contains(string(input), needle) {
atomic.AddUint64(&matches, 1)
log.Println(MATCH_FOUND_STR, atomic.LoadUint64(&matches))
if *mock == 1 {
mockResponse = mocks[key]
}
}
}
return mockResponse
}
/**
* Read the actual response from the endpoint and return it
*/
func getResponse(request []byte, contentType string) (responseBody []byte) {
byteReader := bytes.NewBuffer(request)
response, err := http.Post(*endpoint, contentType, byteReader)
handleError(err)
responseBody, err = ioutil.ReadAll(response.Body)
handleError(err)
response.Body.Close()
return responseBody
}
/**
* Error handler function to improve readability
*/
func handleError(err error) {
if err != nil {
panic(err)
}
}
/**
* HTTP server stop servlet
*/
func KillServer(outputStream http.ResponseWriter, request *http.Request) {
log.Println(SHUTTING_DOWN_STR)
os.Exit(0)
}
/**
* Read needles and mock responses from DB
*/
func getNeedlesAndMocks() {
log.Println(NEEDLE_MOCK_STR)
dbinfo := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable", DB_USER, DB_PASSWORD, DB_NAME)
db, err := sql.Open("postgres", dbinfo)
handleError(err)
defer db.Close()
rows, err := db.Query(NEEDLE_MOCK_SQL)
handleError(err)
for rows.Next() {
var needle string
var mock string
err = rows.Scan(&needle, &mock)
handleError(err)
needles = append(needles, needle)
mocks = append(mocks, mock)
}
}