This repository has been archived by the owner on Mar 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
main.go
executable file
·212 lines (199 loc) · 5.42 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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package main
/*
Blockchain Threat Intelligence Sharing Platform By slowmist.com
http/https反向代理,支持自定义回包规则
1. 开启监听端口
2. 协程处理每一个请求
3. 加载配置文件
4. 解析并处理回包
5. 结束
*/
import (
"bufio"
"bytes"
"crypto/tls"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"net/http/httputil"
"net/url"
"os"
"strings"
"time"
"github.com/slowmist/blockchain-threat-intelligence/src/etc"
"github.com/slowmist/blockchain-threat-intelligence/src/filter"
"github.com/slowmist/blockchain-threat-intelligence/src/logger"
"github.com/urfave/cli"
)
const (
HTTP_TIMEOUT = 30 * time.Second //源站超时时间
)
//全局参数
type SLOWMIST struct {
SLOWMIST_targetURL *url.URL
SLOWMIST_listenAddress string
SLOWMIST_isSSL bool
SLOWMIST_certPath string
SLOWMIST_privateKeyPath string
}
//监听端口
func (s *SLOWMIST) initServer() (net.Listener, error) {
listenAddr := s.SLOWMIST_listenAddress
if s.SLOWMIST_isSSL == true {
fmt.Println("Listening on:", fmt.Sprintf("https://%s", listenAddr))
_, err1 := os.Stat(s.SLOWMIST_certPath)
_, err2 := os.Stat(s.SLOWMIST_privateKeyPath)
var (
cer tls.Certificate
err error
)
if os.IsExist(err1) && os.IsExist(err2) {
cer, err = tls.LoadX509KeyPair(s.SLOWMIST_certPath, s.SLOWMIST_privateKeyPath)
} else {
_cert, _ := etc.Asset(s.SLOWMIST_certPath)
_pkey, _ := etc.Asset(s.SLOWMIST_privateKeyPath)
cer, err = tls.X509KeyPair([]byte(_cert), []byte(_pkey))
}
if err != nil {
log.Println(`certificate default file load failed!`, err.Error())
return nil, err
}
config := &tls.Config{Certificates: []tls.Certificate{cer}}
listener, err := tls.Listen("tcp", listenAddr, config)
if err != nil {
log.Println(`https listener failed!`, err.Error())
return nil, err
}
return listener, err
} else {
fmt.Println("Listening on:", fmt.Sprintf("http://%s", listenAddr))
listener, err := net.Listen("tcp", listenAddr)
if err != nil {
log.Println(`http listener failed!`, err.Error())
return nil, err
}
return listener, err
}
}
//构造新请求
func (s *SLOWMIST) buildNewReq(request *http.Request) (*http.Request, error) {
_n := request.URL
_n.Scheme = s.SLOWMIST_targetURL.Scheme
_n.Host = s.SLOWMIST_targetURL.Host
req, err := http.NewRequest(request.Method, _n.String(), request.Body)
if err != nil {
log.Println("Error new request:", err.Error())
return req, err
}
req.Close = true
for key := range request.Header {
req.Header.Set(key, request.Header.Get(key))
}
if request.Referer() != "" {
req.Header.Set("Referer", strings.Replace(request.Referer(), request.Host, s.SLOWMIST_targetURL.Host, -1))
}
req.Header.Del("Accept-Encoding")
return req, err
}
//连接处理
func (s *SLOWMIST) handleConnection(conn net.Conn) {
defer conn.Close()
reader := bufio.NewReader(conn)
request, err := http.ReadRequest(reader)
request.RemoteAddr = conn.RemoteAddr().String()
if err != nil {
log.Println("Error parsing request:", err.Error())
return
}
requestTextBuff, _ := ioutil.ReadAll(request.Body)
//请求源站
req, _ := s.buildNewReq(request)
client := &http.Client{
Timeout: HTTP_TIMEOUT,
}
resp, err := client.Do(req)
if err != nil {
log.Println("Proxy error:", err.Error())
return
}
//根据请求参数修改响应内容
request.Body = ioutil.NopCloser(bytes.NewBufferString(string(requestTextBuff)))
filter.ModifyResponse(request, resp)
modifiedResponse, err := httputil.DumpResponse(resp, true)
if err != nil {
log.Println("Error converting requests to bytes:", err.Error())
return
}
_, err = conn.Write(modifiedResponse)
if err != nil {
log.Println("Error responding to clientclient:", err.Error())
return
}
}
func main() {
app := cli.NewApp()
app.Version = "0.1.1"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "target",
Value: "https://mainnet.infura.io/LWLRS6nNpQR09kd6j1vE",
Usage: "The ethereum host we used to proxy.",
},
cli.StringFlag{
Name: "listen",
Value: "0.0.0.0:8545",
Usage: "Address and port to run proxy service on. Format address:port.",
},
cli.StringFlag{
Name: "https",
Value: "0",
Usage: "Listen with TLS.",
},
cli.StringFlag{
Name: "cert",
Value: "etc/server.crt",
Usage: "Path to the x509 encoded SSL certificate in PEM format.",
},
cli.StringFlag{
Name: "private-key",
Value: "etc/server.key",
Usage: "Path to the x509 encoded certificate in PEM format.",
},
cli.StringFlag{
Name: "bounty",
Value: "0x1234567890123456789012345678901234567800",
Usage: `Send bounty(SlowMist Zone Token) to this address. (default: "0x1234567890123456789012345678901234567800")`,
},
}
app.Action = func(c *cli.Context) error {
cli.ShowVersion(c)
logger.BountyAddress = c.String("bounty")
_lg := &logger.STARTLOG{
ClientVersion: c.App.Version,
ReporterEthAddr: logger.BountyAddress,
Time: time.Now().Unix(),
}
_lg.Write()
_u, _ := url.Parse(c.String("target"))
_t := &SLOWMIST{
SLOWMIST_targetURL: _u,
SLOWMIST_listenAddress: c.String("listen"),
SLOWMIST_isSSL: c.Bool("https"),
SLOWMIST_certPath: c.String("cert"),
SLOWMIST_privateKeyPath: c.String("private-key"),
}
var s net.Listener
s, _ = _t.initServer()
for {
conn, err := s.Accept()
if err != nil {
log.Println("Error when accepting request,", err.Error())
continue
}
go _t.handleConnection(conn)
}
}
app.Run(os.Args)
}