fix bug: socket not close when connection finished

addon-dailer
lqqyt2423 3 years ago
parent 7faa3ce5ad
commit 0848dd8a28

@ -3,6 +3,9 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
// slog "log"
"os" "os"
"github.com/lqqyt2423/go-mitmproxy/addon" "github.com/lqqyt2423/go-mitmproxy/addon"
@ -50,6 +53,10 @@ func main() {
os.Exit(0) os.Exit(0)
} }
// for debug
// slog.SetFlags(slog.LstdFlags | slog.Lshortfile)
// log.SetReportCaller(true)
log.SetLevel(log.InfoLevel) log.SetLevel(log.InfoLevel)
log.SetReportCaller(false) log.SetReportCaller(false)
log.SetOutput(os.Stdout) log.SetOutput(os.Stdout)

@ -18,6 +18,7 @@ var NormalErrMsgs []string = []string{
"io: read/write on closed pipe", "io: read/write on closed pipe",
"connect: connection refused", "connect: connection refused",
"connect: connection reset by peer", "connect: connection reset by peer",
"use of closed network connection",
} }
// 仅打印预料之外的错误信息 // 仅打印预料之外的错误信息
@ -39,20 +40,14 @@ func LogErr(log *_log.Entry, err error) (loged bool) {
// 转发流量 // 转发流量
// Read a => Write b // Read a => Write b
// Read b => Write a // Read b => Write a
func Transfer(log *_log.Entry, a, b io.ReadWriter) { func Transfer(log *_log.Entry, a, b io.ReadWriteCloser) {
done := make(chan struct{}) done := make(chan struct{})
defer close(done) defer close(done)
forward := func(dst io.Writer, src io.Reader, ec chan<- error) { forward := func(dst io.WriteCloser, src io.Reader, ec chan<- error) {
_, err := io.Copy(dst, src) _, err := io.Copy(dst, src)
if err != nil {
select { dst.Close() // 当一端读结束时,结束另一端的写
case <-done:
return
case ec <- err:
return
}
}
select { select {
case <-done: case <-done:

@ -42,12 +42,12 @@ func NewProxy(opts *Options) (*Proxy, error) {
Transport: &http.Transport{ Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment, Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{ DialContext: (&net.Dialer{
Timeout: 30 * time.Second, Timeout: 15 * time.Second,
KeepAlive: 30 * time.Second, KeepAlive: 30 * time.Second,
DualStack: true, DualStack: true,
}).DialContext, }).DialContext,
MaxIdleConns: 100, MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second, IdleConnTimeout: 5 * time.Second,
TLSHandshakeTimeout: 10 * time.Second, TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second, ExpectContinueTimeout: 1 * time.Second,
ForceAttemptHTTP2: false, // disable http2 ForceAttemptHTTP2: false, // disable http2
@ -280,8 +280,8 @@ func (proxy *Proxy) handleConnect(res http.ResponseWriter, req *http.Request) {
return return
} }
// send RST other than FIN when finished, to avoid TIME_WAIT state cconn.(*net.TCPConn).SetLinger(0) // send RST other than FIN when finished, to avoid TIME_WAIT state
cconn.(*net.TCPConn).SetLinger(0) cconn.(*net.TCPConn).SetKeepAlive(false)
defer cconn.Close() defer cconn.Close()
_, err = io.WriteString(cconn, "HTTP/1.1 200 Connection Established\r\n\r\n") _, err = io.WriteString(cconn, "HTTP/1.1 200 Connection Established\r\n\r\n")

Loading…
Cancel
Save