mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-14 05:11:19 -04:00
Add distinct IP counter
This commit is contained in:
parent
97dea533da
commit
211254fa98
8 changed files with 307 additions and 0 deletions
52
common/ipsetsink/sink.go
Normal file
52
common/ipsetsink/sink.go
Normal file
|
@ -0,0 +1,52 @@
|
|||
package ipsetsink
|
||||
|
||||
import (
|
||||
"crypto/hmac"
|
||||
"hash"
|
||||
"hash/crc64"
|
||||
|
||||
"github.com/clarkduvall/hyperloglog"
|
||||
"golang.org/x/crypto/sha3"
|
||||
)
|
||||
|
||||
func NewIPSetSink(maskingKey string) *IPSetSink {
|
||||
countDistinct, _ := hyperloglog.NewPlus(18)
|
||||
return &IPSetSink{
|
||||
ipMaskingKey: maskingKey,
|
||||
countDistinct: countDistinct,
|
||||
}
|
||||
}
|
||||
|
||||
type IPSetSink struct {
|
||||
ipMaskingKey string
|
||||
countDistinct *hyperloglog.HyperLogLogPlus
|
||||
}
|
||||
|
||||
func (s *IPSetSink) maskIPAddress(ipAddress string) []byte {
|
||||
hmacIPMasker := hmac.New(func() hash.Hash {
|
||||
return sha3.New256()
|
||||
}, []byte(s.ipMaskingKey))
|
||||
hmacIPMasker.Write([]byte(ipAddress))
|
||||
return hmacIPMasker.Sum(nil)
|
||||
}
|
||||
|
||||
func (s *IPSetSink) AddIPToSet(ipAddress string) {
|
||||
s.countDistinct.Add(crc64FromBytes{hashValue(s.maskIPAddress(ipAddress))})
|
||||
}
|
||||
|
||||
func (s *IPSetSink) Dump() ([]byte, error) {
|
||||
return s.countDistinct.GobEncode()
|
||||
}
|
||||
|
||||
func (s *IPSetSink) Reset() {
|
||||
s.countDistinct.Clear()
|
||||
}
|
||||
|
||||
type hashValue []byte
|
||||
type crc64FromBytes struct {
|
||||
hashValue
|
||||
}
|
||||
|
||||
func (c crc64FromBytes) Sum64() uint64 {
|
||||
return crc64.Checksum(c.hashValue, crc64.MakeTable(crc64.ECMA))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue