mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-09-01 03:04:02 +03:00
Direct proxy works
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
package stats
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/9seconds/mtg/mtproto"
|
||||
)
|
||||
|
||||
const (
|
||||
connectionsChanLength = 10
|
||||
trafficChanLength = 10
|
||||
)
|
||||
|
||||
var (
|
||||
crashesChan = make(chan struct{})
|
||||
statsChan = make(chan chan<- Stats)
|
||||
connectionsChan = make(chan connectionData, connectionsChanLength)
|
||||
trafficChan = make(chan trafficData, trafficChanLength)
|
||||
)
|
||||
|
||||
type connectionData struct {
|
||||
connectionType mtproto.ConnectionType
|
||||
connected bool
|
||||
addr *net.TCPAddr
|
||||
}
|
||||
|
||||
type trafficData struct {
|
||||
traffic int
|
||||
ingress bool
|
||||
}
|
||||
|
||||
// NewCrash indicates new crash.
|
||||
func NewCrash() {
|
||||
crashesChan <- struct{}{}
|
||||
}
|
||||
|
||||
// ClientConnected indicates that new client was connected.
|
||||
func ClientConnected(connectionType mtproto.ConnectionType, addr *net.TCPAddr) {
|
||||
connectionsChan <- connectionData{
|
||||
connectionType: connectionType,
|
||||
addr: addr,
|
||||
connected: true,
|
||||
}
|
||||
}
|
||||
|
||||
// ClientDisconnected indicates that client was disconnected.
|
||||
func ClientDisconnected(connectionType mtproto.ConnectionType, addr *net.TCPAddr) {
|
||||
connectionsChan <- connectionData{
|
||||
connectionType: connectionType,
|
||||
addr: addr,
|
||||
connected: false,
|
||||
}
|
||||
}
|
||||
|
||||
// IngressTraffic accounts new ingress traffic.
|
||||
func IngressTraffic(traffic int) {
|
||||
trafficChan <- trafficData{
|
||||
traffic: traffic,
|
||||
ingress: true,
|
||||
}
|
||||
}
|
||||
|
||||
// EgressTraffic accounts new ingress traffic.
|
||||
func EgressTraffic(traffic int) {
|
||||
trafficChan <- trafficData{
|
||||
traffic: traffic,
|
||||
ingress: false,
|
||||
}
|
||||
}
|
||||
|
||||
// GetStats returns a snapshot of Stats instance.
|
||||
func GetStats() Stats {
|
||||
rpcChan := make(chan Stats)
|
||||
statsChan <- rpcChan
|
||||
return <-rpcChan
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package stats
|
||||
|
||||
import (
|
||||
"github.com/juju/errors"
|
||||
|
||||
"github.com/9seconds/mtg/config"
|
||||
)
|
||||
|
||||
// Init initializes stats subsystem.
|
||||
func Init(conf *config.Config) error {
|
||||
if conf.StatsD.Enabled {
|
||||
client, err := newStatsd(conf)
|
||||
if err != nil {
|
||||
return errors.Annotate(err, "Cannot initialize statsd client")
|
||||
}
|
||||
go client.run()
|
||||
}
|
||||
prometheus, err := newPrometheus(conf)
|
||||
if err != nil {
|
||||
return errors.Annotate(err, "Cannot initialize prometheus client")
|
||||
}
|
||||
go prometheus.run()
|
||||
|
||||
go NewStats(conf).start()
|
||||
go startServer(conf, prometheus.getHTTPHandler())
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package stats
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/juju/errors"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
|
||||
"github.com/9seconds/mtg/config"
|
||||
)
|
||||
|
||||
const prometheusPollTime = time.Second
|
||||
|
||||
type prometheusExporter struct {
|
||||
registry prometheus.Gatherer
|
||||
|
||||
connections *prometheus.GaugeVec
|
||||
traffic *prometheus.GaugeVec
|
||||
speed *prometheus.GaugeVec
|
||||
crashes prometheus.Gauge
|
||||
}
|
||||
|
||||
func (p *prometheusExporter) run() {
|
||||
for range time.Tick(prometheusPollTime) {
|
||||
instance := GetStats()
|
||||
|
||||
p.connections.WithLabelValues("abridged", "v4").Set(float64(instance.Connections.Abridged.IPv4))
|
||||
p.connections.WithLabelValues("abridged", "v6").Set(float64(instance.Connections.Abridged.IPv6))
|
||||
p.connections.WithLabelValues("intermediate", "v4").Set(float64(instance.Connections.Intermediate.IPv4))
|
||||
p.connections.WithLabelValues("intermediate", "v6").Set(float64(instance.Connections.Intermediate.IPv6))
|
||||
p.connections.WithLabelValues("secure", "v4").Set(float64(instance.Connections.Secure.IPv4))
|
||||
p.connections.WithLabelValues("secure", "v6").Set(float64(instance.Connections.Secure.IPv6))
|
||||
p.traffic.WithLabelValues("ingress").Set(float64(instance.Traffic.ingress))
|
||||
p.traffic.WithLabelValues("egress").Set(float64(instance.Traffic.egress))
|
||||
p.speed.WithLabelValues("ingress").Set(float64(instance.Speed.ingress))
|
||||
p.speed.WithLabelValues("egress").Set(float64(instance.Speed.egress))
|
||||
p.crashes.Set(float64(instance.Crashes))
|
||||
}
|
||||
}
|
||||
|
||||
func (p *prometheusExporter) getHTTPHandler() http.Handler {
|
||||
return promhttp.HandlerFor(p.registry, promhttp.HandlerOpts{})
|
||||
}
|
||||
|
||||
func newPrometheus(conf *config.Config) (*prometheusExporter, error) {
|
||||
registry := prometheus.NewRegistry()
|
||||
|
||||
connections := prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
||||
Namespace: conf.Prometheus.Prefix,
|
||||
Name: "connections",
|
||||
Help: "Current number of connections to the proxy.",
|
||||
}, []string{"type", "protocol"})
|
||||
traffic := prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
||||
Namespace: conf.Prometheus.Prefix,
|
||||
Name: "traffic",
|
||||
Help: "Traffic passed through the proxy in bytes.",
|
||||
}, []string{"direction"})
|
||||
speed := prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
||||
Namespace: conf.Prometheus.Prefix,
|
||||
Name: "speed",
|
||||
Help: "Current throughput in bytes per second.",
|
||||
}, []string{"direction"})
|
||||
crashes := prometheus.NewGauge(prometheus.GaugeOpts{
|
||||
Namespace: conf.Prometheus.Prefix,
|
||||
Name: "crashes",
|
||||
Help: "How many crashes happened.",
|
||||
})
|
||||
|
||||
if err := registry.Register(connections); err != nil {
|
||||
return nil, errors.Annotate(err, "Cannot register connections collector")
|
||||
}
|
||||
if err := registry.Register(traffic); err != nil {
|
||||
return nil, errors.Annotate(err, "cannot register traffic collector")
|
||||
}
|
||||
if err := registry.Register(speed); err != nil {
|
||||
return nil, errors.Annotate(err, "cannot register speed collector")
|
||||
}
|
||||
if err := registry.Register(crashes); err != nil {
|
||||
return nil, errors.Annotate(err, "cannot register crashes collector")
|
||||
}
|
||||
|
||||
return &prometheusExporter{
|
||||
registry: registry,
|
||||
connections: connections,
|
||||
traffic: traffic,
|
||||
speed: speed,
|
||||
crashes: crashes,
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package stats
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/9seconds/mtg/config"
|
||||
)
|
||||
|
||||
func startServer(conf *config.Config, prometheusHandler http.Handler) {
|
||||
log := zap.S().Named("stats")
|
||||
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
first, err := json.Marshal(GetStats())
|
||||
if err != nil {
|
||||
log.Errorw("Cannot encode json", "error", err)
|
||||
http.Error(w, "Internal server error", 500)
|
||||
return
|
||||
}
|
||||
|
||||
interim := map[string]interface{}{}
|
||||
json.Unmarshal(first, &interim) // nolint: errcheck, gosec
|
||||
|
||||
encoder := json.NewEncoder(w)
|
||||
encoder.SetEscapeHTML(false)
|
||||
encoder.SetIndent("", " ")
|
||||
if err = encoder.Encode(interim); err != nil {
|
||||
log.Errorw("Cannot encode json", "error", err)
|
||||
}
|
||||
})
|
||||
http.Handle("/prometheus/", prometheusHandler)
|
||||
|
||||
if err := http.ListenAndServe(conf.StatAddr(), nil); err != nil {
|
||||
log.Fatalw("Stats server has been stopped", "error", err)
|
||||
}
|
||||
}
|
||||
+175
@@ -0,0 +1,175 @@
|
||||
package stats
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
humanize "github.com/dustin/go-humanize"
|
||||
|
||||
"github.com/9seconds/mtg/config"
|
||||
"github.com/9seconds/mtg/mtproto"
|
||||
)
|
||||
|
||||
type uptime time.Time
|
||||
|
||||
func (u uptime) MarshalJSON() ([]byte, error) {
|
||||
duration := time.Since(time.Time(u))
|
||||
value := map[string]string{
|
||||
"seconds": strconv.Itoa(int(duration.Seconds())),
|
||||
"human": humanize.Time(time.Time(u)),
|
||||
}
|
||||
|
||||
return json.Marshal(value)
|
||||
}
|
||||
|
||||
type connectionType struct {
|
||||
IPv6 uint32 `json:"ipv6"`
|
||||
IPv4 uint32 `json:"ipv4"`
|
||||
}
|
||||
|
||||
type baseConnections struct {
|
||||
All connectionType `json:"all"`
|
||||
Abridged connectionType `json:"abridged"`
|
||||
Intermediate connectionType `json:"intermediate"`
|
||||
Secure connectionType `json:"secure"`
|
||||
}
|
||||
|
||||
type connections struct {
|
||||
baseConnections
|
||||
}
|
||||
|
||||
func (c connections) MarshalJSON() ([]byte, error) {
|
||||
c.All.IPv4 = c.Abridged.IPv4 + c.Intermediate.IPv4 + c.Secure.IPv4
|
||||
c.All.IPv6 = c.Abridged.IPv6 + c.Intermediate.IPv6 + c.Secure.IPv6
|
||||
|
||||
return json.Marshal(c.baseConnections)
|
||||
}
|
||||
|
||||
type traffic struct {
|
||||
ingress uint64
|
||||
egress uint64
|
||||
}
|
||||
|
||||
func (t *traffic) dumpValue(value uint64) map[string]interface{} {
|
||||
return map[string]interface{}{
|
||||
"bytes": value,
|
||||
"human": humanize.Bytes(value),
|
||||
}
|
||||
}
|
||||
|
||||
func (t traffic) MarshalJSON() ([]byte, error) {
|
||||
value := map[string]map[string]interface{}{
|
||||
"ingress": t.dumpValue(t.ingress),
|
||||
"egress": t.dumpValue(t.egress),
|
||||
}
|
||||
|
||||
return json.Marshal(value)
|
||||
}
|
||||
|
||||
type speed struct {
|
||||
ingress uint64
|
||||
egress uint64
|
||||
}
|
||||
|
||||
func (s *speed) dumpValue(value uint64) map[string]interface{} {
|
||||
return map[string]interface{}{
|
||||
"bytes/s": value,
|
||||
"human": fmt.Sprintf("%s/s", humanize.Bytes(value)),
|
||||
}
|
||||
}
|
||||
|
||||
func (s speed) MarshalJSON() ([]byte, error) {
|
||||
value := map[string]map[string]interface{}{
|
||||
"ingress": s.dumpValue(s.ingress),
|
||||
"egress": s.dumpValue(s.egress),
|
||||
}
|
||||
|
||||
return json.Marshal(value)
|
||||
}
|
||||
|
||||
// Stats represents a statistics of the proxy.
|
||||
type Stats struct {
|
||||
URLs config.IPURLs `json:"urls"`
|
||||
Connections connections `json:"connections"`
|
||||
Traffic traffic `json:"traffic"`
|
||||
Speed speed `json:"speed"`
|
||||
Uptime uptime `json:"uptime"`
|
||||
Crashes uint32 `json:"crashes"`
|
||||
|
||||
previousTraffic traffic
|
||||
}
|
||||
|
||||
func (s *Stats) start() {
|
||||
speedChan := time.Tick(time.Second)
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-speedChan:
|
||||
s.handleSpeed()
|
||||
case event := <-trafficChan:
|
||||
s.handleTraffic(event)
|
||||
case event := <-connectionsChan:
|
||||
s.handleConnection(event)
|
||||
case getStatsChan := <-statsChan:
|
||||
s.handleGetStats(getStatsChan)
|
||||
case <-crashesChan:
|
||||
s.handleCrash()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Stats) handleTraffic(evt trafficData) {
|
||||
if evt.ingress {
|
||||
s.Traffic.ingress += uint64(evt.traffic)
|
||||
} else {
|
||||
s.Traffic.egress += uint64(evt.traffic)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Stats) handleSpeed() {
|
||||
s.Speed.ingress = s.Traffic.ingress - s.previousTraffic.ingress
|
||||
s.Speed.egress = s.Traffic.egress - s.previousTraffic.egress
|
||||
s.previousTraffic.ingress = s.Traffic.ingress
|
||||
s.previousTraffic.egress = s.Traffic.egress
|
||||
}
|
||||
|
||||
func (s *Stats) handleConnection(evt connectionData) {
|
||||
var inc uint32 = 1
|
||||
if !evt.connected {
|
||||
inc = ^uint32(0)
|
||||
}
|
||||
|
||||
var conn *connectionType
|
||||
switch evt.connectionType {
|
||||
case mtproto.ConnectionTypeAbridged:
|
||||
conn = &s.Connections.Abridged
|
||||
case mtproto.ConnectionTypeSecure:
|
||||
conn = &s.Connections.Secure
|
||||
default:
|
||||
conn = &s.Connections.Intermediate
|
||||
}
|
||||
|
||||
if evt.addr.IP.To4() != nil {
|
||||
conn.IPv4 += inc
|
||||
} else {
|
||||
conn.IPv6 += inc
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Stats) handleGetStats(getStatsChan chan<- Stats) {
|
||||
getStatsChan <- *s
|
||||
}
|
||||
|
||||
func (s *Stats) handleCrash() {
|
||||
s.Crashes++
|
||||
}
|
||||
|
||||
// NewStats creates a new instance of Stats structure.
|
||||
func NewStats(conf *config.Config) *Stats {
|
||||
return &Stats{
|
||||
URLs: conf.GetURLs(),
|
||||
Uptime: uptime(time.Now()),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package stats
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/juju/errors"
|
||||
statsd "gopkg.in/alexcesaro/statsd.v2"
|
||||
|
||||
"github.com/9seconds/mtg/config"
|
||||
)
|
||||
|
||||
const (
|
||||
statsdConnectionsAbridgedV4 = "connections.abridged.ipv4"
|
||||
statsdConnectionsAbridgedV6 = "connections.abridged.ipv6"
|
||||
|
||||
statsdConnectionsIntermediateV4 = "connections.intermediate.ipv4"
|
||||
statsdConnectionsIntermediateV6 = "connections.intermediate.ipv6"
|
||||
|
||||
statsdConnectionsSecureV4 = "connections.secure.ipv4"
|
||||
statsdConnectionsSecureV6 = "connections.secure.ipv6"
|
||||
|
||||
statsdTrafficIngress = "traffic.ingress"
|
||||
statsdTrafficEgress = "traffic.egress"
|
||||
|
||||
statsdSpeedIngress = "speed.ingress"
|
||||
statsdSpeedEgress = "speed.egress"
|
||||
|
||||
statsdCrashes = "crashes"
|
||||
)
|
||||
|
||||
const statsdPollTime = time.Second
|
||||
|
||||
type statsdExporter struct {
|
||||
client *statsd.Client
|
||||
}
|
||||
|
||||
func (s *statsdExporter) run() {
|
||||
for range time.Tick(statsdPollTime) {
|
||||
instance := GetStats()
|
||||
|
||||
s.client.Gauge(statsdConnectionsAbridgedV4, instance.Connections.Abridged.IPv4)
|
||||
s.client.Gauge(statsdConnectionsAbridgedV6, instance.Connections.Abridged.IPv6)
|
||||
s.client.Gauge(statsdConnectionsIntermediateV4, instance.Connections.Intermediate.IPv4)
|
||||
s.client.Gauge(statsdConnectionsIntermediateV6, instance.Connections.Intermediate.IPv6)
|
||||
s.client.Gauge(statsdConnectionsSecureV4, instance.Connections.Secure.IPv4)
|
||||
s.client.Gauge(statsdConnectionsSecureV6, instance.Connections.Secure.IPv6)
|
||||
s.client.Gauge(statsdTrafficIngress, instance.Traffic.ingress)
|
||||
s.client.Gauge(statsdTrafficEgress, instance.Traffic.egress)
|
||||
s.client.Gauge(statsdSpeedIngress, instance.Speed.ingress)
|
||||
s.client.Gauge(statsdSpeedEgress, instance.Speed.egress)
|
||||
s.client.Gauge(statsdCrashes, instance.Crashes)
|
||||
}
|
||||
}
|
||||
|
||||
func newStatsd(conf *config.Config) (*statsdExporter, error) {
|
||||
options := []statsd.Option{
|
||||
statsd.Network(conf.StatsD.Addr.Network()),
|
||||
statsd.Address(conf.StatsD.Addr.String()),
|
||||
statsd.Prefix(conf.StatsD.Prefix),
|
||||
}
|
||||
|
||||
if conf.StatsD.TagsFormat > 0 {
|
||||
options = append(options, statsd.TagsFormat(conf.StatsD.TagsFormat))
|
||||
tags := make([]string, len(conf.StatsD.Tags)*2)
|
||||
for k, v := range conf.StatsD.Tags {
|
||||
tags = append(tags, k, v)
|
||||
}
|
||||
options = append(options, statsd.Tags(tags...))
|
||||
}
|
||||
|
||||
client, err := statsd.New(options...)
|
||||
if err != nil {
|
||||
return nil, errors.Annotate(err, "Cannot create statsd client")
|
||||
}
|
||||
|
||||
return &statsdExporter{client: client}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user