This commit is contained in:
9seconds
2022-03-18 14:58:08 +03:00
parent 9375552180
commit e0850869ba
10 changed files with 34 additions and 20 deletions
+2 -1
View File
@@ -89,7 +89,8 @@ func makeAntiReplayCache(conf *config.Config) mtglib.AntiReplayCache {
func makeIPBlocklist(conf config.ListConfig, func makeIPBlocklist(conf config.ListConfig,
logger mtglib.Logger, logger mtglib.Logger,
ntw mtglib.Network, ntw mtglib.Network,
updateCallback ipblocklist.FireholUpdateCallback) (mtglib.IPBlocklist, error) { updateCallback ipblocklist.FireholUpdateCallback,
) (mtglib.IPBlocklist, error) {
if !conf.Enabled.Get(false) { if !conf.Enabled.Get(false) {
return ipblocklist.NewNoop(), nil return ipblocklist.NewNoop(), nil
} }
+2 -1
View File
@@ -25,6 +25,7 @@ func (m *MtglibNetworkMock) DialContext(ctx context.Context, network, address st
} }
func (m *MtglibNetworkMock) MakeHTTPClient(dialFunc func(ctx context.Context, func (m *MtglibNetworkMock) MakeHTTPClient(dialFunc func(ctx context.Context,
network, address string) (essentials.Conn, error)) *http.Client { network, address string) (essentials.Conn, error),
) *http.Client {
return m.Called(dialFunc).Get(0).(*http.Client) // nolint: forcetypeassert return m.Called(dialFunc).Get(0).(*http.Client) // nolint: forcetypeassert
} }
+6 -3
View File
@@ -155,7 +155,8 @@ func (f *Firehol) update() {
func (f *Firehol) updateFromFile(mutex sync.Locker, func (f *Firehol) updateFromFile(mutex sync.Locker,
ranger cidranger.Ranger, ranger cidranger.Ranger,
scanner *bufio.Scanner) error { scanner *bufio.Scanner,
) error {
for scanner.Scan() { for scanner.Scan() {
text := scanner.Text() text := scanner.Text()
text = fireholRegexpComment.ReplaceAllLiteralString(text, "") text = fireholRegexpComment.ReplaceAllLiteralString(text, "")
@@ -216,7 +217,8 @@ func NewFirehol(logger mtglib.Logger, network mtglib.Network,
downloadConcurrency uint, downloadConcurrency uint,
urls []string, urls []string,
localFiles []string, localFiles []string,
updateCallback FireholUpdateCallback) (*Firehol, error) { updateCallback FireholUpdateCallback,
) (*Firehol, error) {
blocklists := []files.File{} blocklists := []files.File{}
for _, v := range localFiles { for _, v := range localFiles {
@@ -245,7 +247,8 @@ func NewFirehol(logger mtglib.Logger, network mtglib.Network,
func NewFireholFromFiles(logger mtglib.Logger, func NewFireholFromFiles(logger mtglib.Logger,
downloadConcurrency uint, downloadConcurrency uint,
blocklists []files.File, blocklists []files.File,
updateCallback FireholUpdateCallback) (*Firehol, error) { updateCallback FireholUpdateCallback,
) (*Firehol, error) {
if downloadConcurrency == 0 { if downloadConcurrency == 0 {
downloadConcurrency = DefaultFireholDownloadConcurrency downloadConcurrency = DefaultFireholDownloadConcurrency
} }
@@ -7,10 +7,10 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
var FuzzClientHandshakeSecret = []byte{1,2,3} var FuzzClientHandshakeSecret = []byte{1, 2, 3}
func FuzzClientHandshake(f *testing.F) { func FuzzClientHandshake(f *testing.F) {
f.Add([]byte{1,2,3}) f.Add([]byte{1, 2, 3})
f.Fuzz(func(t *testing.T, frame []byte) { f.Fuzz(func(t *testing.T, frame []byte) {
data := bytes.NewReader(frame) data := bytes.NewReader(frame)
+1 -1
View File
@@ -127,7 +127,7 @@ func NewServerHandshakeTestData(t *testing.T) ServerHandshakeTestData {
return ServerHandshakeTestData{ return ServerHandshakeTestData{
connMock: connMock, connMock: connMock,
proxyConn: obfuscated2.Conn{ proxyConn: obfuscated2.Conn{
Conn: connMock, Conn: connMock,
Encryptor: handshakeEnc, Encryptor: handshakeEnc,
Decryptor: handshakeDec, Decryptor: handshakeDec,
}, },
@@ -23,7 +23,7 @@ func FuzzServerGenerateHandshakeFrame(f *testing.F) {
assert.NotEqualValues( assert.NotEqualValues(
t, t,
0, 0,
frame.data[4] | frame.data[5] | frame.data[6] | frame.data[7]) frame.data[4]|frame.data[5]|frame.data[6]|frame.data[7])
assert.Equal(t, handshakeConnectionType, frame.connectionType()) assert.Equal(t, handshakeConnectionType, frame.connectionType())
}) })
@@ -8,7 +8,7 @@ import (
) )
func FuzzServerSend(f *testing.F) { func FuzzServerSend(f *testing.F) {
f.Add([]byte{1,2,3,4,5}) f.Add([]byte{1, 2, 3, 4, 5})
f.Fuzz(func(t *testing.T, data []byte) { f.Fuzz(func(t *testing.T, data []byte) {
handshakeData := NewServerHandshakeTestData(t) handshakeData := NewServerHandshakeTestData(t)
@@ -32,7 +32,7 @@ func FuzzServerSend(f *testing.F) {
} }
func FuzzServerReceive(f *testing.F) { func FuzzServerReceive(f *testing.F) {
f.Add([]byte{1,2,3,4,5}) f.Add([]byte{1, 2, 3, 4, 5})
f.Fuzz(func(t *testing.T, data []byte) { f.Fuzz(func(t *testing.T, data []byte) {
handshakeData := NewServerHandshakeTestData(t) handshakeData := NewServerHandshakeTestData(t)
+10 -5
View File
@@ -36,7 +36,8 @@ func (c *circuitBreakerDialer) Dial(network, address string) (essentials.Conn, e
} }
func (c *circuitBreakerDialer) DialContext(ctx context.Context, func (c *circuitBreakerDialer) DialContext(ctx context.Context,
network, address string) (essentials.Conn, error) { network, address string,
) (essentials.Conn, error) {
switch atomic.LoadUint32(&c.state) { switch atomic.LoadUint32(&c.state) {
case circuitBreakerStateClosed: case circuitBreakerStateClosed:
return c.doClosed(ctx, network, address) return c.doClosed(ctx, network, address)
@@ -48,7 +49,8 @@ func (c *circuitBreakerDialer) DialContext(ctx context.Context,
} }
func (c *circuitBreakerDialer) doClosed(ctx context.Context, func (c *circuitBreakerDialer) doClosed(ctx context.Context,
network, address string) (essentials.Conn, error) { network, address string,
) (essentials.Conn, error) {
conn, err := c.Dialer.DialContext(ctx, network, address) conn, err := c.Dialer.DialContext(ctx, network, address)
select { select {
@@ -80,7 +82,8 @@ func (c *circuitBreakerDialer) doClosed(ctx context.Context,
} }
func (c *circuitBreakerDialer) doHalfOpened(ctx context.Context, func (c *circuitBreakerDialer) doHalfOpened(ctx context.Context,
network, address string) (essentials.Conn, error) { network, address string,
) (essentials.Conn, error) {
if !atomic.CompareAndSwapUint32(&c.halfOpenAttempts, 0, 1) { if !atomic.CompareAndSwapUint32(&c.halfOpenAttempts, 0, 1) {
return nil, ErrCircuitBreakerOpened return nil, ErrCircuitBreakerOpened
} }
@@ -174,14 +177,16 @@ func (c *circuitBreakerDialer) stopTimer(timerRef **time.Timer) {
} }
func (c *circuitBreakerDialer) ensureTimer(timerRef **time.Timer, func (c *circuitBreakerDialer) ensureTimer(timerRef **time.Timer,
timeout time.Duration, callback func()) { timeout time.Duration, callback func(),
) {
if *timerRef == nil { if *timerRef == nil {
*timerRef = time.AfterFunc(timeout, callback) *timerRef = time.AfterFunc(timeout, callback)
} }
} }
func newCircuitBreakerDialer(baseDialer Dialer, func newCircuitBreakerDialer(baseDialer Dialer,
openThreshold uint32, halfOpenTimeout, resetFailuresTimeout time.Duration) Dialer { openThreshold uint32, halfOpenTimeout, resetFailuresTimeout time.Duration,
) Dialer {
cb := &circuitBreakerDialer{ cb := &circuitBreakerDialer{
Dialer: baseDialer, Dialer: baseDialer,
stateMutexChan: make(chan bool, 1), stateMutexChan: make(chan bool, 1),
+6 -3
View File
@@ -61,7 +61,8 @@ func (n *network) DialContext(ctx context.Context, protocol, address string) (es
} }
func (n *network) MakeHTTPClient(dialFunc func(ctx context.Context, func (n *network) MakeHTTPClient(dialFunc func(ctx context.Context,
network, address string) (essentials.Conn, error)) *http.Client { network, address string) (essentials.Conn, error),
) *http.Client {
if dialFunc == nil { if dialFunc == nil {
dialFunc = n.DialContext dialFunc = n.DialContext
} }
@@ -123,7 +124,8 @@ func (n *network) dnsResolve(protocol, address string) ([]string, error) {
// It brings simple DNS cache and DNS-Over-HTTPS when necessary. // It brings simple DNS cache and DNS-Over-HTTPS when necessary.
func NewNetwork(dialer Dialer, func NewNetwork(dialer Dialer,
userAgent, dohHostname string, userAgent, dohHostname string,
httpTimeout time.Duration) (mtglib.Network, error) { httpTimeout time.Duration,
) (mtglib.Network, error) {
switch { switch {
case httpTimeout < 0: case httpTimeout < 0:
return nil, fmt.Errorf("timeout should be positive number %s", httpTimeout) return nil, fmt.Errorf("timeout should be positive number %s", httpTimeout)
@@ -146,7 +148,8 @@ func NewNetwork(dialer Dialer,
func makeHTTPClient(userAgent string, func makeHTTPClient(userAgent string,
timeout time.Duration, timeout time.Duration,
dialFunc func(ctx context.Context, network, address string) (essentials.Conn, error)) *http.Client { dialFunc func(ctx context.Context, network, address string) (essentials.Conn, error),
) *http.Client {
return &http.Client{ return &http.Client{
Timeout: timeout, Timeout: timeout,
Transport: networkHTTPTransport{ Transport: networkHTTPTransport{
+2 -1
View File
@@ -171,7 +171,8 @@ func (s StatsdFactory) Make() events.Observer {
// //
// Valid tagFormats are 'datadog', 'influxdb' and 'graphite'. // Valid tagFormats are 'datadog', 'influxdb' and 'graphite'.
func NewStatsd(address string, log logger.StdLikeLogger, func NewStatsd(address string, log logger.StdLikeLogger,
metricPrefix, tagFormat string) (StatsdFactory, error) { metricPrefix, tagFormat string,
) (StatsdFactory, error) {
options := []statsd.Option{ options := []statsd.Option{
statsd.MetricPrefix(metricPrefix), statsd.MetricPrefix(metricPrefix),
statsd.Logger(log), statsd.Logger(log),