Skip to content

Commit

Permalink
init repository
Browse files Browse the repository at this point in the history
  • Loading branch information
wreulicke committed Feb 26, 2020
0 parents commit 49eea72
Show file tree
Hide file tree
Showing 20 changed files with 509 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Release
on:
push:
branches:
- "!*"
tags:
- "v*"
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
go:
- 1.13.7
name: GoReleaser
steps:
- uses: actions/checkout@master
- name: Setup Go
uses: actions/setup-go@v1
with:
go-version: ${{ matrix.go }}
- name: Run go mod download
run: go mod download
- name: Run GoReleaser
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: curl -sL https://git.io/goreleaser | bash
31 changes: 31 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

# Created by https://www.gitignore.io/api/go
# Edit at https://www.gitignore.io/?templates=go

### Go ###
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
vendor/

### Go Patch ###
/vendor/
/Godeps/

# End of https://www.gitignore.io/api/go

.idea/
*.iml

flextime-linter
30 changes: 30 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
builds:
- main: ./cmd/http-timeout
binary: http-timeout
goos:
- windows
- darwin
- linux
goarch:
- amd64
env:
- CGO_ENABLED=0
- GO111MODULE=on
ldflags:
- -s -w
checksum:
name_template: checksums.txt
archives:
- format: binary
snapshot:
name_template: master
changelog:
sort: asc
filters:
exclude:
- "Merge pull request"
- "Merge branch"
release:
github:
owner: wreulicke
name: http-timeout
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 wreulicke

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
## http-timeout

http-timeout restrict http server without timeout.
You must not use http server without timeout.

See [The complete guide to Go net/http timeouts](https://blog.cloudflare.com/the-complete-guide-to-golang-net-http-timeouts/).

## Install

```bash
# MacOS
curl -L https://github.com/wreulicke/http-timeout/releases/download/v0.0.1/http-timeout_0.0.1_darwin_amd64 -o /usr/local/bin/http-timeout

# Linux
curl -L https://github.com/wreulicke/http-timeout/releases/download/v0.0.1/http-timeout_0.0.1_linux_amd64 -o /usr/local/bin/http-timeout

# Windows
curl -L https://github.com/wreulicke/http-timeout/releases/download/v0.0.1/http-timeout_0.0.1_windows_amd64.exe -o <path-directory>/http-timeout.exe
```

## Usage


```
http-timeout ./...
```
19 changes: 19 additions & 0 deletions cmd/http-timeout/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
noDefaultClient "github.com/wreulicke/http-timeout/no-default-http-client"
noDefaultServer "github.com/wreulicke/http-timeout/no-default-http-server"
noTimeoutClient "github.com/wreulicke/http-timeout/no-timeout-http-server"
noTimeoutServer "github.com/wreulicke/http-timeout/no-timeout-http-server"

"golang.org/x/tools/go/analysis/multichecker"
)

