Skip to content

Commit c182488

Browse files
committed
add more static analysis tool during tests
1 parent 3cf404a commit c182488

File tree

12 files changed

+99
-47
lines changed

12 files changed

+99
-47
lines changed

.github/workflows/test.yml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@ jobs:
1515

1616
- name: Check out code into the Go module directory
1717
uses: actions/checkout@v1
18-
18+
19+
- name: Get dependencies
20+
run: |
21+
make getdeps
22+
1923
- name: Test
20-
run: go test -v ./...
21-
22-
- name: Build
23-
run: make
24+
run: |
25+
export PATH=/home/runner/go/bin:$PATH
26+
make test
27+

Makefile

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,53 @@ all: client server
22

33
BUILD_FLAGS = -ldflags '-extldflags "-fno-PIC -static"' -buildmode pie -tags 'osusergo netgo static_build'
44

5+
6+
getdeps:
7+
@echo "Installing golint" && go get -u golang.org/x/lint/golint
8+
@echo "Installing gocyclo" && go get -u github.com/fzipp/gocyclo
9+
@echo "Installing deadcode" && go get -u github.com/remyoudompheng/go-misc/deadcode
10+
@echo "Installing misspell" && go get -u github.com/client9/misspell/cmd/misspell
11+
@echo "Installing ineffassign" && go get -u github.com/gordonklaus/ineffassign
12+
13+
verifiers: vet fmt lint cyclo spelling static #deadcode
14+
15+
vet:
16+
@echo "Running $@"
17+
@go vet -atomic -bool -copylocks -nilfunc -printf -rangeloops -unreachable -unsafeptr -unusedresult ./...
18+
19+
fmt:
20+
@echo "Running $@"
21+
@gofmt -d .
22+
23+
lint:
24+
@echo "Running $@"
25+
@${GOPATH}/bin/golint -set_exit_status $(shell go list ./... | grep -v stubs)
26+
27+
ineffassign:
28+
@echo "Running $@"
29+
@${GOPATH}/bin/ineffassign .
30+
31+
cyclo:
32+
@echo "Running $@"
33+
@${GOPATH}/bin/gocyclo -over 100 .
34+
35+
deadcode:
36+
@echo "Running $@"
37+
@${GOPATH}/bin/deadcode -test $(shell go list ./...) || true
38+
39+
spelling:
40+
@${GOPATH}/bin/misspell -i monitord -error `find .`
41+
42+
static:
43+
go run honnef.co/go/tools/cmd/staticcheck -- ./...
44+
45+
# Builds minio, runs the verifiers then runs the tests.
46+
check: test
47+
test: verifiers build
48+
go test -v ./...
49+
50+
build: server client
51+
552
server:
653
mkdir -p bin
754
cd cmds/server && go build $(BUILD_FLAGS) -o ../../bin/trs

client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/rs/zerolog/log"
1111
)
1212

