-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhttp-proxy.go
301 lines (261 loc) · 7.04 KB
/
http-proxy.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only
// Copyright (C) Felix Geyer <debfx@fobos.de>
package main
import (
"bytes"
"context"
"encoding/gob"
"errors"
"fmt"
"io"
"net"
"net/http"
"os"
"path"
"strconv"
"strings"
"sync"
"github.com/elazarl/goproxy"
"github.com/gobwas/glob"
"golang.org/x/sys/unix"
)
type passHttpProxyChild struct {
Settings settingsStruct
SocketPath string
SyncFd uintptr
}
func encodePassHttpProxyChild(settings settingsStruct, socketPath string, syncFd uintptr) ([]byte, error) {
data := passHttpProxyChild{settings, socketPath, syncFd}
b := bytes.Buffer{}
e := gob.NewEncoder(&b)
if err := e.Encode(data); err != nil {
return []byte{}, err
}
return b.Bytes(), nil
}
func decodePassHttpProxyChild(input []byte) (settingsStruct, string, uintptr, error) {
output := passHttpProxyChild{}
b := bytes.Buffer{}
b.Write(input)
d := gob.NewDecoder(&b)
if err := d.Decode(&output); err != nil {
return settingsStruct{}, "", 0, err
}
return output.Settings, output.SocketPath, output.SyncFd, nil
}
func isHostAllowed(hostname string, allowedHosts []glob.Glob) bool {
hostnameWithoutPort := strings.Split(hostname, ":")[0]
for _, g := range allowedHosts {
if g.Match(hostnameWithoutPort) {
return true
}
}
return false
}
func validateAllowedHosts(settings settingsStruct) error {
for _, hostname := range settings.AllowedHosts {
_, err := glob.Compile(hostname)
if err != nil {
return err
}
}
return nil
}
func setupHttpProxy(originalSettings settingsStruct) (proxyPipe uintptr, proxyMount mount, cleanupFile string, err error) {
runtimeDir, err := getUserRuntimeDir()
if err != nil {
return
}
proxySocketDir := path.Join(runtimeDir, ".http-proxy")
if err = os.MkdirAll(proxySocketDir, 0750); err != nil {
return
}
proxySocketFile, err := os.CreateTemp(proxySocketDir, "http-filter-proxy-")
if err != nil {
return
}
cleanupFile = proxySocketFile.Name()
err = proxySocketFile.Close()
if err != nil {
return
}
pipeR, pipeW, err := os.Pipe()
if err != nil {
return
}
// pass pipeW to http proxy and close it in this process afterwards
if err = clearCloseOnExec(pipeW.Fd()); err != nil {
return
}
defer pipeW.Close()
encodedParams, err := encodePassHttpProxyChild(originalSettings, proxySocketFile.Name(), pipeW.Fd())
if err != nil {
return
}
dataFile, err := getDataFileBytes(encodedParams)
if err != nil {
return
}
defer dataFile.Close()
rawMountOptions, err := getDefaultOptions()
if err != nil {
return
}
rawMountOptions.Rw = append(rawMountOptions.Rw, proxySocketDir)
mountOptions, err := parseRawMountOptions(rawMountOptions)
if err != nil {
return
}
settings := getDefaultSettings()
settings.Cwd = "/"
settings.Command = []string{"/proc/self/exe", "http-proxy", strconv.Itoa(int(dataFile.Fd()))}
settings.OverrideArg0 = os.Args[0]
settings.Network = true
settings.Debug = originalSettings.Debug
_, err = run(settings, mountOptions, os.Environ(), true)
if err != nil {
return
}
// wait for http proxy to be initialized
dataRead := make([]byte, 1)
bytesRead, err := pipeR.Read(dataRead)
if err != nil {
return
}
if bytesRead != 1 {
err = fmt.Errorf("failed to initialize http proxy, syncing failed")
return
}
proxyPipe = pipeR.Fd()
proxyMount = mount{Type: mountTypeBindRw, Path: path.Join(runtimeDir, "http-proxy"), Other: proxySocketFile.Name()}
return
}
func runHttpProxy() error {
dataFd, _ := strconv.Atoi(os.Args[2])
dataFile := os.NewFile(uintptr(dataFd), "")
paramsBytes, _ := io.ReadAll(dataFile)
if err := dataFile.Close(); err != nil {
return fmt.Errorf("failed to close the parameters file: %w", err)
}
settings, socketPath, syncFd, _ := decodePassHttpProxyChild(paramsBytes)
var allowedHostGlobs []glob.Glob
for _, hostname := range settings.AllowedHosts {
allowedHostGlobs = append(allowedHostGlobs, glob.MustCompile(hostname, '.'))
}
proxy := goproxy.NewProxyHttpServer()
proxy.Verbose = settings.Debug
proxy.OnRequest().DoFunc(
func(request *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
if isHostAllowed(request.Host, allowedHostGlobs) {
if settings.Debug {
fmt.Printf("HTTP ALLOWED %s\n", request.Host)
}
return request, nil
}
fmt.Printf("HTTP REJECTED %s\n", request.Host)
return request, goproxy.NewResponse(request, goproxy.ContentTypeText, http.StatusForbidden, "Forbidden")
})
proxy.OnRequest().HandleConnectFunc(
func(host string, ctx *goproxy.ProxyCtx) (*goproxy.ConnectAction, string) {
if isHostAllowed(host, allowedHostGlobs) {
if settings.Debug {
fmt.Printf("CONNECT ALLOWED %s\n", host)
}
return goproxy.OkConnect, host
}
fmt.Printf("CONNECT REJECTED %s\n", host)
return goproxy.RejectConnect, host
})
err := os.Remove(socketPath)
if err != nil && !os.IsNotExist(err) {
return err
}
var wg sync.WaitGroup
httpServer := &http.Server{Handler: proxy}
unixListener, err := net.Listen("unix", socketPath)
if err != nil {
return err
}
wg.Add(1)
go func() {
defer wg.Done()
err := httpServer.Serve(unixListener)
if err != nil && err != http.ErrServerClosed {
fmt.Println(err)
}
}()
defer unixListener.Close()
syncFile := os.NewFile(syncFd, "pipe")
_, err = syncFile.Write([]byte("x"))
if err != nil {
return err
}
go func() {
fds := []unix.PollFd{{Fd: int32(syncFd), Events: 0}}
var err error
for {
_, err = unix.Poll(fds, -1)
if err != unix.EINTR {
break
}
}
if err != nil {
fmt.Printf("error polling the sync pipe: %v\n", err)
}
// "This bit [POLLERR] is also set for a file descriptor referring to the write end of a pipe when the read end has been closed."
if (err != nil) || (fds[0].Revents&unix.POLLHUP != 0) || (fds[0].Revents&unix.POLLERR != 0) {
err = httpServer.Shutdown(context.Background())
if err != nil {
fmt.Println(err)
}
}
}()
wg.Wait()
return nil
}
func forwardConnection(localConn net.Conn, proxyServerPath string) {
proxyServerConn, err := net.Dial("unix", proxyServerPath)
if err != nil {
fmt.Printf("Failed to connect to unix socket of the http proxy: %v\n", err)
return
}
go func() {
_, err = io.Copy(proxyServerConn, localConn)
if err != nil {
if !errors.Is(err, unix.EPIPE) {
fmt.Printf("Forwarding from http proxy unix socket to local tcp port failed: %v\n", err)
}
proxyServerConn.Close()
localConn.Close()
}
}()
go func() {
_, err = io.Copy(localConn, proxyServerConn)
if err != nil {
if !errors.Is(err, unix.EPIPE) {
fmt.Printf("Forwarding from local tcp port to http proxy unix socket failed: %v\n", err)
}
proxyServerConn.Close()
localConn.Close()
}
}()
}
func runHttpProxyForwarder() error {
runtimeDir, err := getUserRuntimeDir()
if err != nil {
return err
}
proxyServerPath := path.Join(runtimeDir, "http-proxy")
tcpListener, err := net.Listen("tcp", ":18080")
if err != nil {
return err
}
for {
localConn, err := tcpListener.Accept()
if err != nil {
fmt.Printf("error: listen.Accept failed: %v", err)
continue
}
go forwardConnection(localConn, proxyServerPath)
}
}