add ConnContext.Server
parent
c2143a80d8
commit
949d1a94cf
@ -1,9 +1,85 @@
|
||||
package flow
|
||||
|
||||
import "github.com/lqqyt2423/go-mitmproxy/connection"
|
||||
import (
|
||||
"crypto/tls"
|
||||
"net"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/lqqyt2423/go-mitmproxy/connection"
|
||||
)
|
||||
|
||||
var ConnContextKey = new(struct{})
|
||||
|
||||
type ConnContext struct {
|
||||
Client *connection.Client
|
||||
Server *connection.Server
|
||||
}
|
||||
|
||||
var ConnContextKey = new(struct{})
|
||||
func (connCtx *ConnContext) InitHttpServer(SslInsecure bool) {
|
||||
if connCtx.Server != nil {
|
||||
return
|
||||
}
|
||||
if connCtx.Client.Tls {
|
||||
return
|
||||
}
|
||||
|
||||
server := connection.NewServer()
|
||||
server.Client = &http.Client{
|
||||
Transport: &http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
|
||||
// todo: change here
|
||||
DialContext: (&net.Dialer{
|
||||
Timeout: 30 * time.Second,
|
||||
KeepAlive: 30 * time.Second,
|
||||
}).DialContext,
|
||||
ForceAttemptHTTP2: false, // disable http2
|
||||
|
||||
DisableCompression: true, // To get the original response from the server, set Transport.DisableCompression to true.
|
||||
TLSClientConfig: &tls.Config{
|
||||
InsecureSkipVerify: SslInsecure,
|
||||
KeyLogWriter: GetTlsKeyLogWriter(),
|
||||
},
|
||||
},
|
||||
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
||||
// 禁止自动重定向
|
||||
return http.ErrUseLastResponse
|
||||
},
|
||||
}
|
||||
connCtx.Server = server
|
||||
}
|
||||
|
||||
func (connCtx *ConnContext) InitHttpsServer(SslInsecure bool) {
|
||||
if connCtx.Server != nil {
|
||||
return
|
||||
}
|
||||
if !connCtx.Client.Tls {
|
||||
return
|
||||
}
|
||||
|
||||
server := connection.NewServer()
|
||||
server.Client = &http.Client{
|
||||
Transport: &http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
|
||||
// todo: change here
|
||||
DialContext: (&net.Dialer{
|
||||
Timeout: 30 * time.Second,
|
||||
KeepAlive: 30 * time.Second,
|
||||
}).DialContext,
|
||||
ForceAttemptHTTP2: false, // disable http2
|
||||
|
||||
DisableCompression: true, // To get the original response from the server, set Transport.DisableCompression to true.
|
||||
TLSClientConfig: &tls.Config{
|
||||
InsecureSkipVerify: SslInsecure,
|
||||
KeyLogWriter: GetTlsKeyLogWriter(),
|
||||
},
|
||||
},
|
||||
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
||||
// 禁止自动重定向
|
||||
return http.ErrUseLastResponse
|
||||
},
|
||||
}
|
||||
connCtx.Server = server
|
||||
}
|
||||
|
@ -0,0 +1,29 @@
|
||||
package flow
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Wireshark 解析 https 设置
|
||||
var tlsKeyLogWriter io.Writer
|
||||
var tlsKeyLogOnce sync.Once
|
||||
|
||||
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.WithField("in", "GetTlsKeyLogWriter").Debug(err)
|
||||
return
|
||||
}
|
||||
|
||||
tlsKeyLogWriter = writer
|
||||
})
|
||||
return tlsKeyLogWriter
|
||||
}
|
Loading…
Reference in New Issue