-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreverseproxy.go
More file actions
183 lines (144 loc) · 3.66 KB
/
reverseproxy.go
File metadata and controls
183 lines (144 loc) · 3.66 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
//import package
import (
"fmt"
"os"
"log"
"net/http"
"io/ioutil"
"encoding/json"
"time"
"strconv"
"strings"
)
//data type declaration for config
type Config struct {
Target string
ListenOn string
StatFolder string
CreateNewFileEveryXMinutes int32
}
//variable declaration
var conf Config
//start date point
var tiN time.Time
//current file name
var fname string
func getConfig(cFile string) {
//open a file
content, err := ioutil.ReadFile(cFile)
if err!=nil{
fmt.Print("Error:",err)
}
//decode json
err = json.Unmarshal([]byte(content), &conf)
if err!=nil{
fmt.Print("Error:",err)
}
}
//check error function
func check(e error) {
if e != nil {
log.Fatal(e)
panic(e)
//os.Exit(1)
}
}
/*
Description:
Utility function to copy header information
Input:
ResponseWriter
Request
Return:
return formated information
*/
//copy header function
func copyHeader(source http.Header, dest *http.Header){
for n, v := range source {
for _, vv := range v {
dest.Add(n, vv)
}
}
}
/*
Description:
request handler implementation
Input:
ResponseWriter
Request
Return:
return formated information
*/
func logStat(w http.ResponseWriter, r *http.Request){
//get target + the stuff after it
uri := conf.Target+r.RequestURI
//for logging- need to be REMOVED
fmt.Println(r.Method + ": " + uri)
//remove session
cu := strings.Split(uri, "?")
//create new request to the target server with information from end user's browser
//The Information are method, header and body
rr, err := http.NewRequest(r.Method, cu[0], r.Body)
check(err)
//copy the header that we get from the browser
copyHeader(r.Header, &rr.Header)
// Create a client and query the target
var transport http.Transport
// no need to use http.Client.Do since we dont need to follow redirects nor handles cookies
// and transport.RoundTrip is roughly 60% faster since it only makes single HTTP transaction
resp, err := transport.RoundTrip(rr)
check(err)
//Write stat
writeStat(r.RequestURI)
defer resp.Body.Close()
//read response body we got fromserver
body, err := ioutil.ReadAll(resp.Body)
check(err)
//create http.ResponseWriter Header
dH := w.Header()
copyHeader(resp.Header, &dH)
w.Write(body)
}
/*
Description:
Decide how new information will be written.
Is it in the same file or new file
Input:
The formated result
Return:
return error if exist
*/
func writeStat(data string) {
//declare time duration it needs to wait
var gap time.Duration = time.Duration(conf.CreateNewFileEveryXMinutes) * time.Minute
//calculate duration
duration := time.Since(tiN)
if(duration > gap) {
//if gap has been passed
//reset time start and rename the file name that we are working on
tiN = time.Now()
fname = conf.StatFolder +"log-" + tiN.Format("2006.01.02-15:04:05")+ ".txt"
fmt.Printf("new file created")
}
//open the file
f, err := os.OpenFile(fname, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
check(err)
//split string, after ? is the user session
s := strings.Split(data, "?")
//write inside a file
_, err = f.WriteString(strconv.FormatInt(time.Now().Unix(), 10)+","+s[1]+","+s[0]+"\n")
check(err)
defer f.Close()
}
/**
main function
*/
func main() {
getConfig("config/conf.json") //get config
tiN = time.Now()
//reset time start and rename the file name that we are working on
fname = conf.StatFolder +"log-" + tiN.Format("2006.01.02-15:04:05")+ ".txt"
http.HandleFunc("/", logStat) //request handler for query stat
log.Fatal(http.ListenAndServe(conf.ListenOn, nil)) //listen incoming request
}