diff --git a/Makefile b/Makefile index 92a994c..51ebf17 100644 --- a/Makefile +++ b/Makefile @@ -5,10 +5,10 @@ SOURCE_PATH = /gopath/src/github.com/buger/gor/ release: release-x86 release-x64 release-x64: - docker run -v `pwd`:$(SOURCE_PATH) -t --env GOOS=linux --env GOARCH=amd64 --env CGO_ENABLED=0 -i gor go build && tar -czf gor_x64.tar.gz gor && rm gor + docker run -v `pwd`:$(SOURCE_PATH) -t --env GOOS=linux --env GOARCH=amd64 --env CGO_ENABLED=0 -i gor go build -ldflags "-X main.VERSION $(VERSION)"&& tar -czf gor_$(VERSION)_x64.tar.gz gor && rm gor release-x86: - docker run -v `pwd`:$(SOURCE_PATH) -t --env GOOS=linux --env GOARCH=386 --env CGO_ENABLED=0 -i gor go build && tar -czf gor_x86.tar.gz gor && rm gor + docker run -v `pwd`:$(SOURCE_PATH) -t --env GOOS=linux --env GOARCH=386 --env CGO_ENABLED=0 -i gor go build -ldflags "-X main.VERSION $(VERSION)" && tar -czf gor_$(VERSION)_x86.tar.gz gor && rm gor dbuild: docker build -t gor . diff --git a/README.md b/README.md index b544ee2..2ea19c3 100644 --- a/README.md +++ b/README.md @@ -172,6 +172,11 @@ gor --input-raw :80 --output-http "http://staging.server" \ --http-header "Enable-Feature-X: true" ``` +#### Host header +Host header gets special treatment. By default Host get set to the value specified in --output-http. If you manually set --http-header "Host: anonther.com", Gor will not override Host value. + +If you app accepts traffic from multiple domain, and you want to keep original headers, there is specific `--http-original-host` with tells Gor do not touch Host header at all. + ### Saving requests to file and replaying them You can save requests to file, and replay them later: ``` diff --git a/http_client.go b/http_client.go index dc993d4..6a8632c 100644 --- a/http_client.go +++ b/http_client.go @@ -42,7 +42,9 @@ func NewHTTPClient(baseURL string, config *HTTPClientConfig) *HTTPClient { u, _ := url.Parse(baseURL) if !strings.Contains(u.Host, ":") { - u.Host += ":" + defaultPorts[u.Scheme] + if u.Scheme != "http" { + u.Host += ":" + defaultPorts[u.Scheme] + } } if config.Timeout.Nanoseconds() == 0 { @@ -66,7 +68,11 @@ func NewHTTPClient(baseURL string, config *HTTPClientConfig) *HTTPClient { func (c *HTTPClient) Connect() (err error) { c.Disconnect() - c.conn, err = net.Dial("tcp", c.host) + if !strings.Contains(c.host, ":") { + c.conn, err = net.Dial("tcp", c.host + ":80") + } else { + c.conn, err = net.Dial("tcp", c.host) + } if c.scheme == "https" { tlsConn := tls.Client(c.conn, &tls.Config{InsecureSkipVerify: true}) diff --git a/http_client_test.go b/http_client_test.go index d14d5be..6109a90 100644 --- a/http_client_test.go +++ b/http_client_test.go @@ -15,8 +15,8 @@ import ( func TestHTTPClientURLPort(t *testing.T) { c1 := NewHTTPClient("http://example.com", &HTTPClientConfig{}) - if c1.baseURL != "http://example.com:80" { - t.Error("Sould add 80 port for http:", c1.baseURL) + if c1.baseURL != "http://example.com" { + t.Error("Sould not add 80 port for http:", c1.baseURL) } c2 := NewHTTPClient("https://example.com", &HTTPClientConfig{}) @@ -30,8 +30,8 @@ func TestHTTPClientURLPort(t *testing.T) { } c4 := NewHTTPClient("example.com", &HTTPClientConfig{}) - if c4.baseURL != "http://example.com:80" { - t.Error("Sould add default protocol:", c4.baseURL) + if c4.baseURL != "http://example.com" { + t.Error("Sould not add default protocol:", c4.baseURL) } } diff --git a/input_tcp_test.go b/input_tcp_test.go index bbb0cc7..f440c57 100644 --- a/input_tcp_test.go +++ b/input_tcp_test.go @@ -40,7 +40,7 @@ func TestTCPInput(t *testing.T) { for i := 0; i < 100; i++ { wg.Add(1) - encoded := make([]byte, len(msg)*2+1) + encoded := make([]byte, len(msg)*2) hex.Encode(encoded, msg) conn.Write(append(encoded, '\n')) } diff --git a/settings.go b/settings.go index 3b1c71b..528adcd 100644 --- a/settings.go +++ b/settings.go @@ -7,10 +7,7 @@ import ( "os" ) -const ( - // VERSION specifies Gor current version - VERSION = "0.9.8" -) +var VERSION string // MultiOption allows to specify multiple flags with same name and collects all values into array type MultiOption []string @@ -94,6 +91,7 @@ func init() { flag.DurationVar(&Settings.outputHTTPConfig.Timeout, "output-http-timeout", 0, "Specify HTTP request/response timeout. By default 5s. Example: --output-http-timeout 30s") flag.BoolVar(&Settings.outputHTTPConfig.stats, "output-http-stats", false, "Report http output queue stats to console every 5 seconds.") + flag.BoolVar(&Settings.outputHTTPConfig.OriginalHost, "http-original-host", false, "Normally gor replaces the Host http header with the host supplied with --output-http. This option disables that behavior, preserving the original Host header.") flag.StringVar(&Settings.outputHTTPConfig.elasticSearch, "output-http-elasticsearch", "", "Send request and response stats to ElasticSearch:\n\tgor --input-raw :8080 --output-http staging.com --output-http-elasticsearch 'es_host:api_port/index_name'")