-
Notifications
You must be signed in to change notification settings - Fork 0
/
rawhandle.go
77 lines (62 loc) · 1.42 KB
/
rawhandle.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
package proxy
import (
"bufio"
"fmt"
"net"
"net/http"
"strings"
"sync"
"github.com/rs/zerolog/log"
)
type requestLine struct {
method string
authority string
proto string
}
func readRequestLine(scanner *bufio.Scanner) (rLine *requestLine, err error) {
for scanner.Scan() {
line := scanner.Text()
parts := strings.Split(line, " ")
rLine = &requestLine{
method: parts[0],
authority: parts[1],
proto: parts[2],
}
break
}
err = scanner.Err()
return
}
func ProxyConnectHandle(clientConn net.Conn) {
scanner := bufio.NewScanner(clientConn)
r, err := readRequestLine(scanner)
if err != nil {
writeStatusLine(clientConn, http.StatusInternalServerError, r.proto)
clientConn.Close()
return
}
if r.method != http.MethodConnect {
writeStatusLine(clientConn, http.StatusMethodNotAllowed, r.proto)
clientConn.Close()
return
}
logger := log.With().
Str("from", clientConn.RemoteAddr().String()).
Str("to", r.authority).
Logger()
targetConn, err := net.Dial("tcp", r.authority)
if err != nil {
writeStatusLine(clientConn, http.StatusInternalServerError, r.proto)
clientConn.Close()
return
}
defer targetConn.Close()
writeStatusLine(clientConn, http.StatusOK, r.proto)
logger.Debug().Msg("Transfer start")
wg := &sync.WaitGroup{}
wg.Add(2)
go copyIO(wg, targetConn, clientConn)
go copyIO(wg, clientConn, targetConn)
wg.Wait()
fmt.Printf("Transfer complete\n")
}