/
service.go
150 lines (140 loc) · 3.62 KB
/
service.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 proxy
import (
"context"
"crypto/tls"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"strings"
"github.com/iGoogle-ink/gopher/xlog"
)
type Service struct {
httpCli *http.Client
Schema SchemaType // SchemaHTTP or SchemaHTTPS
r *http.Request
Host string
Port string
Key string
log *log.Logger
}
func NewHandler(c *Config) (handler *ProxyHandler) {
srv = &Service{
httpCli: new(http.Client),
Schema: c.ProxySchema,
Host: c.ProxyHost,
Port: c.ProxyPort,
Key: c.Key,
log: log.New(os.Stdout, string(xlog.Magenta)+" [PROXY] "+string(xlog.Reset), log.Ldate|log.Lmicroseconds),
}
return &ProxyHandler{c: c}
}
// Proxy
func (s *Service) Proxy(c context.Context, w http.ResponseWriter, r *http.Request) {
var (
req *http.Request
reader *strings.Reader
err error
rMethod = r.Method
rHeader = r.Header
rUri = r.RequestURI
pa = r.Form.Encode()
rBody = r.Body
)
s.r = r
// 验证 Key
key := rHeader.Get(HEADER_CONTENT_KEY)
if s.Key != key {
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, fmt.Sprintf("[%s] invalid key", key))
return
}
uri := fmt.Sprintf("%s", s.Schema) + s.Host + s.Port + rUri
// Request
m := strings.ToUpper(r.Method)
ct := rHeader.Get(HEADER_CONTENT_TYPE)
switch m {
case HTTP_METHOD_POST:
switch ct {
case CONTENT_TYPE_JSON:
jsbs, err := ioutil.ReadAll(io.LimitReader(rBody, int64(4<<20))) // default 4MB, change the size you want;
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, err.Error())
return
}
reader = strings.NewReader(string(jsbs))
case CONTENT_TYPE_FORM:
reader = strings.NewReader(pa)
case CONTENT_TYPE_XML:
xmlbs, err := ioutil.ReadAll(io.LimitReader(rBody, int64(4<<20)))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, err.Error())
return
}
reader = strings.NewReader(string(xmlbs))
default:
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, "request type error")
return
}
req, err = http.NewRequest(HTTP_METHOD_POST, uri, reader)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, err.Error())
return
}
case HTTP_METHOD_GET:
req, err = http.NewRequest(HTTP_METHOD_GET, uri, nil)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, err.Error())
return
}
default:
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, "only support GET and POST")
return
}
// Request Content
req.Header = rHeader
s.httpCli.Transport = &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, DisableKeepAlives: true}
resp, err := s.httpCli.Do(req)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, err.Error())
return
}
defer resp.Body.Close()
s.log.Println(fmt.Sprintf("| %d | %s | %s %s", resp.StatusCode, s.clientIP(), rMethod, r.RequestURI))
rsp, err := ioutil.ReadAll(resp.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, err.Error())
return
}
for k, _ := range resp.Header {
w.Header().Set(k, resp.Header.Get(k))
}
w.WriteHeader(http.StatusOK)
w.Write(rsp)
}
func (s *Service) clientIP() string {
rHeader := s.r.Header
clientIP := rHeader.Get("X-Forwarded-For")
clientIP = strings.TrimSpace(strings.Split(clientIP, ",")[0])
if clientIP == "" {
clientIP = strings.TrimSpace(rHeader.Get("X-Real-Ip"))
}
if clientIP != "" {
return clientIP
}
if ip, _, err := net.SplitHostPort(strings.TrimSpace(s.r.RemoteAddr)); err == nil {
return ip
}
return ""
}