try fix use too many port problem

addon-dailer
lqqyt2423 3 years ago
parent 88fe6dbe8d
commit 7faa3ce5ad

@ -12,7 +12,7 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
const version = "0.1.3" const version = "0.1.4"
type Config struct { type Config struct {
version bool version bool

@ -3,7 +3,6 @@ package proxy
import ( import (
"bytes" "bytes"
"io" "io"
"net"
"os" "os"
"strings" "strings"
"sync" "sync"
@ -40,11 +39,11 @@ 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.ReadWriteCloser) { func Transfer(log *_log.Entry, a, b io.ReadWriter) {
done := make(chan struct{}) done := make(chan struct{})
defer close(done) defer close(done)
forward := func(dst io.WriteCloser, src io.Reader, ec chan<- error) { forward := func(dst io.Writer, src io.Reader, ec chan<- error) {
_, err := io.Copy(dst, src) _, err := io.Copy(dst, src)
if err != nil { if err != nil {
select { select {
@ -55,12 +54,6 @@ func Transfer(log *_log.Entry, a, b io.ReadWriteCloser) {
} }
} }
if dstc, ok := dst.(*net.TCPConn); ok {
err = dstc.CloseWrite()
} else {
err = dst.Close()
}
select { select {
case <-done: case <-done:
return return

@ -6,6 +6,7 @@ import (
"net" "net"
"net/http" "net/http"
"strings" "strings"
"time"
"github.com/lqqyt2423/go-mitmproxy/cert" "github.com/lqqyt2423/go-mitmproxy/cert"
) )
@ -85,6 +86,7 @@ func NewMiddle(proxy *Proxy) (Interceptor, error) {
server := &http.Server{ server := &http.Server{
Handler: m, Handler: m,
IdleTimeout: 5 * time.Second,
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)), // disable http2 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) {

@ -33,8 +33,9 @@ func NewProxy(opts *Options) (*Proxy, error) {
proxy := new(Proxy) proxy := new(Proxy)
proxy.Server = &http.Server{ proxy.Server = &http.Server{
Addr: opts.Addr, Addr: opts.Addr,
Handler: proxy, Handler: proxy,
IdleTimeout: 5 * time.Second,
} }
proxy.Client = &http.Client{ proxy.Client = &http.Client{
@ -278,6 +279,9 @@ func (proxy *Proxy) handleConnect(res http.ResponseWriter, req *http.Request) {
res.WriteHeader(502) res.WriteHeader(502)
return return
} }
// send RST other than FIN when finished, to avoid TIME_WAIT state
cconn.(*net.TCPConn).SetLinger(0)
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