From 87de28636a983d7e8c920205ae05a9914e24a68b Mon Sep 17 00:00:00 2001 From: 9seconds Date: Tue, 3 Dec 2019 10:02:56 +0300 Subject: [PATCH] Add new metrics --- faketls/client_protocol.go | 2 ++ proxy/proxy.go | 2 ++ stats/interfaces.go | 10 ++++++++++ stats/multi_stats.go | 12 ++++++++++++ stats/stats_prometheus.go | 32 +++++++++++++++++++++++++++----- stats/stats_statsd.go | 8 ++++++++ 6 files changed, 61 insertions(+), 5 deletions(-) diff --git a/faketls/client_protocol.go b/faketls/client_protocol.go index 418bc8e..b2fa024 100644 --- a/faketls/client_protocol.go +++ b/faketls/client_protocol.go @@ -101,6 +101,8 @@ func (c *ClientProtocol) tlsHandshake(conn io.ReadWriter) error { } func (c *ClientProtocol) cloakHost(clientConn io.ReadWriteCloser) { + stats.Stats.CloakedRequest() + addr := net.JoinHostPort(config.C.CloakHost, strconv.Itoa(config.C.CloakPort)) hostConn, err := net.Dial("tcp", addr) diff --git a/proxy/proxy.go b/proxy/proxy.go index 809482e..a1f0d43 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -69,7 +69,9 @@ func (p *Proxy) accept(conn net.Conn) { clientConn, err := clientProtocol.Handshake(clientConn) if err != nil { + stats.Stats.AuthenticationFailed() logger.Warnw("Cannot perform client handshake", "error", err) + return } diff --git a/stats/interfaces.go b/stats/interfaces.go index 0672982..083a8ea 100644 --- a/stats/interfaces.go +++ b/stats/interfaces.go @@ -38,6 +38,14 @@ type ReplayDetectedInterface interface { ReplayDetected() } +type AuthenticationFailedInterface interface { + AuthenticationFailed() +} + +type CloakedRequestInterface interface { + CloakedRequest() +} + type Interface interface { IngressTrafficInterface EgressTrafficInterface @@ -47,4 +55,6 @@ type Interface interface { TelegramDisconnectedInterface CrashInterface ReplayDetectedInterface + AuthenticationFailedInterface + CloakedRequestInterface } diff --git a/stats/multi_stats.go b/stats/multi_stats.go index f87ee75..569c93c 100644 --- a/stats/multi_stats.go +++ b/stats/multi_stats.go @@ -55,3 +55,15 @@ func (m multiStats) ReplayDetected() { go m[i].ReplayDetected() } } + +func (m multiStats) AuthenticationFailed() { + for i := range m { + go m[i].AuthenticationFailed() + } +} + +func (m multiStats) CloakedRequest() { + for i := range m { + go m[i].CloakedRequest() + } +} diff --git a/stats/stats_prometheus.go b/stats/stats_prometheus.go index ec589e5..7bddbb0 100644 --- a/stats/stats_prometheus.go +++ b/stats/stats_prometheus.go @@ -13,11 +13,13 @@ import ( ) type statsPrometheus struct { - connections *prometheus.GaugeVec - telegramConnections *prometheus.GaugeVec - traffic *prometheus.GaugeVec - crashes prometheus.Counter - replayAttacks prometheus.Counter + connections *prometheus.GaugeVec + telegramConnections *prometheus.GaugeVec + traffic *prometheus.GaugeVec + crashes prometheus.Counter + replayAttacks prometheus.Counter + authenticationFailed prometheus.Counter + cloakedRequests prometheus.Counter } func (s *statsPrometheus) IngressTraffic(traffic int) { @@ -87,6 +89,14 @@ func (s *statsPrometheus) ReplayDetected() { s.replayAttacks.Inc() } +func (s *statsPrometheus) AuthenticationFailed() { + s.authenticationFailed.Inc() +} + +func (s *statsPrometheus) CloakedRequest() { + s.cloakedRequests.Inc() +} + func newStatsPrometheus(mux *http.ServeMux) Interface { registry := prometheus.NewPedanticRegistry() @@ -116,6 +126,16 @@ func newStatsPrometheus(mux *http.ServeMux) Interface { Name: "replay_attacks", Help: "How many replay attacks were prevented.", }), + authenticationFailed: prometheus.NewCounter(prometheus.CounterOpts{ + Namespace: config.C.StatsNamespace, + Name: "authentication_failed", + Help: "How many authentication failed events we've seen.", + }), + cloakedRequests: prometheus.NewCounter(prometheus.CounterOpts{ + Namespace: config.C.StatsNamespace, + Name: "cloaked_requests", + Help: "How many requests were proxified during cloaking.", + }), } registry.MustRegister(instance.connections) @@ -123,6 +143,8 @@ func newStatsPrometheus(mux *http.ServeMux) Interface { registry.MustRegister(instance.traffic) registry.MustRegister(instance.crashes) registry.MustRegister(instance.replayAttacks) + registry.MustRegister(instance.authenticationFailed) + registry.MustRegister(instance.cloakedRequests) handler := promhttp.HandlerFor(registry, promhttp.HandlerOpts{}) mux.Handle("/", handler) diff --git a/stats/stats_statsd.go b/stats/stats_statsd.go index ed9ea88..4c89344 100644 --- a/stats/stats_statsd.go +++ b/stats/stats_statsd.go @@ -137,6 +137,14 @@ func (s *statsStatsd) ReplayDetected() { s.gauge("replay_attacks", 1) } +func (s *statsStatsd) AuthenticationFailed() { + s.gauge("authentication_failed", 1) +} + +func (s *statsStatsd) CloakedRequest() { + s.gauge("cloaked_requests", 1) +} + func (s *statsStatsd) gauge(metric string, value int64, tags ...*statsStatsdTag) { key, tagList := s.prepareVals(metric, tags) s.initGauge(metric, key, tagList)