-
Notifications
You must be signed in to change notification settings - Fork 286
/
http.go
214 lines (198 loc) · 5.02 KB
/
http.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
213
214
package shuttle
import (
"bufio"
"io"
"net"
"net/http"
"strconv"
"strings"
"time"
"context"
"github.com/sipt/shuttle/config"
connect "github.com/sipt/shuttle/conn"
"github.com/sipt/shuttle/log"
"github.com/sipt/shuttle/proxy"
rule2 "github.com/sipt/shuttle/rule"
"github.com/sipt/shuttle/util"
)
const (
HTTP = "http"
HTTPS = "https"
)
var allowMitm = false
var allowDump = false
var MitMRules []string
func SetAllowMitm(b bool) {
allowMitm = b
}
func SetAllowDump(b bool) {
allowDump = b
}
func GetAllowMitm() bool {
return allowMitm
}
func GetAllowDump() bool {
return allowDump
}
// init MitMRules
func SetMitMRules(rs []string) {
MitMRules = rs
}
func GetMitMRules() []string { // For controller API
return MitMRules
}
func AppendMitMRules(r string) { // For controller API
MitMRules = append(MitMRules, r)
conf := config.CurrentConfig()
conf.Mitm.Rules = MitMRules
config.SaveConfig(config.CurrentConfigFile(), conf)
}
func RemoveMitMRules(r string) { // For controller API
for i, v := range MitMRules {
if v == r {
MitMRules[i] = MitMRules[len(MitMRules)-1]
MitMRules = MitMRules[:len(MitMRules)-1]
conf := config.CurrentConfig()
conf.Mitm.Rules = MitMRules
config.SaveConfig(config.CurrentConfigFile(), conf)
return
}
}
}
func HandleHTTP(co net.Conn) {
log.Logger.Debug("start conn.IConn wrap net.Con")
conn, err := connect.NewDefaultConn(co, connect.TCP)
if err != nil {
log.Logger.Errorf("[HTTP] shuttle.IConn wrap net.Conn failed: %v", err)
return
}
log.Logger.Debugf("[HTTP] [ID:%d] shuttle.IConn wrap net.Conn success", conn.GetID())
log.Logger.Debugf("[HTTP] [ID:%d] start read http request", conn.GetID())
//prepare request
hreq, err := prepareRequest(conn)
if err != nil {
if err != io.EOF {
log.Logger.Errorf("[HTTP] [ID:%d] prepareRequest failed: %s", conn.GetID(), err.Error())
}
return
}
//switch hreq.Proto {
//case "HTTP/2":
// ProxyHTTP2()
//case "HTTP/1.1":
if hreq.URL.Scheme == HTTP { // HTTP
ProxyHTTP(conn, hreq)
} else { // HTTPS
ProxyHTTPS(conn, hreq)
}
//}
}
func ProxyHTTP(lc connect.IConn, hreq *http.Request) {
HttpTransport(lc, nil, allowDump, hreq)
}
func ProxyHTTPS(lc connect.IConn, hreq *http.Request) {
// Handshake
_, err := lc.Write([]byte("HTTP/1.1 200 Connection established\r\n\r\n"))
if err != nil {
log.Logger.Errorf("[HTTPS] [ID:%d] reply https-CONNECT failed: %s", lc.GetID(), err.Error())
lc.Close()
return
}
domain := hreq.URL.Hostname()
rule, server, sc, err := ConnectFilter(hreq, lc.GetID())
record := &Record{
Protocol: HTTPS,
Created: time.Now(),
Status: RecordStatusActive,
URL: hreq.URL.String(),
Proxy: server,
Rule: rule,
}
if hreq.URL.Scheme == "" {
record.URL = "https:" + record.URL
}
if err != nil {
if err == ErrorReject {
record.Status = RecordStatusReject
} else {
record.Status = RecordStatusFailed
record.Rule = rule2.FailedRule
record.Proxy = proxy.FailedServer
}
record.ID = util.NextID()
boxChan <- &Box{Op: RecordAppend, Value: record}
return
}
// MitM
mitm := false
if allowMitm {
for _, v := range MitMRules {
if v == "*" { // 通配
log.Logger.Debugf("[HTTPS] [ID:%d] MitM RuleFilter [%s] use [%s]", lc.GetID(), domain, v)
mitm = true
break
} else if v == domain { // 全区配
log.Logger.Debugf("[HTTPS] [ID:%d] MitM RuleFilter [%s] use [%s]", lc.GetID(), domain, v)
mitm = true
break
} else if v[0] == '*' && strings.HasSuffix(domain, v[1:]) { // 后缀匹配
log.Logger.Debugf("[HTTPS] [ID:%d] MitM RuleFilter [%s] use [%s]", lc.GetID(), domain, v)
mitm = true
break
}
}
}
//MitM Decorate
if mitm {
log.Logger.Debugf("[HTTPS] [ID:%d] MitM Decorate", lc.GetID())
lct, sct, err := Mimt(lc, sc)
if err != nil {
log.Logger.Error("[HTTPS] [ID:%d] MitM failed: %s", lc.GetID(), err.Error())
record.Status = RecordStatusFailed
boxChan <- &Box{Op: RecordAppend, Value: record}
lc.Close()
sc.Close()
return
}
lc, sc = lct, sct
ctx := sc.Context()
ctx = context.WithValue(ctx, "rule", rule)
ctx = context.WithValue(ctx, "server", server)
sc.SetContext(ctx)
HttpTransport(lc, sc, allowDump, nil)
return
}
record.ID = util.NextID()
boxChan <- &Box{Op: RecordAppend, Value: record}
sc.SetRecordID(record.ID)
direct := &DirectChannel{}
direct.Transport(lc, sc)
boxChan <- &Box{record.ID, RecordStatus, RecordStatusCompleted}
}
func ProxyHTTP2() {
}
func prepareRequest(conn connect.IConn) (*http.Request, error) {
br := bufio.NewReader(conn)
hreq, err := http.ReadRequest(br)
if err != nil {
return nil, err
}
log.Logger.Debugf("[ID:%d] [HTTP/HTTPS] %s:%s", conn.GetID(), hreq.URL.Hostname(), hreq.URL.Port())
return hreq, nil
}
func StrToUint16(v string) (i uint16, err error) {
r, err := strconv.ParseUint(v, 10, 2*8)
if err == nil {
i = uint16(r)
}
return
}
func IsPass(host, port, ip string) bool {
if host == ControllerDomain {
return true
}
if (host == "localhost" || host == "127.0.0.1" || ip == "127.0.0.1") && ControllerPort == port {
return true
}
return false
}