-
Notifications
You must be signed in to change notification settings - Fork 11
/
Protocol.go
141 lines (128 loc) · 4.76 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package HTTP
import (
"bufio"
"bytes"
"encoding/base64"
"errors"
"github.com/shoriwe/FullProxy/pkg/Proxies"
"github.com/shoriwe/FullProxy/pkg/Templates"
"github.com/shoriwe/FullProxy/pkg/Templates/Types"
"gopkg.in/elazarl/goproxy.v1"
"net"
"net/http"
"net/http/httptest"
"strings"
"time"
)
type CustomResponseWriter struct {
httptest.ResponseRecorder
http.Hijacker
ClientConnection net.Conn
ClientReadWriter *bufio.ReadWriter
}
func (customResponseWriter *CustomResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
return customResponseWriter.ClientConnection, customResponseWriter.ClientReadWriter, nil
}
func CreateCustomResponseWriter(
clientConnection net.Conn,
clientConnectionReader *bufio.Reader,
clientConnectionWriter *bufio.Writer) *CustomResponseWriter {
slaveResponseWriter := new(CustomResponseWriter)
slaveResponseWriter.Body = new(bytes.Buffer)
slaveResponseWriter.Code = 200
slaveResponseWriter.ClientConnection = clientConnection
slaveResponseWriter.ClientReadWriter = bufio.NewReadWriter(
clientConnectionReader,
clientConnectionWriter)
return slaveResponseWriter
}
type HTTP struct {
AuthenticationMethod Types.AuthenticationMethod
ProxyController *goproxy.ProxyHttpServer
LoggingMethod Types.LoggingMethod
InboundFilter Types.IOFilter
OutboundFilter Types.IOFilter
}
func (httpProtocol *HTTP) SetLoggingMethod(loggingMethod Types.LoggingMethod) error {
httpProtocol.LoggingMethod = loggingMethod
return nil
}
func (httpProtocol *HTTP) SetInboundFilter(filter Types.IOFilter) error {
if httpProtocol.ProxyController == nil {
return errors.New("No HTTP proxy controller was set")
}
httpProtocol.InboundFilter = filter
httpProtocol.ProxyController.OnRequest().DoFunc(func(
request *http.Request,
proxyCtx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
if !Templates.FilterInbound(httpProtocol.InboundFilter, Templates.ParseIP(request.RemoteAddr)) {
Templates.LogData(httpProtocol.LoggingMethod, "Connection denied to: ", request.RemoteAddr)
return request, goproxy.NewResponse(request, goproxy.ContentTypeText, http.StatusProxyAuthRequired, "Don't waste your time!")
}
Templates.LogData(httpProtocol.LoggingMethod, "Connection Received from: ", request.RemoteAddr)
return request, nil
})
return nil
}
func (httpProtocol *HTTP) SetOutboundFilter(filter Types.IOFilter) error {
panic("Not implemented yet")
/*
if httpProtocol.ProxyController == nil {
return errors.New("No HTTP proxy controller was set")
}
*/
return nil
}
func (httpProtocol *HTTP) SetAuthenticationMethod(authenticationMethod Types.AuthenticationMethod) error {
if httpProtocol.ProxyController == nil {
return errors.New("No HTTP proxy controller was set")
}
httpProtocol.AuthenticationMethod = authenticationMethod
httpProtocol.ProxyController.OnRequest().DoFunc(func(
request *http.Request,
proxyCtx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
authentication := strings.Split(request.Header.Get("Proxy-Authorization"), " ")
if len(authentication) == 2 {
if authentication[0] == "Basic" {
rawCredentials, decodingError := base64.StdEncoding.DecodeString(authentication[1])
if decodingError == nil {
credentials := bytes.Split(rawCredentials, []byte(":"))
if len(credentials) == 2 {
if Proxies.Authenticate(httpProtocol.AuthenticationMethod, credentials[0], credentials[1]) {
Templates.LogData(httpProtocol.LoggingMethod, "Login successful with: ", request.RemoteAddr)
return request, nil
}
}
}
}
}
Templates.LogData(httpProtocol.LoggingMethod, "Login failed with invalid credentials from: ", request.RemoteAddr)
return request, goproxy.NewResponse(request, goproxy.ContentTypeText, http.StatusProxyAuthRequired, "Don't waste your time!")
})
return nil
}
func (httpProtocol *HTTP) SetTries(_ int) error {
return errors.New("Custom tries is not supported by HTTP proxy")
}
func (httpProtocol *HTTP) SetTimeout(_ time.Duration) error {
return errors.New("Custom timeout is not supported by HTTP proxy")
}
func (httpProtocol *HTTP) Handle(
clientConnection net.Conn,
clientConnectionReader *bufio.Reader,
clientConnectionWriter *bufio.Writer) error {
request, parsingError := http.ReadRequest(clientConnectionReader)
if parsingError != nil {
Templates.LogData(httpProtocol.LoggingMethod, parsingError)
} else {
request.RemoteAddr = clientConnection.RemoteAddr().String()
responseWriter := CreateCustomResponseWriter(clientConnection, clientConnectionReader, clientConnectionWriter)
httpProtocol.ProxyController.ServeHTTP(responseWriter, request)
if responseWriter.Result().ContentLength > 0 {
_ = responseWriter.Result().Write(clientConnectionWriter)
_ = clientConnectionWriter.Flush()
}
}
_ = clientConnection.Close()
return parsingError
}