13+
// Client connect to a tpc router server and opens a reverse tunnel
1314
type Client struct {
1415
localAddr string
1516
remoteAddr string

cmds/client/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"os/signal"
77
"sync"
8+
"syscall"
89
"time"
910

1011
"github.com/cenkalti/backoff/v3"
@@ -45,8 +46,8 @@ func main() {
4546
backoff := c.Int("backoff")
4647
secret := c.String("secret")
4748

48-
cSig := make(chan os.Signal)
49-
signal.Notify(cSig, os.Interrupt, os.Kill)
49+
cSig := make(chan os.Signal, 1)
50+
signal.Notify(cSig, os.Interrupt, syscall.SIGTERM)
5051

5152
wg := sync.WaitGroup{}
5253
wg.Add(len(remotes))

cmds/server/main.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import (
44
"context"
55
"fmt"
66
"os/signal"
7+
"syscall"
78

9+
"github.com/BurntSushi/toml"
810
"github.com/rs/zerolog"
911
"github.com/rs/zerolog/log"
1012
"github.com/urfave/cli/v2"
@@ -34,7 +36,8 @@ func readConfig(path string) (tcprouter.Config, error) {
3436
}
3537
defer f.Close()
3638

37-
c, err := tcprouter.ParseCfg(f)
39+
var c tcprouter.Config
40+
_, err = toml.DecodeReader(f, &c)
3841
if err != nil {
3942
return c, fmt.Errorf("failed to read configuration %w", err)
4043
}
@@ -62,10 +65,6 @@ func initStore(backend store.Backend, addr string) (store.Store, error) {
6265
)
6366
}
6467

65-
var (
66-
cfgPath string
67-
)
68-
6968
func main() {
7069
app := cli.NewApp()
7170
app.Version = "0.0.1"
@@ -111,7 +110,7 @@ func main() {
111110
s := tcprouter.NewServer(serverOpts, kv, cfg.Server.Services)
112111

113112
cSig := make(chan os.Signal, 1)
114-
signal.Notify(cSig, os.Interrupt, os.Kill)
113+
signal.Notify(cSig, os.Interrupt, syscall.SIGTERM)
115114

116115
ctx, cancel := context.WithCancel(context.Background())
117116
go func() {

config.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ package tcprouter
22

33
import (
44
"fmt"
5-
"io"
65

7-
"github.com/BurntSushi/toml"
86
"github.com/abronan/valkeyrie/store"
97
)
108

@@ -14,10 +12,12 @@ var validBackends = map[string]store.Backend{
1412
"etcd": store.ETCDV3,
1513
}
1614

15+
// Config hold the server configuration
1716
type Config struct {
1817
Server ServerConfig `toml:"server"`
1918
}
2019

20+
// ServerConfig configures the server listeners and backend
2121
type ServerConfig struct {
2222
Host string `toml:"addr"`
2323
Port uint `toml:"port"`
@@ -27,17 +27,20 @@ type ServerConfig struct {
2727
Services map[string]Service `toml:"services"`
2828
}
2929

30+
// Addr returns the listenting address of the server
3031
func (s ServerConfig) Addr() string {
3132
return fmt.Sprintf("%s:%d", s.Host, s.Port)
3233
}
3334

35+
// Service defines a proxy configuration
3436
type Service struct {
3537
Addr string `toml:"addr"`
3638
ClientSecret string `toml:"clientsecret` // will forward connection to it directly instead of hitting the Addr.
3739
TLSPort int `toml:"tlsport"`
3840
HTTPPort int `toml:"httpport"`
3941
}
4042

43+
// DbBackendConfig define the connection to a backend store
4144
type DbBackendConfig struct {
4245
DbType string `toml:"type"`
4346
Host string `toml:"addr"`
@@ -49,20 +52,16 @@ type DbBackendConfig struct {
4952
//Bucket string `toml:"bucket"`
5053
}
5154

55+
// Addr returns the listenting address of the server
5256
func (b DbBackendConfig) Addr() string {
5357
return fmt.Sprintf("%s:%d", b.Host, b.Port)
5458
}
5559

60+
// Backend return the Backend object of the b.DbType
5661
func (b DbBackendConfig) Backend() store.Backend {
5762
backend, ok := validBackends[b.DbType]
5863
if !ok {
5964
panic(fmt.Sprintf("unsupported backend type '%s'", b.DbType))
6065
}
6166
return backend
6267
}
63-
64-
func ParseCfg(r io.Reader) (Config, error) {
65-
var conf Config
66-
_, err := toml.DecodeReader(r, &conf)
67-
return conf, err
68-
}

examples/client.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,16 @@
55

66
# Require a certificate from the server. We used a self-signed certificate
77
# so here ca_certs must be the server certificate itself.
8-
ssl_sock = ssl.wrap_socket(s,
9-
ca_certs="server.crt",
10-
cert_reqs=ssl.CERT_REQUIRED)
8+
ssl_sock = ssl.wrap_socket(s, ca_certs="server.crt", cert_reqs=ssl.CERT_REQUIRED)
119

12-
ssl_sock.connect(('localhost', 5500))
10+
ssl_sock.connect(("localhost", 5500))
1311
# ssl_sock.connect(('localhost', 9092))
1412

15-
ssl_sock.sendall(b'login superadmin password\n')
13+
ssl_sock.sendall(b"login superadmin password\n")
1614

1715
while True:
1816
time.sleep(5)
19-
ssl_sock.sendall(b'ping null\n')
17+
ssl_sock.sendall(b"ping null\n")
2018
print(ssl_sock.recv(4096))
2119

2220
ssl_sock.close()

examples/main.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,8 @@ import (
77

88
"github.com/abronan/valkeyrie"
99
"github.com/abronan/valkeyrie/store"
10-
//"github.com/abronan/valkeyrie/store/boltdb"
11-
//etcd "github.com/abronan/valkeyrie/store/etcd/v3"
1210
"github.com/abronan/valkeyrie/store/redis"
1311
)
14-
var validBackends = map[string]store.Backend{
15-
"redis": store.REDIS,
16-
//"boltdb": store.BOLTDB,
17-
//"etcd": store.ETCDV3,
18-
19-
}
2012

2113
func init() {
2214

@@ -26,7 +18,7 @@ func init() {
2618

2719
}
2820

29-
type Service struct {
21+
type service struct {
3022
Addr string `json:"addr"`
3123
SNI string `json:"sni"`
3224
Name string `json:"bing"`
@@ -37,23 +29,20 @@ func main() {
3729
// Initialize a new store with redis
3830
kv, err := valkeyrie.NewStore(
3931
store.REDIS,
40-
[]string{"127.0.0.1:6379" },
32+
[]string{"127.0.0.1:6379"},
4133
&store.Config{
4234
ConnectionTimeout: 10 * time.Second,
4335
},
4436
)
4537
if err != nil {
4638
log.Fatal("Cannot create store redis")
4739
}
48-
google := &Service{Addr:"172.217.19.46:443", SNI:"www.google.com", Name:"google"}
40+
google := &service{Addr: "172.217.19.46:443", SNI: "www.google.com", Name: "google"}
4941
encGoogle, _ := json.Marshal(google)
50-
bing := &Service{Addr:"13.107.21.200:443", SNI:"www.bing.com", Name:"bing"}
42+
bing := &service{Addr: "13.107.21.200:443", SNI: "www.bing.com", Name: "bing"}
5143
encBing, _ := json.Marshal(bing)
5244

5345
kv.Put("/tcprouter/service/google", encGoogle, nil)
5446
kv.Put("/tcprouter/service/bing", encBing, nil)
5547

56-
5748
}
58-
59-

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ require (
1111
github.com/rs/zerolog v1.15.0
1212
github.com/stretchr/testify v1.3.0
1313
github.com/urfave/cli/v2 v2.1.1
14+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859 // indirect
1415
)

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ github.com/cenkalti/backoff/v3 v3.1.1 h1:UBHElAnr3ODEbpqPzX8g5sBcASjoLFtt3L/xwJ0
1919
github.com/cenkalti/backoff/v3 v3.1.1/go.mod h1:cIeZDE3IrqwwJl6VUwCN6trj1oXrTS4rc0ij+ULvLYs=
2020
github.com/circonus-labs/circonus-gometrics v2.3.1+incompatible/go.mod h1:nmEj6Dob7S7YxXgwXpfOuvO54S+tGdZdw9fuRZt25Ag=
2121
github.com/circonus-labs/circonusllhist v0.1.3/go.mod h1:kMXHVDlOchFAehlya5ePtbp5jckzBHf4XRpQvBOLI+I=
22+
github.com/client9/misspell v0.3.4 h1:ta993UF76GwbvJcIo3Y68y/M3WxlpEHPWIGDkJYwzJI=
2223
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
2324
github.com/coreos/bbolt v1.3.3/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
2425
github.com/coreos/etcd v3.3.13+incompatible h1:8F3hqu9fGYLBifCmRCJsicFqDx/D68Rt3q1JMazcgBQ=
@@ -179,6 +180,7 @@ golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnf
179180
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
180181
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 h1:HuIa8hRrWRSrqYzx1qI49NNxhdi2PrY7gxVSq1JjLDc=
181182
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
183+
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3 h1:x/bBzNauLQAlE3fLku/xy92Y8QwKX5HZymrMz2IiKFc=
182184
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
183185
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
184186
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
@@ -190,6 +192,8 @@ golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn
190192
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
191193
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980 h1:dfGZHvZk057jK2MCeWus/TowKpJ8y4AmooUzdBSR9GU=
192194
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
195+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859 h1:R/3boaszxrf1GEUWTVDzSKVwLmSJpwZ1yqXm8j0v2QI=
196+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
193197
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be h1:vEDujvNQGv4jgYKudGeI/+DAX4Jffq6hpD55MmoEvKs=
194198
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
195199
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -212,6 +216,7 @@ golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
212216
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
213217
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
214218
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
219+
golang.org/x/tools v0.0.0-20190425163242-31fd60d6bfdc h1:N3zlSgxkefUH/ecsl37RWTkESTB026kmXzNly8TuZCI=
215220
golang.org/x/tools v0.0.0-20190425163242-31fd60d6bfdc/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
216221
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
217222
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8 h1:Nw54tB0rB7hY/N0NQvRW8DG4Yk3Q6T9cu9RcFQDu1tc=
@@ -234,4 +239,5 @@ gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=
234239
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
235240
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
236241
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
242+
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099 h1:XJP7lxbSxWLOMNdBE4B/STaqVy6L73o0knwj2vIlxnw=
237243
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

handshake.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
)
77

88
const (
9+
// MagicNr is the bytes sent during handshake to identity a tcprouter client connection
910
// TODO: chose a valid magic number
1011
MagicNr = 0x1111
1112
)

0 commit comments

Comments
 (0)