Sfoglia il codice sorgente

Update golangci-lint

tags/v1.0.6^2^2
9seconds 5 anni fa
parent
commit
7a6695f6c4
47 ha cambiato i file con 133 aggiunte e 114 eliminazioni
  1. 1
    1
      .golangci.toml
  2. 1
    1
      Makefile
  3. 1
    2
      antireplay/init.go
  4. 2
    3
      cli/proxy.go
  5. 2
    0
      config/config.go
  6. 1
    1
      config/global_ips.go
  7. 2
    0
      conntypes/protocol.go
  8. 3
    1
      conntypes/type.go
  9. 3
    2
      faketls/client_protocol.go
  10. 2
    0
      faketls/cloak.go
  11. 2
    2
      hub/connection.go
  12. 1
    0
      hub/connection_list.go
  13. 1
    2
      main.go
  14. 1
    1
      mtproto/protocol.go
  15. 1
    1
      mtproto/rpc/consts.go
  16. 4
    4
      mtproto/rpc/handshake_response.go
  17. 6
    6
      mtproto/rpc/nonce_response.go
  18. 3
    3
      ntp/ntp.go
  19. 1
    0
      obfuscated2/client_protocol.go
  20. 1
    1
      obfuscated2/frame.go
  21. 1
    2
      protocol/request.go
  22. 1
    2
      proxy/direct.go
  23. 6
    4
      proxy/middle.go
  24. 3
    3
      proxy/proxy.go
  25. 7
    4
      stats/stats_prometheus.go
  26. 5
    4
      stats/stats_statsd.go
  27. 5
    1
      telegram/api/api.go
  28. 4
    3
      telegram/base.go
  29. 1
    2
      telegram/middle.go
  30. 10
    6
      tlstypes/consts.go
  31. 3
    4
      tlstypes/server_hello.go
  32. 2
    1
      utils/stream_cipher.go
  33. 2
    3
      wrappers/packet/mtproto_frame.go
  34. 1
    2
      wrappers/packetack/client_abridged.go
  35. 1
    2
      wrappers/packetack/client_intermediate.go
  36. 2
    3
      wrappers/packetack/client_intermediate_secure.go
  37. 2
    1
      wrappers/packetack/proxy.go
  38. 1
    2
      wrappers/stream/blockcipher.go
  39. 5
    3
      wrappers/stream/conn.go
  40. 6
    2
      wrappers/stream/ctx.go
  41. 4
    2
      wrappers/stream/faketls.go
  42. 16
    17
      wrappers/stream/mtproto_cipher.go
  43. 1
    2
      wrappers/stream/obfuscated2.go
  44. 2
    2
      wrappers/stream/rewind.go
  45. 1
    2
      wrappers/stream/stats_telegram.go
  46. 1
    2
      wrappers/stream/stats_traffic.go
  47. 1
    2
      wrappers/stream/timeout.go

+ 1
- 1
.golangci.toml Vedi File

@@ -10,4 +10,4 @@ format = "colored-line-number"
10 10
 
11 11
 [linters]
12 12
 enable-all = true
13
-disable = ["gochecknoglobals", "gomnd"]
13
+disable = ["gochecknoglobals", "gas", "gomnd", "goerr113"]

+ 1
- 1
Makefile Vedi File

@@ -71,4 +71,4 @@ prepare: install-lint
71 71
 .PHONY: install-lint
72 72
 install-lint:
73 73
 	@curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh \
74
-		| $(MOD_OFF) bash -s -- -b $(GOPATH)/bin $(GOLANGCI_LINT_VERSION)
74
+		| $(MOD_OFF) bash -s -- -b . $(GOLANGCI_LINT_VERSION)

+ 1
- 2
antireplay/init.go Vedi File

@@ -3,9 +3,8 @@ package antireplay
3 3
 import (
4 4
 	"sync"
5 5
 
6
-	"github.com/VictoriaMetrics/fastcache"
7
-
8 6
 	"github.com/9seconds/mtg/config"
7
+	"github.com/VictoriaMetrics/fastcache"
9 8
 )
10 9
 
