-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.go
More file actions
90 lines (75 loc) · 1.71 KB
/
util.go
File metadata and controls
90 lines (75 loc) · 1.71 KB
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
package main
import (
"crypto/tls"
"encoding/base64"
"io"
"log"
"net"
"strings"
)
// Helper functions for base64 encoding/decoding
func encodeBase64(s string) string {
return base64.StdEncoding.EncodeToString([]byte(s))
}
func decodeBase64(s string) (string, error) {
decoded, err := base64.StdEncoding.DecodeString(s)
if err != nil {
return "", err
}
return string(decoded), nil
}
// transparentProxy switches to transparent proxy mode after authentication
func transparentProxy(id string, debug bool, tlsConn *tls.Conn, serverConn net.Conn) {
if debug {
log.Printf("[%s] Switching to transparent proxy mode", id)
}
// Verify connections are established
if tlsConn == nil {
if debug {
log.Printf("[%s] ERROR: clientConn is nil in transparentProxy", id)
}
return
}
if serverConn == nil {
if debug {
log.Printf("[%s] ERROR: serverConn is nil in transparentProxy", id)
}
return
}
errc := make(chan error, 2)
go func() {
_, err := io.Copy(tlsConn, serverConn)
errc <- err
}()
go func() {
_, err := io.Copy(serverConn, tlsConn)
errc <- err
}()
err2 := <-errc
if debug {
log.Printf("[%s] Close starting", id)
}
err1 := <-errc
ignore := func(err error) bool {
if err == nil {
return true
}
s := err.Error()
return strings.Contains(s, "use of closed network connection") ||
strings.Contains(s, "protocol is shutdown") ||
strings.Contains(s, "close_notify") ||
strings.Contains(s, "i/o timeout") ||
err == io.EOF
}
if debug && !ignore(err1) {
log.Printf("[%s] copy error: %v", id, err1)
}
if debug && !ignore(err2) {
log.Printf("[%s] copy error: %v", id, err2)
}
if debug {
log.Printf("[%s] Goodbye!", id)
}
tlsConn.Close()
serverConn.Close()
}