-
Notifications
You must be signed in to change notification settings - Fork 2
/
esproxy.go
180 lines (168 loc) · 6.21 KB
/
esproxy.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
177
178
179
180
package main
import (
"bytes"
"flag"
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
)
var debugLog,errorLog,infoLog *log.Logger
var ProxyUrl1,ProxyUrl2,Port string
var RetryTime int
var TimeOut int64
var help bool
var (
Proxy_receiver_total = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "proxy_receiver_total",
Help: "count proxy receiver request",
},
[]string {"Server","Method","Uri"},
)
//model.AlertsFromCounter.WithLabelValues("from","to","message","level","host","index").Add(1)
Proxy_send_success_total = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "proxy_send_success_total",
Help: "count proxy send success request",
},
[]string {"Server","Method","Uri"},
)
//model.AlertToCounter.WithLabelValues("to","message").Add(1)
Proxy_send_fail_total = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "proxy_send_fail_total",
Help: "count proxy fail request",
},
[]string {"Server","Method","Uri"},
)
)
func init() {
flag.StringVar(&ProxyUrl1, "hosta", "http://10.25.60.212:9200", "指定默认代理的目标`http://10.25.60.212:9200`")
flag.StringVar(&ProxyUrl2, "hostb", "http://10.25.60.213:9200", "指定流量镜像的目标`http://10.25.60.213:9200`")
flag.StringVar(&Port, "p", "9200", "代理服务监听端口`9200`")
flag.IntVar(&RetryTime, "r", 3, "`后端代理失败重试次数`")
flag.Int64Var(&TimeOut, "t", 30, "`后端代理超时时间(秒)`")
flag.BoolVar(&help,"h", false, "显示帮助")
flag.Usage = usage
}
func usage() {
fmt.Fprintf(os.Stderr, `Version 0.1 If you need help contact [email protected]
Usage: esproxy [-h] [-hosta ProxyTarget] [-hostb CopyTarget] [-p Port] [-r RetryTime] [-t TimeOut]
Example:esproxy -hosta http://10.25.60.212:9200 -hostb http://10.25.60.213:9200 -p 9200 -r 3 -t 30
Options:
`)
flag.PrintDefaults()
}
func main() {
flag.Parse()
if help {
flag.Usage()
return
}
infoFile, err := os.OpenFile("log/proxy.log", os.O_WRONLY | os.O_CREATE | os.O_APPEND, 0644)
if err != nil {
fmt.Println("[ "+time.Now().Format("2006/01/02 15:04:05")+" ]",err.Error())
}
errorFile, err := os.OpenFile("log/proxy-error.log", os.O_WRONLY | os.O_CREATE | os.O_APPEND, 0644)
if err != nil {
fmt.Println("[ "+time.Now().Format("2006/01/02 15:04:05")+" ]",err.Error())
}
debugLog = log.New(infoFile, "[DEBUG] [ ", log.Ldate|log.Ltime )
errorLog = log.New(errorFile, "[ERROR] [ ", log.Ldate|log.Ltime)
infoLog = log.New(infoFile, "[INFO] [ ", log.Ldate|log.Ltime)
prometheus.MustRegister(Proxy_receiver_total,Proxy_send_success_total,Proxy_send_fail_total)
http.HandleFunc("/", handler)
http.Handle("/app/metrics", promhttp.Handler())
fmt.Println("[ "+time.Now().Format("2006/01/02 15:04:05")+" ] Start Elasticsearch Proxy On 0.0.0.0:"+Port)
http.ListenAndServe(":"+Port, nil)
os.Exit(0)
}
func handler(w http.ResponseWriter, r *http.Request) {
//var tr *http.Transport
//tr = &http.Transport{
// TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
// Proxy: proxy,
//}
//超时时间设置
HttpTimeOut:=time.Duration(TimeOut)*time.Second
client := &http.Client{Timeout:HttpTimeOut}
HttpBody,_:=ioutil.ReadAll(r.Body)
//缓存request body
ReqBody1:=ioutil.NopCloser(bytes.NewBuffer(HttpBody))
ReqBody2:=ioutil.NopCloser(bytes.NewBuffer(HttpBody))
//记录请求内容
Proxy_receiver_total.WithLabelValues("127.0.0.1:9200",r.Method,r.RequestURI).Add(1)
infoLog.Println("] [Server] 127.0.0.1:9200 [Method] "+r.Method," [Uri] "+r.RequestURI,"[HttpBody] "+strings.Replace(string(HttpBody),"\n","",-1))
//异步转发给第二个ES
infoLog.Println("] [Server] "+ProxyUrl2, "[Method] "+r.Method, " [Uri] "+r.RequestURI, "[HttpBody] "+strings.Replace(string(HttpBody), "\n", "", -1))
channel := make(chan int)
go func(ch chan int) {
for Retry:=0; Retry<RetryTime+1;Retry++ {
req2, err := http.NewRequest(r.Method,ProxyUrl2+r.RequestURI,ReqBody2)
req2.Header=r.Header
_, err = client.Do(req2)
if err != nil {
Proxy_send_fail_total.WithLabelValues(ProxyUrl2,r.Method,r.RequestURI).Add(1)
infoLog.Println("] [Server] "+ProxyUrl2,"[Method] "+r.Method," [Uri] "+r.RequestURI,err.Error())
if Retry == RetryTime {
errorLog.Println("] [Server] "+ProxyUrl2, "[Method] "+r.Method, " [Uri] "+r.RequestURI, "[HttpBody] "+strings.Replace(string(HttpBody), "\n", "", -1))
break
}
debugLog.Println("] [Server] "+ProxyUrl2,"[Method] "+r.Method," [Uri] "+r.RequestURI," retry time "+strconv.Itoa(Retry+1))
}else {
Proxy_send_success_total.WithLabelValues(ProxyUrl2,r.Method,r.RequestURI).Add(1)
break
}
defer req2.Body.Close()
}
ch <- 1
<- channel
}(channel)
//请求第一个ES
var resp = &http.Response{}
infoLog.Println("] [Server] "+ProxyUrl1, "[Method] "+r.Method, " [Uri] "+r.RequestURI, "[HttpBody] "+strings.Replace(string(HttpBody), "\n", "", -1))
for Retry:=0; Retry<RetryTime+1;Retry++ {
req, err := http.NewRequest(r.Method,ProxyUrl1+r.RequestURI,ReqBody1)
req.Header=r.Header
resp, err = client.Do(req)
if err != nil {
Proxy_send_fail_total.WithLabelValues(ProxyUrl1,r.Method,r.RequestURI).Add(1)
infoLog.Println("] [Server] "+ProxyUrl1,"[Method] "+r.Method," [Uri] "+r.RequestURI,err.Error())
if Retry == RetryTime {
http.NotFound(w, r)
errorLog.Println("] [Server] "+ProxyUrl1, "[Method] "+r.Method, " [Uri] "+r.RequestURI, "[HttpBody] "+strings.Replace(string(HttpBody), "\n", "", -1))
return
}
debugLog.Println("] [Server] "+ProxyUrl1,"[Method] "+r.Method," [Uri] "+r.RequestURI," retry time "+strconv.Itoa(Retry+1))
}else {
Proxy_send_success_total.WithLabelValues(ProxyUrl1,r.Method,r.RequestURI).Add(1)
break
}
defer req.Body.Close()
}
//处理返回
data, err := ioutil.ReadAll(resp.Body)
if err != nil && err != io.EOF {
http.NotFound(w, r)
return
}
for i, j := range resp.Header {
for _, k := range j {
w.Header().Add(i, k)
}
}
for _, c := range resp.Cookies() {
w.Header().Add("Set-Cookie", c.Raw)
}
_, ok := resp.Header["Content-Length"]
if !ok && resp.ContentLength > 0 {
w.Header().Add("Content-Length", fmt.Sprint(resp.ContentLength))
}
w.WriteHeader(resp.StatusCode)
w.Write(data)
}