-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
105 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,8 @@ | |
.vscode/ | ||
lets-proxy.iml | ||
log.txt | ||
lets-proxy.log | ||
lets-proxy2.log | ||
storage/ | ||
output/ | ||
config.toml | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.