11 10
 type CacheInterface interface {

+ 2
- 3
cli/proxy.go Vedi File

@@ -5,9 +5,6 @@ import (
5 5
 	"os"
6 6
 	"time"
7 7
 
8
-	"go.uber.org/zap"
9
-	"go.uber.org/zap/zapcore"
10
-
11 8
 	"github.com/9seconds/mtg/antireplay"
12 9
 	"github.com/9seconds/mtg/config"
13 10
 	"github.com/9seconds/mtg/faketls"
@@ -18,6 +15,8 @@ import (
18 15
 	"github.com/9seconds/mtg/stats"
19 16
 	"github.com/9seconds/mtg/telegram"
20 17
 	"github.com/9seconds/mtg/utils"
18
+	"go.uber.org/zap"
19
+	"go.uber.org/zap/zapcore"
21 20
 )
22 21
 
23 22
 func Proxy() error { // nolint: funlen

+ 2
- 0
config/config.go Vedi File

@@ -22,6 +22,8 @@ func (s SecretMode) String() string {
22 22
 		return "simple"
23 23
 	case SecretModeSecured:
24 24
 		return "secured"
25
+	case SecretModeTLS:
26
+		return "tls"
25 27
 	}
26 28
 
27 29
 	return "tls"

+ 1
- 1
config/global_ips.go Vedi File

@@ -60,7 +60,7 @@ func fetchIP(ctx context.Context, network string) (net.IP, error) {
60 60
 		return nil, fmt.Errorf("cannot perform a request: %w", err)
61 61
 	}
62 62
 
63
-	defer resp.Body.Close() // nolint: errcheck
63
+	defer resp.Body.Close()
64 64
 
65 65
 	respDataBytes, err := ioutil.ReadAll(resp.Body)
66 66
 	if err != nil {

+ 2
- 0
conntypes/protocol.go Vedi File

@@ -8,6 +8,8 @@ func (c ConnectionProtocol) String() string {
8 8
 		return "any"
9 9
 	case ConnectionProtocolIPv4:
10 10
 		return "ipv4"
11
+	case ConnectionProtocolIPv6:
12
+		return "ipv6"
11 13
 	}
12 14
 
13 15
 	return "ipv6"

+ 3
- 1
conntypes/type.go Vedi File

@@ -21,7 +21,9 @@ func (t ConnectionType) Tag() []byte {
21 21
 		return ConnectionTagAbridged
22 22
 	case ConnectionTypeIntermediate:
23 23
 		return ConnectionTagIntermediate
24
-	default:
24
+	case ConnectionTypeSecure, ConnectionTypeUnknown:
25 25
 		return ConnectionTagSecure
26 26
 	}
27
+
28
+	return ConnectionTagSecure
27 29
 }

+ 3
- 2
faketls/client_protocol.go Vedi File

@@ -49,8 +49,8 @@ func (c *ClientProtocol) Handshake(socket conntypes.StreamReadWriteCloser) (conn
49 49
 	}
50 50
 
51 51
 	conn := stream.NewFakeTLS(socket)
52
-	conn, err := c.ClientProtocol.Handshake(conn)
53 52
 
53
+	conn, err := c.ClientProtocol.Handshake(conn)
54 54
 	if err != nil {
55 55
 		return nil, err
56 56
 	}
@@ -89,6 +89,7 @@ func (c *ClientProtocol) tlsHandshake(conn io.ReadWriter) error {
89 89
 
90 90
 	if antireplay.Cache.HasTLS(clientHello.Random[:]) {
91 91
 		stats.Stats.ReplayDetected()
92
+
92 93
 		return errors.New("replay attack is detected")
93 94
 	}
94 95
 
@@ -107,8 +108,8 @@ func (c *ClientProtocol) cloakHost(clientConn io.ReadWriteCloser) {
107 108
 	stats.Stats.CloakedRequest()
108 109
 
109 110
 	addr := net.JoinHostPort(config.C.CloakHost, strconv.Itoa(config.C.CloakPort))
110
-	hostConn, err := net.Dial("tcp", addr)
111 111
 
112
+	hostConn, err := net.Dial("tcp", addr)
112 113
 	if err != nil {
113 114
 		return
114 115
 	}

+ 2
- 0
faketls/cloak.go Vedi File

@@ -53,9 +53,11 @@ func cloak(one, another io.ReadWriteCloser) {
53 53
 				return
54 54
 			case <-lastActivityTimer.C:
55 55
 				cancel()
56
+
56 57
 				return
57 58
 			case <-maxTimer.C:
58 59
 				cancel()
60
+
59 61
 				return
60 62
 			}
61 63
 		}

+ 2
- 2
hub/connection.go Vedi File

@@ -6,12 +6,11 @@ import (
6 6
 	"sync"
7 7
 	"time"
8 8
 
9
-	"go.uber.org/zap"
10
-
11 9
 	"github.com/9seconds/mtg/conntypes"
12 10
 	"github.com/9seconds/mtg/mtproto"
13 11
 	"github.com/9seconds/mtg/mtproto/rpc"
14 12
 	"github.com/9seconds/mtg/protocol"
13
+	"go.uber.org/zap"
15 14
 )
16 15
 
17 16
 const connectionTTL = time.Hour
@@ -90,6 +89,7 @@ func (c *connection) readLoop() {
90 89
 		response, err := rpc.ParseProxyResponse(packet)
91 90
 		if err != nil {
92 91
 			c.logger.Debugw("Failed response", "error", err)
92
+
93 93
 			continue
94 94
 		}
95 95
 

+ 1
- 0
hub/connection_list.go Vedi File

@@ -25,6 +25,7 @@ func (c *connectionList) get(conn *ProxyConn) (*connection, error) {
25 25
 
26 26
 	if err = newConn.Attach(conn); err != nil {
27 27
 		newConn.Close()
28
+
28 29
 		return nil, fmt.Errorf("cannot attach to the newly created connection: %w", err)
29 30
 	}
30 31
 

+ 1
- 2
main.go Vedi File

@@ -7,11 +7,10 @@ import (
7 7
 	"strings"
8 8
 	"time"
9 9
 
10
-	kingpin "gopkg.in/alecthomas/kingpin.v2"
11
-
12 10
 	"github.com/9seconds/mtg/cli"
13 11
 	"github.com/9seconds/mtg/config"
14 12
 	"github.com/9seconds/mtg/utils"
13
+	kingpin "gopkg.in/alecthomas/kingpin.v2"
15 14
 )
16 15
 
17 16
 var version = "dev" // has to be set by ldflags

+ 1
- 1
mtproto/protocol.go Vedi File

@@ -19,8 +19,8 @@ func TelegramProtocol(req *protocol.TelegramRequest) (conntypes.PacketReadWriteC
19 19
 	}
20 20
 
21 21
 	rpcNonceConn := packet.NewMtprotoFrame(conn, rpc.SeqNoNonce)
22
-	rpcNonceReq, err := doRPCNonceRequest(rpcNonceConn)
23 22
 
23
+	rpcNonceReq, err := doRPCNonceRequest(rpcNonceConn)
24 24
 	if err != nil {
25 25
 		return nil, fmt.Errorf("cannot do nonce request: %w", err)
26 26
 	}

+ 1
- 1
mtproto/rpc/consts.go Vedi File

@@ -7,7 +7,7 @@ const (
7 7
 	SeqNoHandshake = -1
8 8
 )
9 9
 
10
-// Different constants for RPC protocol
10
+// Different constants for RPC protocol.
11 11
 var (
12 12
 	TagCloseExt     = []byte{0xa2, 0x34, 0xb6, 0x5e}
13 13
 	TagProxyAns     = []byte{0x0d, 0xda, 0x03, 0x44}

+ 4
- 4
mtproto/rpc/handshake_response.go Vedi File

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

+ 6
- 6
mtproto/rpc/nonce_response.go Vedi File

@@ -15,13 +15,13 @@ type NonceResponse struct {
15 15
 
16 16
 // Bytes returns serialized form of the nonce response.
17 17
 func (r *NonceResponse) Bytes() []byte {
18
-	buf := &bytes.Buffer{}
18
+	buf := bytes.Buffer{}
19 19
 
20
-	buf.Write(r.Type)        // nolint: gosec
21
-	buf.Write(r.KeySelector) // nolint: gosec
22
-	buf.Write(r.Crypto)      // nolint: gosec
23
-	buf.Write(r.CryptoTS)    // nolint: gosec
24
-	buf.Write(r.Nonce)       // nolint: gosec
20
+	buf.Write(r.Type)
21
+	buf.Write(r.KeySelector)
22
+	buf.Write(r.Crypto)
23
+	buf.Write(r.CryptoTS)
24
+	buf.Write(r.Nonce)
25 25
 
26 26
 	return buf.Bytes()
27 27
 }

+ 3
- 3
ntp/ntp.go Vedi File

@@ -5,17 +5,16 @@ import (
5 5
 	"math/rand"
6 6
 	"time"
7 7
 
8
+	"github.com/9seconds/mtg/config"
8 9
 	"github.com/beevik/ntp"
9 10
 	"go.uber.org/zap"
10
-
11
-	"github.com/9seconds/mtg/config"
12 11
 )
13 12
 
14 13
 const autoUpdatePeriod = time.Minute
15 14
 
16 15
 // Fetch fetches the data on time drift.
17 16
 func Fetch() (time.Duration, error) {
18
-	url := config.C.NTPServers[rand.Intn(len(config.C.NTPServers))]
17
+	url := config.C.NTPServers[rand.Intn(len(config.C.NTPServers))] // nolint: gosec
19 18
 
20 19
 	resp, err := ntp.Query(url)
21 20
 	if err != nil {
@@ -40,6 +39,7 @@ func AutoUpdate() {
40 39
 		diff, err := Fetch()
41 40
 		if err != nil {
42 41
 			logger.Debugw("Cannot fetch time from NTP", "error", err)
42
+
43 43
 			continue
44 44
 		}
45 45
 

+ 1
- 0
obfuscated2/client_protocol.go Vedi File

@@ -84,6 +84,7 @@ func (c *ClientProtocol) Handshake(socket conntypes.StreamReadWriteCloser) (conn
84 84
 	replayKey := decryptedFrame.Unique()
85 85
 	if antireplay.Cache.HasObfuscated2(replayKey) {
86 86
 		stats.Stats.ReplayDetected()
87
+
87 88
 		return nil, errors.New("replay attack is detected")
88 89
 	}
89 90
 

+ 1
- 1
obfuscated2/frame.go Vedi File

@@ -15,7 +15,7 @@ const (
15 15
 	frameLen = 64
16 16
 )
17 17
 
18
-// [frameOffsetFirst:frameOffsetKey:frameOffsetIV:frameOffsetMagic:frameOffsetDC:frameOffsetEnd]
18
+// [frameOffsetFirst:frameOffsetKey:frameOffsetIV:frameOffsetMagic:frameOffsetDC:frameOffsetEnd].
19 19
 type Frame struct {
20 20
 	data [frameLen]byte
21 21
 }

+ 1
- 2
protocol/request.go Vedi File

@@ -3,9 +3,8 @@ package protocol
3 3
 import (
4 4
 	"context"
5 5
 
6
-	"go.uber.org/zap"
7
-
8 6
 	"github.com/9seconds/mtg/conntypes"
7
+	"go.uber.org/zap"
9 8
 )
10 9
 
11 10
 type TelegramRequest struct {

+ 1
- 2
proxy/direct.go Vedi File

@@ -4,11 +4,10 @@ import (
4 4
 	"io"
5 5
 	"sync"
6 6
 
7
-	"go.uber.org/zap"
8
-
9 7
 	"github.com/9seconds/mtg/conntypes"
10 8
 	"github.com/9seconds/mtg/obfuscated2"
11 9
 	"github.com/9seconds/mtg/protocol"
10
+	"go.uber.org/zap"
12 11
 )
13 12
 
14 13
 const directPipeBufferSize = 1024

+ 6
- 4
proxy/middle.go Vedi File

@@ -3,17 +3,17 @@ package proxy
3 3
 import (
4 4
 	"sync"
5 5
 
6
-	"go.uber.org/zap"
7
-
8 6
 	"github.com/9seconds/mtg/conntypes"
9 7
 	"github.com/9seconds/mtg/protocol"
10 8
 	"github.com/9seconds/mtg/wrappers/packetack"
9
+	"go.uber.org/zap"
11 10
 )
12 11
 
13 12
 func middleConnection(request *protocol.TelegramRequest) {
14 13
 	telegramConn, err := packetack.NewProxy(request)
15 14
 	if err != nil {
16 15
 		request.Logger.Debugw("Cannot dial to Telegram", "error", err)
16
+
17 17
 		return
18 18
 	}
19 19
 	defer telegramConn.Close()
@@ -27,7 +27,7 @@ func middleConnection(request *protocol.TelegramRequest) {
27 27
 		clientConn = packetack.NewClientIntermediate(request.ClientConn)
28 28
 	case conntypes.ConnectionTypeSecure:
29 29
 		clientConn = packetack.NewClientIntermediateSecure(request.ClientConn)
30
-	default:
30
+	case conntypes.ConnectionTypeUnknown:
31 31
 		panic("unknown connection type")
32 32
 	}
33 33
 
@@ -53,15 +53,17 @@ func middlePipe(dst conntypes.PacketAckWriteCloser,
53 53
 
54 54
 	for {
55 55
 		acks := conntypes.ConnectionAcks{}
56
-		packet, err := src.Read(&acks)
57 56
 
57
+		packet, err := src.Read(&acks)
58 58
 		if err != nil {
59 59
 			logger.Debugw("Cannot read packet", "error", err)
60
+
60 61
 			return
61 62
 		}
62 63
 
63 64
 		if err = dst.Write(packet, &acks); err != nil {
64 65
 			logger.Debugw("Cannot send packet", "error", err)
66
+
65 67
 			return
66 68
 		}
67 69
 	}

+ 3
- 3
proxy/proxy.go Vedi File

@@ -4,14 +4,13 @@ import (
4 4
 	"context"
5 5
 	"net"
6 6
 
7
-	"go.uber.org/zap"
8
-
9 7
 	"github.com/9seconds/mtg/config"
10 8
 	"github.com/9seconds/mtg/conntypes"
11 9
 	"github.com/9seconds/mtg/protocol"
12 10
 	"github.com/9seconds/mtg/stats"
13 11
 	"github.com/9seconds/mtg/utils"
14 12
 	"github.com/9seconds/mtg/wrappers/stream"
13
+	"go.uber.org/zap"
15 14
 )
16 15
 
17 16
 type Proxy struct {
@@ -53,6 +52,7 @@ func (p *Proxy) accept(conn net.Conn) {
53 52
 
54 53
 	if err := utils.InitTCP(conn, config.C.ClientReadBuffer(), config.C.ClientWriteBuffer()); err != nil {
55 54
 		logger.Errorw("Cannot initialize client TCP connection", "error", err)
55
+
56 56
 		return
57 57
 	}
58 58
 
@@ -66,8 +66,8 @@ func (p *Proxy) accept(conn net.Conn) {
66 66
 	defer clientConn.Close()
67 67
 
68 68
 	clientProtocol := p.ClientProtocolMaker()
69
-	clientConn, err := clientProtocol.Handshake(clientConn)
70 69
 
70
+	clientConn, err := clientProtocol.Handshake(clientConn)
71 71
 	if err != nil {
72 72
 		stats.Stats.AuthenticationFailed()
73 73
 		logger.Warnw("Cannot perform client handshake", "error", err)

+ 7
- 4
stats/stats_prometheus.go Vedi File

@@ -5,11 +5,10 @@ import (
5 5
 	"net/http"
6 6
 	"strconv"
7 7
 
8
-	"github.com/prometheus/client_golang/prometheus"
9
-	"github.com/prometheus/client_golang/prometheus/promhttp"
10
-
11 8
 	"github.com/9seconds/mtg/config"
12 9
 	"github.com/9seconds/mtg/conntypes"
10
+	"github.com/prometheus/client_golang/prometheus"
11
+	"github.com/prometheus/client_golang/prometheus/promhttp"
13 12
 )
14 13
 
15 14
 type statsPrometheus struct {
@@ -51,10 +50,14 @@ func (s *statsPrometheus) changeConnections(connectionType conntypes.ConnectionT
51 50
 		labels[0] = "abridged"
52 51
 	case conntypes.ConnectionTypeSecure:
53 52
 		labels[0] = "secured"
53
+	case conntypes.ConnectionTypeIntermediate:
54
+		labels[0] = "intermediate"
55
+	case conntypes.ConnectionTypeUnknown:
56
+		panic("unknown connection type")
54 57
 	}
55 58
 
56 59
 	if addr.IP.To4() == nil {
57
-		labels[1] = "ipv6" // nolint: goconst
60
+		labels[1] = "ipv6"
58 61
 	}
59 62
 
60 63
 	s.connections.WithLabelValues(labels[:]...).Add(increment)

+ 5
- 4
stats/stats_statsd.go Vedi File

@@ -8,11 +8,10 @@ import (
8 8
 	"sync"
9 9
 	"time"
10 10
 
11
-	statsd "github.com/smira/go-statsd"
12
-	"go.uber.org/zap"
13
-
14 11
 	"github.com/9seconds/mtg/config"
15 12
 	"github.com/9seconds/mtg/conntypes"
13
+	statsd "github.com/smira/go-statsd"
14
+	"go.uber.org/zap"
16 15
 )
17 16
 
18 17
 var (
@@ -91,8 +90,10 @@ func (s *statsStatsd) changeConnections(connectionType conntypes.ConnectionType,
91 90
 		tags = append(tags, tagConnectionTypeAbridged)
92 91
 	case conntypes.ConnectionTypeIntermediate:
93 92
 		tags = append(tags, tagConnectionTypeIntermediate)
94
-	default:
93
+	case conntypes.ConnectionTypeSecure:
95 94
 		tags = append(tags, tagConnectionTypeSecured)
95
+	case conntypes.ConnectionTypeUnknown:
96
+		panic("Unknown connection type")
96 97
 	}
97 98
 
98 99
 	if addr.IP.To4() == nil {

+ 5
- 1
telegram/api/api.go Vedi File

@@ -1,6 +1,7 @@
1 1
 package api
2 2
 
3 3
 import (
4
+	"context"
4 5
 	"fmt"
5 6
 	"io"
6 7
 	"io/ioutil"
@@ -18,7 +19,10 @@ var httpClient = http.Client{
18 19
 }
19 20
 
20 21
 func request(url string) (io.ReadCloser, error) {
21
-	req, err := http.NewRequest("GET", url, nil)
22
+	ctx, cancel := context.WithTimeout(context.Background(), apiHTTPTimeout)
23
+	defer cancel()
24
+
25
+	req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
22 26
 	if err != nil {
23 27
 		panic(err)
24 28
 	}

+ 4
- 3
telegram/base.go Vedi File

@@ -5,12 +5,11 @@ import (
5 5
 	"math/rand"
6 6
 	"net"
7 7
 
8
-	"go.uber.org/zap"
9
-
10 8
 	"github.com/9seconds/mtg/config"
11 9
 	"github.com/9seconds/mtg/conntypes"
12 10
 	"github.com/9seconds/mtg/utils"
13 11
 	"github.com/9seconds/mtg/wrappers/stream"
12
+	"go.uber.org/zap"
14 13
 )
15 14
 
16 15
 type baseTelegram struct {
@@ -34,11 +33,13 @@ func (b *baseTelegram) dial(dc conntypes.DC,
34 33
 		conn, err := b.dialer.Dial("tcp", addr)
35 34
 		if err != nil {
36 35
 			b.logger.Infow("Cannot dial to Telegram", "address", addr, "error", err)
36
+
37 37
 			continue
38 38
 		}
39 39
 
40 40
 		if err := utils.InitTCP(conn, config.C.ProxyReadBuffer(), config.C.ProxyWriteBuffer()); err != nil {
41 41
 			b.logger.Infow("Cannot initialize TCP socket", "address", addr, "error", err)
42
+
42 43
 			continue
43 44
 		}
44 45
 
@@ -83,7 +84,7 @@ func (b *baseTelegram) chooseAddress(addresses map[conntypes.DC][]string,
83 84
 	case len(addrs) == 1:
84 85
 		return addrs[0]
85 86
 	case len(addrs) > 1:
86
-		return addrs[rand.Intn(len(addrs))]
87
+		return addrs[rand.Intn(len(addrs))] // nolint: gosec
87 88
 	}
88 89
 
89 90
 	return ""

+ 1
- 2
telegram/middle.go Vedi File

@@ -5,10 +5,9 @@ import (
5 5
 	"sync"
6 6
 	"time"
7 7
 
8
-	"go.uber.org/zap"
9
-
10 8
 	"github.com/9seconds/mtg/conntypes"
11 9
 	"github.com/9seconds/mtg/telegram/api"
10
+	"go.uber.org/zap"
12 11
 )
13 12
 
14 13
 const middleTelegramBackgroundUpdateEvery = time.Hour

+ 10
- 6
tlstypes/consts.go Vedi File

@@ -20,9 +20,9 @@ const (
20 20
 type CipherSuiteType uint8
21 21
 
22 22
 const (
23
-	CipherSuiteType_TLS_AES_128_GCM_SHA256       CipherSuiteType = iota // nolint: stylecheck, golint
24
-	CipherSuiteType_TLS_AES_256_GCM_SHA384                              // nolint: stylecheck, golint
25
-	CipherSuiteType_TLS_CHACHA20_POLY1305_SHA256                        // nolint: stylecheck, golint
23
+	CipherSuiteType_TLS_AES_128_GCM_SHA256       CipherSuiteType = iota // nolint: stylecheck,golint
24
+	CipherSuiteType_TLS_AES_256_GCM_SHA384                              // nolint: stylecheck,golint
25
+	CipherSuiteType_TLS_CHACHA20_POLY1305_SHA256                        // nolint: stylecheck,golint
26 26
 )
27 27
 
28 28
 func (c CipherSuiteType) Bytes() []byte {
@@ -31,6 +31,8 @@ func (c CipherSuiteType) Bytes() []byte {
31 31
 		return CipherSuiteType_TLS_AES_128_GCM_SHA256_Bytes
32 32
 	case CipherSuiteType_TLS_AES_256_GCM_SHA384:
33 33
 		return CipherSuiteType_TLS_AES_256_GCM_SHA384_Bytes
34
+	case CipherSuiteType_TLS_CHACHA20_POLY1305_SHA256:
35
+		return CipherSuiteType_TLS_CHACHA20_POLY1305_SHA256_Bytes
34 36
 	}
35 37
 
36 38
 	return CipherSuiteType_TLS_CHACHA20_POLY1305_SHA256_Bytes
@@ -46,6 +48,8 @@ func (v Version) Bytes() []byte {
46 48
 		return Version12Bytes
47 49
 	case Version11:
48 50
 		return Version11Bytes
51
+	case Version10, VersionUnknown:
52
+		return Version10Bytes
49 53
 	}
50 54
 
51 55
 	return Version10Bytes
@@ -65,9 +69,9 @@ var (
65 69
 	Version12Bytes = []byte{0x03, 0x03}
66 70
 	Version13Bytes = []byte{0x03, 0x04}
67 71
 
68
-	CipherSuiteType_TLS_AES_128_GCM_SHA256_Bytes       = []byte{0x13, 0x01} // nolint: stylecheck, golint
69
-	CipherSuiteType_TLS_AES_256_GCM_SHA384_Bytes       = []byte{0x13, 0x02} // nolint: stylecheck, golint
70
-	CipherSuiteType_TLS_CHACHA20_POLY1305_SHA256_Bytes = []byte{0x13, 0x03} // nolint; stylecheck, golint
72
+	CipherSuiteType_TLS_AES_128_GCM_SHA256_Bytes       = []byte{0x13, 0x01} // nolint: stylecheck,golint
73
+	CipherSuiteType_TLS_AES_256_GCM_SHA384_Bytes       = []byte{0x13, 0x02} // nolint: stylecheck,golint
74
+	CipherSuiteType_TLS_CHACHA20_POLY1305_SHA256_Bytes = []byte{0x13, 0x03} // nolint: stylecheck,golint
71 75
 )
72 76
 
73 77
 type Byter interface {

+ 3
- 4
tlstypes/server_hello.go Vedi File

@@ -8,9 +8,8 @@ import (
8 8
 	"io"
9 9
 	mrand "math/rand"
10 10
 
11
-	"golang.org/x/crypto/curve25519"
12
-
13 11
 	"github.com/9seconds/mtg/config"
12
+	"golang.org/x/crypto/curve25519"
14 13
 )
15 14
 
16 15
 type ServerHello struct {
@@ -37,8 +36,8 @@ func (s ServerHello) WelcomePacket() []byte {
37 36
 	}
38 37
 	recChangeCipher.WriteBytes(buf)
39 38
 
40
-	hostCert := make([]byte, 1024+mrand.Intn(3092))
41
-	rand.Read(hostCert) // nolint: errcheck
39
+	hostCert := make([]byte, 1024+mrand.Intn(3092)) // nolint: gosec
40
+	rand.Read(hostCert)                             // nolint: errcheck
42 41
 
43 42
 	recData := Record{
44 43
 		Type:    RecordTypeApplicationData,

+ 2
- 1
utils/stream_cipher.go Vedi File

@@ -6,6 +6,7 @@ import (
6 6
 )
7 7
 
8 8
 func MakeStreamCipher(key, iv []byte) cipher.Stream {
9
-	block, _ := aes.NewCipher(key) // nolint: gosec
9
+	block, _ := aes.NewCipher(key)
10
+
10 11
 	return cipher.NewCTR(block, iv)
11 12
 }

+ 2
- 3
wrappers/packet/mtproto_frame.go Vedi File

@@ -10,9 +10,8 @@ import (
10 10
 	"io/ioutil"
11 11
 	"net"
12 12
 
13
-	"go.uber.org/zap"
14
-
15 13
 	"github.com/9seconds/mtg/conntypes"
14
+	"go.uber.org/zap"
16 15
 )
17 16
 
18 17
 const (
@@ -85,7 +84,7 @@ func (w *wrapperMtprotoFrame) Read() (conntypes.Packet, error) { // nolint: funl
85 84
 		return nil, fmt.Errorf("unexpected sequence number %d (wait for %d)", seqNo, w.readSeqNo)
86 85
 	}
87 86
 
88
-	data, _ := ioutil.ReadAll(buf) // nolint: gosec
87
+	data, _ := ioutil.ReadAll(buf)
89 88
 	buf.Reset()
90 89
 	// write to buf, not to writer. This is because we are going to fetch
91 90
 	// crc32 checksum.

+ 1
- 2
wrappers/packetack/client_abridged.go Vedi File

@@ -6,10 +6,9 @@ import (
6 6
 	"io"
7 7
 	"net"
8 8
 
9
-	"go.uber.org/zap"
10
-
11 9
 	"github.com/9seconds/mtg/conntypes"
12 10
 	"github.com/9seconds/mtg/utils"
11
+	"go.uber.org/zap"
13 12
 )
14 13
 
15 14
 const (

+ 1
- 2
wrappers/packetack/client_intermediate.go Vedi File

@@ -7,9 +7,8 @@ import (
7 7
 	"io"
8 8
 	"net"
9 9
 
10
-	"go.uber.org/zap"
11
-
12 10
 	"github.com/9seconds/mtg/conntypes"
11
+	"go.uber.org/zap"
13 12
 )
14 13
 
15 14
 const clientIntermediateQuickAckLength = 0x80000000

+ 2
- 3
wrappers/packetack/client_intermediate_secure.go Vedi File

@@ -6,9 +6,8 @@ import (
6 6
 	"fmt"
7 7
 	"math/rand"
8 8
 
9
-	"go.uber.org/zap"
10
-
11 9
 	"github.com/9seconds/mtg/conntypes"
10
+	"go.uber.org/zap"
12 11
 )
13 12
 
14 13
 type wrapperClientIntermediateSecure struct {
@@ -36,7 +35,7 @@ func (w *wrapperClientIntermediateSecure) Write(packet conntypes.Packet, acks *c
36 35
 	}
37 36
 
38 37
 	buf := &bytes.Buffer{}
39
-	paddingLength := rand.Intn(4)
38
+	paddingLength := rand.Intn(4) // nolint: gosec
40 39
 
41 40
 	buf.Grow(4 + len(packet) + paddingLength)
42 41
 

+ 2
- 1
wrappers/packetack/proxy.go Vedi File

@@ -64,6 +64,7 @@ func (w *wrapperProxy) Read(acks *conntypes.ConnectionAcks) (conntypes.Packet, e
64 64
 
65 65
 func (w *wrapperProxy) Close() error {
66 66
 	w.proxy.Close()
67
+
67 68
 	return nil
68 69
 }
69 70
 
@@ -77,7 +78,7 @@ func NewProxy(request *protocol.TelegramRequest) (conntypes.PacketAckReadWriteCl
77 78
 		flags |= rpc.ProxyRequestFlagsIntermediate
78 79
 	case conntypes.ConnectionTypeSecure:
79 80
 		flags |= rpc.ProxyRequestFlagsIntermediate | rpc.ProxyRequestFlagsPad
80
-	default:
81
+	case conntypes.ConnectionTypeUnknown:
81 82
 		panic("unknown connection type")
82 83
 	}
83 84
 

+ 1
- 2
wrappers/stream/blockcipher.go Vedi File

@@ -7,10 +7,9 @@ import (
7 7
 	"net"
8 8
 	"time"
9 9
 
10
-	"go.uber.org/zap"
11
-
12 10
 	"github.com/9seconds/mtg/conntypes"
13 11
 	"github.com/9seconds/mtg/utils"
12
+	"go.uber.org/zap"
14 13
 )
15 14
 
16 15
 type wrapperBlockCipher struct {

+ 5
- 3
wrappers/stream/conn.go Vedi File

@@ -5,10 +5,9 @@ import (
5 5
 	"net"
6 6
 	"time"
7 7
 
8
-	"go.uber.org/zap"
9
-
10 8
 	"github.com/9seconds/mtg/config"
11 9
 	"github.com/9seconds/mtg/conntypes"
10
+	"go.uber.org/zap"
12 11
 )
13 12
 
14 13
 type connPurpose uint8
@@ -29,6 +28,7 @@ type wrapperConn struct {
29 28
 func (w *wrapperConn) WriteTimeout(p []byte, timeout time.Duration) (int, error) {
30 29
 	if err := w.parent.SetWriteDeadline(time.Now().Add(timeout)); err != nil {
31 30
 		w.Close()
31
+
32 32
 		return 0, fmt.Errorf("cannot set write deadline to the socket: %w", err)
33 33
 	}
34 34
 
@@ -40,7 +40,7 @@ func (w *wrapperConn) Write(p []byte) (int, error) {
40 40
 	w.logger.Debugw("write to stream", "bytes", n, "error", err)
41 41
 
42 42
 	if err != nil {
43
-		w.Close() // nolint: gosec
43
+		w.Close()
44 44
 	}
45 45
 
46 46
 	return n, err
@@ -49,6 +49,7 @@ func (w *wrapperConn) Write(p []byte) (int, error) {
49 49
 func (w *wrapperConn) ReadTimeout(p []byte, timeout time.Duration) (int, error) {
50 50
 	if err := w.parent.SetReadDeadline(time.Now().Add(timeout)); err != nil {
51 51
 		w.Close()
52
+
52 53
 		return 0, fmt.Errorf("cannot set read deadline to the socket: %w", err)
53 54
 	}
54 55
 
@@ -68,6 +69,7 @@ func (w *wrapperConn) Read(p []byte) (int, error) {
68 69
 
69 70
 func (w *wrapperConn) Close() error {
70 71
 	w.logger.Debugw("Close connection")
72
+
71 73
 	return w.parent.Close()
72 74
 }
73 75
 

+ 6
- 2
wrappers/stream/ctx.go Vedi File

@@ -6,9 +6,8 @@ import (
6 6
 	"net"
7 7
 	"time"
8 8
 
9
-	"go.uber.org/zap"
10
-
11 9
 	"github.com/9seconds/mtg/conntypes"
10
+	"go.uber.org/zap"
12 11
 )
13 12
 
14 13
 type wrapperCtx struct {
@@ -21,6 +20,7 @@ func (w *wrapperCtx) WriteTimeout(p []byte, timeout time.Duration) (int, error)
21 20
 	select {
22 21
 	case <-w.ctx.Done():
23 22
 		w.Close()
23
+
24 24
 		return 0, fmt.Errorf("cannot write because context was closed: %w", w.ctx.Err())
25 25
 	default:
26 26
 		return w.parent.WriteTimeout(p, timeout)
@@ -31,6 +31,7 @@ func (w *wrapperCtx) Write(p []byte) (int, error) {
31 31
 	select {
32 32
 	case <-w.ctx.Done():
33 33
 		w.Close()
34
+
34 35
 		return 0, fmt.Errorf("cannot write because context was closed: %w", w.ctx.Err())
35 36
 	default:
36 37
 		return w.parent.Write(p)
@@ -41,6 +42,7 @@ func (w *wrapperCtx) ReadTimeout(p []byte, timeout time.Duration) (int, error) {
41 42
 	select {
42 43
 	case <-w.ctx.Done():
43 44
 		w.Close()
45
+
44 46
 		return 0, fmt.Errorf("cannot write because context was closed: %w", w.ctx.Err())
45 47
 	default:
46 48
 		return w.parent.ReadTimeout(p, timeout)
@@ -51,6 +53,7 @@ func (w *wrapperCtx) Read(p []byte) (int, error) {
51 53
 	select {
52 54
 	case <-w.ctx.Done():
53 55
 		w.Close()
56
+
54 57
 		return 0, fmt.Errorf("cannot write because context was closed: %w", w.ctx.Err())
55 58
 	default:
56 59
 		return w.parent.Read(p)
@@ -59,6 +62,7 @@ func (w *wrapperCtx) Read(p []byte) (int, error) {
59 62
 
60 63
 func (w *wrapperCtx) Close() error {
61 64
 	w.cancel()
65
+
62 66
 	return w.parent.Close()
63 67
 }
64 68
 

+ 4
- 2
wrappers/stream/faketls.go Vedi File

@@ -7,10 +7,9 @@ import (
7 7
 	"net"
8 8
 	"time"
9 9
 
10
-	"go.uber.org/zap"
11
-
12 10
 	"github.com/9seconds/mtg/conntypes"
13 11
 	"github.com/9seconds/mtg/tlstypes"
12
+	"go.uber.org/zap"
14 13
 )
15 14
 
16 15
 type wrapperFakeTLS struct {
@@ -33,6 +32,7 @@ func (w *wrapperFakeTLS) WriteTimeout(p []byte, timeout time.Duration) (int, err
33 32
 		if elapsed > timeout {
34 33
 			return w.parent.WriteTimeout(b, timeout-elapsed)
35 34
 		}
35
+
36 36
 		return 0, errors.New("timeout")
37 37
 	})
38 38
 }
@@ -95,6 +95,8 @@ func NewFakeTLS(socket conntypes.StreamReadWriteCloser) conntypes.StreamReadWrit
95 95
 				rec.Data.WriteBytes(buf)
96 96
 
97 97
 				return buf.Bytes(), nil
98
+			case tlstypes.RecordTypeHandshake:
99
+				return nil, errors.New("unsupported record type handshake")
98 100
 			default:
99 101
 				return nil, fmt.Errorf("unsupported record type %v", rec.Type)
100 102
 			}

+ 16
- 17
wrappers/stream/mtproto_cipher.go Vedi File

@@ -4,8 +4,8 @@ import (
4 4
 	"bytes"
5 5
 	"crypto/aes"
6 6
 	"crypto/cipher"
7
-	"crypto/md5"  // nolint: gosec
8
-	"crypto/sha1" // nolint: gosec
7
+	"crypto/md5"
8
+	"crypto/sha1"
9 9
 	"encoding/binary"
10 10
 	"net"
11 11
 
@@ -54,12 +54,11 @@ func mtprotoDeriveKeys(purpose mtprotoCipherPurpose,
54 54
 	resp *rpc.NonceResponse,
55 55
 	client, remote *net.TCPAddr,
56 56
 	secret []byte) ([]byte, []byte) {
57
-
58 57
 	message := bytes.Buffer{}
59 58
 
60
-	message.Write(resp.Nonce)   // nolint: gosec
61
-	message.Write(req.Nonce)    // nolint: gosec
62
-	message.Write(req.CryptoTS) // nolint: gosec
59
+	message.Write(resp.Nonce)
60
+	message.Write(req.Nonce)
61
+	message.Write(req.CryptoTS)
63 62
 
64 63
 	clientIPv4 := mtprotoEmptyIP[:]
65 64
 	serverIPv4 := mtprotoEmptyIP[:]
@@ -69,34 +68,34 @@ func mtprotoDeriveKeys(purpose mtprotoCipherPurpose,
69 68
 		serverIPv4 = utils.ReverseBytes(remote.IP.To4())
70 69
 	}
71 70
 
72
-	message.Write(serverIPv4) // nolint: gosec
71
+	message.Write(serverIPv4)
73 72
 
74 73
 	var port [2]byte
75 74
 
76 75
 	binary.LittleEndian.PutUint16(port[:], uint16(client.Port))
77
-	message.Write(port[:]) // nolint: gosec
76
+	message.Write(port[:])
78 77
 
79 78
 	switch purpose {
80 79
 	case mtprotoCipherPurposeClient:
81
-		message.WriteString("CLIENT") // nolint: gosec
80
+		message.WriteString("CLIENT")
82 81
 	case mtprotoCipherPurposeServer:
83
-		message.WriteString("SERVER") // nolint: gosec
82
+		message.WriteString("SERVER")
84 83
 	default:
85 84
 		panic("Unexpected cipher purpose")
86 85
 	}
87 86
 
88
-	message.Write(clientIPv4) // nolint: gosec
87
+	message.Write(clientIPv4)
89 88
 	binary.LittleEndian.PutUint16(port[:], uint16(remote.Port))
90
-	message.Write(port[:])    // nolint: gosec
91
-	message.Write(secret)     // nolint: gosec
92
-	message.Write(resp.Nonce) // nolint: gosec
89
+	message.Write(port[:])
90
+	message.Write(secret)
91
+	message.Write(resp.Nonce)
93 92
 
94 93
 	if client.IP.To4() == nil {
95
-		message.Write(client.IP.To16()) // nolint: gosec
96
-		message.Write(remote.IP.To16()) // nolint: gosec
94
+		message.Write(client.IP.To16())
95
+		message.Write(remote.IP.To16())
97 96
 	}
98 97
 
99
-	message.Write(req.Nonce) // nolint: gosec
98
+	message.Write(req.Nonce)
100 99
 
101 100
 	data := message.Bytes()
102 101
 	md5sum := md5.Sum(data[1:]) // nolint: gas

+ 1
- 2
wrappers/stream/obfuscated2.go Vedi File

@@ -7,9 +7,8 @@ import (
7 7
 	"net"
8 8
 	"time"
9 9
 
10
-	"go.uber.org/zap"
11
-
12 10
 	"github.com/9seconds/mtg/conntypes"
11
+	"go.uber.org/zap"
13 12
 )
14 13
 
15 14
 type wrapperObfuscated2 struct {

+ 2
- 2
wrappers/stream/rewind.go Vedi File

@@ -7,9 +7,8 @@ import (
7 7
 	"sync"
8 8
 	"time"
9 9
 
10
-	"go.uber.org/zap"
11
-
12 10
 	"github.com/9seconds/mtg/conntypes"
11
+	"go.uber.org/zap"
13 12
 )
14 13
 
15 14
 type ReadWriteCloseRewinder interface {
@@ -88,6 +87,7 @@ func (w *wrapperRewind) RemoteAddr() *net.TCPAddr {
88 87
 
89 88
 func (w *wrapperRewind) Close() error {
90 89
 	w.buf.Reset()
90
+
91 91
 	return w.parent.Close()
92 92
 }
93 93
 

+ 1
- 2
wrappers/stream/stats_telegram.go Vedi File

@@ -5,10 +5,9 @@ import (
5 5
 	"sync"
6 6
 	"time"
7 7
 
8
-	"go.uber.org/zap"
9
-
10 8
 	"github.com/9seconds/mtg/conntypes"
11 9
 	"github.com/9seconds/mtg/stats"
10
+	"go.uber.org/zap"
12 11
 )
13 12
 
14 13
 type wrapperTelegramStats struct {

+ 1
- 2
wrappers/stream/stats_traffic.go Vedi File

@@ -4,10 +4,9 @@ import (
4 4
 	"net"
5 5
 	"time"
6 6
 
7
-	"go.uber.org/zap"
8
-
9 7
 	"github.com/9seconds/mtg/conntypes"
10 8
 	"github.com/9seconds/mtg/stats"
9
+	"go.uber.org/zap"
11 10
 )
12 11
 
13 12
 type wrapperTrafficStats struct {

+ 1
- 2
wrappers/stream/timeout.go Vedi File

@@ -4,9 +4,8 @@ import (
4 4
 	"net"
5 5
 	"time"
6 6
 
7
-	"go.uber.org/zap"
8
-
9 7
 	"github.com/9seconds/mtg/conntypes"
8
+	"go.uber.org/zap"
10 9
 )
11 10
 
12 11
 const (

Loading…
Annulla
Salva