9seconds 7 лет назад
Родитель
Сommit
c5f99d17b7
7 измененных файлов: 37 добавлений и 33 удалений
  1. 1
    0
      .travis.yml
  2. 9
    1
      Makefile
  3. 7
    10
      config/config.go
  4. 3
    3
      config/urls.go
  5. 8
    10
      main.go
  6. 4
    4
      mtproto/rpc/handshake_response.go
  7. 5
    5
      wrappers/mtproto_cipher.go

+ 1
- 0
.travis.yml Просмотреть файл

@@ -13,6 +13,7 @@ script:
13 13
   - make prepare
14 14
   - make all
15 15
   - make lint
16
+  - make critic
16 17
   - make test
17 18
 
18 19
 cache:

+ 9
- 1
Makefile Просмотреть файл

@@ -58,6 +58,10 @@ test: vendor version.go
58 58
 lint: version.go
59 59
 	@golangci-lint run
60 60
 
61
+.PHONY: critic
62
+critic: version.go
63
+	@gocritic check-project "$(ROOT_DIR)"
64
+
61 65
 .PHONY: clean
62 66
 clean:
63 67
 	@git clean -xfd && \
@@ -69,7 +73,7 @@ docker:
69 73
 	@docker build --pull -t "$(IMAGE_NAME)" "$(ROOT_DIR)"
70 74
 
71 75
 .PHONY: prepare
72
-prepare: install-dep install-lint
76
+prepare: install-dep install-lint install-critic
73 77
 
74 78
 .PHONY: install-dep
75 79
 install-dep:
@@ -79,3 +83,7 @@ install-dep:
79 83
 install-lint:
80 84
 	@curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh \
81 85
 		| bash -s -- -b $(GOPATH)/bin $(GOLANGCI_LINT_VERSION)
86
+
87
+.PHONY: install-critic
88
+install-critic:
89
+	@go get -u github.com/go-critic/go-critic/...

+ 7
- 10
config/config.go Просмотреть файл

