-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
129 lines (110 loc) · 2.72 KB
/
main.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
package main
import (
"crypto/tls"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"time"
)
var clientHttps *http.Client
var clientHttp *http.Client
func init() {
initConf()
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
clientHttps = &http.Client{Transport: tr}
clientHttp = &http.Client{}
}
// handler entrance
func handler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
for i := 0; i < len(appConf.Uris); i++ {
if r.URL.Path == appConf.Uris[i].Uri {
handleRequest(w, r, appConf.Uris[i].Delay, appConf.Uris[i])
return
}
}
}
func handleRequest(w http.ResponseWriter, r *http.Request, delay int, uri Uri) {
time.Sleep(time.Duration(delay) * time.Millisecond)
// from outer
if uri.Source != "" {
handleSource(w, r, uri)
return
}
// from json
for _, entry := range uri.Headers {
for key, value := range entry {
w.Header().Set(key, value)
}
}
fmt.Fprintf(w, uri.Body)
}
func handleSource(w http.ResponseWriter, r *http.Request, uri Uri) {
srcArry := strings.SplitN(uri.Source, " ", 2)
if strings.HasPrefix(srcArry[1], "http://") {
handleHttp(w, r, clientHttp, srcArry)
return
}
if strings.HasPrefix(srcArry[1], "https://") {
handleHttp(w, r, clientHttps, srcArry)
return
}
// handle local file
fmt.Fprintf(w, loadCert(uri.Body))
return
}
// handle http and https
func handleHttp(w http.ResponseWriter, r *http.Request, client *http.Client, srcArry []string) {
var resp *http.Response
var err error
if srcArry[0] == "GET" {
fmt.Println("GET:[", srcArry[1], "]")
resp, err = client.Get(srcArry[1])
} else if srcArry[0] == "POST" {
fmt.Println("POST:[", srcArry[1], "]")
resp, err = client.Post(srcArry[1], "application/json", r.Body)
} else {
err = errors.New("Method not supported yet:" + srcArry[0])
}
if err != nil {
fmt.Fprintf(w, err.Error())
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Fprintf(w, err.Error())
return
}
for key, entry := range resp.Header {
w.Header().Set(key, strings.Join(entry, ","))
}
fmt.Fprintf(w, string(body))
}
func main() {
for i := 0; i < len(appConf.Uris); i++ {
http.HandleFunc(appConf.Uris[i].Uri, handler)
}
//http.HandleFunc("/", handler)
if exist("config/server.cert") && exist("config/server.key") {
http.ListenAndServeTLS(":20175", "config/server.cert", "config/server.key", nil)
} else {
http.ListenAndServe(":20175", nil)
}
}
func loadCert(filename string) string {
data, err := ioutil.ReadFile(filename)
if err != nil {
fmt.Println("ERROR:", err)
}
return string(data)
}
func exist(filename string) bool {
_, err := os.Stat(filename)
return err == nil || os.IsExist(err)
}