Skip to content

Commit 1f841ca

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

File tree

11 files changed

+89
-46
lines changed

11 files changed

+89
-46
lines changed

.github/workflows/test.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
uses: actions/checkout@v1
1818

1919
- name: Test
20-
run: go test -v ./...
21-
22-
- name: Build
23-
run: make
20+
run: |
21+
export PATH=/home/runner/go/bin:$PATH
22+
make test
23+

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.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
212212
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
213213
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
214214
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
215+
golang.org/x/tools v0.0.0-20190425163242-31fd60d6bfdc h1:N3zlSgxkefUH/ecsl37RWTkESTB026kmXzNly8TuZCI=
215216
golang.org/x/tools v0.0.0-20190425163242-31fd60d6bfdc/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
216217
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
217218
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8 h1:Nw54tB0rB7hY/N0NQvRW8DG4Yk3Q6T9cu9RcFQDu1tc=
@@ -234,4 +235,5 @@ gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=
234235
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
235236
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
236237
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
238+
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099 h1:XJP7lxbSxWLOMNdBE4B/STaqVy6L73o0knwj2vIlxnw=
237239
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
)

server.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,30 @@ import (
1616
"github.com/abronan/valkeyrie/store"
1717
)
1818

19+
// ServerOptions hold the configuration of server listeners
1920
type ServerOptions struct {
2021
ListeningAddr string
2122
ListeningTLSPort uint
2223
ListeningHTTPPort uint
2324
ListeningForClientsPort uint
2425
}
2526

27+
// HTTPAddr returns the HTTP listener address
2628
func (o ServerOptions) HTTPAddr() string {
2729
return fmt.Sprintf("%s:%d", o.ListeningAddr, o.ListeningHTTPPort)
2830
}
2931

32+
// TLSAddr returns the TLS listener address
3033
func (o ServerOptions) TLSAddr() string {
3134
return fmt.Sprintf("%s:%d", o.ListeningAddr, o.ListeningTLSPort)
3235
}
3336

37+
// ClientsAddr returns the client listener address
3438
func (o ServerOptions) ClientsAddr() string {
3539
return fmt.Sprintf("%s:%d", o.ListeningAddr, o.ListeningForClientsPort)
3640
}
3741

42+
//Server is tcp router server
3843
type Server struct {
3944
ServerOptions ServerOptions
4045
DbStore store.Store
@@ -45,9 +50,9 @@ type Server struct {
4550
listeners []net.Listener
4651
listenersMU sync.Mutex
4752
wg sync.WaitGroup
48-
ctx context.Context
4953
}
5054

55+
// NewServer creates a new server
5156
func NewServer(forwardOptions ServerOptions, store store.Store, services map[string]Service) *Server {
5257
if services == nil {
5358
services = make(map[string]Service)
@@ -62,6 +67,7 @@ func NewServer(forwardOptions ServerOptions, store store.Store, services map[str
6267
}
6368
}
6469

70+
// Start starts the server and blocks forever
6571
func (s *Server) Start(ctx context.Context) error {
6672

6773
s.wg.Add(3)
@@ -139,7 +145,7 @@ func (s *Server) getHost(host string) (Service, error) {
139145

140146
err = json.Unmarshal(servicePair.Value, &service)
141147
if err != nil {
142-
return service, fmt.Errorf("Invalid service content")
148+
return service, fmt.Errorf("invalid service content")
143149
}
144150

145151
log.Debug().
@@ -242,16 +248,16 @@ func (s *Server) handleHTTPConnection(conn WriteCloser) {
242248
func (s *Server) handleService(incoming WriteCloser, serverName, peeked string, isTLS bool) error {
243249
serverName = strings.ToLower(serverName)
244250
service, exists := s.Services[serverName]
245-
if exists == false {
251+
if !exists {
246252
log.Info().Msg("not found in file config, try to load it from db backend")
247253
var err error
248254
service, err = s.getHost(serverName)
249255
exists = err == nil
250256
}
251257

252-
if exists == false {
258+
if !exists {
253259
service, exists = s.Services["CATCH_ALL"]
254-
if exists == false {
260+
if !exists {
255261
return fmt.Errorf("service doesn't exist: %v and no 'CATCH_ALL' service for request", service)
256262
}
257263
}

0 commit comments

Comments
 (0)