@@ -114,13 +114,10 @@ func getAddr(host fmt.Stringer, port uint16) string {
114 114
 // fetches data from external sources. Parameters passed to this
115 115
 // function, should come from command line arguments.
116 116
 func NewConfig(debug, verbose bool, // nolint: gocyclo
117
-	bindIP net.IP, bindPort uint16,
118
-	publicIPv4 net.IP, PublicIPv4Port uint16,
119
-	publicIPv6 net.IP, publicIPv6Port uint16,
120
-	statsIP net.IP, statsPort uint16,
121
-	secret, adtag string,
122
-	statsdIP string, statsdPort uint16, statsdNetwork string, statsdPrefix string,
123
-	statsdTagsFormat string, statsdTags map[string]string) (*Config, error) {
117
+	bindIP, publicIPv4, publicIPv6, statsIP net.IP,
118
+	bindPort, publicIPv4Port, publicIPv6Port, statsPort, statsdPort uint16,
119
+	secret, adtag, statsdIP, statsdNetwork, statsdPrefix, statsdTagsFormat string,
120
+	statsdTags map[string]string) (*Config, error) {
124 121
 	secureMode := false
125 122
 	if strings.HasPrefix(secret, "dd") && len(secret) == 34 {
126 123
 		secureMode = true
@@ -149,8 +146,8 @@ func NewConfig(debug, verbose bool, // nolint: gocyclo
149 146
 			return nil, errors.Errorf("IP %s is not IPv4", publicIPv4.String())
150 147
 		}
151 148
 	}
152
-	if PublicIPv4Port == 0 {
153
-		PublicIPv4Port = bindPort
149
+	if publicIPv4Port == 0 {
150
+		publicIPv4Port = bindPort
154 151
 	}
155 152
 
156 153
 	if publicIPv6 == nil {
@@ -175,7 +172,7 @@ func NewConfig(debug, verbose bool, // nolint: gocyclo
175 172
 		BindIP:         bindIP,
176 173
 		BindPort:       bindPort,
177 174
 		PublicIPv4:     publicIPv4,
178
-		PublicIPv4Port: PublicIPv4Port,
175
+		PublicIPv4Port: publicIPv4Port,
179 176
 		PublicIPv6:     publicIPv6,
180 177
 		PublicIPv6Port: publicIPv6Port,
181 178
 		StatsIP:        statsIP,

+ 3
- 3
config/urls.go Просмотреть файл

@@ -42,7 +42,7 @@ func makeTMeURL(values url.Values) string {
42 42
 }
43 43
 
44 44
 func makeQRCodeURL(data string) string {
45
-	QRURL := url.URL{
45
+	qr := url.URL{
46 46
 		Scheme: "https",
47 47
 		Host:   "api.qrserver.com",
48 48
 		Path:   "v1/create-qr-code",
@@ -52,7 +52,7 @@ func makeQRCodeURL(data string) string {
52 52
 	values.Set("qzone", "4")
53 53
 	values.Set("format", "svg")
54 54
 	values.Set("data", data)
55
-	QRURL.RawQuery = values.Encode()
55
+	qr.RawQuery = values.Encode()
56 56
 
57
-	return QRURL.String()
57
+	return qr.String()
58 58
 }

+ 8
- 10
main.go Просмотреть файл

@@ -129,24 +129,22 @@ func main() {
129 129
 	}
130 130
 
131 131
 	conf, err := config.NewConfig(*debug, *verbose,
132
-		*bindIP, *bindPort,
133
-		*publicIPv4, *publicIPv4Port,
134
-		*publicIPv6, *publicIPv6Port,
135
-		*statsIP, *statsPort,
136
-		*secret, *adtag,
137
-		*statsdIP, *statsdPort, *statsdNetwork, *statsdPrefix,
138
-		*statsdTagsFormat, *statsdTags,
132
+		*bindIP, *publicIPv4, *publicIPv6, *statsIP,
133
+		*bindPort, *publicIPv4Port, *publicIPv6Port, *statsPort, *statsdPort,
134
+		*secret, *adtag, *statsdIP, *statsdNetwork, *statsdPrefix, *statsdTagsFormat,
135
+		*statsdTags,
139 136
 	)
140 137
 	if err != nil {
141 138
 		usage(err.Error())
142 139
 	}
143 140
 
144 141
 	atom := zap.NewAtomicLevel()
145
-	if conf.Debug {
142
+	switch {
143
+	case conf.Debug:
146 144
 		atom.SetLevel(zapcore.DebugLevel)
147
-	} else if conf.Verbose {
145
+	case conf.Verbose:
148 146
 		atom.SetLevel(zapcore.InfoLevel)
149
-	} else {
147
+	default:
150 148
 		atom.SetLevel(zapcore.ErrorLevel)
151 149
 	}
152 150
 	encoderCfg := zap.NewProductionEncoderConfig()

+ 4
- 4
mtproto/rpc/handshake_response.go Просмотреть файл

@@ -19,10 +19,10 @@ type HandshakeResponse struct {
19 19
 func (r *HandshakeResponse) Bytes() []byte {
20 20
 	buf := &bytes.Buffer{}
21 21
 
22
-	buf.Write(r.Type[:])
23
-	buf.Write(r.Flags[:])
24
-	buf.Write(r.SenderPID[:])
25
-	buf.Write(r.PeerPID[:])
22
+	buf.Write(r.Type)
23
+	buf.Write(r.Flags)
24
+	buf.Write(r.SenderPID)
25
+	buf.Write(r.PeerPID)
26 26
 
27 27
 	return buf.Bytes()
28 28
 }

+ 5
- 5
wrappers/mtproto_cipher.go Просмотреть файл

@@ -41,9 +41,9 @@ func NewMiddleProxyCipher(conn StreamReadWriteCloser,
41 41
 func deriveKeys(purpose cipherPurpose, req *rpc.NonceRequest, resp *rpc.NonceResponse,
42 42
 	client, remote *net.TCPAddr, secret []byte) ([]byte, []byte) {
43 43
 	message := bytes.Buffer{}
44
-	message.Write(resp.Nonce[:])
45
-	message.Write(req.Nonce[:])
46
-	message.Write(req.CryptoTS[:])
44
+	message.Write(resp.Nonce)
45
+	message.Write(req.Nonce)
46
+	message.Write(req.CryptoTS)
47 47
 
48 48
 	clientIPv4 := emptyIP[:]
49 49
 	serverIPv4 := emptyIP[:]
@@ -70,13 +70,13 @@ func deriveKeys(purpose cipherPurpose, req *rpc.NonceRequest, resp *rpc.NonceRes
70 70
 	binary.LittleEndian.PutUint16(port[:], uint16(remote.Port))
71 71
 	message.Write(port[:])
72 72
 	message.Write(secret)
73
-	message.Write(resp.Nonce[:])
73
+	message.Write(resp.Nonce)
74 74
 
75 75
 	if client.IP.To4() == nil {
76 76
 		message.Write(client.IP.To16())
77 77
 		message.Write(remote.IP.To16())
78 78
 	}
79
-	message.Write(req.Nonce[:])
79
+	message.Write(req.Nonce)
80 80
 
81 81
 	data := message.Bytes()
82 82
 	md5sum := md5.Sum(data[1:]) // nolint: gas

Загрузка…
Отмена
Сохранить