func main() {
multichecker.Main(
noDefaultClient.Analyzer,
noDefaultServer.Analyzer,
noTimeoutClient.Analyzer,
noTimeoutServer.Analyzer,
)
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/wreulicke/http-timeout

go 1.13

require golang.org/x/tools v0.0.0-20191011165106-638914d249c5
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20191011165106-638914d249c5 h1:Jj8+k9DBERcVYkyiWRkP4WnWwjhC4dcfPCjxcXFNo58=
golang.org/x/tools v0.0.0-20191011165106-638914d249c5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
65 changes: 65 additions & 0 deletions no-default-http-client/analyzer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package linter

import (
"go/ast"
"go/types"

"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)

var Analyzer = &analysis.Analyzer{
Name: "no-default-http-client",
Run: run,
Requires: []*analysis.Analyzer{
inspect.Analyzer,
},
}

func run(pass *analysis.Pass) (interface{}, error) {
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
nodeFilter := []ast.Node{
(*ast.CallExpr)(nil),
(*ast.SelectorExpr)(nil),
}
inspect.Preorder(nodeFilter, func(n ast.Node) {
switch n := n.(type) {
case *ast.SelectorExpr:
pkgIdent, ok := n.X.(*ast.Ident)
if !ok {
return
}
pkgName, ok := pass.TypesInfo.Uses[pkgIdent].(*types.PkgName)
if !ok || pkgName.Imported().Path() != "net/http" {
return
}
if n.Sel.Name == "DefaultClient" {
pass.Reportf(n.Pos(), "Do not use net/http.%s because default client has no timeout", n.Sel.Name)
}
case *ast.CallExpr:
s, ok := n.Fun.(*ast.SelectorExpr)
if !ok {
return
}
pkgIdent, ok := s.X.(*ast.Ident)
if !ok {
return
}
pkgName, ok := pass.TypesInfo.Uses[pkgIdent].(*types.PkgName)
if !ok || pkgName.Imported().Path() != "net/http" {
return
}
switch s.Sel.Name {
case "Get":
pass.Reportf(n.Pos(), "Do not use net/http.%s because default client has no timeout", s.Sel.Name)
case "Post":
pass.Reportf(n.Pos(), "Do not use net/http.%s because default client has no timeout", s.Sel.Name)
case "PostForm":
pass.Reportf(n.Pos(), "Do not use net/http.%s because default client has no timeout", s.Sel.Name)
}
}

})
return nil, nil
}
12 changes: 12 additions & 0 deletions no-default-http-client/analyzer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package linter

import (
"testing"

"golang.org/x/tools/go/analysis/analysistest"
)

func Test(t *testing.T) {
testdata := analysistest.TestData()
analysistest.Run(t, testdata, Analyzer, "a")
}
12 changes: 12 additions & 0 deletions no-default-http-client/testdata/src/a/a.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package a

import (
"net/http"
)

func main() {
http.Get("test") // want "Do not use net/http.Get because default client has no timeout"
http.Post("test", "", nil) // want "Do not use net/http.Post because default client has no timeout"
http.PostForm("test", nil) // want "Do not use net/http.PostForm because default client has no timeout"
_ = http.DefaultClient // want "Do not use net/http.DefaultClient because default client has no timeout"
}
52 changes: 52 additions & 0 deletions no-default-http-server/analyzer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package linter

import (
"go/ast"
"go/types"

"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)

var Analyzer = &analysis.Analyzer{
Name: "no-default-http-server",
Run: run,
Requires: []*analysis.Analyzer{
inspect.Analyzer,
},
}

func run(pass *analysis.Pass) (interface{}, error) {
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
nodeFilter := []ast.Node{
(*ast.CallExpr)(nil),
}
inspect.Preorder(nodeFilter, func(n ast.Node) {
switch n := n.(type) {
case *ast.CallExpr:
s, ok := n.Fun.(*ast.SelectorExpr)
if !ok {
return
}
pkgIdent, ok := s.X.(*ast.Ident)
if !ok {
return
}
pkgName, ok := pass.TypesInfo.Uses[pkgIdent].(*types.PkgName)
if !ok || pkgName.Imported().Path() != "net/http" {
return
}
switch s.Sel.Name {
case "Handle":
pass.Reportf(n.Pos(), "Do not use net/http.%s because default http server has no timeout", s.Sel.Name)
case "HandleFunc":
pass.Reportf(n.Pos(), "Do not use net/http.%s because default http server has no timeout", s.Sel.Name)
case "ListenAndServe":
pass.Reportf(n.Pos(), "Do not use net/http.%s because default http server has no timeout", s.Sel.Name)
}
}

})
return nil, nil
}
12 changes: 12 additions & 0 deletions no-default-http-server/analyzer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package linter

import (
"testing"

"golang.org/x/tools/go/analysis/analysistest"
)

func Test(t *testing.T) {
testdata := analysistest.TestData()
analysistest.Run(t, testdata, Analyzer, "a")
}
14 changes: 14 additions & 0 deletions no-default-http-server/testdata/src/a/a.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package a

import (
"net/http"
)

func main() {
f := func(http.ResponseWriter, *http.Request) {
}
http.Handle("/", http.HandlerFunc(f)) // want "Do not use net/http.Handle because default http server has no timeout"
http.HandleFunc("/", func(http.ResponseWriter, *http.Request) { // want "Do not use net/http.HandleFunc because default http server has no timeout"
})
http.ListenAndServe(":8080", nil) // want "Do not use net/http.ListenAndServe because default http server has no timeout"
}
Loading

0 comments on commit 49eea72

Please sign in to comment.