Make easier to extend the list of known proxy types

And include iptproxy as a valid proxy type.
This commit is contained in:
meskio 2022-03-11 14:32:35 +01:00
parent bd636a1374
commit b265bd3092
No known key found for this signature in database
GPG key ID: 52B8F5AC97A2DA86
4 changed files with 57 additions and 64 deletions

View file

@ -25,18 +25,15 @@ type IPC struct {
} }
func (i *IPC) Debug(_ interface{}, response *string) error { func (i *IPC) Debug(_ interface{}, response *string) error {
var webexts, browsers, standalones, unknowns int var unknowns int
var natRestricted, natUnrestricted, natUnknown int var natRestricted, natUnrestricted, natUnknown int
proxyTypes := make(map[string]int)
i.ctx.snowflakeLock.Lock() i.ctx.snowflakeLock.Lock()
s := fmt.Sprintf("current snowflakes available: %d\n", len(i.ctx.idToSnowflake)) s := fmt.Sprintf("current snowflakes available: %d\n", len(i.ctx.idToSnowflake))
for _, snowflake := range i.ctx.idToSnowflake { for _, snowflake := range i.ctx.idToSnowflake {
if snowflake.proxyType == "badge" { if messages.KnownProxyTypes[snowflake.proxyType] {
browsers++ proxyTypes[snowflake.proxyType]++
} else if snowflake.proxyType == "webext" {
webexts++
} else if snowflake.proxyType == "standalone" {
standalones++
} else { } else {
unknowns++ unknowns++
} }
@ -53,10 +50,10 @@ func (i *IPC) Debug(_ interface{}, response *string) error {
} }
i.ctx.snowflakeLock.Unlock() i.ctx.snowflakeLock.Unlock()
s += fmt.Sprintf("\tstandalone proxies: %d", standalones) for pType, num := range proxyTypes {
s += fmt.Sprintf("\n\tbrowser proxies: %d", browsers) s += fmt.Sprintf("\t%s proxies: %d\n", pType, num)
s += fmt.Sprintf("\n\twebext proxies: %d", webexts) }
s += fmt.Sprintf("\n\tunknown proxies: %d", unknowns) s += fmt.Sprintf("\tunknown proxies: %d", unknowns)
s += fmt.Sprintf("\nNAT Types available:") s += fmt.Sprintf("\nNAT Types available:")
s += fmt.Sprintf("\n\trestricted: %d", natRestricted) s += fmt.Sprintf("\n\trestricted: %d", natRestricted)

View file

@ -14,6 +14,7 @@ import (
"sync" "sync"
"time" "time"
"git.torproject.org/pluggable-transports/snowflake.git/v2/common/messages"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"gitlab.torproject.org/tpo/anti-censorship/geoip" "gitlab.torproject.org/tpo/anti-censorship/geoip"
) )
@ -24,9 +25,8 @@ const (
) )
type CountryStats struct { type CountryStats struct {
standalone map[string]bool // map[proxyType][address]bool
badge map[string]bool proxies map[string]map[string]bool
webext map[string]bool
unknown map[string]bool unknown map[string]bool
natRestricted map[string]bool natRestricted map[string]bool
@ -96,22 +96,17 @@ func (m *Metrics) UpdateCountryStats(addr string, proxyType string, natType stri
var country string var country string
var ok bool var ok bool
if proxyType == "standalone" { addresses, ok := m.countryStats.proxies[proxyType]
if m.countryStats.standalone[addr] { if !ok {
return
}
} else if proxyType == "badge" {
if m.countryStats.badge[addr] {
return
}
} else if proxyType == "webext" {
if m.countryStats.webext[addr] {
return
}
} else {
if m.countryStats.unknown[addr] { if m.countryStats.unknown[addr] {
return return
} }
m.countryStats.unknown[addr] = true
} else {
if addresses[addr] {
return
}
addresses[addr] = true
} }
ip := net.ParseIP(addr) ip := net.ParseIP(addr)
@ -122,18 +117,7 @@ func (m *Metrics) UpdateCountryStats(addr string, proxyType string, natType stri
if !ok { if !ok {
country = "??" country = "??"
} }
//update map of unique ips and counts
m.countryStats.counts[country]++ m.countryStats.counts[country]++
if proxyType == "standalone" {
m.countryStats.standalone[addr] = true
} else if proxyType == "badge" {
m.countryStats.badge[addr] = true
} else if proxyType == "webext" {
m.countryStats.webext[addr] = true
} else {
m.countryStats.unknown[addr] = true
}
m.promMetrics.ProxyTotal.With(prometheus.Labels{ m.promMetrics.ProxyTotal.With(prometheus.Labels{
"nat": natType, "nat": natType,
@ -166,14 +150,15 @@ func NewMetrics(metricsLogger *log.Logger) (*Metrics, error) {
m.countryStats = CountryStats{ m.countryStats = CountryStats{
counts: make(map[string]int), counts: make(map[string]int),
standalone: make(map[string]bool), proxies: make(map[string]map[string]bool),
badge: make(map[string]bool),
webext: make(map[string]bool),
unknown: make(map[string]bool), unknown: make(map[string]bool),
natRestricted: make(map[string]bool), natRestricted: make(map[string]bool),
natUnrestricted: make(map[string]bool), natUnrestricted: make(map[string]bool),
natUnknown: make(map[string]bool), natUnknown: make(map[string]bool),
} }
for pType := range messages.KnownProxyTypes {
m.countryStats.proxies[pType] = make(map[string]bool)
}
m.logger = metricsLogger m.logger = metricsLogger
m.promMetrics = initPrometheus() m.promMetrics = initPrometheus()
@ -197,11 +182,12 @@ func (m *Metrics) printMetrics() {
m.lock.Lock() m.lock.Lock()
m.logger.Println("snowflake-stats-end", time.Now().UTC().Format("2006-01-02 15:04:05"), fmt.Sprintf("(%d s)", int(metricsResolution.Seconds()))) m.logger.Println("snowflake-stats-end", time.Now().UTC().Format("2006-01-02 15:04:05"), fmt.Sprintf("(%d s)", int(metricsResolution.Seconds())))
m.logger.Println("snowflake-ips", m.countryStats.Display()) m.logger.Println("snowflake-ips", m.countryStats.Display())
m.logger.Println("snowflake-ips-total", len(m.countryStats.standalone)+ total := len(m.countryStats.unknown)
len(m.countryStats.badge)+len(m.countryStats.webext)+len(m.countryStats.unknown)) for pType, addresses := range m.countryStats.proxies {
m.logger.Println("snowflake-ips-standalone", len(m.countryStats.standalone)) m.logger.Printf("snowflake-ips-%s %d\n", pType, len(addresses))
m.logger.Println("snowflake-ips-badge", len(m.countryStats.badge)) total += len(addresses)
m.logger.Println("snowflake-ips-webext", len(m.countryStats.webext)) }
m.logger.Println("snowflake-ips-total", total)
m.logger.Println("snowflake-idle-count", binCount(m.proxyIdleCount)) m.logger.Println("snowflake-idle-count", binCount(m.proxyIdleCount))
m.logger.Println("client-denied-count", binCount(m.clientDeniedCount)) m.logger.Println("client-denied-count", binCount(m.clientDeniedCount))
m.logger.Println("client-restricted-denied-count", binCount(m.clientRestrictedDeniedCount)) m.logger.Println("client-restricted-denied-count", binCount(m.clientRestrictedDeniedCount))
@ -221,9 +207,9 @@ func (m *Metrics) zeroMetrics() {
m.clientUnrestrictedDeniedCount = 0 m.clientUnrestrictedDeniedCount = 0
m.clientProxyMatchCount = 0 m.clientProxyMatchCount = 0
m.countryStats.counts = make(map[string]int) m.countryStats.counts = make(map[string]int)
m.countryStats.standalone = make(map[string]bool) for pType := range m.countryStats.proxies {
m.countryStats.badge = make(map[string]bool) m.countryStats.proxies[pType] = make(map[string]bool)
m.countryStats.webext = make(map[string]bool) }
m.countryStats.unknown = make(map[string]bool) m.countryStats.unknown = make(map[string]bool)
m.countryStats.natRestricted = make(map[string]bool) m.countryStats.natRestricted = make(map[string]bool)
m.countryStats.natUnrestricted = make(map[string]bool) m.countryStats.natUnrestricted = make(map[string]bool)

View file

@ -549,8 +549,13 @@ func TestMetrics(t *testing.T) {
p.offerChannel <- nil p.offerChannel <- nil
<-done <-done
ctx.metrics.printMetrics() ctx.metrics.printMetrics()
So(buf.String(), ShouldResemble, "snowflake-stats-end "+time.Now().UTC().Format("2006-01-02 15:04:05")+" (86400 s)\nsnowflake-ips CA=4\nsnowflake-ips-total 4\nsnowflake-ips-standalone 1\nsnowflake-ips-badge 1\nsnowflake-ips-webext 1\nsnowflake-idle-count 8\nclient-denied-count 0\nclient-restricted-denied-count 0\nclient-unrestricted-denied-count 0\nclient-snowflake-match-count 0\nsnowflake-ips-nat-restricted 0\nsnowflake-ips-nat-unrestricted 0\nsnowflake-ips-nat-unknown 1\n")
metricsStr := buf.String()
So(metricsStr, ShouldStartWith, "snowflake-stats-end "+time.Now().UTC().Format("2006-01-02 15:04:05")+" (86400 s)\nsnowflake-ips CA=4\n")
So(metricsStr, ShouldContainSubstring, "\nsnowflake-ips-standalone 1\n")
So(metricsStr, ShouldContainSubstring, "\nsnowflake-ips-badge 1\n")
So(metricsStr, ShouldContainSubstring, "\nsnowflake-ips-webext 1\n")
So(metricsStr, ShouldEndWith, "\nsnowflake-ips-total 4\nsnowflake-idle-count 8\nclient-denied-count 0\nclient-restricted-denied-count 0\nclient-unrestricted-denied-count 0\nclient-snowflake-match-count 0\nsnowflake-ips-nat-restricted 0\nsnowflake-ips-nat-unrestricted 0\nsnowflake-ips-nat-unknown 1\n")
}) })
//Test addition of client failures //Test addition of client failures
@ -570,7 +575,11 @@ func TestMetrics(t *testing.T) {
buf.Reset() buf.Reset()
ctx.metrics.zeroMetrics() ctx.metrics.zeroMetrics()
ctx.metrics.printMetrics() ctx.metrics.printMetrics()
So(buf.String(), ShouldContainSubstring, "snowflake-ips \nsnowflake-ips-total 0\nsnowflake-ips-standalone 0\nsnowflake-ips-badge 0\nsnowflake-ips-webext 0\nsnowflake-idle-count 0\nclient-denied-count 0\nclient-restricted-denied-count 0\nclient-unrestricted-denied-count 0\nclient-snowflake-match-count 0\nsnowflake-ips-nat-restricted 0\nsnowflake-ips-nat-unrestricted 0\nsnowflake-ips-nat-unknown 0\n") So(buf.String(), ShouldContainSubstring, "\nsnowflake-ips \n")
So(buf.String(), ShouldContainSubstring, "\nsnowflake-ips-standalone 0\n")
So(buf.String(), ShouldContainSubstring, "\nsnowflake-ips-badge 0\n")
So(buf.String(), ShouldContainSubstring, "\nsnowflake-ips-webext 0\n")
So(buf.String(), ShouldContainSubstring, "\nsnowflake-ips-total 0\nsnowflake-idle-count 0\nclient-denied-count 0\nclient-restricted-denied-count 0\nclient-unrestricted-denied-count 0\nclient-snowflake-match-count 0\nsnowflake-ips-nat-restricted 0\nsnowflake-ips-nat-unrestricted 0\nsnowflake-ips-nat-unknown 0\n")
}) })
//Test addition of client matches //Test addition of client matches
Convey("for client-proxy match", func() { Convey("for client-proxy match", func() {
@ -690,7 +699,9 @@ func TestMetrics(t *testing.T) {
<-done <-done
ctx.metrics.printMetrics() ctx.metrics.printMetrics()
So(buf.String(), ShouldContainSubstring, "snowflake-ips CA=1\nsnowflake-ips-total 1") metricsStr := buf.String()
So(metricsStr, ShouldContainSubstring, "snowflake-ips CA=1\n")
So(metricsStr, ShouldContainSubstring, "snowflake-ips-total 1\n")
}) })
//Test NAT types //Test NAT types
Convey("proxy counts by NAT type", func() { Convey("proxy counts by NAT type", func() {

View file

@ -13,13 +13,16 @@ import (
const ( const (
version = "1.2" version = "1.2"
ProxyStandalone = "standalone"
ProxyWebext = "webext"
ProxyBadge = "badge"
ProxyUnknown = "unknown" ProxyUnknown = "unknown"
) )
var KnownProxyTypes = map[string]bool{
"standalone": true,
"webext": true,
"badge": true,
"iptproxy": true,
}
/* Version 1.2 specification: /* Version 1.2 specification:
== ProxyPollRequest == == ProxyPollRequest ==
@ -138,11 +141,7 @@ func DecodeProxyPollRequest(data []byte) (sid string, proxyType string, natType
// we don't reject polls with an unknown proxy type because we encourage // we don't reject polls with an unknown proxy type because we encourage
// projects that embed proxy code to include their own type // projects that embed proxy code to include their own type
switch message.Type { if !KnownProxyTypes[message.Type] {
case ProxyStandalone:
case ProxyWebext:
case ProxyBadge:
default:
message.Type = ProxyUnknown message.Type = ProxyUnknown
} }