-
Notifications
You must be signed in to change notification settings - Fork 11
/
Protocol.go
126 lines (106 loc) · 3.28 KB
/
Protocol.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
package HTTP
import (
"bytes"
"encoding/base64"
"github.com/shoriwe/FullProxy/v2/pkg/Tools"
"github.com/shoriwe/FullProxy/v2/pkg/Tools/Types"
"gopkg.in/elazarl/goproxy.v1"
"net"
"net/http"
"strconv"
"strings"
)
type customListener struct {
clientConnections chan net.Conn
}
func (c customListener) Accept() (net.Conn, error) {
return <-c.clientConnections, nil
}
func (c customListener) Close() error {
close(c.clientConnections)
return nil
}
func (c customListener) Addr() net.Addr {
return nil
}
func newCustomListener() *customListener {
return &customListener{make(chan net.Conn, strconv.IntSize)}
}
type HTTP struct {
AuthenticationMethod Types.AuthenticationMethod
proxyHttpServer *goproxy.ProxyHttpServer
LoggingMethod Types.LoggingMethod
listener *customListener
OutboundFilter Types.IOFilter
}
func (protocol *HTTP) SetDial(dialFunc Types.DialFunc) {
protocol.proxyHttpServer.Tr.Dial = dialFunc
protocol.proxyHttpServer.ConnectDial = dialFunc
}
func NewHTTP(
authenticationMethod Types.AuthenticationMethod,
loggingMethod Types.LoggingMethod,
outboundFilter Types.IOFilter,
) *HTTP {
proxyHttpServer := goproxy.NewProxyHttpServer()
listener := newCustomListener()
go http.Serve(listener, proxyHttpServer)
result := &HTTP{
AuthenticationMethod: authenticationMethod,
proxyHttpServer: proxyHttpServer,
LoggingMethod: loggingMethod,
listener: listener,
OutboundFilter: outboundFilter,
}
proxyHttpServer.OnRequest().HandleConnect(goproxy.AlwaysMitm)
proxyHttpServer.OnRequest().DoFunc(
func(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
onErrorResponse := goproxy.NewResponse(req,
goproxy.ContentTypeText, http.StatusForbidden,
"Don't waste your time!")
if result.AuthenticationMethod != nil {
entry := req.Header.Get("Proxy-Authorization")
splitEntry := strings.Split(entry, " ")
if len(splitEntry) != 2 {
return req, onErrorResponse
}
if splitEntry[0] != "Basic" {
return req, onErrorResponse
}
rawUsernamePassword, decodingError := base64.StdEncoding.DecodeString(splitEntry[1])
if decodingError != nil {
return req, onErrorResponse
}
splitRawUsernamePassword := bytes.Split(rawUsernamePassword, []byte{':'})
if len(splitRawUsernamePassword) != 2 {
return req, onErrorResponse
}
authentication, authenticationError := result.AuthenticationMethod(splitRawUsernamePassword[0], splitRawUsernamePassword[1])
if !authentication || authenticationError != nil {
return req, onErrorResponse
}
}
if !Tools.FilterOutbound(result.OutboundFilter, req.Host) {
return req, onErrorResponse
}
return req, nil
},
)
return result
}
func (protocol *HTTP) SetLoggingMethod(loggingMethod Types.LoggingMethod) error {
protocol.LoggingMethod = loggingMethod
return nil
}
func (protocol *HTTP) SetOutboundFilter(filter Types.IOFilter) error {
protocol.OutboundFilter = filter
return nil
}
func (protocol *HTTP) SetAuthenticationMethod(authenticationMethod Types.AuthenticationMethod) error {
protocol.AuthenticationMethod = authenticationMethod
return nil
}
func (protocol *HTTP) Handle(clientConnection net.Conn) error {
protocol.listener.clientConnections <- clientConnection
return nil
}