disable http2 && Wireshark 解析 https 设置

addon-dailer
lqqyt2423 4 years ago
parent 538f930833
commit f3762ed7fc

@ -8,3 +8,4 @@
- [x] http connect - [x] http connect
- [ ] cert - [ ] cert
- [ ] https handler - [ ] https handler
- [ ] http2

@ -45,7 +45,8 @@ func NewMitmServer(proxy *Proxy) (Mitm, error) {
} }
server := &http.Server{ server := &http.Server{
Handler: m, Handler: m,
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)), // disable http2
TLSConfig: &tls.Config{ TLSConfig: &tls.Config{
GetCertificate: func(chi *tls.ClientHelloInfo) (*tls.Certificate, error) { GetCertificate: func(chi *tls.ClientHelloInfo) (*tls.Certificate, error) {
// log.Printf("MitmServer GetCertificate ServerName: %v\n", chi.ServerName) // log.Printf("MitmServer GetCertificate ServerName: %v\n", chi.ServerName)

@ -1,9 +1,13 @@
package proxy package proxy
import ( import (
"crypto/tls"
"io" "io"
"log" "log"
"net"
"net/http" "net/http"
"os"
"sync"
"time" "time"
) )
@ -13,6 +17,7 @@ type Options struct {
type Proxy struct { type Proxy struct {
Server *http.Server Server *http.Server
Client *http.Client
Mitm Mitm Mitm Mitm
} }
@ -53,11 +58,12 @@ func (proxy *Proxy) ServeHTTP(res http.ResponseWriter, req *http.Request) {
return return
} }
// TODO: handle Proxy- header
for key, value := range req.Header { for key, value := range req.Header {
proxyReq.Header[key] = value for _, v := range value {
proxyReq.Header.Add(key, v)
}
} }
proxyRes, err := http.DefaultClient.Do(proxyReq) proxyRes, err := proxy.Client.Do(proxyReq)
if err != nil { if err != nil {
log.Printf("error: %v, url: %v\n", err, req.URL.String()) log.Printf("error: %v, url: %v\n", err, req.URL.String())
res.WriteHeader(502) res.WriteHeader(502)
@ -66,7 +72,9 @@ func (proxy *Proxy) ServeHTTP(res http.ResponseWriter, req *http.Request) {
defer proxyRes.Body.Close() defer proxyRes.Body.Close()
for key, value := range proxyRes.Header { for key, value := range proxyRes.Header {
res.Header()[key] = value for _, v := range value {
res.Header().Add(key, v)
}
} }
res.WriteHeader(proxyRes.StatusCode) res.WriteHeader(proxyRes.StatusCode)
_, err = io.Copy(res, proxyRes.Body) _, err = io.Copy(res, proxyRes.Body)
@ -128,6 +136,27 @@ func NewProxy(opts *Options) (*Proxy, error) {
Handler: proxy, Handler: proxy,
} }
proxy.Client = &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
ForceAttemptHTTP2: false, // disable http2
DisableCompression: true,
TLSClientConfig: &tls.Config{
KeyLogWriter: GetTlsKeyLogWriter(),
},
},
}
mitm, err := NewMitmServer(proxy) mitm, err := NewMitmServer(proxy)
if err != nil { if err != nil {
return nil, err return nil, err
@ -137,3 +166,25 @@ func NewProxy(opts *Options) (*Proxy, error) {
return proxy, nil return proxy, nil
} }
var tlsKeyLogWriter io.Writer
var tlsKeyLogOnce sync.Once
// Wireshark 解析 https 设置
func GetTlsKeyLogWriter() io.Writer {
tlsKeyLogOnce.Do(func() {
logfile := os.Getenv("SSLKEYLOGFILE")
if logfile == "" {
return
}
writer, err := os.OpenFile(logfile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Printf("GetTlsKeyLogWriter error: %v\n", err)
return
}
tlsKeyLogWriter = writer
})
return tlsKeyLogWriter
}

Loading…
Cancel
Save