Skip to content

Commit

Permalink
remove log files
Browse files Browse the repository at this point in the history
  • Loading branch information
rekby committed Feb 1, 2020
1 parent df32d62 commit d2af41c
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 17 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
.vscode/
lets-proxy.iml
log.txt
lets-proxy.log
lets-proxy2.log
storage/
output/
config.toml
Expand Down
64 changes: 64 additions & 0 deletions internal/proxy/transport.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package proxy

import (
"crypto/tls"
"net"
"net/http"
"strings"
"time"

"go.uber.org/zap"

zc "github.com/rekby/zapcontext"
)

// copy from go 1.10, need for compile with go 1.10 compiler
// https://github.com/golang/go/blob/b0cb374daf646454998bac7b393f3236a2ab6aca/src/net/http/transport.go#L40
var defaultTransport = &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,
}

type Transport struct {
IgnoreHttpsCertificate bool
}

func (t Transport) RoundTrip(req *http.Request) (*http.Response, error) {
return t.getTransport(req).RoundTrip(req)
}

func (t Transport) getTransport(req *http.Request) *http.Transport {
logger := zc.L(req.Context())

if req.URL.Scheme == ProtocolHTTP {
logger.Debug("Use default http transport")
return defaultTransport
}

host := req.Host
if strings.Contains(host, ":") { //strip port
parts := strings.SplitN(host, ":", 2)
host = parts[0]
}

var transport http.Transport
transport = *defaultTransport
transport.TLSClientConfig = &tls.Config{ServerName: host}
transport.TLSClientConfig.InsecureSkipVerify = t.IgnoreHttpsCertificate

logger.Debug("Use https transport",
zap.Bool("ignore_cert", transport.TLSClientConfig.InsecureSkipVerify),
zap.String("tls_server_name", host),
zap.String("header_host", req.Header.Get("HOST")),
)

return &transport
}
39 changes: 39 additions & 0 deletions internal/proxy/transport_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package proxy

import (
"net/http"
"testing"

"github.com/rekby/lets-proxy2/internal/th"

"github.com/maxatome/go-testdeep"
)

func TestTransport_GetTransport(t *testing.T) {
ctx, flush := th.TestContext()
defer flush()

td := testdeep.NewT(t)

tr := Transport{}
r, _ := http.NewRequest(http.MethodGet, "http://www.ru", nil)
r = r.WithContext(ctx)
httpTransport := tr.getTransport(r)
td.True(httpTransport == defaultTransport) // equal pointers

tr = Transport{IgnoreHttpsCertificate: false}
r, _ = http.NewRequest(http.MethodGet, "https://www.ru", nil)
r = r.WithContext(ctx)
httpTransport = tr.getTransport(r)
td.True(httpTransport != defaultTransport) // different pointers
td.Cmp(httpTransport.TLSClientConfig.ServerName, "www.ru")
td.Cmp(httpTransport.TLSClientConfig.InsecureSkipVerify, false)

tr = Transport{IgnoreHttpsCertificate: true}
r, _ = http.NewRequest(http.MethodGet, "https://www.ru", nil)
r = r.WithContext(ctx)
httpTransport = tr.getTransport(r)
td.True(httpTransport != defaultTransport) // different pointers
td.Cmp(httpTransport.TLSClientConfig.ServerName, "www.ru")
td.Cmp(httpTransport.TLSClientConfig.InsecureSkipVerify, true)
}
7 changes: 0 additions & 7 deletions lets-proxy.log

This file was deleted.

10 changes: 0 additions & 10 deletions lets-proxy2.log

This file was deleted.

0 comments on commit d2af41c

Please sign in to comment.