forked from LyleMi/ja3proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
150 lines (128 loc) · 3.06 KB
/
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
package main
import (
"crypto/tls"
"fmt"
"io"
"log"
"net"
"net/http"
"os"
"strings"
utls "github.com/refraction-networking/utls"
)
func fileExists(filename string) bool {
_, err := os.Stat(filename)
return !os.IsNotExist(err)
}
func customTLSWrap(conn net.Conn, sni string) (*utls.UConn, error) {
clientHelloID := utls.ClientHelloID{
Client: Config.TLSClient, Version: Config.TLSVersion, Seed: nil, Weights: nil,
}
uTLSConn := utls.UClient(
conn,
&utls.Config{
ServerName: sni,
InsecureSkipVerify: true,
},
clientHelloID,
)
if err := uTLSConn.Handshake(); err != nil {
return nil, err
}
return uTLSConn, nil
}
func handleTunneling(w http.ResponseWriter, r *http.Request) {
log.Printf("proxy to %s", r.Host)
destConn, err := CustomDialer.Dial("tcp", r.Host)
if err != nil {
http.Error(w, err.Error(), http.StatusServiceUnavailable)
log.Println("Tunneling err: ", err)
return
}
w.WriteHeader(http.StatusOK)
hijacker, ok := w.(http.Hijacker)
if !ok {
http.Error(w, "Hijacking not supported", http.StatusInternalServerError)
log.Println("Hijacking not supported")
return
}
clientConn, _, err := hijacker.Hijack()
if err != nil {
http.Error(w, err.Error(), http.StatusServiceUnavailable)
log.Println("Hijack error: ", err)
}
go connect(strings.Split(r.Host, ":")[0], destConn, clientConn)
}
func handleHTTP(w http.ResponseWriter, req *http.Request) {
resp, err := http.DefaultTransport.RoundTrip(req)
if err != nil {
http.Error(w, err.Error(), http.StatusServiceUnavailable)
log.Println(err)
return
}
defer resp.Body.Close()
copyHeader(w.Header(), resp.Header)
w.WriteHeader(resp.StatusCode)
io.Copy(w, resp.Body)
}
func connect(sni string, destConn net.Conn, clientConn net.Conn) {
defer destConn.Close()
defer clientConn.Close()
destTLSConn, err := customTLSWrap(destConn, sni)
if err != nil {
fmt.Println("TLS handshake failed: ", err)
return
}
tlsCert, err := generateCertificate(sni)
if err != nil {
fmt.Println("Error generating certificate: ", err)
}
config := &tls.Config{
InsecureSkipVerify: true,
Certificates: []tls.Certificate{tlsCert},
}
state := destTLSConn.ConnectionState()
protocols := state.NegotiatedProtocol
if protocols == "h2" {
config.NextProtos = []string{"h2", "http/1.1"}
}
clientTLSConn := tls.Server(
clientConn,
config,
)
err = clientTLSConn.Handshake()
if err != nil {
log.Println("Failed to perform TLS handshake: ", err)
return
}
if Config.Debug {
debugJunction(destTLSConn, clientTLSConn)
} else {
junction(destTLSConn, clientTLSConn)
}
}
func junction(destConn net.Conn, clientConn net.Conn) {
chDone := make(chan bool)
go func() {
_, err := io.Copy(destConn, clientConn)
if err != nil {
log.Println("copy dest to client error: ", err)
}
chDone <- true
}()
go func() {
_, err := io.Copy(clientConn, destConn)
if err != nil {
log.Println("copy client to dest error: ", err)
}
chDone <- true
}()
<-chDone
}
func copyHeader(dst, src http.Header) {
for k, vv := range src {
for _, v := range vv {
dst.Add(k, v)
}
}
}