Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style: reformat all go files #437

Merged
merged 10 commits into from
Jun 19, 2024
22 changes: 20 additions & 2 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,30 @@ jobs:
go-fmt:
name: Go Formatter
runs-on: ubuntu-latest
if: false # Pending activation
steps:
- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: 1.18
- name: Run go fmt
run: go fmt ./...
run: |
NON_COMPLIANT_FILES=$(gofmt -s -l $(find . -type f -name '*.go'| grep -v "/.template/"))
if [ -n "$NON_COMPLIANT_FILES" ]; then
echo "The following files are not formatted correctly:"
echo "$NON_COMPLIANT_FILES"

mkdir -p /tmp/gofmt
for file in $NON_COMPLIANT_FILES; do
mkdir -p /tmp/gofmt/$(dirname $file)
gofmt -s $file > /tmp/gofmt/$file
done

exit 1
fi
- name: Upload formatted files
if: failure()
uses: actions/upload-artifact@v4
with:
name: formatted-files
path: /tmp/gofmt
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ test:
cd modules && go test -v ./...

gofmt:
goimports -w -l $(GO_FILES)
gofmt -s -w $(shell find . -type f -name '*.go'| grep -v "/.template/")

zgrab2: $(GO_FILES)
cd cmd/zgrab2 && go build && cd ../..
Expand Down
6 changes: 3 additions & 3 deletions conn_timeout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func _write(writer io.Writer, data []byte) error {

// Run the configured server. As soon as it returns, it is listening.
// Returns a channel that receives a timeoutTestError on error, or is closed on successful completion.
func (cfg *connTimeoutTestConfig) runServer(t *testing.T) (chan *timeoutTestError) {
func (cfg *connTimeoutTestConfig) runServer(t *testing.T) chan *timeoutTestError {
errorChan := make(chan *timeoutTestError)
if cfg.endpoint != "" {
// Only listen on localhost
Expand Down Expand Up @@ -307,7 +307,7 @@ var connTestConfigs = []connTimeoutTestConfig{
serverToClientPayload: []byte("abc"),
clientToServerPayload: []byte("defghi"),

failStep: testStepConnect,
failStep: testStepConnect,
failError: "i/o timeout",
},
// short session timeout, medium connect timeout, with connect to nonexistent endpoint.
Expand All @@ -326,7 +326,7 @@ var connTestConfigs = []connTimeoutTestConfig{
serverToClientPayload: []byte("abc"),
clientToServerPayload: []byte("defghi"),

failStep: testStepConnect,
failStep: testStepConnect,
failError: "i/o timeout",
},
// Get an IO timeout on the read.
Expand Down
22 changes: 11 additions & 11 deletions input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,17 +175,17 @@ example.com
`
port := uint(443)
expected := []ScanTarget{
ScanTarget{IP: net.ParseIP("10.0.0.1"), Domain: "example.com", Tag: "tag"},
ScanTarget{IP: net.ParseIP("10.0.0.1"), Domain: "example.com"},
ScanTarget{IP: net.ParseIP("10.0.0.1")},
ScanTarget{Domain: "example.com"},
ScanTarget{Domain: "example.com"},
ScanTarget{IP: net.ParseIP("2.2.2.0"), Tag: "tag"},
ScanTarget{IP: net.ParseIP("2.2.2.1"), Tag: "tag"},
ScanTarget{IP: net.ParseIP("2.2.2.2"), Tag: "tag"},
ScanTarget{IP: net.ParseIP("2.2.2.3"), Tag: "tag"},
ScanTarget{IP: net.ParseIP("10.0.0.1"), Domain: "example.com", Tag: "tag", Port: &port},
ScanTarget{IP: net.ParseIP("10.0.0.1"), Port: &port},
{IP: net.ParseIP("10.0.0.1"), Domain: "example.com", Tag: "tag"},
{IP: net.ParseIP("10.0.0.1"), Domain: "example.com"},
{IP: net.ParseIP("10.0.0.1")},
{Domain: "example.com"},
{Domain: "example.com"},
{IP: net.ParseIP("2.2.2.0"), Tag: "tag"},
{IP: net.ParseIP("2.2.2.1"), Tag: "tag"},
{IP: net.ParseIP("2.2.2.2"), Tag: "tag"},
{IP: net.ParseIP("2.2.2.3"), Tag: "tag"},
{IP: net.ParseIP("10.0.0.1"), Domain: "example.com", Tag: "tag", Port: &port},
{IP: net.ParseIP("10.0.0.1"), Port: &port},
}

ch := make(chan ScanTarget, 0)
Expand Down
9 changes: 5 additions & 4 deletions lib/http/chunked.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,11 @@ func isASCIISpace(b byte) bool {

// removeChunkExtension removes any chunk-extension from p.
// For example,
// "0" => "0"
// "0;token" => "0"
// "0;token=val" => "0"
// `0;token="quoted string"` => "0"
//
// "0" => "0"
// "0;token" => "0"
// "0;token=val" => "0"
// `0;token="quoted string"` => "0"
func removeChunkExtension(p []byte) ([]byte, error) {
semi := bytes.IndexByte(p, ';')
if semi == -1 {
Expand Down
75 changes: 37 additions & 38 deletions lib/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,20 @@ import (
// When following redirects, the Client will forward all headers set on the
// initial Request except:
//
// * when forwarding sensitive headers like "Authorization",
// "WWW-Authenticate", and "Cookie" to untrusted targets.
// These headers will be ignored when following a redirect to a domain
// that is not a subdomain match or exact match of the initial domain.
// For example, a redirect from "foo.com" to either "foo.com" or "sub.foo.com"
// will forward the sensitive headers, but a redirect to "bar.com" will not.
//
// * when forwarding the "Cookie" header with a non-nil cookie Jar.
// Since each redirect may mutate the state of the cookie jar,
// a redirect may possibly alter a cookie set in the initial request.
// When forwarding the "Cookie" header, any mutated cookies will be omitted,
// with the expectation that the Jar will insert those mutated cookies
// with the updated values (assuming the origin matches).
// If Jar is nil, the initial cookies are forwarded without change.
//
// - when forwarding sensitive headers like "Authorization",
// "WWW-Authenticate", and "Cookie" to untrusted targets.
// These headers will be ignored when following a redirect to a domain
// that is not a subdomain match or exact match of the initial domain.
// For example, a redirect from "foo.com" to either "foo.com" or "sub.foo.com"
// will forward the sensitive headers, but a redirect to "bar.com" will not.
//
// - when forwarding the "Cookie" header with a non-nil cookie Jar.
// Since each redirect may mutate the state of the cookie jar,
// a redirect may possibly alter a cookie set in the initial request.
// When forwarding the "Cookie" header, any mutated cookies will be omitted,
// with the expectation that the Jar will insert those mutated cookies
// with the updated values (assuming the origin matches).
// If Jar is nil, the initial cookies are forwarded without change.
type Client struct {
// Transport specifies the mechanism by which individual
// HTTP requests are made.
Expand Down Expand Up @@ -344,11 +343,11 @@ func basicAuth(username, password string) string {
// the following redirect codes, Get follows the redirect, up to a
// maximum of 10 redirects:
//
// 301 (Moved Permanently)
// 302 (Found)
// 303 (See Other)
// 307 (Temporary Redirect)
// 308 (Permanent Redirect)
// 301 (Moved Permanently)
// 302 (Found)
// 303 (See Other)
// 307 (Temporary Redirect)
// 308 (Permanent Redirect)
//
// An error is returned if there were too many redirects or if there
// was an HTTP protocol error. A non-2xx response doesn't cause an
Expand All @@ -373,11 +372,11 @@ func (c *Client) Get(url string) (r *Response, err error) {
// following redirect codes, Get follows the redirect after calling the
// Client's CheckRedirect function:
//
// 301 (Moved Permanently)
// 302 (Found)
// 303 (See Other)
// 307 (Temporary Redirect)
// 308 (Permanent Redirect)
// 301 (Moved Permanently)
// 302 (Found)
// 303 (See Other)
// 307 (Temporary Redirect)
// 308 (Permanent Redirect)
//
// An error is returned if the Client's CheckRedirect function fails
// or if there was an HTTP protocol error. A non-2xx response doesn't
Expand Down Expand Up @@ -770,11 +769,11 @@ func (c *Client) PostForm(url string, data url.Values) (resp *Response, err erro
// the following redirect codes, Head follows the redirect, up to a
// maximum of 10 redirects:
//
// 301 (Moved Permanently)
// 302 (Found)
// 303 (See Other)
// 307 (Temporary Redirect)
// 308 (Permanent Redirect)
// 301 (Moved Permanently)
// 302 (Found)
// 303 (See Other)
// 307 (Temporary Redirect)
// 308 (Permanent Redirect)
//
// Head is a wrapper around DefaultClient.Head
func Head(url string) (resp *Response, err error) {
Expand All @@ -785,11 +784,11 @@ func Head(url string) (resp *Response, err error) {
// following redirect codes, Head follows the redirect after calling the
// Client's CheckRedirect function:
//
// 301 (Moved Permanently)
// 302 (Found)
// 303 (See Other)
// 307 (Temporary Redirect)
// 308 (Permanent Redirect)
// 301 (Moved Permanently)
// 302 (Found)
// 303 (See Other)
// 307 (Temporary Redirect)
// 308 (Permanent Redirect)
func (c *Client) HeadWithHost(url, host string) (resp *Response, err error) {
req, err := NewRequestWithHost("HEAD", url, host, nil)
if err != nil {
Expand All @@ -803,9 +802,9 @@ func (c *Client) Head(url string) (resp *Response, err error) {
}

// cancelTimerBody is an io.ReadCloser that wraps rc with two features:
// 1) on Read error or close, the stop func is called.
// 2) On Read failure, if reqDidTimeout is true, the error is wrapped and
// marked as net.Error that hit its timeout.
// 1. on Read error or close, the stop func is called.
// 2. On Read failure, if reqDidTimeout is true, the error is wrapped and
// marked as net.Error that hit its timeout.
type cancelTimerBody struct {
stop func() // stops the time.Timer waiting to cancel the request
rc io.ReadCloser
Expand Down
8 changes: 4 additions & 4 deletions lib/http/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1067,9 +1067,9 @@ func TestClientHeadContentLength_h1(t *testing.T) {
}

/*
func TestClientHeadContentLength_h2(t *testing.T) {
testClientHeadContentLength(t, h2Mode)
}
func TestClientHeadContentLength_h2(t *testing.T) {
testClientHeadContentLength(t, h2Mode)
}
*/
func testClientHeadContentLength(t *testing.T, h2 bool) {
defer afterTest(t)
Expand Down Expand Up @@ -1379,7 +1379,7 @@ func TestClientTimeoutCancel(t *testing.T) {

func TestClientRedirectEatsBody_h1(t *testing.T) { testClientRedirectEatsBody(t, h1Mode) }

//func TestClientRedirectEatsBody_h2(t *testing.T) { testClientRedirectEatsBody(t, h2Mode) }
// func TestClientRedirectEatsBody_h2(t *testing.T) { testClientRedirectEatsBody(t, h2Mode) }
func testClientRedirectEatsBody(t *testing.T, h2 bool) {
setParallel(t)
defer afterTest(t)
Expand Down
Loading
Loading