Pārlūkot izejas kodu

bug fixes

tags/1.0^2
9seconds 6 gadus atpakaļ
vecāks
revīzija
d459efcf9c
9 mainītis faili ar 38 papildinājumiem un 12 dzēšanām
  1. 2
    0
      cli/proxy.go
  2. 9
    2
      config/config.go
  3. 1
    1
      go.mod
  4. 1
    0
      hub/connection.go
  5. 17
    0
      hub/connection_hub.go
  6. 3
    0
      hub/hub.go
  7. 0
    7
      hub/registry.go
  8. 4
    1
      stats/stats_prometheus.go
  9. 1
    1
      stats/stats_statsd.go

+ 2
- 0
cli/proxy.go Parādīt failu

@@ -10,6 +10,7 @@ import (
10 10
 
11 11
 	"github.com/9seconds/mtg/antireplay"
12 12
 	"github.com/9seconds/mtg/config"
13
+	"github.com/9seconds/mtg/hub"
13 14
 	"github.com/9seconds/mtg/ntp"
14 15
 	"github.com/9seconds/mtg/obfuscated2"
15 16
 	"github.com/9seconds/mtg/proxy"
@@ -66,6 +67,7 @@ func Proxy() error {
66 67
 		Fatal(err)
67 68
 	}
68 69
 	telegram.Init()
70
+	hub.Init(ctx)
69 71
 
70 72
 	proxyListener, err := net.Listen("tcp", config.C.Bind.String())
71 73
 	if err != nil {

+ 9
- 2
config/config.go Parādīt failu

@@ -9,6 +9,7 @@ import (
9 9
 	"net"
10 10
 	"time"
11 11
 
12
+	"github.com/alecthomas/units"
12 13
 	"go.uber.org/zap"
13 14
 	statsd "gopkg.in/alexcesaro/statsd.v2"
14 15
 )
@@ -104,8 +105,14 @@ func Init(options ...Opt) error { // nolint: gocyclo, funlen
104 105
 			C.Bind = opt.Value.(*net.TCPAddr)
105 106
 		case OptionTypePublicIPv4:
106 107
 			C.PublicIPv4 = opt.Value.(*net.TCPAddr)
108
+			if C.PublicIPv4 == nil {
109
+				C.PublicIPv4 = &net.TCPAddr{}
110
+			}
107 111
 		case OptionTypePublicIPv6:
108 112
 			C.PublicIPv6 = opt.Value.(*net.TCPAddr)
113
+			if C.PublicIPv6 == nil {
114
+				C.PublicIPv6 = &net.TCPAddr{}
115
+			}
109 116
 		case OptionTypeStatsBind:
110 117
 			C.StatsBind = opt.Value.(*net.TCPAddr)
111 118
 		case OptionTypeStatsNamespace:
@@ -133,9 +140,9 @@ func Init(options ...Opt) error { // nolint: gocyclo, funlen
133 140
 		case OptionTypeStatsdTags:
134 141
 			C.StatsdTags = opt.Value.(map[string]string)
135 142
 		case OptionTypeWriteBufferSize:
136
-			C.WriteBuffer = int(opt.Value.(uint32))
143
+			C.WriteBuffer = int(opt.Value.(units.Base2Bytes))
137 144
 		case OptionTypeReadBufferSize:
138
-			C.ReadBuffer = int(opt.Value.(uint32))
145
+			C.ReadBuffer = int(opt.Value.(units.Base2Bytes))
139 146
 		case OptionTypeAntiReplayMaxSize:
140 147
 			C.AntiReplayMaxSize = opt.Value.(int)
141 148
 		case OptionTypeAntiReplayEvictionTime:

+ 1
- 1
go.mod Parādīt failu

@@ -5,7 +5,7 @@ replace github.com/golang/lint => github.com/golang/lint v0.0.0-20190227174305-8
5 5
 require (
6 6
 	github.com/OneOfOne/xxhash v1.2.5 // indirect
7 7
 	github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
8
-	github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4 // indirect
8
+	github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4
9 9
 	github.com/allegro/bigcache v1.2.1
10 10
 	github.com/beevik/ntp v0.2.0
11 11
 	github.com/cespare/xxhash v1.1.0

+ 1
- 0
hub/connection.go Parādīt failu

@@ -109,6 +109,7 @@ func newConnection(req *protocol.TelegramRequest, hub *connectionHub) (*connecti
109 109
 		conn: conn,
110 110
 		hub:  hub,
111 111
 		id:   rand.Int(), // nolint: gosec
112
+		done: make(chan struct{}),
112 113
 	}
113 114
 	go rv.run()
114 115
 

+ 17
- 0
hub/connection_hub.go Parādīt failu

@@ -43,11 +43,16 @@ func (c *connectionHub) run() {
43 43
 }
44 44
 
45 45
 func (c *connectionHub) runGC() {
46
+	logger := c.logger.Named("gc")
47
+
46 48
 	for key, conn := range c.sockets {
47 49
 		switch {
48 50
 		case conn.closed():
51
+			logger.Debugw("Delete closed socket", "key", key)
49 52
 			delete(c.sockets, key)
53
+
50 54
 		case conn.idle():
55
+			logger.Debugw("Delete idle socket", "key", key)
51 56
 			conn.shutdown()
52 57
 			delete(c.sockets, key)
53 58
 			return
@@ -56,9 +61,14 @@ func (c *connectionHub) runGC() {
56 61
 }
57 62
 
58 63
 func (c *connectionHub) runConnectionRequest(req *connectionHubRequest) {
64
+	logger := c.logger.Named("request").With("connection-id", req.request.ConnID)
65
+
59 66
 	for key, conn := range c.sockets {
60 67
 		delete(c.sockets, key)
61 68
 		if !conn.closed() {
69
+			logger.Debugw("Choose connection",
70
+				"id", conn.id,
71
+				"remote_addr", conn.conn.RemoteAddr())
62 72
 			req.response <- conn
63 73
 			close(req.response)
64 74
 			return
@@ -66,16 +76,23 @@ func (c *connectionHub) runConnectionRequest(req *connectionHubRequest) {
66 76
 	}
67 77
 
68 78
 	if conn, err := newConnection(req.request, c); err == nil {
79
+		logger.Debugw("New connection",
80
+			"id", conn.id,
81
+			"remote_addr", conn.conn.RemoteAddr())
69 82
 		req.response <- conn
70 83
 	}
71 84
 	close(req.response)
72 85
 }
73 86
 
74 87
 func (c *connectionHub) runBrokenSocket(id int) {
88
+	c.logger.Named("broken-socket").Debugw("Delete broken socket", "id", id)
75 89
 	delete(c.sockets, id)
76 90
 }
77 91
 
78 92
 func (c *connectionHub) runReturnConnection(conn *connection) {
93
+	c.logger.Named("return-connection").Debugw("Return connection",
94
+		"id", conn.id,
95
+		"remote_addr", conn.conn.RemoteAddr())
79 96
 	c.sockets[conn.id] = conn
80 97
 }
81 98
 

+ 3
- 0
hub/hub.go Parādīt failu

@@ -32,8 +32,11 @@ func (h *hub) Write(packet conntypes.Packet, req *protocol.TelegramRequest) erro
32 32
 	}
33 33
 
34 34
 	if err := conn.write(packet); err != nil {
35
+		conn.shutdown()
35 36
 		return fmt.Errorf("cannot send packet: %w", err)
36 37
 	}
38
+	sub.channelReturnConnections <- conn
39
+
37 40
 	return nil
38 41
 }
39 42
 

+ 0
- 7
hub/registry.go Parādīt failu

@@ -42,10 +42,3 @@ func (r *registry) getChannel(id conntypes.ConnID) (*ctxChannel, bool) {
42 42
 	}
43 43
 	return nil, false
44 44
 }
45
-
46
-func InitRegistry(ctx context.Context) {
47
-	Registry = &registry{
48
-		ctx:   ctx,
49
-		conns: map[string]*ctxChannel{},
50
-	}
51
-}

+ 4
- 1
stats/stats_prometheus.go Parādīt failu

@@ -77,7 +77,7 @@ func (s *statsPrometheus) changeTelegramConnections(dc conntypes.DC, addr *net.T
77 77
 		labels[1] = "ipv6"
78 78
 	}
79 79
 
80
-	s.connections.WithLabelValues(labels[:]...).Add(increment)
80
+	s.telegramConnections.WithLabelValues(labels[:]...).Add(increment)
81 81
 }
82 82
 
83 83
 func (s *statsPrometheus) Crash() {
@@ -122,6 +122,9 @@ func newStatsPrometheus(mux *http.ServeMux) (Interface, error) {
122 122
 	if err := registry.Register(instance.connections); err != nil {
123 123
 		return nil, fmt.Errorf("cannot register metrics for connections: %w", err)
124 124
 	}
125
+	if err := registry.Register(instance.telegramConnections); err != nil {
126
+		return nil, fmt.Errorf("cannot register metrics for telegram connections: %w", err)
127
+	}
125 128
 	if err := registry.Register(instance.traffic); err != nil {
126 129
 		return nil, fmt.Errorf("cannot register metrics for traffic: %w", err)
127 130
 	}

+ 1
- 1
stats/stats_statsd.go Parādīt failu

@@ -63,7 +63,7 @@ func (s *statsStatsd) TelegramDisconnected(dc conntypes.DC, addr *net.TCPAddr) {
63 63
 
64 64
 func (s *statsStatsd) changeTelegramConnections(dc conntypes.DC, addr *net.TCPAddr, value int) {
65 65
 	labels := [...]string{
66
-		"telegram",
66
+		"telegram_connections",
67 67
 		strconv.Itoa(int(dc)),
68 68
 		"ipv4",
69 69
 	}

Notiek ielāde…
Atcelt
Saglabāt