Explorar el Código

Merge pull request #122 from 9seconds/close-connections

Make cloaking more robust
tags/v1.0.2^2
Sergey Arkhipov hace 6 años
padre
commit
8a783d6b4e
No account linked to committer's email address
Se han modificado 10 ficheros con 206 adiciones y 51 borrados
  1. 3
    20
      faketls/client_protocol.go
  2. 71
    0
      faketls/cloak.go
  3. 8
    7
      go.mod
  4. 17
    19
      go.sum
  5. 2
    0
      proxy/proxy.go
  6. 10
    0
      stats/interfaces.go
  7. 12
    0
      stats/multi_stats.go
  8. 27
    5
      stats/stats_prometheus.go
  9. 8
    0
      stats/stats_statsd.go
  10. 48
    0
      wrappers/rwc/ping.go

+ 3
- 20
faketls/client_protocol.go Ver fichero

@@ -8,7 +8,6 @@ import (
8 8
 	"io"
9 9
 	"net"
10 10
 	"strconv"
11
-	"sync"
12 11
 	"time"
13 12
 
14 13
 	"github.com/9seconds/mtg/antireplay"
@@ -101,6 +100,8 @@ func (c *ClientProtocol) tlsHandshake(conn io.ReadWriter) error {
101 100
 }
102 101
 
103 102
 func (c *ClientProtocol) cloakHost(clientConn io.ReadWriteCloser) {
103
+	stats.Stats.CloakedRequest()
104
+
104 105
 	addr := net.JoinHostPort(config.C.CloakHost, strconv.Itoa(config.C.CloakPort))
105 106
 	hostConn, err := net.Dial("tcp", addr)
106 107
 
@@ -108,25 +109,7 @@ func (c *ClientProtocol) cloakHost(clientConn io.ReadWriteCloser) {
108 109
 		return
109 110
 	}
110 111
 
111
-	defer hostConn.Close()
112
-
113
-	wg := &sync.WaitGroup{}
114
-	wg.Add(2)
115
-
116
-	go c.pipe(hostConn, clientConn, wg)
117
-
118
-	go c.pipe(clientConn, hostConn, wg)
119
-
120
-	wg.Wait()
121
-}
122
-
123
-func (c *ClientProtocol) pipe(dst io.WriteCloser, src io.Reader, wg *sync.WaitGroup) {
124
-	defer func() {
125
-		wg.Done()
126
-		dst.Close()
127
-	}()
128
-
129
-	io.Copy(dst, src) // nolint: errcheck
112
+	cloak(clientConn, hostConn)
130 113
 }
131 114
 
132 115
 func MakeClientProtocol() protocol.ClientProtocol {

+ 71
- 0
faketls/cloak.go Ver fichero

@@ -0,0 +1,71 @@
1
+package faketls
2
+
3
+import (
4
+	"context"
5
+	"io"
6
+	"sync"
7
+	"time"
8
+
9
+	"github.com/9seconds/mtg/wrappers/rwc"
10
+)
11
+
12
+const (
13
+	cloakLastActivityTimeout = 5 * time.Second
14
+	cloakMaxTimeout          = 30 * time.Second
15
+)
16
+
17
+func cloak(one, another io.ReadWriteCloser) {
18
+	defer func() {
19
+		one.Close()
20
+		another.Close()
21
+	}()
22
+
23
+	channelPing := make(chan struct{}, 1)
24
+	ctx, cancel := context.WithCancel(context.Background())
25
+	one = rwc.NewPing(ctx, one, channelPing)
26
+	another = rwc.NewPing(ctx, another, channelPing)
27
+	wg := &sync.WaitGroup{}
28
+
29
+	wg.Add(2)
30
+
31
+	go func() {
32
+		defer wg.Done()
33
+		io.Copy(one, another) // nolint: errcheck
34
+	}()
35
+
36
+	go func() {
37
+		defer wg.Done()
38
+		io.Copy(another, one) // nolint: errcheck
39
+	}()
40
+
41
+	go func() {
42
+		wg.Wait()
43
+		cancel()
44
+	}()
45
+
46
+	go func() {
47
+		lastActivityTimer := time.NewTimer(cloakLastActivityTimeout)
48
+		defer lastActivityTimer.Stop()
49
+
50
+		maxTimer := time.NewTimer(cloakMaxTimeout)
51
+		defer maxTimer.Stop()
52
+
53
+		for {
54
+			select {
55
+			case <-channelPing:
56
+				lastActivityTimer.Stop()
57
+				lastActivityTimer = time.NewTimer(cloakLastActivityTimeout)
58
+			case <-ctx.Done():
59
+				return
60
+			case <-lastActivityTimer.C:
61
+				cancel()
62
+				return
63
+			case <-maxTimer.C:
64
+				cancel()
65
+				return
66
+			}
67
+		}
68
+	}()
69
+
70
+	<-ctx.Done()
71
+}

+ 8
- 7
go.mod Ver fichero

@@ -3,19 +3,20 @@ module github.com/9seconds/mtg
3 3
 go 1.13
4 4
 
5 5
 require (
6
-	github.com/VictoriaMetrics/fastcache v1.5.2
6
+	github.com/VictoriaMetrics/fastcache v1.5.4
7 7
 	github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d
8 8
 	github.com/beevik/ntp v0.2.0
9
-	github.com/cespare/xxhash/v2 v2.1.1 // indirect
10 9
 	github.com/prometheus/client_golang v1.2.1
11
-	github.com/prometheus/procfs v0.0.7 // indirect
10
+	github.com/prometheus/client_model v0.0.0-20191202183732-d1d2010b5bee // indirect
11
+	github.com/prometheus/procfs v0.0.8 // indirect
12 12
 	github.com/smira/go-statsd v1.3.1
13 13
 	go.uber.org/atomic v1.5.1 // indirect
14 14
 	go.uber.org/multierr v1.4.0 // indirect
15 15
 	go.uber.org/zap v1.13.0
16
-	golang.org/x/crypto v0.0.0-20191122220453-ac88ee75c92c
17
-	golang.org/x/net v0.0.0-20191124235446-72fef5d5e266 // indirect
18
-	golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e
19
-	golang.org/x/tools v0.0.0-20191125011157-cc15fab314e3 // indirect
16
+	golang.org/x/crypto v0.0.0-20191202143827-86a70503ff7e
17
+	golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f // indirect
18
+	golang.org/x/net v0.0.0-20191126235420-ef20fe5d7933 // indirect
19
+	golang.org/x/sys v0.0.0-20191128015809-6d18c012aee9
20
+	golang.org/x/tools v0.0.0-20191203051722-db047d72ee39 // indirect
20 21
 	gopkg.in/alecthomas/kingpin.v2 v2.2.6
21 22
 )

+ 17
- 19
go.sum Ver fichero

@@ -1,9 +1,7 @@
1 1
 github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
2 2
 github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
3
-github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
4
-github.com/OneOfOne/xxhash v1.2.5/go.mod h1:eZbhyaAYD41SGSSsnmcpxVoRiQ/MPUTjUdIIOT9Um7Q=
5
-github.com/VictoriaMetrics/fastcache v1.5.2 h1:Erd8iIuBAL9kke8JzM4+WxkKuFkHh3ktwLanJvDgR44=
6
-github.com/VictoriaMetrics/fastcache v1.5.2/go.mod h1:+jv9Ckb+za/P1ZRg/sulP5Ni1v49daAVERr0H3CuscE=
3
+github.com/VictoriaMetrics/fastcache v1.5.4 h1:0BaXbRH01RycJk79OOBwMCXlNryko9z4yEf6RqbP+Xo=
4
+github.com/VictoriaMetrics/fastcache v1.5.4/go.mod h1:ptDBkNMQI4RtmVo8VS/XwRY6RoTu1dAWCbrk+6WsEM8=
7 5
 github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc h1:cAKDfWh5VpdgMhJosfJnn5/FoN2SRZ4p7fJNX58YPaU=
8 6
 github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
9 7
 github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafoB+tBA3gMyHYHrpOtNuDiK/uB5uXxq5wM=
@@ -24,9 +22,6 @@ github.com/beorn7/perks v1.0.0 h1:HWo1m869IqiPhD389kmkxeTalrjNbbJTC8LXupb+sl0=
24 22
 github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
25 23
 github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
26 24
 github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
27
-github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
28
-github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
29
-github.com/cespare/xxhash/v2 v2.0.1-0.20190104013014-3767db7a7e18/go.mod h1:HD5P3vAIAh+Y2GAxg0PrPN1P8WkepXGpjbUPDHJqqKM=
30 25
 github.com/cespare/xxhash/v2 v2.1.0 h1:yTUvW7Vhb89inJ+8irsUqiWjh8iT6sQPZiQzI6ReGkA=
31 26
 github.com/cespare/xxhash/v2 v2.1.0/go.mod h1:dgIUBU3pDso/gPgZ1osOZ0iQf77oPR28Tjxl5dIMyVM=
32 27
 github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
@@ -89,6 +84,8 @@ github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90 h1:S/YWwWx
89 84
 github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
90 85
 github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4 h1:gQz4mCbXsO+nc9n1hCxHcGA3Zx3Eo+UHZoInFGUIXNM=
91 86
 github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
87
+github.com/prometheus/client_model v0.0.0-20191202183732-d1d2010b5bee h1:iBZPTYkGLvdu6+A5TsMUJQkQX9Ad4aCEnSQtdxPuTCQ=
88
+github.com/prometheus/client_model v0.0.0-20191202183732-d1d2010b5bee/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
92 89
 github.com/prometheus/common v0.4.1 h1:K0MGApIoQvMw27RTdJkPbr3JZ7DNbtxQNyi5STVM6Kw=
93 90
 github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
94 91
 github.com/prometheus/common v0.7.0 h1:L+1lyG48J1zAQXA3RBX/nG/B3gjlHq0zTt2tlbJLyCY=
@@ -99,15 +96,13 @@ github.com/prometheus/procfs v0.0.2 h1:6LJUbpNm42llc4HRCuvApCSWB/WfhuNo9K98Q9sNG
99 96
 github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
100 97
 github.com/prometheus/procfs v0.0.5 h1:3+auTFlqw+ZaQYJARz6ArODtkaIwtvBTx3N2NehQlL8=
101 98
 github.com/prometheus/procfs v0.0.5/go.mod h1:4A/X28fw3Fc593LaREMrKMqOKvUAntwMDaekg4FpcdQ=
102
-github.com/prometheus/procfs v0.0.7 h1:RS5GAlMbnkWkhs4+bPocMTmGjYkuCY5djjqEDdXOhcQ=
103
-github.com/prometheus/procfs v0.0.7/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+GxbHq6oeK9A=
99
+github.com/prometheus/procfs v0.0.8 h1:+fpWZdT24pJBiqJdAwYBjPSk+5YmQzYNPYzQsdzLkt8=
100
+github.com/prometheus/procfs v0.0.8/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+GxbHq6oeK9A=
104 101
 github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
105 102
 github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
106 103
 github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
107 104
 github.com/smira/go-statsd v1.3.1 h1:JalGiHNdK7GqVAPpg7j0Kwp2jZrz/fCg/B4ZuNuBY2w=
108 105
 github.com/smira/go-statsd v1.3.1/go.mod h1:1srXJ9/pbnN04G8f4F1jUzsGOnwkPKXciyqpewGlkC4=
109
-github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
110
-github.com/spaolacci/murmur3 v1.0.1-0.20190317074736-539464a789e9/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
111 106
 github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
112 107
 github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
113 108
 github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
@@ -131,18 +126,20 @@ golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnf
131 126
 golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
132 127
 golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
133 128
 golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
134
-golang.org/x/crypto v0.0.0-20191122220453-ac88ee75c92c h1:/nJuwDLoL/zrqY6gf57vxC+Pi+pZ8bfhpPkicO5H7W4=
135
-golang.org/x/crypto v0.0.0-20191122220453-ac88ee75c92c/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
129
+golang.org/x/crypto v0.0.0-20191202143827-86a70503ff7e h1:egKlR8l7Nu9vHGWbcUV8lqR4987UfUbBd7GbhqGzNYU=
130
+golang.org/x/crypto v0.0.0-20191202143827-86a70503ff7e/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
136 131
 golang.org/x/lint v0.0.0-20190930215403-16217165b5de h1:5hukYrvBGR8/eNkX5mdUezrA6JiaEZDtJb9Ei+1LlBs=
137 132
 golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
133
+golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f h1:J5lckAjkw6qYlOZNj90mLYNTEKDvWeuc1yieZ8qUzUE=
134
+golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f/go.mod h1:5qLYkcX4OjUUV8bRuDixDT3tpyyb+LUpUlRWLxfhWrs=
138 135
 golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
139 136
 golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
140 137
 golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
141 138
 golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
142 139
 golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
143 140
 golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
144
-golang.org/x/net v0.0.0-20191124235446-72fef5d5e266 h1:QuOiA7GCO0OSDzlNlFyOWOywDsjuzW8M2yvBfCqw+cY=
145
-golang.org/x/net v0.0.0-20191124235446-72fef5d5e266/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
141
+golang.org/x/net v0.0.0-20191126235420-ef20fe5d7933 h1:e6HwijUxhDe+hPNjZQQn9bA5PW3vNmnN64U2ZW759Lk=
142
+golang.org/x/net v0.0.0-20191126235420-ef20fe5d7933/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
146 143
 golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
147 144
 golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 h1:YUO/7uOKsKeq9UokNS62b8FYywz3ker1l1vDZRCRefw=
148 145
 golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -155,16 +152,17 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w
155 152
 golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
156 153
 golang.org/x/sys v0.0.0-20191010194322-b09406accb47 h1:/XfQ9z7ib8eEJX2hdgFTZJ/ntt0swNk5oYBziWeTCvY=
157 154
 golang.org/x/sys v0.0.0-20191010194322-b09406accb47/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
158
-golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e h1:N7DeIrjYszNmSW409R3frPPwglRwMkXSBzwVbkOjLLA=
159
-golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
155
+golang.org/x/sys v0.0.0-20191128015809-6d18c012aee9 h1:ZBzSG/7F4eNKz2L3GE9o300RX0Az1Bw5HF7PDraD+qU=
156
+golang.org/x/sys v0.0.0-20191128015809-6d18c012aee9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
160 157
 golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
161 158
 golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
162 159
 golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
163 160
 golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
164 161
 golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5 h1:hKsoRgsbwY1NafxrwTs+k64bikrLBkAgPir1TNCj3Zs=
165 162
 golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
166
-golang.org/x/tools v0.0.0-20191125011157-cc15fab314e3 h1:aHkNOJLg6a84bdLJN1yjqMSTadeAuaudhEPNSkLAWoA=
167
-golang.org/x/tools v0.0.0-20191125011157-cc15fab314e3/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
163
+golang.org/x/tools v0.0.0-20191125144606-a911d9008d1f/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
164
+golang.org/x/tools v0.0.0-20191203051722-db047d72ee39 h1:zARK4PTmTfx1BC6iKP21qIRjz0nFzFj4ZAlbUy6Q6pM=
165
+golang.org/x/tools v0.0.0-20191203051722-db047d72ee39/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
168 166
 golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
169 167
 gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc=
170 168
 gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=

+ 2
- 0
proxy/proxy.go Ver fichero

@@ -69,7 +69,9 @@ func (p *Proxy) accept(conn net.Conn) {
69 69
 	clientConn, err := clientProtocol.Handshake(clientConn)
70 70
 
71 71
 	if err != nil {
72
+		stats.Stats.AuthenticationFailed()
72 73
 		logger.Warnw("Cannot perform client handshake", "error", err)
74
+
73 75
 		return
74 76
 	}
75 77
 

+ 10
- 0
stats/interfaces.go Ver fichero

@@ -38,6 +38,14 @@ type ReplayDetectedInterface interface {
38 38
 	ReplayDetected()
39 39
 }
40 40
 
41
+type AuthenticationFailedInterface interface {
42
+	AuthenticationFailed()
43
+}
44
+
45
+type CloakedRequestInterface interface {
46
+	CloakedRequest()
47
+}
48
+
41 49
 type Interface interface {
42 50
 	IngressTrafficInterface
43 51
 	EgressTrafficInterface
@@ -47,4 +55,6 @@ type Interface interface {
47 55
 	TelegramDisconnectedInterface
48 56
 	CrashInterface
49 57
 	ReplayDetectedInterface
58
+	AuthenticationFailedInterface
59
+	CloakedRequestInterface
50 60
 }

+ 12
- 0
stats/multi_stats.go Ver fichero

@@ -55,3 +55,15 @@ func (m multiStats) ReplayDetected() {
55 55
 		go m[i].ReplayDetected()
56 56
 	}
57 57
 }
58
+
59
+func (m multiStats) AuthenticationFailed() {
60
+	for i := range m {
61
+		go m[i].AuthenticationFailed()
62
+	}
63
+}
64
+
65
+func (m multiStats) CloakedRequest() {
66
+	for i := range m {
67
+		go m[i].CloakedRequest()
68
+	}
69
+}

+ 27
- 5
stats/stats_prometheus.go Ver fichero

@@ -13,11 +13,13 @@ import (
13 13
 )
14 14
 
15 15
 type statsPrometheus struct {
16
-	connections         *prometheus.GaugeVec
17
-	telegramConnections *prometheus.GaugeVec
18
-	traffic             *prometheus.GaugeVec
19
-	crashes             prometheus.Counter
20
-	replayAttacks       prometheus.Counter
16
+	connections          *prometheus.GaugeVec
17
+	telegramConnections  *prometheus.GaugeVec
18
+	traffic              *prometheus.GaugeVec
19
+	crashes              prometheus.Counter
20
+	replayAttacks        prometheus.Counter
21
+	authenticationFailed prometheus.Counter
22
+	cloakedRequests      prometheus.Counter
21 23
 }
22 24
 
23 25
 func (s *statsPrometheus) IngressTraffic(traffic int) {
@@ -87,6 +89,14 @@ func (s *statsPrometheus) ReplayDetected() {
87 89
 	s.replayAttacks.Inc()
88 90
 }
89 91
 
92
+func (s *statsPrometheus) AuthenticationFailed() {
93
+	s.authenticationFailed.Inc()
94
+}
95
+
96
+func (s *statsPrometheus) CloakedRequest() {
97
+	s.cloakedRequests.Inc()
98
+}
99
+
90 100
 func newStatsPrometheus(mux *http.ServeMux) Interface {
91 101
 	registry := prometheus.NewPedanticRegistry()
92 102
 
@@ -116,6 +126,16 @@ func newStatsPrometheus(mux *http.ServeMux) Interface {
116 126
 			Name:      "replay_attacks",
117 127
 			Help:      "How many replay attacks were prevented.",
118 128
 		}),
129
+		authenticationFailed: prometheus.NewCounter(prometheus.CounterOpts{
130
+			Namespace: config.C.StatsNamespace,
131
+			Name:      "authentication_failed",
132
+			Help:      "How many authentication failed events we've seen.",
133
+		}),
134
+		cloakedRequests: prometheus.NewCounter(prometheus.CounterOpts{
135
+			Namespace: config.C.StatsNamespace,
136
+			Name:      "cloaked_requests",
137
+			Help:      "How many requests were proxified during cloaking.",
138
+		}),
119 139
 	}
120 140
 
121 141
 	registry.MustRegister(instance.connections)
@@ -123,6 +143,8 @@ func newStatsPrometheus(mux *http.ServeMux) Interface {
123 143
 	registry.MustRegister(instance.traffic)
124 144
 	registry.MustRegister(instance.crashes)
125 145
 	registry.MustRegister(instance.replayAttacks)
146
+	registry.MustRegister(instance.authenticationFailed)
147
+	registry.MustRegister(instance.cloakedRequests)
126 148
 
127 149
 	handler := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
128 150
 	mux.Handle("/", handler)

+ 8
- 0
stats/stats_statsd.go Ver fichero

@@ -137,6 +137,14 @@ func (s *statsStatsd) ReplayDetected() {
137 137
 	s.gauge("replay_attacks", 1)
138 138
 }
139 139
 
140
+func (s *statsStatsd) AuthenticationFailed() {
141
+	s.gauge("authentication_failed", 1)
142
+}
143
+
144
+func (s *statsStatsd) CloakedRequest() {
145
+	s.gauge("cloaked_requests", 1)
146
+}
147
+
140 148
 func (s *statsStatsd) gauge(metric string, value int64, tags ...*statsStatsdTag) {
141 149
 	key, tagList := s.prepareVals(metric, tags)
142 150
 	s.initGauge(metric, key, tagList)

+ 48
- 0
wrappers/rwc/ping.go Ver fichero

@@ -0,0 +1,48 @@
1
+package rwc
2
+
3
+import (
4
+	"context"
5
+	"io"
6
+)
7
+
8
+type wrapperPing struct {
9
+	parent      io.ReadWriteCloser
10
+	ctx         context.Context
11
+	channelPing chan<- struct{}
12
+}
13
+
14
+func (w *wrapperPing) Read(p []byte) (int, error) {
15
+	n, err := w.parent.Read(p)
16
+	if err == nil {
17
+		select {
18
+		case <-w.ctx.Done():
19
+		case w.channelPing <- struct{}{}:
20
+		}
21
+	}
22
+
23
+	return n, err
24
+}
25
+
26
+func (w *wrapperPing) Write(p []byte) (int, error) {
27
+	n, err := w.parent.Write(p)
28
+	if err == nil {
29
+		select {
30
+		case <-w.ctx.Done():
31
+		case w.channelPing <- struct{}{}:
32
+		}
33
+	}
34
+
35
+	return n, err
36
+}
37
+
38
+func (w *wrapperPing) Close() error {
39
+	return w.parent.Close()
40
+}
41
+
42
+func NewPing(ctx context.Context, parent io.ReadWriteCloser, channelPing chan<- struct{}) io.ReadWriteCloser {
43
+	return &wrapperPing{
44
+		parent:      parent,
45
+		ctx:         ctx,
46
+		channelPing: channelPing,
47
+	}
48
+}

Loading…
Cancelar
Guardar