From ecf5564f1cff2c394665efa5036dd0c60b1ce7c8 Mon Sep 17 00:00:00 2001 From: renbou Date: Wed, 27 Jul 2022 02:30:38 +0300 Subject: [PATCH 01/17] wip: migrate to http proxy architecture - http+redis registerer and proxy client finished - golangci setup --- .github/workflows/lint.yml | 13 ++ .golangci.yml | 48 +++++++ cmd/aboba-handler/main.go | 6 +- cmd/core/main.go | 5 +- cmd/debug-handler/main.go | 4 +- cmd/shake-cat-handler/main.go | 9 +- go.mod | 2 +- pkg/handlers/config.go | 2 +- pkg/handlers/simple.go | 3 +- pkg/services/models.go | 15 ++ pkg/services/services.go | 261 ++++++++++++++++++++++++++++++++++ pkg/utils/telegram.go | 3 +- 12 files changed, 356 insertions(+), 15 deletions(-) create mode 100644 .github/workflows/lint.yml create mode 100644 .golangci.yml create mode 100644 pkg/services/models.go create mode 100644 pkg/services/services.go diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..133bdb5 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,13 @@ +name: lint +on: + - pull_request +jobs: + code: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Set up Go + uses: actions/setup-go@v3 + - name: golangci-lint + uses: golangci/golangci-lint-action@v3 diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..ec7c97b --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,48 @@ +run: + timeout: 2m + +linters: + enable: + - asciicheck + - bodyclose + - containedctx + - contextcheck + - deadcode + - dogsled + - durationcheck + - dupl + - errchkjson + - errname + - errorlint + - exhaustive + - exportloopref + - gocritic + - gofumpt + - goimports + - gomnd + - gomoddirectives + - gosimple + - govet + - ifshort + - ineffassign + - importas + - misspell + - noctx + - prealloc + - predeclared + - revive + - staticcheck + - thelper + - tparallel + - unconvert + - unparam + - unused + - varcheck + - whitespace + - wrapcheck + +linters-settings: + unparam: + check-exported: false + unused: + check-exported: false diff --git a/cmd/aboba-handler/main.go b/cmd/aboba-handler/main.go index cbc3ad0..523cc0f 100644 --- a/cmd/aboba-handler/main.go +++ b/cmd/aboba-handler/main.go @@ -1,11 +1,11 @@ package main import ( + "github.com/bbralion/CTFloodBot/pkg/core" + "github.com/bbralion/CTFloodBot/pkg/handlers" + "github.com/bbralion/CTFloodBot/pkg/utils" telegramapi "github.com/go-telegram-bot-api/telegram-bot-api" "github.com/jinzhu/configor" - "github.com/kbats183/CTFloodBot/pkg/core" - "github.com/kbats183/CTFloodBot/pkg/handlers" - "github.com/kbats183/CTFloodBot/pkg/utils" "go.uber.org/zap" ) diff --git a/cmd/core/main.go b/cmd/core/main.go index d79abce..dec4bb8 100644 --- a/cmd/core/main.go +++ b/cmd/core/main.go @@ -3,10 +3,11 @@ package main import ( "context" "encoding/json" + + "github.com/bbralion/CTFloodBot/pkg/core" + "github.com/bbralion/CTFloodBot/pkg/utils" telegramapi "github.com/go-telegram-bot-api/telegram-bot-api" "github.com/jinzhu/configor" - "github.com/kbats183/CTFloodBot/pkg/core" - "github.com/kbats183/CTFloodBot/pkg/utils" "go.uber.org/zap" ) diff --git a/cmd/debug-handler/main.go b/cmd/debug-handler/main.go index 975d12f..d9dbee7 100644 --- a/cmd/debug-handler/main.go +++ b/cmd/debug-handler/main.go @@ -1,10 +1,10 @@ package main import ( + "github.com/bbralion/CTFloodBot/pkg/handlers" + "github.com/bbralion/CTFloodBot/pkg/utils" telegramapi "github.com/go-telegram-bot-api/telegram-bot-api" "github.com/jinzhu/configor" - "github.com/kbats183/CTFloodBot/pkg/handlers" - "github.com/kbats183/CTFloodBot/pkg/utils" "go.uber.org/zap" ) diff --git a/cmd/shake-cat-handler/main.go b/cmd/shake-cat-handler/main.go index 74b0468..792b112 100644 --- a/cmd/shake-cat-handler/main.go +++ b/cmd/shake-cat-handler/main.go @@ -1,13 +1,14 @@ package main import ( + "strings" + + "github.com/bbralion/CTFloodBot/pkg/core" + "github.com/bbralion/CTFloodBot/pkg/handlers" + "github.com/bbralion/CTFloodBot/pkg/utils" telegramapi "github.com/go-telegram-bot-api/telegram-bot-api" "github.com/jinzhu/configor" - "github.com/kbats183/CTFloodBot/pkg/core" - "github.com/kbats183/CTFloodBot/pkg/handlers" - "github.com/kbats183/CTFloodBot/pkg/utils" "go.uber.org/zap" - "strings" ) var config handlers.HandlerConfig diff --git a/go.mod b/go.mod index 5674edf..d22c607 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/kbats183/CTFloodBot +module github.com/bbralion/CTFloodBot go 1.18 diff --git a/pkg/handlers/config.go b/pkg/handlers/config.go index ac641a5..4d55112 100644 --- a/pkg/handlers/config.go +++ b/pkg/handlers/config.go @@ -1,7 +1,7 @@ package handlers import ( - "github.com/kbats183/CTFloodBot/pkg/core" + "github.com/bbralion/CTFloodBot/pkg/core" ) type HandlerConfig struct { diff --git a/pkg/handlers/simple.go b/pkg/handlers/simple.go index a86ed05..66daae7 100644 --- a/pkg/handlers/simple.go +++ b/pkg/handlers/simple.go @@ -3,8 +3,9 @@ package handlers import ( "context" "encoding/json" + + "github.com/bbralion/CTFloodBot/pkg/core" telegramapi "github.com/go-telegram-bot-api/telegram-bot-api" - "github.com/kbats183/CTFloodBot/pkg/core" "go.uber.org/zap" ) diff --git a/pkg/services/models.go b/pkg/services/models.go new file mode 100644 index 0000000..0ca20f1 --- /dev/null +++ b/pkg/services/models.go @@ -0,0 +1,15 @@ +package services + +import "time" + +type RegisterHandlerRequest struct { + Name string `json:"name"` + Matchers []string `json:"matchers"` +} + +type RegisterHandlerResponse struct { + // Must be consistent on renewals + Channel string `json:"channel"` + // Deadline is expected to be at least 30 seconds from now + Deadline time.Time `json:"deadline"` +} diff --git a/pkg/services/services.go b/pkg/services/services.go new file mode 100644 index 0000000..26c3ecb --- /dev/null +++ b/pkg/services/services.go @@ -0,0 +1,261 @@ +package services + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "fmt" + "io" + "net/http" + "net/url" + "path" + "regexp" + "time" + + "github.com/go-redis/redis/v8" + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" + "go.uber.org/zap" +) + +// HandlerRegisterer allows registration of command handlers for subsequent receival of updates +type HandlerRegisterer interface { + // RegisterHandler registers a new command handler with the given name and matchers. + // The context should span the lifetime of the registered handler and canceled when it dies. + RegisterHandler(ctx context.Context, name string, matchers []regexp.Regexp) (tgbotapi.UpdatesChannel, error) +} + +// Proxy is a HandlerRegisterer which also handles bot API initialization +type Proxy interface { + HandlerRegisterer + InitBotAPI() (*tgbotapi.BotAPI, error) +} + +// renewalCoef defines how soon a handler's registration is +// renewed compared to the actual deadline +const renewalCoef = 2 + +// tgAPIError is an internal error wrapper for errors which +// occurred during registration, updates, etc +type tgAPIError struct { + wrapped error + message string + operation string +} + +func (e *tgAPIError) Unwrap() error { + return e.wrapped +} + +func (e *tgAPIError) Error() string { + return e.message +} + +func logTgAPIError(logger *zap.Logger, e *tgAPIError) { + logger.Warn(e.message, zap.String("operation", e.operation), zap.Error(e.wrapped)) +} + +// redisHTTPProxy implements registration using http and update receival using redis +type redisHTTPProxy struct { + r *redis.Client + h *http.Client + l *zap.Logger + endpoint *url.URL +} + +// internalHTTPTransport is a roundtripper for the http proxy +// with added authorization and error handling +type internalHTTPTransport struct { + http.RoundTripper + logger *zap.Logger + token string +} + +func (t *internalHTTPTransport) RoundTrip(r *http.Request) (*http.Response, error) { + r.Header.Set("Authorization", t.token) + resp, err := t.RoundTripper.RoundTrip(r) + if err != nil { + err := &tgAPIError{ + wrapped: err, + message: "roundtripping request to bot proxy failed", + operation: "roundtrip", + } + logTgAPIError(t.logger, err) + return nil, err + } + + if resp.StatusCode == http.StatusForbidden || resp.StatusCode == http.StatusUnauthorized { + return nil, errors.New("request to bot proxy failed: unauthorized") + } + return resp, nil +} + +// RedisHTTPConfig specifies the configuration of the redis-http-based proxy. +// All fields are expected to be set unless specified otherwise. +type RedisHTTPConfig struct { + Logger *zap.Logger + Redis *redis.Client + // RoundTripper can be nil, in which case http.DefaultTransport will be used + RoundTripper http.RoundTripper + // Token is the authorization token for the http API + Token string + // Endpoint of the http API + Endpoint *url.URL +} + +// NewRedisHTTPProxy constructs a new redis-http-based proxy +func NewRedisHTTPProxy(config *RedisHTTPConfig) (Proxy, error) { + if config.Logger == nil || config.Redis == nil { + return nil, errors.New("unable to create registerer without required components") + } else if config.Token == "" || config.Endpoint == nil { + return nil, errors.New("token and endpoint of http API must be set") + } + + transport := config.RoundTripper + if transport == nil { + transport = http.DefaultTransport + } + return &redisHTTPProxy{ + r: config.Redis, + h: &http.Client{ + Transport: &internalHTTPTransport{ + RoundTripper: transport, + logger: config.Logger, + token: config.Token, + }, + }, + l: config.Logger, + endpoint: config.Endpoint, + }, nil +} + +func urlJoin(base *url.URL, relative ...string) string { + cp := *base + cp.RawPath = "" + cp.Path = path.Join(append([]string{cp.Path}, relative...)...) + return cp.String() +} + +func (p *redisHTTPProxy) updateRegistration(ctx context.Context, request *RegisterHandlerRequest) (*RegisterHandlerResponse, error) { + b, err := json.Marshal(request) + if err != nil { + return nil, fmt.Errorf("marshaling register request failed: %w", err) + } + buffer := bytes.NewBuffer(b) + + // Do registration request + httpreq, err := http.NewRequestWithContext(ctx, "POST", urlJoin(p.endpoint, "internal", "register"), buffer) + if err != nil { + return nil, fmt.Errorf("http request construction failed: %w", err) + } + httpreq.Header.Set("Content-Type", "application/json") + + httpresp, err := p.h.Do(httpreq) + if err != nil { + return nil, fmt.Errorf("registration request failed: %w", err) + } + + // Ensure that registration was successful + body, err := io.ReadAll(httpresp.Body) + if err != nil { + return nil, fmt.Errorf("reading response failed: %w", err) + } + + if httpresp.StatusCode != http.StatusOK && httpresp.StatusCode != http.StatusCreated { + return nil, fmt.Errorf("proxy responded with bad status code (%d): %s", httpresp.StatusCode, body) + } + + var resp RegisterHandlerResponse + if err := json.Unmarshal(body, &resp); err != nil { + return nil, fmt.Errorf("failed to unmarshal response body %s: %w", body, err) + } + return &resp, nil +} + +func (p *redisHTTPProxy) RegisterHandler(ctx context.Context, name string, matchers []regexp.Regexp) (tgbotapi.UpdatesChannel, error) { + var apiError *tgAPIError + defer func() { + if apiError != nil { + logTgAPIError(p.l, apiError) + } + }() + + // Construct registration to be used for all future registrations + request := RegisterHandlerRequest{ + Name: name, + Matchers: make([]string, len(matchers)), + } + for i, m := range matchers { + request.Matchers[i] = m.String() + } + + response, err := p.updateRegistration(ctx, &request) + if err != nil { + apiError = &tgAPIError{ + wrapped: err, + message: "failed to register handler", + operation: "registration", + } + return nil, apiError + } + getRenewalCh := func() <-chan time.Time { + return time.After(time.Until(response.Deadline) / renewalCoef) + } + + updates := make(chan tgbotapi.Update) + go func() { + defer close(updates) + + // Subscribe to updates + subscriber := p.r.Subscribe(ctx, response.Channel) + + // Wait until we have to renew or are killed + renewalCh := getRenewalCh() + for { + select { + case <-ctx.Done(): + return + case message := <-subscriber.Channel(): + var update tgbotapi.Update + if err := json.Unmarshal([]byte(message.Payload), &update); err != nil { + logTgAPIError(p.l, &tgAPIError{ + wrapped: err, + message: fmt.Sprintf("failed to unmarshal update message (%s)", message.Payload), + operation: "update", + }) + return + } + + updates <- update + case <-renewalCh: + // renew our registration and setup the renewal channel again + if response, err = p.updateRegistration(ctx, &request); err != nil { + logTgAPIError(p.l, &tgAPIError{ + wrapped: err, + message: "failed to renew handler", + operation: "renewal", + }) + return + } + renewalCh = getRenewalCh() + } + } + }() + + return updates, nil +} + +func (p *redisHTTPProxy) InitBotAPI() (*tgbotapi.BotAPI, error) { + // construct bot api format with meaningless token + bot, err := tgbotapi.NewBotAPIWithClient("fake-token", urlJoin(p.endpoint, "proxy%s", "%s"), p.h) + if err != nil { + err := &tgAPIError{ + wrapped: err, + message: "failed to initialize bot API", + operation: "init", + } + logTgAPIError(p.l, err) + return nil, err + } + return bot, nil +} diff --git a/pkg/utils/telegram.go b/pkg/utils/telegram.go index 8d77c22..16fed2d 100644 --- a/pkg/utils/telegram.go +++ b/pkg/utils/telegram.go @@ -1,8 +1,9 @@ package utils import ( - telegramapi "github.com/go-telegram-bot-api/telegram-bot-api" "strings" + + telegramapi "github.com/go-telegram-bot-api/telegram-bot-api" ) func addIfNotNil(slice []string, name string, objectIsNil bool) []string { From 285d7516856bab75610011116db60a7eeab2c4ca Mon Sep 17 00:00:00 2001 From: renbou Date: Wed, 27 Jul 2022 22:10:49 +0300 Subject: [PATCH 02/17] wip: grpc-based registrar implemented --- Makefile | 22 ++ api/proto/mux.proto | 28 +++ go.mod | 18 +- go.sum | 136 +++++++++- internal/genproto/mux.pb.go | 406 ++++++++++++++++++++++++++++++ internal/genproto/mux_grpc.pb.go | 169 +++++++++++++ internal/mockproto/mux_mock.go | 419 +++++++++++++++++++++++++++++++ pkg/services/error.go | 24 ++ pkg/services/registrar.go | 90 +++++++ pkg/services/registrar_test.go | 92 +++++++ 10 files changed, 1393 insertions(+), 11 deletions(-) create mode 100644 Makefile create mode 100644 api/proto/mux.proto create mode 100644 internal/genproto/mux.pb.go create mode 100644 internal/genproto/mux_grpc.pb.go create mode 100644 internal/mockproto/mux_mock.go create mode 100644 pkg/services/error.go create mode 100644 pkg/services/registrar.go create mode 100644 pkg/services/registrar_test.go diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..ff17ace --- /dev/null +++ b/Makefile @@ -0,0 +1,22 @@ +.PHONY: proto +proto: + cd api/proto && \ + protoc \ + --go_opt=Mmux.proto=github.com/bbralion/CTFloodBot/internal/genproto \ + --go-grpc_opt=Mmux.proto=github.com/bbralion/CTFloodBot/internal/genproto \ + --go_opt=paths=source_relative \ + --go-grpc_opt=paths=source_relative \ + --go_out=../../internal/genproto \ + --go-grpc_out=../../internal/genproto \ + mux.proto + +.PHONY: mocks +mocks: + mockgen \ + -package=mockproto \ + -destination=internal/mockproto/mux_mock.go \ + -source=internal/genproto/mux_grpc.pb.go MultiplexerServiceClient + +.PHONY: test +test: + go test -race ./... \ No newline at end of file diff --git a/api/proto/mux.proto b/api/proto/mux.proto new file mode 100644 index 0000000..9f996fd --- /dev/null +++ b/api/proto/mux.proto @@ -0,0 +1,28 @@ +syntax = "proto3"; +package mux; + +// Config specifies the information clients require to connect to the proxy. +message Config { + string proxy_endpoint = 1; +} + +// Update is a single update received by the proxy, passed as the actual stringified update object. +message Update { + bytes json = 1; +} + +message ConfigRequest {} + +message ConfigResponse { + Config config = 1; +} + +message RegisterRequest { + string name = 1; + repeated string matchers = 2; +} + +service MultiplexerService { + rpc GetConfig(ConfigRequest) returns (ConfigResponse); + rpc RegisterHandler(RegisterRequest) returns (stream Update); +} \ No newline at end of file diff --git a/go.mod b/go.mod index d22c607..6292021 100644 --- a/go.mod +++ b/go.mod @@ -5,16 +5,28 @@ go 1.18 require ( github.com/go-redis/redis/v8 v8.11.5 github.com/go-telegram-bot-api/telegram-bot-api v1.0.1-0.20201107014523-54104a08f947 + github.com/golang/mock v1.6.0 github.com/jinzhu/configor v1.2.1 + github.com/stretchr/testify v1.8.0 go.uber.org/zap v1.21.0 + google.golang.org/grpc v1.48.0 + google.golang.org/protobuf v1.28.0 ) require ( - github.com/BurntSushi/toml v0.3.1 // indirect + github.com/BurntSushi/toml v1.2.0 // indirect github.com/cespare/xxhash/v2 v2.1.2 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect + github.com/golang/protobuf v1.5.2 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/technoweenie/multipartstreamer v1.0.1 // indirect - go.uber.org/atomic v1.7.0 // indirect - go.uber.org/multierr v1.6.0 // indirect + go.uber.org/atomic v1.9.0 // indirect + go.uber.org/multierr v1.8.0 // indirect + golang.org/x/net v0.0.0-20220726230323-06994584191e // indirect + golang.org/x/sys v0.0.0-20220727055044-e65921a090b8 // indirect + golang.org/x/text v0.3.7 // indirect + google.golang.org/genproto v0.0.0-20220725144611-272f38e5d71b // indirect gopkg.in/yaml.v2 v2.4.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 566607e..8d29136 100644 --- a/go.sum +++ b/go.sum @@ -1,19 +1,67 @@ -github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= +cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0= +github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= +github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= +github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= +github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE= github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= +github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= +github.com/cncf/udpa/go v0.0.0-20210930031921-04548b0d99d4/go.mod h1:6pvJx4me5XPnfI9Z40ddWsdw2W/uZgQLFXToKeRcDiI= +github.com/cncf/xds/go v0.0.0-20210922020428-25de7278fc84/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= +github.com/cncf/xds/go v0.0.0-20211001041855-01bcc9b48dfe/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= +github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= +github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= +github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk= +github.com/envoyproxy/go-control-plane v0.10.2-0.20220325020618-49ff273808a1/go.mod h1:KJwIaB5Mv44NWtYuAOFCVOjcI94vtpEz2JU/D2v6IjE= +github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= +github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI= github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo= github.com/go-telegram-bot-api/telegram-bot-api v1.0.1-0.20201107014523-54104a08f947 h1:CguiLTREMSU5GMaHMlAUAVb2cT8M+IpZVhgRK1te6Ds= github.com/go-telegram-bot-api/telegram-bot-api v1.0.1-0.20201107014523-54104a08f947/go.mod h1:lDm2E64X4OjFdBUA4hlN4mEvbSitvhJdKw7rsA8KHgI= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= +github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc= +github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= +github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= +github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= +github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= +github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= +github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= +github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= +github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= +github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= +github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= github.com/jinzhu/configor v1.2.1 h1:OKk9dsR8i6HPOCZR8BcMtcEImAFjIhbJFZNyn5GCZko= github.com/jinzhu/configor v1.2.1/go.mod h1:nX89/MOmDba7ZX7GCyU/VIaQ2Ar2aizBl2d3JLF/rDc= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= @@ -28,57 +76,129 @@ github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/technoweenie/multipartstreamer v1.0.1 h1:XRztA5MXiR1TIRHxH2uNxXxaIkKQDeX7m2XsSOlQEnM= github.com/technoweenie/multipartstreamer v1.0.1/go.mod h1:jNVxdtShOxzAsukZwTSw6MDx5eUJoiEBsSvzDU9uzog= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= -go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw= +go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= +go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI= go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ= -go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4= go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= +go.uber.org/multierr v1.8.0 h1:dg6GjLku4EH+249NNmoIciG9N/jURbDG+pFlTkhzIC8= +go.uber.org/multierr v1.8.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak= go.uber.org/zap v1.21.0 h1:WefMeulhovoZ2sYXz7st6K0sLj7bBhpiFaud4r4zST8= go.uber.org/zap v1.21.0/go.mod h1:wjWOCqI0f2ZZrJF/UufIOkiC8ii6tm1iqIsLo76RfJw= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= +golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= +golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= -golang.org/x/net v0.0.0-20210428140749-89ef3d95e781 h1:DzZ89McO9/gWPsQXS/FVKAlG02ZjaQ6AlZRBimEYOd0= +golang.org/x/net v0.0.0-20220726230323-06994584191e h1:wOQNKh1uuDGRnmgF0jDxh7ctgGy/3P4rYWQRVJD4/Yg= +golang.org/x/net v0.0.0-20220726230323-06994584191e/go.mod h1:AaygXjzTFtRAg2ttMY5RMuhpJ3cNnI0XpyFJD1iQRSM= +golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= +golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM= +golang.org/x/sys v0.0.0-20220727055044-e65921a090b8 h1:dyU22nBWzrmTQxtNrr4dzVOvaw35nUYE279vF9UmsI8= +golang.org/x/sys v0.0.0-20220727055044-e65921a090b8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= +golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= +google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= +google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= +google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= +google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= +google.golang.org/genproto v0.0.0-20220725144611-272f38e5d71b h1:SfSkJugek6xm7lWywqth4r2iTrYLpD8lOj1nMIIhMNM= +google.golang.org/genproto v0.0.0-20220725144611-272f38e5d71b/go.mod h1:iHe1svFLAZg9VWz891+QbRMwUv9O/1Ww+/mngYeThbc= +google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= +google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= +google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= +google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= +google.golang.org/grpc v1.33.1/go.mod h1:fr5YgcSWrqhRRxogOsw7RzIpsmvOZ6IcH4kBYTpR3n0= +google.golang.org/grpc v1.36.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU= +google.golang.org/grpc v1.48.0 h1:rQOsyJ/8+ufEDJd/Gdsz7HG220Mh9HAhFHRGnIjda0w= +google.golang.org/grpc v1.48.0/go.mod h1:vN9eftEi1UMyUsIF80+uQXhHjbXYbm0uXoFCACuMGWk= +google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= +google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= +google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= +google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= +google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= +google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw= +google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/internal/genproto/mux.pb.go b/internal/genproto/mux.pb.go new file mode 100644 index 0000000..7c87559 --- /dev/null +++ b/internal/genproto/mux.pb.go @@ -0,0 +1,406 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.28.0 +// protoc v3.21.3 +// source: mux.proto + +package genproto + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +// Config specifies the information clients require to connect to the proxy. +type Config struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + ProxyEndpoint string `protobuf:"bytes,1,opt,name=proxy_endpoint,json=proxyEndpoint,proto3" json:"proxy_endpoint,omitempty"` +} + +func (x *Config) Reset() { + *x = Config{} + if protoimpl.UnsafeEnabled { + mi := &file_mux_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Config) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Config) ProtoMessage() {} + +func (x *Config) ProtoReflect() protoreflect.Message { + mi := &file_mux_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Config.ProtoReflect.Descriptor instead. +func (*Config) Descriptor() ([]byte, []int) { + return file_mux_proto_rawDescGZIP(), []int{0} +} + +func (x *Config) GetProxyEndpoint() string { + if x != nil { + return x.ProxyEndpoint + } + return "" +} + +// Update is a single update received by the proxy, passed as the actual stringified update object. +type Update struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Json []byte `protobuf:"bytes,1,opt,name=json,proto3" json:"json,omitempty"` +} + +func (x *Update) Reset() { + *x = Update{} + if protoimpl.UnsafeEnabled { + mi := &file_mux_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Update) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Update) ProtoMessage() {} + +func (x *Update) ProtoReflect() protoreflect.Message { + mi := &file_mux_proto_msgTypes[1] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Update.ProtoReflect.Descriptor instead. +func (*Update) Descriptor() ([]byte, []int) { + return file_mux_proto_rawDescGZIP(), []int{1} +} + +func (x *Update) GetJson() []byte { + if x != nil { + return x.Json + } + return nil +} + +type ConfigRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields +} + +func (x *ConfigRequest) Reset() { + *x = ConfigRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_mux_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *ConfigRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ConfigRequest) ProtoMessage() {} + +func (x *ConfigRequest) ProtoReflect() protoreflect.Message { + mi := &file_mux_proto_msgTypes[2] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use ConfigRequest.ProtoReflect.Descriptor instead. +func (*ConfigRequest) Descriptor() ([]byte, []int) { + return file_mux_proto_rawDescGZIP(), []int{2} +} + +type ConfigResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Config *Config `protobuf:"bytes,1,opt,name=config,proto3" json:"config,omitempty"` +} + +func (x *ConfigResponse) Reset() { + *x = ConfigResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_mux_proto_msgTypes[3] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *ConfigResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ConfigResponse) ProtoMessage() {} + +func (x *ConfigResponse) ProtoReflect() protoreflect.Message { + mi := &file_mux_proto_msgTypes[3] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use ConfigResponse.ProtoReflect.Descriptor instead. +func (*ConfigResponse) Descriptor() ([]byte, []int) { + return file_mux_proto_rawDescGZIP(), []int{3} +} + +func (x *ConfigResponse) GetConfig() *Config { + if x != nil { + return x.Config + } + return nil +} + +type RegisterRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` + Matchers []string `protobuf:"bytes,2,rep,name=matchers,proto3" json:"matchers,omitempty"` +} + +func (x *RegisterRequest) Reset() { + *x = RegisterRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_mux_proto_msgTypes[4] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *RegisterRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*RegisterRequest) ProtoMessage() {} + +func (x *RegisterRequest) ProtoReflect() protoreflect.Message { + mi := &file_mux_proto_msgTypes[4] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use RegisterRequest.ProtoReflect.Descriptor instead. +func (*RegisterRequest) Descriptor() ([]byte, []int) { + return file_mux_proto_rawDescGZIP(), []int{4} +} + +func (x *RegisterRequest) GetName() string { + if x != nil { + return x.Name + } + return "" +} + +func (x *RegisterRequest) GetMatchers() []string { + if x != nil { + return x.Matchers + } + return nil +} + +var File_mux_proto protoreflect.FileDescriptor + +var file_mux_proto_rawDesc = []byte{ + 0x0a, 0x09, 0x6d, 0x75, 0x78, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x03, 0x6d, 0x75, 0x78, + 0x22, 0x2f, 0x0a, 0x06, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, 0x25, 0x0a, 0x0e, 0x70, 0x72, + 0x6f, 0x78, 0x79, 0x5f, 0x65, 0x6e, 0x64, 0x70, 0x6f, 0x69, 0x6e, 0x74, 0x18, 0x01, 0x20, 0x01, + 0x28, 0x09, 0x52, 0x0d, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x45, 0x6e, 0x64, 0x70, 0x6f, 0x69, 0x6e, + 0x74, 0x22, 0x1c, 0x0a, 0x06, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x6a, + 0x73, 0x6f, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x04, 0x6a, 0x73, 0x6f, 0x6e, 0x22, + 0x0f, 0x0a, 0x0d, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, + 0x22, 0x35, 0x0a, 0x0e, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, + 0x73, 0x65, 0x12, 0x23, 0x0a, 0x06, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x18, 0x01, 0x20, 0x01, + 0x28, 0x0b, 0x32, 0x0b, 0x2e, 0x6d, 0x75, 0x78, 0x2e, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x52, + 0x06, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x22, 0x41, 0x0a, 0x0f, 0x52, 0x65, 0x67, 0x69, 0x73, + 0x74, 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, + 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x1a, + 0x0a, 0x08, 0x6d, 0x61, 0x74, 0x63, 0x68, 0x65, 0x72, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x09, + 0x52, 0x08, 0x6d, 0x61, 0x74, 0x63, 0x68, 0x65, 0x72, 0x73, 0x32, 0x82, 0x01, 0x0a, 0x12, 0x4d, + 0x75, 0x6c, 0x74, 0x69, 0x70, 0x6c, 0x65, 0x78, 0x65, 0x72, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, + 0x65, 0x12, 0x34, 0x0a, 0x09, 0x47, 0x65, 0x74, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, 0x12, + 0x2e, 0x6d, 0x75, 0x78, 0x2e, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x52, 0x65, 0x71, 0x75, 0x65, + 0x73, 0x74, 0x1a, 0x13, 0x2e, 0x6d, 0x75, 0x78, 0x2e, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x52, + 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x36, 0x0a, 0x0f, 0x52, 0x65, 0x67, 0x69, 0x73, + 0x74, 0x65, 0x72, 0x48, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x72, 0x12, 0x14, 0x2e, 0x6d, 0x75, 0x78, + 0x2e, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, + 0x1a, 0x0b, 0x2e, 0x6d, 0x75, 0x78, 0x2e, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x30, 0x01, 0x62, + 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_mux_proto_rawDescOnce sync.Once + file_mux_proto_rawDescData = file_mux_proto_rawDesc +) + +func file_mux_proto_rawDescGZIP() []byte { + file_mux_proto_rawDescOnce.Do(func() { + file_mux_proto_rawDescData = protoimpl.X.CompressGZIP(file_mux_proto_rawDescData) + }) + return file_mux_proto_rawDescData +} + +var file_mux_proto_msgTypes = make([]protoimpl.MessageInfo, 5) +var file_mux_proto_goTypes = []interface{}{ + (*Config)(nil), // 0: mux.Config + (*Update)(nil), // 1: mux.Update + (*ConfigRequest)(nil), // 2: mux.ConfigRequest + (*ConfigResponse)(nil), // 3: mux.ConfigResponse + (*RegisterRequest)(nil), // 4: mux.RegisterRequest +} +var file_mux_proto_depIdxs = []int32{ + 0, // 0: mux.ConfigResponse.config:type_name -> mux.Config + 2, // 1: mux.MultiplexerService.GetConfig:input_type -> mux.ConfigRequest + 4, // 2: mux.MultiplexerService.RegisterHandler:input_type -> mux.RegisterRequest + 3, // 3: mux.MultiplexerService.GetConfig:output_type -> mux.ConfigResponse + 1, // 4: mux.MultiplexerService.RegisterHandler:output_type -> mux.Update + 3, // [3:5] is the sub-list for method output_type + 1, // [1:3] is the sub-list for method input_type + 1, // [1:1] is the sub-list for extension type_name + 1, // [1:1] is the sub-list for extension extendee + 0, // [0:1] is the sub-list for field type_name +} + +func init() { file_mux_proto_init() } +func file_mux_proto_init() { + if File_mux_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_mux_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Config); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_mux_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Update); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_mux_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*ConfigRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_mux_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*ConfigResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_mux_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*RegisterRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_mux_proto_rawDesc, + NumEnums: 0, + NumMessages: 5, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_mux_proto_goTypes, + DependencyIndexes: file_mux_proto_depIdxs, + MessageInfos: file_mux_proto_msgTypes, + }.Build() + File_mux_proto = out.File + file_mux_proto_rawDesc = nil + file_mux_proto_goTypes = nil + file_mux_proto_depIdxs = nil +} diff --git a/internal/genproto/mux_grpc.pb.go b/internal/genproto/mux_grpc.pb.go new file mode 100644 index 0000000..cfdc02a --- /dev/null +++ b/internal/genproto/mux_grpc.pb.go @@ -0,0 +1,169 @@ +// Code generated by protoc-gen-go-grpc. DO NOT EDIT. +// versions: +// - protoc-gen-go-grpc v1.2.0 +// - protoc v3.21.3 +// source: mux.proto + +package genproto + +import ( + context "context" + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" +) + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +// Requires gRPC-Go v1.32.0 or later. +const _ = grpc.SupportPackageIsVersion7 + +// MultiplexerServiceClient is the client API for MultiplexerService service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. +type MultiplexerServiceClient interface { + GetConfig(ctx context.Context, in *ConfigRequest, opts ...grpc.CallOption) (*ConfigResponse, error) + RegisterHandler(ctx context.Context, in *RegisterRequest, opts ...grpc.CallOption) (MultiplexerService_RegisterHandlerClient, error) +} + +type multiplexerServiceClient struct { + cc grpc.ClientConnInterface +} + +func NewMultiplexerServiceClient(cc grpc.ClientConnInterface) MultiplexerServiceClient { + return &multiplexerServiceClient{cc} +} + +func (c *multiplexerServiceClient) GetConfig(ctx context.Context, in *ConfigRequest, opts ...grpc.CallOption) (*ConfigResponse, error) { + out := new(ConfigResponse) + err := c.cc.Invoke(ctx, "/mux.MultiplexerService/GetConfig", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *multiplexerServiceClient) RegisterHandler(ctx context.Context, in *RegisterRequest, opts ...grpc.CallOption) (MultiplexerService_RegisterHandlerClient, error) { + stream, err := c.cc.NewStream(ctx, &MultiplexerService_ServiceDesc.Streams[0], "/mux.MultiplexerService/RegisterHandler", opts...) + if err != nil { + return nil, err + } + x := &multiplexerServiceRegisterHandlerClient{stream} + if err := x.ClientStream.SendMsg(in); err != nil { + return nil, err + } + if err := x.ClientStream.CloseSend(); err != nil { + return nil, err + } + return x, nil +} + +type MultiplexerService_RegisterHandlerClient interface { + Recv() (*Update, error) + grpc.ClientStream +} + +type multiplexerServiceRegisterHandlerClient struct { + grpc.ClientStream +} + +func (x *multiplexerServiceRegisterHandlerClient) Recv() (*Update, error) { + m := new(Update) + if err := x.ClientStream.RecvMsg(m); err != nil { + return nil, err + } + return m, nil +} + +// MultiplexerServiceServer is the server API for MultiplexerService service. +// All implementations must embed UnimplementedMultiplexerServiceServer +// for forward compatibility +type MultiplexerServiceServer interface { + GetConfig(context.Context, *ConfigRequest) (*ConfigResponse, error) + RegisterHandler(*RegisterRequest, MultiplexerService_RegisterHandlerServer) error + mustEmbedUnimplementedMultiplexerServiceServer() +} + +// UnimplementedMultiplexerServiceServer must be embedded to have forward compatible implementations. +type UnimplementedMultiplexerServiceServer struct { +} + +func (UnimplementedMultiplexerServiceServer) GetConfig(context.Context, *ConfigRequest) (*ConfigResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method GetConfig not implemented") +} +func (UnimplementedMultiplexerServiceServer) RegisterHandler(*RegisterRequest, MultiplexerService_RegisterHandlerServer) error { + return status.Errorf(codes.Unimplemented, "method RegisterHandler not implemented") +} +func (UnimplementedMultiplexerServiceServer) mustEmbedUnimplementedMultiplexerServiceServer() {} + +// UnsafeMultiplexerServiceServer may be embedded to opt out of forward compatibility for this service. +// Use of this interface is not recommended, as added methods to MultiplexerServiceServer will +// result in compilation errors. +type UnsafeMultiplexerServiceServer interface { + mustEmbedUnimplementedMultiplexerServiceServer() +} + +func RegisterMultiplexerServiceServer(s grpc.ServiceRegistrar, srv MultiplexerServiceServer) { + s.RegisterService(&MultiplexerService_ServiceDesc, srv) +} + +func _MultiplexerService_GetConfig_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(ConfigRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(MultiplexerServiceServer).GetConfig(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/mux.MultiplexerService/GetConfig", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(MultiplexerServiceServer).GetConfig(ctx, req.(*ConfigRequest)) + } + return interceptor(ctx, in, info, handler) +} + +func _MultiplexerService_RegisterHandler_Handler(srv interface{}, stream grpc.ServerStream) error { + m := new(RegisterRequest) + if err := stream.RecvMsg(m); err != nil { + return err + } + return srv.(MultiplexerServiceServer).RegisterHandler(m, &multiplexerServiceRegisterHandlerServer{stream}) +} + +type MultiplexerService_RegisterHandlerServer interface { + Send(*Update) error + grpc.ServerStream +} + +type multiplexerServiceRegisterHandlerServer struct { + grpc.ServerStream +} + +func (x *multiplexerServiceRegisterHandlerServer) Send(m *Update) error { + return x.ServerStream.SendMsg(m) +} + +// MultiplexerService_ServiceDesc is the grpc.ServiceDesc for MultiplexerService service. +// It's only intended for direct use with grpc.RegisterService, +// and not to be introspected or modified (even as a copy) +var MultiplexerService_ServiceDesc = grpc.ServiceDesc{ + ServiceName: "mux.MultiplexerService", + HandlerType: (*MultiplexerServiceServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "GetConfig", + Handler: _MultiplexerService_GetConfig_Handler, + }, + }, + Streams: []grpc.StreamDesc{ + { + StreamName: "RegisterHandler", + Handler: _MultiplexerService_RegisterHandler_Handler, + ServerStreams: true, + }, + }, + Metadata: "mux.proto", +} diff --git a/internal/mockproto/mux_mock.go b/internal/mockproto/mux_mock.go new file mode 100644 index 0000000..433471c --- /dev/null +++ b/internal/mockproto/mux_mock.go @@ -0,0 +1,419 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: internal/genproto/mux_grpc.pb.go + +// Package mockproto is a generated GoMock package. +package mockproto + +import ( + context "context" + reflect "reflect" + + genproto "github.com/bbralion/CTFloodBot/internal/genproto" + gomock "github.com/golang/mock/gomock" + grpc "google.golang.org/grpc" + metadata "google.golang.org/grpc/metadata" +) + +// MockMultiplexerServiceClient is a mock of MultiplexerServiceClient interface. +type MockMultiplexerServiceClient struct { + ctrl *gomock.Controller + recorder *MockMultiplexerServiceClientMockRecorder +} + +// MockMultiplexerServiceClientMockRecorder is the mock recorder for MockMultiplexerServiceClient. +type MockMultiplexerServiceClientMockRecorder struct { + mock *MockMultiplexerServiceClient +} + +// NewMockMultiplexerServiceClient creates a new mock instance. +func NewMockMultiplexerServiceClient(ctrl *gomock.Controller) *MockMultiplexerServiceClient { + mock := &MockMultiplexerServiceClient{ctrl: ctrl} + mock.recorder = &MockMultiplexerServiceClientMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockMultiplexerServiceClient) EXPECT() *MockMultiplexerServiceClientMockRecorder { + return m.recorder +} + +// GetConfig mocks base method. +func (m *MockMultiplexerServiceClient) GetConfig(ctx context.Context, in *genproto.ConfigRequest, opts ...grpc.CallOption) (*genproto.ConfigResponse, error) { + m.ctrl.T.Helper() + varargs := []interface{}{ctx, in} + for _, a := range opts { + varargs = append(varargs, a) + } + ret := m.ctrl.Call(m, "GetConfig", varargs...) + ret0, _ := ret[0].(*genproto.ConfigResponse) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetConfig indicates an expected call of GetConfig. +func (mr *MockMultiplexerServiceClientMockRecorder) GetConfig(ctx, in interface{}, opts ...interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + varargs := append([]interface{}{ctx, in}, opts...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetConfig", reflect.TypeOf((*MockMultiplexerServiceClient)(nil).GetConfig), varargs...) +} + +// RegisterHandler mocks base method. +func (m *MockMultiplexerServiceClient) RegisterHandler(ctx context.Context, in *genproto.RegisterRequest, opts ...grpc.CallOption) (genproto.MultiplexerService_RegisterHandlerClient, error) { + m.ctrl.T.Helper() + varargs := []interface{}{ctx, in} + for _, a := range opts { + varargs = append(varargs, a) + } + ret := m.ctrl.Call(m, "RegisterHandler", varargs...) + ret0, _ := ret[0].(genproto.MultiplexerService_RegisterHandlerClient) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// RegisterHandler indicates an expected call of RegisterHandler. +func (mr *MockMultiplexerServiceClientMockRecorder) RegisterHandler(ctx, in interface{}, opts ...interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + varargs := append([]interface{}{ctx, in}, opts...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RegisterHandler", reflect.TypeOf((*MockMultiplexerServiceClient)(nil).RegisterHandler), varargs...) +} + +// MockMultiplexerService_RegisterHandlerClient is a mock of MultiplexerService_RegisterHandlerClient interface. +type MockMultiplexerService_RegisterHandlerClient struct { + ctrl *gomock.Controller + recorder *MockMultiplexerService_RegisterHandlerClientMockRecorder +} + +// MockMultiplexerService_RegisterHandlerClientMockRecorder is the mock recorder for MockMultiplexerService_RegisterHandlerClient. +type MockMultiplexerService_RegisterHandlerClientMockRecorder struct { + mock *MockMultiplexerService_RegisterHandlerClient +} + +// NewMockMultiplexerService_RegisterHandlerClient creates a new mock instance. +func NewMockMultiplexerService_RegisterHandlerClient(ctrl *gomock.Controller) *MockMultiplexerService_RegisterHandlerClient { + mock := &MockMultiplexerService_RegisterHandlerClient{ctrl: ctrl} + mock.recorder = &MockMultiplexerService_RegisterHandlerClientMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockMultiplexerService_RegisterHandlerClient) EXPECT() *MockMultiplexerService_RegisterHandlerClientMockRecorder { + return m.recorder +} + +// CloseSend mocks base method. +func (m *MockMultiplexerService_RegisterHandlerClient) CloseSend() error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "CloseSend") + ret0, _ := ret[0].(error) + return ret0 +} + +// CloseSend indicates an expected call of CloseSend. +func (mr *MockMultiplexerService_RegisterHandlerClientMockRecorder) CloseSend() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CloseSend", reflect.TypeOf((*MockMultiplexerService_RegisterHandlerClient)(nil).CloseSend)) +} + +// Context mocks base method. +func (m *MockMultiplexerService_RegisterHandlerClient) Context() context.Context { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Context") + ret0, _ := ret[0].(context.Context) + return ret0 +} + +// Context indicates an expected call of Context. +func (mr *MockMultiplexerService_RegisterHandlerClientMockRecorder) Context() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Context", reflect.TypeOf((*MockMultiplexerService_RegisterHandlerClient)(nil).Context)) +} + +// Header mocks base method. +func (m *MockMultiplexerService_RegisterHandlerClient) Header() (metadata.MD, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Header") + ret0, _ := ret[0].(metadata.MD) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Header indicates an expected call of Header. +func (mr *MockMultiplexerService_RegisterHandlerClientMockRecorder) Header() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Header", reflect.TypeOf((*MockMultiplexerService_RegisterHandlerClient)(nil).Header)) +} + +// Recv mocks base method. +func (m *MockMultiplexerService_RegisterHandlerClient) Recv() (*genproto.Update, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Recv") + ret0, _ := ret[0].(*genproto.Update) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Recv indicates an expected call of Recv. +func (mr *MockMultiplexerService_RegisterHandlerClientMockRecorder) Recv() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Recv", reflect.TypeOf((*MockMultiplexerService_RegisterHandlerClient)(nil).Recv)) +} + +// RecvMsg mocks base method. +func (m_2 *MockMultiplexerService_RegisterHandlerClient) RecvMsg(m interface{}) error { + m_2.ctrl.T.Helper() + ret := m_2.ctrl.Call(m_2, "RecvMsg", m) + ret0, _ := ret[0].(error) + return ret0 +} + +// RecvMsg indicates an expected call of RecvMsg. +func (mr *MockMultiplexerService_RegisterHandlerClientMockRecorder) RecvMsg(m interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RecvMsg", reflect.TypeOf((*MockMultiplexerService_RegisterHandlerClient)(nil).RecvMsg), m) +} + +// SendMsg mocks base method. +func (m_2 *MockMultiplexerService_RegisterHandlerClient) SendMsg(m interface{}) error { + m_2.ctrl.T.Helper() + ret := m_2.ctrl.Call(m_2, "SendMsg", m) + ret0, _ := ret[0].(error) + return ret0 +} + +// SendMsg indicates an expected call of SendMsg. +func (mr *MockMultiplexerService_RegisterHandlerClientMockRecorder) SendMsg(m interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SendMsg", reflect.TypeOf((*MockMultiplexerService_RegisterHandlerClient)(nil).SendMsg), m) +} + +// Trailer mocks base method. +func (m *MockMultiplexerService_RegisterHandlerClient) Trailer() metadata.MD { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Trailer") + ret0, _ := ret[0].(metadata.MD) + return ret0 +} + +// Trailer indicates an expected call of Trailer. +func (mr *MockMultiplexerService_RegisterHandlerClientMockRecorder) Trailer() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Trailer", reflect.TypeOf((*MockMultiplexerService_RegisterHandlerClient)(nil).Trailer)) +} + +// MockMultiplexerServiceServer is a mock of MultiplexerServiceServer interface. +type MockMultiplexerServiceServer struct { + ctrl *gomock.Controller + recorder *MockMultiplexerServiceServerMockRecorder +} + +// MockMultiplexerServiceServerMockRecorder is the mock recorder for MockMultiplexerServiceServer. +type MockMultiplexerServiceServerMockRecorder struct { + mock *MockMultiplexerServiceServer +} + +// NewMockMultiplexerServiceServer creates a new mock instance. +func NewMockMultiplexerServiceServer(ctrl *gomock.Controller) *MockMultiplexerServiceServer { + mock := &MockMultiplexerServiceServer{ctrl: ctrl} + mock.recorder = &MockMultiplexerServiceServerMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockMultiplexerServiceServer) EXPECT() *MockMultiplexerServiceServerMockRecorder { + return m.recorder +} + +// GetConfig mocks base method. +func (m *MockMultiplexerServiceServer) GetConfig(arg0 context.Context, arg1 *genproto.ConfigRequest) (*genproto.ConfigResponse, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetConfig", arg0, arg1) + ret0, _ := ret[0].(*genproto.ConfigResponse) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetConfig indicates an expected call of GetConfig. +func (mr *MockMultiplexerServiceServerMockRecorder) GetConfig(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetConfig", reflect.TypeOf((*MockMultiplexerServiceServer)(nil).GetConfig), arg0, arg1) +} + +// RegisterHandler mocks base method. +func (m *MockMultiplexerServiceServer) RegisterHandler(arg0 *genproto.RegisterRequest, arg1 genproto.MultiplexerService_RegisterHandlerServer) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "RegisterHandler", arg0, arg1) + ret0, _ := ret[0].(error) + return ret0 +} + +// RegisterHandler indicates an expected call of RegisterHandler. +func (mr *MockMultiplexerServiceServerMockRecorder) RegisterHandler(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RegisterHandler", reflect.TypeOf((*MockMultiplexerServiceServer)(nil).RegisterHandler), arg0, arg1) +} + +// mustEmbedUnimplementedMultiplexerServiceServer mocks base method. +func (m *MockMultiplexerServiceServer) mustEmbedUnimplementedMultiplexerServiceServer() { + m.ctrl.T.Helper() + m.ctrl.Call(m, "mustEmbedUnimplementedMultiplexerServiceServer") +} + +// mustEmbedUnimplementedMultiplexerServiceServer indicates an expected call of mustEmbedUnimplementedMultiplexerServiceServer. +func (mr *MockMultiplexerServiceServerMockRecorder) mustEmbedUnimplementedMultiplexerServiceServer() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "mustEmbedUnimplementedMultiplexerServiceServer", reflect.TypeOf((*MockMultiplexerServiceServer)(nil).mustEmbedUnimplementedMultiplexerServiceServer)) +} + +// MockUnsafeMultiplexerServiceServer is a mock of UnsafeMultiplexerServiceServer interface. +type MockUnsafeMultiplexerServiceServer struct { + ctrl *gomock.Controller + recorder *MockUnsafeMultiplexerServiceServerMockRecorder +} + +// MockUnsafeMultiplexerServiceServerMockRecorder is the mock recorder for MockUnsafeMultiplexerServiceServer. +type MockUnsafeMultiplexerServiceServerMockRecorder struct { + mock *MockUnsafeMultiplexerServiceServer +} + +// NewMockUnsafeMultiplexerServiceServer creates a new mock instance. +func NewMockUnsafeMultiplexerServiceServer(ctrl *gomock.Controller) *MockUnsafeMultiplexerServiceServer { + mock := &MockUnsafeMultiplexerServiceServer{ctrl: ctrl} + mock.recorder = &MockUnsafeMultiplexerServiceServerMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockUnsafeMultiplexerServiceServer) EXPECT() *MockUnsafeMultiplexerServiceServerMockRecorder { + return m.recorder +} + +// mustEmbedUnimplementedMultiplexerServiceServer mocks base method. +func (m *MockUnsafeMultiplexerServiceServer) mustEmbedUnimplementedMultiplexerServiceServer() { + m.ctrl.T.Helper() + m.ctrl.Call(m, "mustEmbedUnimplementedMultiplexerServiceServer") +} + +// mustEmbedUnimplementedMultiplexerServiceServer indicates an expected call of mustEmbedUnimplementedMultiplexerServiceServer. +func (mr *MockUnsafeMultiplexerServiceServerMockRecorder) mustEmbedUnimplementedMultiplexerServiceServer() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "mustEmbedUnimplementedMultiplexerServiceServer", reflect.TypeOf((*MockUnsafeMultiplexerServiceServer)(nil).mustEmbedUnimplementedMultiplexerServiceServer)) +} + +// MockMultiplexerService_RegisterHandlerServer is a mock of MultiplexerService_RegisterHandlerServer interface. +type MockMultiplexerService_RegisterHandlerServer struct { + ctrl *gomock.Controller + recorder *MockMultiplexerService_RegisterHandlerServerMockRecorder +} + +// MockMultiplexerService_RegisterHandlerServerMockRecorder is the mock recorder for MockMultiplexerService_RegisterHandlerServer. +type MockMultiplexerService_RegisterHandlerServerMockRecorder struct { + mock *MockMultiplexerService_RegisterHandlerServer +} + +// NewMockMultiplexerService_RegisterHandlerServer creates a new mock instance. +func NewMockMultiplexerService_RegisterHandlerServer(ctrl *gomock.Controller) *MockMultiplexerService_RegisterHandlerServer { + mock := &MockMultiplexerService_RegisterHandlerServer{ctrl: ctrl} + mock.recorder = &MockMultiplexerService_RegisterHandlerServerMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockMultiplexerService_RegisterHandlerServer) EXPECT() *MockMultiplexerService_RegisterHandlerServerMockRecorder { + return m.recorder +} + +// Context mocks base method. +func (m *MockMultiplexerService_RegisterHandlerServer) Context() context.Context { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Context") + ret0, _ := ret[0].(context.Context) + return ret0 +} + +// Context indicates an expected call of Context. +func (mr *MockMultiplexerService_RegisterHandlerServerMockRecorder) Context() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Context", reflect.TypeOf((*MockMultiplexerService_RegisterHandlerServer)(nil).Context)) +} + +// RecvMsg mocks base method. +func (m_2 *MockMultiplexerService_RegisterHandlerServer) RecvMsg(m interface{}) error { + m_2.ctrl.T.Helper() + ret := m_2.ctrl.Call(m_2, "RecvMsg", m) + ret0, _ := ret[0].(error) + return ret0 +} + +// RecvMsg indicates an expected call of RecvMsg. +func (mr *MockMultiplexerService_RegisterHandlerServerMockRecorder) RecvMsg(m interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RecvMsg", reflect.TypeOf((*MockMultiplexerService_RegisterHandlerServer)(nil).RecvMsg), m) +} + +// Send mocks base method. +func (m *MockMultiplexerService_RegisterHandlerServer) Send(arg0 *genproto.Update) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Send", arg0) + ret0, _ := ret[0].(error) + return ret0 +} + +// Send indicates an expected call of Send. +func (mr *MockMultiplexerService_RegisterHandlerServerMockRecorder) Send(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Send", reflect.TypeOf((*MockMultiplexerService_RegisterHandlerServer)(nil).Send), arg0) +} + +// SendHeader mocks base method. +func (m *MockMultiplexerService_RegisterHandlerServer) SendHeader(arg0 metadata.MD) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "SendHeader", arg0) + ret0, _ := ret[0].(error) + return ret0 +} + +// SendHeader indicates an expected call of SendHeader. +func (mr *MockMultiplexerService_RegisterHandlerServerMockRecorder) SendHeader(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SendHeader", reflect.TypeOf((*MockMultiplexerService_RegisterHandlerServer)(nil).SendHeader), arg0) +} + +// SendMsg mocks base method. +func (m_2 *MockMultiplexerService_RegisterHandlerServer) SendMsg(m interface{}) error { + m_2.ctrl.T.Helper() + ret := m_2.ctrl.Call(m_2, "SendMsg", m) + ret0, _ := ret[0].(error) + return ret0 +} + +// SendMsg indicates an expected call of SendMsg. +func (mr *MockMultiplexerService_RegisterHandlerServerMockRecorder) SendMsg(m interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SendMsg", reflect.TypeOf((*MockMultiplexerService_RegisterHandlerServer)(nil).SendMsg), m) +} + +// SetHeader mocks base method. +func (m *MockMultiplexerService_RegisterHandlerServer) SetHeader(arg0 metadata.MD) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "SetHeader", arg0) + ret0, _ := ret[0].(error) + return ret0 +} + +// SetHeader indicates an expected call of SetHeader. +func (mr *MockMultiplexerService_RegisterHandlerServerMockRecorder) SetHeader(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetHeader", reflect.TypeOf((*MockMultiplexerService_RegisterHandlerServer)(nil).SetHeader), arg0) +} + +// SetTrailer mocks base method. +func (m *MockMultiplexerService_RegisterHandlerServer) SetTrailer(arg0 metadata.MD) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "SetTrailer", arg0) +} + +// SetTrailer indicates an expected call of SetTrailer. +func (mr *MockMultiplexerService_RegisterHandlerServerMockRecorder) SetTrailer(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetTrailer", reflect.TypeOf((*MockMultiplexerService_RegisterHandlerServer)(nil).SetTrailer), arg0) +} diff --git a/pkg/services/error.go b/pkg/services/error.go new file mode 100644 index 0000000..2088d15 --- /dev/null +++ b/pkg/services/error.go @@ -0,0 +1,24 @@ +package services + +// error is the shared wrapper to be used for errors returned by services +type svcError struct { + Wrapped error + Prefix string + Message string +} + +func (e *svcError) Unwrap() error { + return e.Wrapped +} + +func (e *svcError) Error() string { + return e.Message +} + +func wrap(e error, p, m string) *svcError { + return &svcError{ + Wrapped: e, + Prefix: p, + Message: m, + } +} diff --git a/pkg/services/registrar.go b/pkg/services/registrar.go new file mode 100644 index 0000000..679158a --- /dev/null +++ b/pkg/services/registrar.go @@ -0,0 +1,90 @@ +package services + +import ( + "context" + "encoding/json" + "errors" + "regexp" + + "github.com/bbralion/CTFloodBot/internal/genproto" + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" +) + +// Registrar allows registration of command handlers for subsequent receival of updates +type Registrar interface { + // Register registers a new command handler with the given name and matchers. + // The context should span the lifetime of the registered handler and canceled when it dies. + Register(ctx context.Context, name string, matchers []regexp.Regexp) (tgbotapi.UpdatesChannel, <-chan error, error) +} + +// gRPCRegistrar is an implementation of Registrar using grpc +type gRPCRegistrar struct { + client genproto.MultiplexerServiceClient +} + +func (r *gRPCRegistrar) Register(ctx context.Context, name string, matchers []regexp.Regexp) (tgbotapi.UpdatesChannel, <-chan error, error) { + if len(matchers) < 1 { + return nil, nil, errors.New("cannot register handler with no matchers") + } + + request := &genproto.RegisterRequest{ + Name: name, + Matchers: make([]string, len(matchers)), + } + for i, m := range matchers { + request.Matchers[i] = m.String() + } + + stream, err := r.client.RegisterHandler(ctx, request) + if err != nil { + return nil, nil, wrap(err, "RegisterHandler request failed", "failed to register command handler") + } + + updatech := make(chan tgbotapi.Update) + errorch := make(chan error, 1) + go func() { + defer close(updatech) + defer close(errorch) + // Should only be used once as errorch has capacity of 1 + sendError := func(err error, info string) { + wrerr := wrap(err, info, "failed to receive updates") + errorch <- wrerr + } + + for { + updatePB, err := stream.Recv() + if err != nil { + if s, ok := status.FromError(err); ok && (s.Code() == codes.Canceled || s.Code() == codes.DeadlineExceeded) { + // If the updates are simply stopped, then no error has happened + return + } + sendError(err, "unexpected error receiving next update") + return + } + + var update tgbotapi.Update + if err := json.Unmarshal(updatePB.GetJson(), &update); err != nil { + sendError(err, "failed to unmarshal json update") + return + } + + select { + case updatech <- update: + case <-ctx.Done(): + return + } + } + }() + + return updatech, errorch, nil +} + +// NewGRPCRegistrar creates a Registrar based on the gRPC API client +func NewGRPCRegistrar(client genproto.MultiplexerServiceClient) (Registrar, error) { + if client == nil { + return nil, errors.New("gRPC client must not be nil") + } + return &gRPCRegistrar{client}, nil +} diff --git a/pkg/services/registrar_test.go b/pkg/services/registrar_test.go new file mode 100644 index 0000000..2fae1de --- /dev/null +++ b/pkg/services/registrar_test.go @@ -0,0 +1,92 @@ +package services + +import ( + "context" + "encoding/json" + "errors" + "regexp" + "testing" + "time" + + "github.com/bbralion/CTFloodBot/internal/genproto" + "github.com/bbralion/CTFloodBot/internal/mockproto" + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/require" + "google.golang.org/grpc/status" +) + +func TestGRPCRegistrarRegister(t *testing.T) { + ctrl := gomock.NewController(t) + req := require.New(t) + + // Creation of registrar + mockMuxClient := mockproto.NewMockMultiplexerServiceClient(ctrl) + registrar, err := NewGRPCRegistrar(mockMuxClient) + req.NoError(err, "registrar creation shouldn't fail") + + // Registration without any matchers + ctx, name := context.Background(), "fake" + _, _, err = registrar.Register(ctx, name, []regexp.Regexp{}) + req.Error(err, "shouldn't be able to register with no matchers") + + // Failed registration request + mockMuxClient.EXPECT().RegisterHandler(ctx, &genproto.RegisterRequest{ + Name: name, + Matchers: []string{"/command"}, + }).Return(nil, errors.New("fake register error")) + _, _, err = registrar.Register(ctx, name, []regexp.Regexp{*regexp.MustCompile("/command")}) + req.Error(err, "shouldn't be able to continue if register request fails") + + // Successful registration with single update + var tgUpdate tgbotapi.Update + tgUpdate.Message = &tgbotapi.Message{ + Text: "message text", + } + tgUpdateBytes, err := json.Marshal(tgUpdate) + req.NoError(err, "should be able to marshal telegram update") + + ctx, cancel := context.WithCancel(ctx) + mockUpdateStream := mockproto.NewMockMultiplexerService_RegisterHandlerClient(ctrl) + mockMuxClient.EXPECT().RegisterHandler(ctx, &genproto.RegisterRequest{ + Name: name, + Matchers: []string{"/command"}, + }).Return(mockUpdateStream, nil) + mockUpdateStream.EXPECT().Recv().Return(&genproto.Update{Json: tgUpdateBytes}, nil) + mockUpdateStream.EXPECT().Recv().Return(nil, status.FromContextError(context.Canceled).Err()) + + updatech, errorch, err := registrar.Register(ctx, name, []regexp.Regexp{*regexp.MustCompile("/command")}) + req.NoError(err, "registration shouldn't fail") + + var update tgbotapi.Update + req.Eventually(func() bool { + select { + case update = <-updatech: + return true + default: + return false + } + }, time.Second, time.Millisecond*50, "expected update on channel") + req.Equal(tgUpdate, update, "received incorrect update") + cancel() + req.Eventually(func() bool { + select { + case update, ok := <-updatech: + if ok { + req.Fail("received unexpected update on channel", update) + } + default: + return false + } + + select { + case err, ok := <-errorch: + if ok { + req.Fail("received unexpected error on channel", `errs: "%v" "%v"`, err, errors.Unwrap(err)) + } + default: + return false + } + return true + }, time.Second, time.Millisecond*50, "expected updater goroutine to shut down and close channels") +} From 20149f3082738ec0f2ed826d702f515483cec72d Mon Sep 17 00:00:00 2001 From: renbou Date: Thu, 28 Jul 2022 03:50:18 +0300 Subject: [PATCH 03/17] feat: registrar and multiplexer done --- .golangci.yml | 1 - Makefile | 5 +- pkg/services/models.go | 28 ++-- pkg/services/multiplexer.go | 87 +++++++++++ pkg/services/multiplexer_test.go | 69 ++++++++ pkg/services/registrar.go | 9 +- pkg/services/registrar_test.go | 10 +- pkg/services/services.go | 261 ------------------------------- 8 files changed, 188 insertions(+), 282 deletions(-) create mode 100644 pkg/services/multiplexer.go create mode 100644 pkg/services/multiplexer_test.go delete mode 100644 pkg/services/services.go diff --git a/.golangci.yml b/.golangci.yml index ec7c97b..5300f90 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -5,7 +5,6 @@ linters: enable: - asciicheck - bodyclose - - containedctx - contextcheck - deadcode - dogsled diff --git a/Makefile b/Makefile index ff17ace..c366c52 100644 --- a/Makefile +++ b/Makefile @@ -19,4 +19,7 @@ mocks: .PHONY: test test: - go test -race ./... \ No newline at end of file + go test -race ./... +.PHONY: lint +lint: + golangci-lint run -v \ No newline at end of file diff --git a/pkg/services/models.go b/pkg/services/models.go index 0ca20f1..ed653a1 100644 --- a/pkg/services/models.go +++ b/pkg/services/models.go @@ -1,15 +1,23 @@ package services -import "time" +import ( + "regexp" -type RegisterHandlerRequest struct { - Name string `json:"name"` - Matchers []string `json:"matchers"` -} + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" +) + +type ( + UpdateChan tgbotapi.UpdatesChannel + ErrorChan <-chan error +) + +type MatcherGroup []*regexp.Regexp -type RegisterHandlerResponse struct { - // Must be consistent on renewals - Channel string `json:"channel"` - // Deadline is expected to be at least 30 seconds from now - Deadline time.Time `json:"deadline"` +func (g MatcherGroup) MatchString(s string) bool { + for _, m := range g { + if m.MatchString(s) { + return true + } + } + return false } diff --git a/pkg/services/multiplexer.go b/pkg/services/multiplexer.go new file mode 100644 index 0000000..9657529 --- /dev/null +++ b/pkg/services/multiplexer.go @@ -0,0 +1,87 @@ +package services + +import ( + "context" + "sync" + "sync/atomic" + + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" +) + +// Multiplexer allows multiplexing various update handlers based on matchers +type Multiplexer interface { + // Register registers a new handler which will receive updates until the context is canceled. + // Safe for concurrent use, so matchers can be registered from anywhere. + Register(ctx context.Context, matchers MatcherGroup) (UpdateChan, error) + // Serve multiplexes the update across the registered handlers. + // Isn't safe for concurrent use, so all calls to Serve must be from a single goroutine. + Serve(update tgbotapi.Update) +} + +type ( + muxKey uint64 + muxHandler struct { + ctx context.Context + matchers MatcherGroup + channel chan tgbotapi.Update + } +) + +// mapMux is a default implementation of Multiplexer +type mapMux struct { + curKey muxKey + store sync.Map + bufferLen int +} + +func (m *mapMux) Register(ctx context.Context, matchers MatcherGroup) (UpdateChan, error) { + if len(matchers) < 1 { + return nil, ErrNoMatchers + } + + key := muxKey(atomic.AddUint64((*uint64)(&m.curKey), 1)) + h := &muxHandler{ctx, matchers, make(chan tgbotapi.Update, m.bufferLen)} + + m.store.Store(key, h) + return h.channel, nil +} + +func (m *mapMux) delete(key muxKey, h *muxHandler) { + m.store.Delete(key) + close(h.channel) +} + +func (m *mapMux) Serve(update tgbotapi.Update) { + // Currently only messages are supported + if update.Message == nil { + return + } + + m.store.Range(func(key, value any) bool { + mkey, mvalue := key.(muxKey), value.(*muxHandler) + + // Fail-fast if the handler is already dead + select { + case <-mvalue.ctx.Done(): + m.delete(mkey, mvalue) + return true + default: + } + + // Match and try to send if needed + if mvalue.matchers.MatchString(update.Message.Text) { + select { + case <-mvalue.ctx.Done(): + m.delete(mkey, mvalue) + case mvalue.channel <- update: + } + } + return true + }) +} + +// NewMultiplexer creates a new multiplexer with the +// specified buffer size of created update channels +func NewMultiplexer(bufferLen int) Multiplexer { + return &mapMux{bufferLen: bufferLen} +} diff --git a/pkg/services/multiplexer_test.go b/pkg/services/multiplexer_test.go new file mode 100644 index 0000000..7061f0a --- /dev/null +++ b/pkg/services/multiplexer_test.go @@ -0,0 +1,69 @@ +package services + +import ( + "context" + "regexp" + "sync" + "testing" + "time" + + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" + "github.com/stretchr/testify/require" +) + +func startExpectingMuxClient(wg *sync.WaitGroup, req *require.Assertions, mux Multiplexer, updates []tgbotapi.Update, matchers MatcherGroup) { + ctx, cancel := context.WithCancel(context.Background()) + ch, err := mux.Register(ctx, matchers) + req.NoError(err, "should register without error") + + wg.Add(1) + go func() { + defer wg.Done() + for i := range updates { + update, ok := <-ch + req.True(ok, "wanted %d updates but got %d", len(updates), i) + + // Currently only test message matches, since no others are supported + req.True(matchers.MatchString(update.Message.Text), "got non-matching update") + req.Equal(updates[i], update, "invalid order of updates") + } + cancel() + // Channel should close on next send + req.Eventually(func() bool { + _, ok := <-ch + return !ok + }, time.Second*10, time.Millisecond*50) + }() +} + +func TestMultiplexer(t *testing.T) { + req := require.New(t) + + updates := make([]tgbotapi.Update, 0, 13) + for _, text := range []string{"/a", "/b", "/c", "/d", "/e", "/f", "/g", "/h", "/j", "/aboba", "/sus", "/0", "/aboba"} { + updates = append(updates, tgbotapi.Update{Message: &tgbotapi.Message{Text: text}}) + } + + var wg sync.WaitGroup + mux := NewMultiplexer(1) + startExpectingMuxClient(&wg, req, mux, updates[:2], MatcherGroup{regexp.MustCompile("^/[ab]$")}) + startExpectingMuxClient(&wg, req, mux, updates[:6], MatcherGroup{regexp.MustCompile("^/[a-f]$")}) + startExpectingMuxClient(&wg, req, mux, updates[:9], MatcherGroup{regexp.MustCompile("^/[a-j]$")}) + startExpectingMuxClient(&wg, req, mux, updates[:9], MatcherGroup{regexp.MustCompile(".*")}) + startExpectingMuxClient(&wg, req, mux, []tgbotapi.Update{ + {Message: &tgbotapi.Message{Text: "/aboba"}}, + {Message: &tgbotapi.Message{Text: "/sus"}}, + {Message: &tgbotapi.Message{Text: "/aboba"}}, + }, MatcherGroup{regexp.MustCompile("^/(aboba|sus)$")}) + + for _, update := range updates { + mux.Serve(update) + } + + // Wait for all clients to finish + time.Sleep(time.Second * 2) + + // serve one last fake update to close the channels + mux.Serve(updates[0]) + wg.Wait() +} diff --git a/pkg/services/registrar.go b/pkg/services/registrar.go index 679158a..af06160 100644 --- a/pkg/services/registrar.go +++ b/pkg/services/registrar.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "errors" - "regexp" "github.com/bbralion/CTFloodBot/internal/genproto" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" @@ -12,11 +11,13 @@ import ( "google.golang.org/grpc/status" ) +var ErrNoMatchers = errors.New("cannot register with zero matchers") + // Registrar allows registration of command handlers for subsequent receival of updates type Registrar interface { // Register registers a new command handler with the given name and matchers. // The context should span the lifetime of the registered handler and canceled when it dies. - Register(ctx context.Context, name string, matchers []regexp.Regexp) (tgbotapi.UpdatesChannel, <-chan error, error) + Register(ctx context.Context, name string, matchers MatcherGroup) (UpdateChan, ErrorChan, error) } // gRPCRegistrar is an implementation of Registrar using grpc @@ -24,9 +25,9 @@ type gRPCRegistrar struct { client genproto.MultiplexerServiceClient } -func (r *gRPCRegistrar) Register(ctx context.Context, name string, matchers []regexp.Regexp) (tgbotapi.UpdatesChannel, <-chan error, error) { +func (r *gRPCRegistrar) Register(ctx context.Context, name string, matchers MatcherGroup) (UpdateChan, ErrorChan, error) { if len(matchers) < 1 { - return nil, nil, errors.New("cannot register handler with no matchers") + return nil, nil, ErrNoMatchers } request := &genproto.RegisterRequest{ diff --git a/pkg/services/registrar_test.go b/pkg/services/registrar_test.go index 2fae1de..42b2775 100644 --- a/pkg/services/registrar_test.go +++ b/pkg/services/registrar_test.go @@ -16,7 +16,7 @@ import ( "google.golang.org/grpc/status" ) -func TestGRPCRegistrarRegister(t *testing.T) { +func TestGRPCRegistrar(t *testing.T) { ctrl := gomock.NewController(t) req := require.New(t) @@ -27,15 +27,15 @@ func TestGRPCRegistrarRegister(t *testing.T) { // Registration without any matchers ctx, name := context.Background(), "fake" - _, _, err = registrar.Register(ctx, name, []regexp.Regexp{}) - req.Error(err, "shouldn't be able to register with no matchers") + _, _, err = registrar.Register(ctx, name, MatcherGroup{}) + req.ErrorIs(err, ErrNoMatchers, "shouldn't be able to register with no matchers") // Failed registration request mockMuxClient.EXPECT().RegisterHandler(ctx, &genproto.RegisterRequest{ Name: name, Matchers: []string{"/command"}, }).Return(nil, errors.New("fake register error")) - _, _, err = registrar.Register(ctx, name, []regexp.Regexp{*regexp.MustCompile("/command")}) + _, _, err = registrar.Register(ctx, name, MatcherGroup{regexp.MustCompile("/command")}) req.Error(err, "shouldn't be able to continue if register request fails") // Successful registration with single update @@ -55,7 +55,7 @@ func TestGRPCRegistrarRegister(t *testing.T) { mockUpdateStream.EXPECT().Recv().Return(&genproto.Update{Json: tgUpdateBytes}, nil) mockUpdateStream.EXPECT().Recv().Return(nil, status.FromContextError(context.Canceled).Err()) - updatech, errorch, err := registrar.Register(ctx, name, []regexp.Regexp{*regexp.MustCompile("/command")}) + updatech, errorch, err := registrar.Register(ctx, name, MatcherGroup{regexp.MustCompile("/command")}) req.NoError(err, "registration shouldn't fail") var update tgbotapi.Update diff --git a/pkg/services/services.go b/pkg/services/services.go deleted file mode 100644 index 26c3ecb..0000000 --- a/pkg/services/services.go +++ /dev/null @@ -1,261 +0,0 @@ -package services - -import ( - "bytes" - "context" - "encoding/json" - "errors" - "fmt" - "io" - "net/http" - "net/url" - "path" - "regexp" - "time" - - "github.com/go-redis/redis/v8" - tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" - "go.uber.org/zap" -) - -// HandlerRegisterer allows registration of command handlers for subsequent receival of updates -type HandlerRegisterer interface { - // RegisterHandler registers a new command handler with the given name and matchers. - // The context should span the lifetime of the registered handler and canceled when it dies. - RegisterHandler(ctx context.Context, name string, matchers []regexp.Regexp) (tgbotapi.UpdatesChannel, error) -} - -// Proxy is a HandlerRegisterer which also handles bot API initialization -type Proxy interface { - HandlerRegisterer - InitBotAPI() (*tgbotapi.BotAPI, error) -} - -// renewalCoef defines how soon a handler's registration is -// renewed compared to the actual deadline -const renewalCoef = 2 - -// tgAPIError is an internal error wrapper for errors which -// occurred during registration, updates, etc -type tgAPIError struct { - wrapped error - message string - operation string -} - -func (e *tgAPIError) Unwrap() error { - return e.wrapped -} - -func (e *tgAPIError) Error() string { - return e.message -} - -func logTgAPIError(logger *zap.Logger, e *tgAPIError) { - logger.Warn(e.message, zap.String("operation", e.operation), zap.Error(e.wrapped)) -} - -// redisHTTPProxy implements registration using http and update receival using redis -type redisHTTPProxy struct { - r *redis.Client - h *http.Client - l *zap.Logger - endpoint *url.URL -} - -// internalHTTPTransport is a roundtripper for the http proxy -// with added authorization and error handling -type internalHTTPTransport struct { - http.RoundTripper - logger *zap.Logger - token string -} - -func (t *internalHTTPTransport) RoundTrip(r *http.Request) (*http.Response, error) { - r.Header.Set("Authorization", t.token) - resp, err := t.RoundTripper.RoundTrip(r) - if err != nil { - err := &tgAPIError{ - wrapped: err, - message: "roundtripping request to bot proxy failed", - operation: "roundtrip", - } - logTgAPIError(t.logger, err) - return nil, err - } - - if resp.StatusCode == http.StatusForbidden || resp.StatusCode == http.StatusUnauthorized { - return nil, errors.New("request to bot proxy failed: unauthorized") - } - return resp, nil -} - -// RedisHTTPConfig specifies the configuration of the redis-http-based proxy. -// All fields are expected to be set unless specified otherwise. -type RedisHTTPConfig struct { - Logger *zap.Logger - Redis *redis.Client - // RoundTripper can be nil, in which case http.DefaultTransport will be used - RoundTripper http.RoundTripper - // Token is the authorization token for the http API - Token string - // Endpoint of the http API - Endpoint *url.URL -} - -// NewRedisHTTPProxy constructs a new redis-http-based proxy -func NewRedisHTTPProxy(config *RedisHTTPConfig) (Proxy, error) { - if config.Logger == nil || config.Redis == nil { - return nil, errors.New("unable to create registerer without required components") - } else if config.Token == "" || config.Endpoint == nil { - return nil, errors.New("token and endpoint of http API must be set") - } - - transport := config.RoundTripper - if transport == nil { - transport = http.DefaultTransport - } - return &redisHTTPProxy{ - r: config.Redis, - h: &http.Client{ - Transport: &internalHTTPTransport{ - RoundTripper: transport, - logger: config.Logger, - token: config.Token, - }, - }, - l: config.Logger, - endpoint: config.Endpoint, - }, nil -} - -func urlJoin(base *url.URL, relative ...string) string { - cp := *base - cp.RawPath = "" - cp.Path = path.Join(append([]string{cp.Path}, relative...)...) - return cp.String() -} - -func (p *redisHTTPProxy) updateRegistration(ctx context.Context, request *RegisterHandlerRequest) (*RegisterHandlerResponse, error) { - b, err := json.Marshal(request) - if err != nil { - return nil, fmt.Errorf("marshaling register request failed: %w", err) - } - buffer := bytes.NewBuffer(b) - - // Do registration request - httpreq, err := http.NewRequestWithContext(ctx, "POST", urlJoin(p.endpoint, "internal", "register"), buffer) - if err != nil { - return nil, fmt.Errorf("http request construction failed: %w", err) - } - httpreq.Header.Set("Content-Type", "application/json") - - httpresp, err := p.h.Do(httpreq) - if err != nil { - return nil, fmt.Errorf("registration request failed: %w", err) - } - - // Ensure that registration was successful - body, err := io.ReadAll(httpresp.Body) - if err != nil { - return nil, fmt.Errorf("reading response failed: %w", err) - } - - if httpresp.StatusCode != http.StatusOK && httpresp.StatusCode != http.StatusCreated { - return nil, fmt.Errorf("proxy responded with bad status code (%d): %s", httpresp.StatusCode, body) - } - - var resp RegisterHandlerResponse - if err := json.Unmarshal(body, &resp); err != nil { - return nil, fmt.Errorf("failed to unmarshal response body %s: %w", body, err) - } - return &resp, nil -} - -func (p *redisHTTPProxy) RegisterHandler(ctx context.Context, name string, matchers []regexp.Regexp) (tgbotapi.UpdatesChannel, error) { - var apiError *tgAPIError - defer func() { - if apiError != nil { - logTgAPIError(p.l, apiError) - } - }() - - // Construct registration to be used for all future registrations - request := RegisterHandlerRequest{ - Name: name, - Matchers: make([]string, len(matchers)), - } - for i, m := range matchers { - request.Matchers[i] = m.String() - } - - response, err := p.updateRegistration(ctx, &request) - if err != nil { - apiError = &tgAPIError{ - wrapped: err, - message: "failed to register handler", - operation: "registration", - } - return nil, apiError - } - getRenewalCh := func() <-chan time.Time { - return time.After(time.Until(response.Deadline) / renewalCoef) - } - - updates := make(chan tgbotapi.Update) - go func() { - defer close(updates) - - // Subscribe to updates - subscriber := p.r.Subscribe(ctx, response.Channel) - - // Wait until we have to renew or are killed - renewalCh := getRenewalCh() - for { - select { - case <-ctx.Done(): - return - case message := <-subscriber.Channel(): - var update tgbotapi.Update - if err := json.Unmarshal([]byte(message.Payload), &update); err != nil { - logTgAPIError(p.l, &tgAPIError{ - wrapped: err, - message: fmt.Sprintf("failed to unmarshal update message (%s)", message.Payload), - operation: "update", - }) - return - } - - updates <- update - case <-renewalCh: - // renew our registration and setup the renewal channel again - if response, err = p.updateRegistration(ctx, &request); err != nil { - logTgAPIError(p.l, &tgAPIError{ - wrapped: err, - message: "failed to renew handler", - operation: "renewal", - }) - return - } - renewalCh = getRenewalCh() - } - } - }() - - return updates, nil -} - -func (p *redisHTTPProxy) InitBotAPI() (*tgbotapi.BotAPI, error) { - // construct bot api format with meaningless token - bot, err := tgbotapi.NewBotAPIWithClient("fake-token", urlJoin(p.endpoint, "proxy%s", "%s"), p.h) - if err != nil { - err := &tgAPIError{ - wrapped: err, - message: "failed to initialize bot API", - operation: "init", - } - logTgAPIError(p.l, err) - return nil, err - } - return bot, nil -} From b9dc29529d8cca3f9ddc74e343845cdc0e40648e Mon Sep 17 00:00:00 2001 From: renbou Date: Thu, 28 Jul 2022 05:01:42 +0300 Subject: [PATCH 04/17] refactor: remove name from register request --- api/proto/mux.proto | 3 +-- internal/genproto/mux.pb.go | 37 +++++++++++++--------------------- pkg/services/registrar.go | 7 +++---- pkg/services/registrar_test.go | 10 ++++----- 4 files changed, 22 insertions(+), 35 deletions(-) diff --git a/api/proto/mux.proto b/api/proto/mux.proto index 9f996fd..18d4a28 100644 --- a/api/proto/mux.proto +++ b/api/proto/mux.proto @@ -18,8 +18,7 @@ message ConfigResponse { } message RegisterRequest { - string name = 1; - repeated string matchers = 2; + repeated string matchers = 1; } service MultiplexerService { diff --git a/internal/genproto/mux.pb.go b/internal/genproto/mux.pb.go index 7c87559..c20e285 100644 --- a/internal/genproto/mux.pb.go +++ b/internal/genproto/mux.pb.go @@ -206,8 +206,7 @@ type RegisterRequest struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` - Matchers []string `protobuf:"bytes,2,rep,name=matchers,proto3" json:"matchers,omitempty"` + Matchers []string `protobuf:"bytes,1,rep,name=matchers,proto3" json:"matchers,omitempty"` } func (x *RegisterRequest) Reset() { @@ -242,13 +241,6 @@ func (*RegisterRequest) Descriptor() ([]byte, []int) { return file_mux_proto_rawDescGZIP(), []int{4} } -func (x *RegisterRequest) GetName() string { - if x != nil { - return x.Name - } - return "" -} - func (x *RegisterRequest) GetMatchers() []string { if x != nil { return x.Matchers @@ -269,20 +261,19 @@ var file_mux_proto_rawDesc = []byte{ 0x22, 0x35, 0x0a, 0x0e, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x23, 0x0a, 0x06, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0b, 0x2e, 0x6d, 0x75, 0x78, 0x2e, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x52, - 0x06, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x22, 0x41, 0x0a, 0x0f, 0x52, 0x65, 0x67, 0x69, 0x73, - 0x74, 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, - 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x1a, - 0x0a, 0x08, 0x6d, 0x61, 0x74, 0x63, 0x68, 0x65, 0x72, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x09, - 0x52, 0x08, 0x6d, 0x61, 0x74, 0x63, 0x68, 0x65, 0x72, 0x73, 0x32, 0x82, 0x01, 0x0a, 0x12, 0x4d, - 0x75, 0x6c, 0x74, 0x69, 0x70, 0x6c, 0x65, 0x78, 0x65, 0x72, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, - 0x65, 0x12, 0x34, 0x0a, 0x09, 0x47, 0x65, 0x74, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, 0x12, - 0x2e, 0x6d, 0x75, 0x78, 0x2e, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x52, 0x65, 0x71, 0x75, 0x65, - 0x73, 0x74, 0x1a, 0x13, 0x2e, 0x6d, 0x75, 0x78, 0x2e, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x52, - 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x36, 0x0a, 0x0f, 0x52, 0x65, 0x67, 0x69, 0x73, - 0x74, 0x65, 0x72, 0x48, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x72, 0x12, 0x14, 0x2e, 0x6d, 0x75, 0x78, - 0x2e, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, - 0x1a, 0x0b, 0x2e, 0x6d, 0x75, 0x78, 0x2e, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x30, 0x01, 0x62, - 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x06, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x22, 0x2d, 0x0a, 0x0f, 0x52, 0x65, 0x67, 0x69, 0x73, + 0x74, 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x1a, 0x0a, 0x08, 0x6d, 0x61, + 0x74, 0x63, 0x68, 0x65, 0x72, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x09, 0x52, 0x08, 0x6d, 0x61, + 0x74, 0x63, 0x68, 0x65, 0x72, 0x73, 0x32, 0x82, 0x01, 0x0a, 0x12, 0x4d, 0x75, 0x6c, 0x74, 0x69, + 0x70, 0x6c, 0x65, 0x78, 0x65, 0x72, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x34, 0x0a, + 0x09, 0x47, 0x65, 0x74, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, 0x12, 0x2e, 0x6d, 0x75, 0x78, + 0x2e, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x13, + 0x2e, 0x6d, 0x75, 0x78, 0x2e, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x52, 0x65, 0x73, 0x70, 0x6f, + 0x6e, 0x73, 0x65, 0x12, 0x36, 0x0a, 0x0f, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x48, + 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x72, 0x12, 0x14, 0x2e, 0x6d, 0x75, 0x78, 0x2e, 0x52, 0x65, 0x67, + 0x69, 0x73, 0x74, 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x0b, 0x2e, 0x6d, + 0x75, 0x78, 0x2e, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x30, 0x01, 0x62, 0x06, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x33, } var ( diff --git a/pkg/services/registrar.go b/pkg/services/registrar.go index af06160..1e1b214 100644 --- a/pkg/services/registrar.go +++ b/pkg/services/registrar.go @@ -15,9 +15,9 @@ var ErrNoMatchers = errors.New("cannot register with zero matchers") // Registrar allows registration of command handlers for subsequent receival of updates type Registrar interface { - // Register registers a new command handler with the given name and matchers. + // Register registers a new command handler with the given matchers. // The context should span the lifetime of the registered handler and canceled when it dies. - Register(ctx context.Context, name string, matchers MatcherGroup) (UpdateChan, ErrorChan, error) + Register(ctx context.Context, matchers MatcherGroup) (UpdateChan, ErrorChan, error) } // gRPCRegistrar is an implementation of Registrar using grpc @@ -25,13 +25,12 @@ type gRPCRegistrar struct { client genproto.MultiplexerServiceClient } -func (r *gRPCRegistrar) Register(ctx context.Context, name string, matchers MatcherGroup) (UpdateChan, ErrorChan, error) { +func (r *gRPCRegistrar) Register(ctx context.Context, matchers MatcherGroup) (UpdateChan, ErrorChan, error) { if len(matchers) < 1 { return nil, nil, ErrNoMatchers } request := &genproto.RegisterRequest{ - Name: name, Matchers: make([]string, len(matchers)), } for i, m := range matchers { diff --git a/pkg/services/registrar_test.go b/pkg/services/registrar_test.go index 42b2775..0250a7f 100644 --- a/pkg/services/registrar_test.go +++ b/pkg/services/registrar_test.go @@ -26,16 +26,15 @@ func TestGRPCRegistrar(t *testing.T) { req.NoError(err, "registrar creation shouldn't fail") // Registration without any matchers - ctx, name := context.Background(), "fake" - _, _, err = registrar.Register(ctx, name, MatcherGroup{}) + ctx := context.Background() + _, _, err = registrar.Register(ctx, MatcherGroup{}) req.ErrorIs(err, ErrNoMatchers, "shouldn't be able to register with no matchers") // Failed registration request mockMuxClient.EXPECT().RegisterHandler(ctx, &genproto.RegisterRequest{ - Name: name, Matchers: []string{"/command"}, }).Return(nil, errors.New("fake register error")) - _, _, err = registrar.Register(ctx, name, MatcherGroup{regexp.MustCompile("/command")}) + _, _, err = registrar.Register(ctx, MatcherGroup{regexp.MustCompile("/command")}) req.Error(err, "shouldn't be able to continue if register request fails") // Successful registration with single update @@ -49,13 +48,12 @@ func TestGRPCRegistrar(t *testing.T) { ctx, cancel := context.WithCancel(ctx) mockUpdateStream := mockproto.NewMockMultiplexerService_RegisterHandlerClient(ctrl) mockMuxClient.EXPECT().RegisterHandler(ctx, &genproto.RegisterRequest{ - Name: name, Matchers: []string{"/command"}, }).Return(mockUpdateStream, nil) mockUpdateStream.EXPECT().Recv().Return(&genproto.Update{Json: tgUpdateBytes}, nil) mockUpdateStream.EXPECT().Recv().Return(nil, status.FromContextError(context.Canceled).Err()) - updatech, errorch, err := registrar.Register(ctx, name, MatcherGroup{regexp.MustCompile("/command")}) + updatech, errorch, err := registrar.Register(ctx, MatcherGroup{regexp.MustCompile("/command")}) req.NoError(err, "registration shouldn't fail") var update tgbotapi.Update From a03ef31ebb81bc6767f2d8c0634f3d75937ddfc6 Mon Sep 17 00:00:00 2001 From: renbou Date: Thu, 28 Jul 2022 06:19:54 +0300 Subject: [PATCH 05/17] feat: implement retries for grpc registrar --- Makefile | 2 +- pkg/retry/retry.go | 69 ++++++++++++++++++++++ pkg/retry/retry_test.go | 35 +++++++++++ pkg/services/error.go | 10 +++- pkg/services/registrar.go | 105 ++++++++++++++++++++++----------- pkg/services/registrar_test.go | 23 +++++--- 6 files changed, 198 insertions(+), 46 deletions(-) create mode 100644 pkg/retry/retry.go create mode 100644 pkg/retry/retry_test.go diff --git a/Makefile b/Makefile index c366c52..ffd08cf 100644 --- a/Makefile +++ b/Makefile @@ -19,7 +19,7 @@ mocks: .PHONY: test test: - go test -race ./... + go test -v -race -count=1 ./... .PHONY: lint lint: golangci-lint run -v \ No newline at end of file diff --git a/pkg/retry/retry.go b/pkg/retry/retry.go new file mode 100644 index 0000000..8b07818 --- /dev/null +++ b/pkg/retry/retry.go @@ -0,0 +1,69 @@ +package retry + +import ( + "errors" + "fmt" + "time" +) + +const ( + DefaultBackoffMinDelay = time.Millisecond * 50 + DefaultBackoffMaxDelay = time.Minute * 10 + DefaultBackoffFactor = 2 +) + +type RecoverFunc func() error + +type unrecoverableError struct { + wrapped error +} + +func (e unrecoverableError) Error() string { + return fmt.Sprintf("unrecoverable error: %s", e.Error()) +} + +func (e unrecoverableError) Unwrap() error { + return e.wrapped +} + +// Unrecoverable wraps an error to indicate that it is not recoverable from, +// after which retries will be stopped and it will be returned +func Unrecoverable(err error) error { + return unrecoverableError{err} +} + +// Backoff runs the function using the backoff retry algorithm +func Backoff(f RecoverFunc) error { + delay := DefaultBackoffMinDelay + for { + var ue unrecoverableError + if err := f(); err == nil { + return nil + } else if errors.As(err, &ue) { + return ue.Unwrap() + } + + time.Sleep(delay) + + delay *= DefaultBackoffFactor + if delay > DefaultBackoffMaxDelay { + delay = DefaultBackoffMaxDelay + } + } +} + +const DefaultStaticDelay = time.Second + +// Static runs the function using a static retry delay +func Static(f RecoverFunc) error { + for { + var ue unrecoverableError + if err := f(); err == nil { + return nil + } else if errors.As(err, &ue) { + return ue.Unwrap() + } + + time.Sleep(DefaultStaticDelay) + } +} diff --git a/pkg/retry/retry_test.go b/pkg/retry/retry_test.go new file mode 100644 index 0000000..72ddb50 --- /dev/null +++ b/pkg/retry/retry_test.go @@ -0,0 +1,35 @@ +package retry + +import ( + "errors" + "testing" + + "github.com/stretchr/testify/require" +) + +func assertNumCallsFunc(req *require.Assertions, n int, err error) RecoverFunc { + ctr := 0 + return func() error { + req.Less(ctr, n, "should be called %d times at most", n) + ctr++ + if ctr == n { + return err + } + return errors.New("fake recoverable error") + } +} + +func testStrategy(req *require.Assertions, n int, strategy func(RecoverFunc) error) { + req.NoError(strategy(assertNumCallsFunc(req, n, nil))) + e := errors.New("fake unrecoverable error") + req.ErrorIs(e, strategy(assertNumCallsFunc(req, n, Unrecoverable(e)))) +} + +func TestRetry(t *testing.T) { + req := require.New(t) + + for i := 1; i < 4; i++ { + testStrategy(req, i, Backoff) + testStrategy(req, i, Static) + } +} diff --git a/pkg/services/error.go b/pkg/services/error.go index 2088d15..eb3f770 100644 --- a/pkg/services/error.go +++ b/pkg/services/error.go @@ -1,9 +1,11 @@ package services +import "go.uber.org/zap" + // error is the shared wrapper to be used for errors returned by services type svcError struct { Wrapped error - Prefix string + Info string Message string } @@ -15,10 +17,14 @@ func (e *svcError) Error() string { return e.Message } +func (e *svcError) ZapFields() []zap.Field { + return []zap.Field{zap.Error(e.Unwrap()), zap.String("info", e.Info), zap.String("message", e.Message)} +} + func wrap(e error, p, m string) *svcError { return &svcError{ Wrapped: e, - Prefix: p, + Info: p, Message: m, } } diff --git a/pkg/services/registrar.go b/pkg/services/registrar.go index 1e1b214..ea7a007 100644 --- a/pkg/services/registrar.go +++ b/pkg/services/registrar.go @@ -6,7 +6,9 @@ import ( "errors" "github.com/bbralion/CTFloodBot/internal/genproto" + "github.com/bbralion/CTFloodBot/pkg/retry" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" + "go.uber.org/zap" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" ) @@ -20,11 +22,61 @@ type Registrar interface { Register(ctx context.Context, matchers MatcherGroup) (UpdateChan, ErrorChan, error) } -// gRPCRegistrar is an implementation of Registrar using grpc +// gRPCRegistrar is an implementation of Registrar using grpc with retries type gRPCRegistrar struct { + logger *zap.Logger client genproto.MultiplexerServiceClient } +func (r *gRPCRegistrar) tryRegister(ctx context.Context, request *genproto.RegisterRequest, updatech chan tgbotapi.Update) *svcError { + var stream genproto.MultiplexerService_RegisterHandlerClient + err := retry.Backoff(func() error { + var err error + stream, err = r.client.RegisterHandler(ctx, request) + + if err == nil { + return nil + } else if s, ok := status.FromError(err); ok && s.Code() == codes.Unavailable { + r.logger.Warn("gRPC registrar failed to connect", zap.Error(err)) + return err + } + return retry.Unrecoverable(err) + }) + if err != nil { + if s, ok := status.FromError(err); ok && (s.Code() == codes.Canceled || s.Code() == codes.DeadlineExceeded) { + // Client disconnected before we managed to register + return nil + } + return wrap(err, "RegisterHandler request failed", "failed to register command handler") + } + + wrapUpdateErr := func(err error, info string) *svcError { + return wrap(err, info, "failed to receive updates") + } + + for { + updatePB, err := stream.Recv() + if err != nil { + if s, ok := status.FromError(err); ok && (s.Code() == codes.Canceled || s.Code() == codes.DeadlineExceeded) { + // If the updates are simply stopped, then no error has happened + return nil + } + return wrapUpdateErr(err, "unexpected error receiving next update") + } + + var update tgbotapi.Update + if err := json.Unmarshal(updatePB.GetJson(), &update); err != nil { + return wrapUpdateErr(err, "failed to unmarshal json update") + } + + select { + case updatech <- update: + case <-ctx.Done(): + return nil + } + } +} + func (r *gRPCRegistrar) Register(ctx context.Context, matchers MatcherGroup) (UpdateChan, ErrorChan, error) { if len(matchers) < 1 { return nil, nil, ErrNoMatchers @@ -37,54 +89,39 @@ func (r *gRPCRegistrar) Register(ctx context.Context, matchers MatcherGroup) (Up request.Matchers[i] = m.String() } - stream, err := r.client.RegisterHandler(ctx, request) - if err != nil { - return nil, nil, wrap(err, "RegisterHandler request failed", "failed to register command handler") - } - updatech := make(chan tgbotapi.Update) errorch := make(chan error, 1) go func() { defer close(updatech) defer close(errorch) - // Should only be used once as errorch has capacity of 1 - sendError := func(err error, info string) { - wrerr := wrap(err, info, "failed to receive updates") - errorch <- wrerr - } - for { - updatePB, err := stream.Recv() - if err != nil { - if s, ok := status.FromError(err); ok && (s.Code() == codes.Canceled || s.Code() == codes.DeadlineExceeded) { - // If the updates are simply stopped, then no error has happened - return - } - sendError(err, "unexpected error receiving next update") - return + err := retry.Static(func() error { + err := r.tryRegister(ctx, request, updatech) + if err == nil { + return nil } - var update tgbotapi.Update - if err := json.Unmarshal(updatePB.GetJson(), &update); err != nil { - sendError(err, "failed to unmarshal json update") - return - } - - select { - case updatech <- update: - case <-ctx.Done(): - return + // We can retry only due to connectivity issues + if s, ok := status.FromError(err.Unwrap()); ok && s.Code() == codes.Unavailable { + r.logger.Warn("gRPC registrar reconnecting stream", err.ZapFields()...) + return err } + return retry.Unrecoverable(err) + }) + if err != nil { + // Recovery failed, log and return the error + serr := err.(*svcError) + r.logger.Error("gRPC registrar unable to reconnect", serr.ZapFields()...) + errorch <- serr } }() - return updatech, errorch, nil } -// NewGRPCRegistrar creates a Registrar based on the gRPC API client -func NewGRPCRegistrar(client genproto.MultiplexerServiceClient) (Registrar, error) { +// NewGRPCRegistrar creates a Registrar based on the gRPC API client with preconfigured retries +func NewGRPCRegistrar(logger *zap.Logger, client genproto.MultiplexerServiceClient) (Registrar, error) { if client == nil { return nil, errors.New("gRPC client must not be nil") } - return &gRPCRegistrar{client}, nil + return &gRPCRegistrar{logger, client}, nil } diff --git a/pkg/services/registrar_test.go b/pkg/services/registrar_test.go index 0250a7f..69859e0 100644 --- a/pkg/services/registrar_test.go +++ b/pkg/services/registrar_test.go @@ -13,16 +13,22 @@ import ( tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" "github.com/golang/mock/gomock" "github.com/stretchr/testify/require" + "go.uber.org/zap" + "google.golang.org/grpc/codes" "google.golang.org/grpc/status" ) func TestGRPCRegistrar(t *testing.T) { ctrl := gomock.NewController(t) req := require.New(t) + logcfg := zap.NewDevelopmentConfig() + logcfg.DisableStacktrace = true + logger, err := logcfg.Build() + req.NoError(err, "should be able to create logger") // Creation of registrar mockMuxClient := mockproto.NewMockMultiplexerServiceClient(ctrl) - registrar, err := NewGRPCRegistrar(mockMuxClient) + registrar, err := NewGRPCRegistrar(logger, mockMuxClient) req.NoError(err, "registrar creation shouldn't fail") // Registration without any matchers @@ -30,14 +36,7 @@ func TestGRPCRegistrar(t *testing.T) { _, _, err = registrar.Register(ctx, MatcherGroup{}) req.ErrorIs(err, ErrNoMatchers, "shouldn't be able to register with no matchers") - // Failed registration request - mockMuxClient.EXPECT().RegisterHandler(ctx, &genproto.RegisterRequest{ - Matchers: []string{"/command"}, - }).Return(nil, errors.New("fake register error")) - _, _, err = registrar.Register(ctx, MatcherGroup{regexp.MustCompile("/command")}) - req.Error(err, "shouldn't be able to continue if register request fails") - - // Successful registration with single update + // Two failed registration requests followed by a successful registration with single update var tgUpdate tgbotapi.Update tgUpdate.Message = &tgbotapi.Message{ Text: "message text", @@ -47,6 +46,12 @@ func TestGRPCRegistrar(t *testing.T) { ctx, cancel := context.WithCancel(ctx) mockUpdateStream := mockproto.NewMockMultiplexerService_RegisterHandlerClient(ctrl) + mockMuxClient.EXPECT().RegisterHandler(ctx, &genproto.RegisterRequest{ + Matchers: []string{"/command"}, + }).Return(nil, status.Error(codes.Unavailable, "fake network error 1")) + mockMuxClient.EXPECT().RegisterHandler(ctx, &genproto.RegisterRequest{ + Matchers: []string{"/command"}, + }).Return(nil, status.Error(codes.Unavailable, "fake network error 2")) mockMuxClient.EXPECT().RegisterHandler(ctx, &genproto.RegisterRequest{ Matchers: []string{"/command"}, }).Return(mockUpdateStream, nil) From 400606db522c8f5d220fad952a3036d1f5a03f80 Mon Sep 17 00:00:00 2001 From: renbou Date: Thu, 28 Jul 2022 08:21:03 +0300 Subject: [PATCH 06/17] feat: grpc authentication with AuthProvider --- .golangci.yml | 7 + Makefile | 1 + {pkg => internal}/services/error.go | 0 {pkg => internal}/services/models.go | 0 {pkg => internal}/services/multiplexer.go | 0 .../services/multiplexer_test.go | 0 {pkg => internal}/services/registrar.go | 5 +- {pkg => internal}/services/registrar_test.go | 26 ++- pkg/auth/auth.go | 151 ++++++++++++++++++ pkg/retry/retry.go | 7 +- pkg/services/authprovider.go | 32 ++++ 11 files changed, 225 insertions(+), 4 deletions(-) rename {pkg => internal}/services/error.go (100%) rename {pkg => internal}/services/models.go (100%) rename {pkg => internal}/services/multiplexer.go (100%) rename {pkg => internal}/services/multiplexer_test.go (100%) rename {pkg => internal}/services/registrar.go (97%) rename {pkg => internal}/services/registrar_test.go (79%) create mode 100644 pkg/auth/auth.go create mode 100644 pkg/services/authprovider.go diff --git a/.golangci.yml b/.golangci.yml index 5300f90..314eef8 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -45,3 +45,10 @@ linters-settings: check-exported: false unused: check-exported: false + wrapcheck: + ignoreSigs: + - .Errorf( + - errors.New( + - status.Error( + - retry.Recoverable( + - retry.Unrecoverable( diff --git a/Makefile b/Makefile index ffd08cf..4612645 100644 --- a/Makefile +++ b/Makefile @@ -20,6 +20,7 @@ mocks: .PHONY: test test: go test -v -race -count=1 ./... + .PHONY: lint lint: golangci-lint run -v \ No newline at end of file diff --git a/pkg/services/error.go b/internal/services/error.go similarity index 100% rename from pkg/services/error.go rename to internal/services/error.go diff --git a/pkg/services/models.go b/internal/services/models.go similarity index 100% rename from pkg/services/models.go rename to internal/services/models.go diff --git a/pkg/services/multiplexer.go b/internal/services/multiplexer.go similarity index 100% rename from pkg/services/multiplexer.go rename to internal/services/multiplexer.go diff --git a/pkg/services/multiplexer_test.go b/internal/services/multiplexer_test.go similarity index 100% rename from pkg/services/multiplexer_test.go rename to internal/services/multiplexer_test.go diff --git a/pkg/services/registrar.go b/internal/services/registrar.go similarity index 97% rename from pkg/services/registrar.go rename to internal/services/registrar.go index ea7a007..5b46a45 100644 --- a/pkg/services/registrar.go +++ b/internal/services/registrar.go @@ -38,7 +38,7 @@ func (r *gRPCRegistrar) tryRegister(ctx context.Context, request *genproto.Regis return nil } else if s, ok := status.FromError(err); ok && s.Code() == codes.Unavailable { r.logger.Warn("gRPC registrar failed to connect", zap.Error(err)) - return err + return retry.Recoverable(err) } return retry.Unrecoverable(err) }) @@ -110,7 +110,8 @@ func (r *gRPCRegistrar) Register(ctx context.Context, matchers MatcherGroup) (Up }) if err != nil { // Recovery failed, log and return the error - serr := err.(*svcError) + var serr *svcError + errors.As(err, &serr) r.logger.Error("gRPC registrar unable to reconnect", serr.ZapFields()...) errorch <- serr } diff --git a/pkg/services/registrar_test.go b/internal/services/registrar_test.go similarity index 79% rename from pkg/services/registrar_test.go rename to internal/services/registrar_test.go index 69859e0..9e10c1b 100644 --- a/pkg/services/registrar_test.go +++ b/internal/services/registrar_test.go @@ -36,6 +36,30 @@ func TestGRPCRegistrar(t *testing.T) { _, _, err = registrar.Register(ctx, MatcherGroup{}) req.ErrorIs(err, ErrNoMatchers, "shouldn't be able to register with no matchers") + // Unrecoverable registration request fail + mockMuxClient.EXPECT().RegisterHandler(gomock.Any(), gomock.Any()).Return(nil, status.Error(codes.Unauthenticated, "fake unauthenticated error")) + updatech, errorch, err := registrar.Register(ctx, MatcherGroup{regexp.MustCompile("/command")}) + req.NoError(err, "registration shouldn't fail") + req.Eventually(func() bool { + select { + case e, ok := <-errorch: + req.True(ok, "error channel should not be empty") + req.Error(e, "should receive proper error on channel") + return true + default: + return false + } + }, time.Second, time.Millisecond*50) + req.Eventually(func() bool { + select { + case _, ok := <-updatech: + req.False(ok, "update channel should close without update") + return true + default: + return false + } + }, time.Second, time.Millisecond*50) + // Two failed registration requests followed by a successful registration with single update var tgUpdate tgbotapi.Update tgUpdate.Message = &tgbotapi.Message{ @@ -58,7 +82,7 @@ func TestGRPCRegistrar(t *testing.T) { mockUpdateStream.EXPECT().Recv().Return(&genproto.Update{Json: tgUpdateBytes}, nil) mockUpdateStream.EXPECT().Recv().Return(nil, status.FromContextError(context.Canceled).Err()) - updatech, errorch, err := registrar.Register(ctx, MatcherGroup{regexp.MustCompile("/command")}) + updatech, errorch, err = registrar.Register(ctx, MatcherGroup{regexp.MustCompile("/command")}) req.NoError(err, "registration shouldn't fail") var update tgbotapi.Update diff --git a/pkg/auth/auth.go b/pkg/auth/auth.go new file mode 100644 index 0000000..05926cb --- /dev/null +++ b/pkg/auth/auth.go @@ -0,0 +1,151 @@ +package auth + +import ( + "context" + "encoding/json" + "errors" + "fmt" + + "github.com/bbralion/CTFloodBot/pkg/services" + "go.uber.org/zap" + "google.golang.org/grpc" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/metadata" + "google.golang.org/grpc/status" +) + +const ( + tokenKey = "authorization" + clientKey = "authenticated_client" +) + +type GRPCClientInterceptor string + +// NewGRPCClientInterceptor creates a new gRPC client interceptor which uses the given token +func NewGRPCClientInterceptor(token string) GRPCClientInterceptor { + return GRPCClientInterceptor(token) +} + +func (t GRPCClientInterceptor) attach(ctx context.Context) context.Context { + return metadata.AppendToOutgoingContext(ctx, tokenKey, string(t)) +} + +// Unary returns a unary gRPC client interceptor with authentication using the setup token +func (t GRPCClientInterceptor) Unary() grpc.UnaryClientInterceptor { + return func( + ctx context.Context, + method string, + req, reply interface{}, + cc *grpc.ClientConn, + invoker grpc.UnaryInvoker, + opts ...grpc.CallOption, + ) error { + return invoker(t.attach(ctx), method, req, reply, cc, opts...) + } +} + +// Stream returns a stream gRPC client interceptor with authentication using the setup token +func (t GRPCClientInterceptor) Stream() grpc.StreamClientInterceptor { + return func( + ctx context.Context, + desc *grpc.StreamDesc, + cc *grpc.ClientConn, + method string, + streamer grpc.Streamer, opts ...grpc.CallOption, + ) (grpc.ClientStream, error) { + return streamer(t.attach(ctx), desc, cc, method, opts...) + } +} + +// GRPCServerInterceptor is a Unary and Stream interceptor provider which +// uses an underlying AuthProvider for authentication of clients +type GRPCServerInterceptor struct { + logger *zap.Logger + provider services.AuthProvider +} + +// NewGRPCServerInterceptor returns a new gRPC server interceptor +// which authenticates clients using the specified provider. +func NewGRPCServerInterceptor(logger *zap.Logger, provider services.AuthProvider) *GRPCServerInterceptor { + return &GRPCServerInterceptor{logger, provider} +} + +func (i *GRPCServerInterceptor) authorize(ctx context.Context) (context.Context, error) { + md, ok := metadata.FromIncomingContext(ctx) + if !ok || len(md[tokenKey]) != 1 { + return nil, status.Error(codes.Unauthenticated, "must contain metadata with single auth token") + } + if len(md[clientKey]) != 0 { + return nil, status.Error(codes.Unauthenticated, "illegal metadata contained in request") + } + + client, err := i.provider.Authenticate(md[tokenKey][0]) + if err != nil { + return nil, status.Error(codes.Unauthenticated, err.Error()) + } + + data, err := json.Marshal(client) + if err != nil { + i.logger.Error("failed to marshal client struct for storing in gRPC metadata", zap.Error(err)) + return nil, status.Error(codes.Internal, "internal error while authenticating client") + } + return metadata.NewIncomingContext(ctx, metadata.Join(md, metadata.Pairs(clientKey, string(data)))), nil +} + +// Unary returns a unary gRPC server interceptor for authentication +func (i *GRPCServerInterceptor) Unary() grpc.UnaryServerInterceptor { + return func( + ctx context.Context, + req interface{}, + info *grpc.UnaryServerInfo, + handler grpc.UnaryHandler, + ) (interface{}, error) { + ctx, err := i.authorize(ctx) + if err != nil { + return nil, err + } + return handler(ctx, req) + } +} + +type authenticatedStream struct { + grpc.ServerStream + ctx context.Context +} + +func (s authenticatedStream) Context() context.Context { return s.ctx } +func (s authenticatedStream) RecvMsg(msg interface{}) error { return s.ServerStream.RecvMsg(msg) } //nolint:wrapcheck +func (s authenticatedStream) SendMsg(msg interface{}) error { return s.ServerStream.SendMsg(msg) } //nolint:wrapcheck +func (s authenticatedStream) SendHeader(md metadata.MD) error { return s.ServerStream.SendHeader(md) } //nolint:wrapcheck +func (s authenticatedStream) SetHeader(md metadata.MD) error { return s.ServerStream.SetHeader(md) } //nolint:wrapcheck +func (s authenticatedStream) SetTrailer(md metadata.MD) { s.ServerStream.SetTrailer(md) } + +// Stream returns a stream gRPC server interceptor for authentication +func (i *GRPCServerInterceptor) Stream() grpc.StreamServerInterceptor { + return func( + srv interface{}, + stream grpc.ServerStream, + info *grpc.StreamServerInfo, + handler grpc.StreamHandler, + ) error { + ctx, err := i.authorize(stream.Context()) + if err != nil { + return err + } + return handler(srv, authenticatedStream{stream, ctx}) + } +} + +// ClientFromCtx returns the client from an authenticated context +func ClientFromCtx(ctx context.Context) (*services.Client, error) { + md, ok := metadata.FromIncomingContext(ctx) + if !ok || len(md[clientKey]) != 1 { + return nil, errors.New("ClientFromCtx must only be called with an authenticated context") + } + + var client services.Client + if err := json.Unmarshal([]byte(md[clientKey][0]), &client); err != nil { + return nil, fmt.Errorf("failed to unmarshal stored client: %w", err) + } + return &client, nil +} diff --git a/pkg/retry/retry.go b/pkg/retry/retry.go index 8b07818..e58cf15 100644 --- a/pkg/retry/retry.go +++ b/pkg/retry/retry.go @@ -19,13 +19,18 @@ type unrecoverableError struct { } func (e unrecoverableError) Error() string { - return fmt.Sprintf("unrecoverable error: %s", e.Error()) + return fmt.Sprintf("unrecoverable error: %s", e.wrapped.Error()) } func (e unrecoverableError) Unwrap() error { return e.wrapped } +// Recoverable is used to explicitly mark an error as recoverable +func Recoverable(err error) error { + return err +} + // Unrecoverable wraps an error to indicate that it is not recoverable from, // after which retries will be stopped and it will be returned func Unrecoverable(err error) error { diff --git a/pkg/services/authprovider.go b/pkg/services/authprovider.go new file mode 100644 index 0000000..a086f91 --- /dev/null +++ b/pkg/services/authprovider.go @@ -0,0 +1,32 @@ +package services + +import "errors" + +// Client is an identification of a single client of a service +type Client struct { + Name string +} + +var ErrInvalidToken = errors.New("invalid authentication token provided") + +// AuthProvider represents a token-based authentication provider +type AuthProvider interface { + Authenticate(token string) (*Client, error) +} + +type staticAuthProvider struct { + clients map[string]*Client +} + +func (p *staticAuthProvider) Authenticate(token string) (*Client, error) { + if c, ok := p.clients[token]; ok { + return c, nil + } + return nil, ErrInvalidToken +} + +// NewStaticAuthProvider returns an auth provider which authenticates clients +// using a static token->Client map specified at creation time +func NewStaticAuthProvider(clients map[string]*Client) AuthProvider { + return &staticAuthProvider{clients} +} From 424e57b1c13b709ddbbc95aa377e0189ca03da29 Mon Sep 17 00:00:00 2001 From: renbou Date: Thu, 28 Jul 2022 17:45:53 +0300 Subject: [PATCH 07/17] feat: proxy client finished --- internal/services/models.go | 13 -- internal/services/multiplexer.go | 7 +- internal/services/multiplexer_test.go | 13 +- internal/services/registrar.go | 42 +++--- internal/services/registrar_test.go | 7 +- pkg/models/models.go | 23 +++ pkg/proxy/client.go | 201 ++++++++++++++++++++++++++ pkg/retry/retry.go | 100 ++++++++----- pkg/retry/retry_test.go | 20 +-- 9 files changed, 332 insertions(+), 94 deletions(-) create mode 100644 pkg/models/models.go create mode 100644 pkg/proxy/client.go diff --git a/internal/services/models.go b/internal/services/models.go index ed653a1..18d3de0 100644 --- a/internal/services/models.go +++ b/internal/services/models.go @@ -1,8 +1,6 @@ package services import ( - "regexp" - tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" ) @@ -10,14 +8,3 @@ type ( UpdateChan tgbotapi.UpdatesChannel ErrorChan <-chan error ) - -type MatcherGroup []*regexp.Regexp - -func (g MatcherGroup) MatchString(s string) bool { - for _, m := range g { - if m.MatchString(s) { - return true - } - } - return false -} diff --git a/internal/services/multiplexer.go b/internal/services/multiplexer.go index 9657529..cbbbe33 100644 --- a/internal/services/multiplexer.go +++ b/internal/services/multiplexer.go @@ -5,6 +5,7 @@ import ( "sync" "sync/atomic" + "github.com/bbralion/CTFloodBot/pkg/models" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" ) @@ -12,7 +13,7 @@ import ( type Multiplexer interface { // Register registers a new handler which will receive updates until the context is canceled. // Safe for concurrent use, so matchers can be registered from anywhere. - Register(ctx context.Context, matchers MatcherGroup) (UpdateChan, error) + Register(ctx context.Context, matchers models.MatcherGroup) (models.UpdateChan, error) // Serve multiplexes the update across the registered handlers. // Isn't safe for concurrent use, so all calls to Serve must be from a single goroutine. Serve(update tgbotapi.Update) @@ -22,7 +23,7 @@ type ( muxKey uint64 muxHandler struct { ctx context.Context - matchers MatcherGroup + matchers models.MatcherGroup channel chan tgbotapi.Update } ) @@ -34,7 +35,7 @@ type mapMux struct { bufferLen int } -func (m *mapMux) Register(ctx context.Context, matchers MatcherGroup) (UpdateChan, error) { +func (m *mapMux) Register(ctx context.Context, matchers models.MatcherGroup) (models.UpdateChan, error) { if len(matchers) < 1 { return nil, ErrNoMatchers } diff --git a/internal/services/multiplexer_test.go b/internal/services/multiplexer_test.go index 7061f0a..c6d8045 100644 --- a/internal/services/multiplexer_test.go +++ b/internal/services/multiplexer_test.go @@ -7,11 +7,12 @@ import ( "testing" "time" + "github.com/bbralion/CTFloodBot/pkg/models" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" "github.com/stretchr/testify/require" ) -func startExpectingMuxClient(wg *sync.WaitGroup, req *require.Assertions, mux Multiplexer, updates []tgbotapi.Update, matchers MatcherGroup) { +func startExpectingMuxClient(wg *sync.WaitGroup, req *require.Assertions, mux Multiplexer, updates []tgbotapi.Update, matchers models.MatcherGroup) { ctx, cancel := context.WithCancel(context.Background()) ch, err := mux.Register(ctx, matchers) req.NoError(err, "should register without error") @@ -46,15 +47,15 @@ func TestMultiplexer(t *testing.T) { var wg sync.WaitGroup mux := NewMultiplexer(1) - startExpectingMuxClient(&wg, req, mux, updates[:2], MatcherGroup{regexp.MustCompile("^/[ab]$")}) - startExpectingMuxClient(&wg, req, mux, updates[:6], MatcherGroup{regexp.MustCompile("^/[a-f]$")}) - startExpectingMuxClient(&wg, req, mux, updates[:9], MatcherGroup{regexp.MustCompile("^/[a-j]$")}) - startExpectingMuxClient(&wg, req, mux, updates[:9], MatcherGroup{regexp.MustCompile(".*")}) + startExpectingMuxClient(&wg, req, mux, updates[:2], models.MatcherGroup{regexp.MustCompile("^/[ab]$")}) + startExpectingMuxClient(&wg, req, mux, updates[:6], models.MatcherGroup{regexp.MustCompile("^/[a-f]$")}) + startExpectingMuxClient(&wg, req, mux, updates[:9], models.MatcherGroup{regexp.MustCompile("^/[a-j]$")}) + startExpectingMuxClient(&wg, req, mux, updates[:9], models.MatcherGroup{regexp.MustCompile(".*")}) startExpectingMuxClient(&wg, req, mux, []tgbotapi.Update{ {Message: &tgbotapi.Message{Text: "/aboba"}}, {Message: &tgbotapi.Message{Text: "/sus"}}, {Message: &tgbotapi.Message{Text: "/aboba"}}, - }, MatcherGroup{regexp.MustCompile("^/(aboba|sus)$")}) + }, models.MatcherGroup{regexp.MustCompile("^/(aboba|sus)$")}) for _, update := range updates { mux.Serve(update) diff --git a/internal/services/registrar.go b/internal/services/registrar.go index 5b46a45..6c99322 100644 --- a/internal/services/registrar.go +++ b/internal/services/registrar.go @@ -6,11 +6,10 @@ import ( "errors" "github.com/bbralion/CTFloodBot/internal/genproto" + "github.com/bbralion/CTFloodBot/pkg/models" "github.com/bbralion/CTFloodBot/pkg/retry" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" "go.uber.org/zap" - "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" ) var ErrNoMatchers = errors.New("cannot register with zero matchers") @@ -19,7 +18,7 @@ var ErrNoMatchers = errors.New("cannot register with zero matchers") type Registrar interface { // Register registers a new command handler with the given matchers. // The context should span the lifetime of the registered handler and canceled when it dies. - Register(ctx context.Context, matchers MatcherGroup) (UpdateChan, ErrorChan, error) + Register(ctx context.Context, matchers models.MatcherGroup) (models.UpdateChan, models.ErrorChan, error) } // gRPCRegistrar is an implementation of Registrar using grpc with retries @@ -29,21 +28,16 @@ type gRPCRegistrar struct { } func (r *gRPCRegistrar) tryRegister(ctx context.Context, request *genproto.RegisterRequest, updatech chan tgbotapi.Update) *svcError { - var stream genproto.MultiplexerService_RegisterHandlerClient - err := retry.Backoff(func() error { - var err error - stream, err = r.client.RegisterHandler(ctx, request) - - if err == nil { - return nil - } else if s, ok := status.FromError(err); ok && s.Code() == codes.Unavailable { - r.logger.Warn("gRPC registrar failed to connect", zap.Error(err)) - return retry.Recoverable(err) + stream, err := retry.Backoff(func() (error, genproto.MultiplexerService_RegisterHandlerClient) { + stream, err := r.client.RegisterHandler(ctx, request) + if retry.IsGRPCUnavailable(err) { + r.logger.Info("gRPC registrar retrying connection to server", zap.Error(err)) + return retry.Recoverable(err), nil } - return retry.Unrecoverable(err) + return retry.Unrecoverable(err), stream }) if err != nil { - if s, ok := status.FromError(err); ok && (s.Code() == codes.Canceled || s.Code() == codes.DeadlineExceeded) { + if retry.IsGRPCCanceled(err) { // Client disconnected before we managed to register return nil } @@ -57,7 +51,7 @@ func (r *gRPCRegistrar) tryRegister(ctx context.Context, request *genproto.Regis for { updatePB, err := stream.Recv() if err != nil { - if s, ok := status.FromError(err); ok && (s.Code() == codes.Canceled || s.Code() == codes.DeadlineExceeded) { + if retry.IsGRPCCanceled(err) { // If the updates are simply stopped, then no error has happened return nil } @@ -77,7 +71,7 @@ func (r *gRPCRegistrar) tryRegister(ctx context.Context, request *genproto.Regis } } -func (r *gRPCRegistrar) Register(ctx context.Context, matchers MatcherGroup) (UpdateChan, ErrorChan, error) { +func (r *gRPCRegistrar) Register(ctx context.Context, matchers models.MatcherGroup) (models.UpdateChan, models.ErrorChan, error) { if len(matchers) < 1 { return nil, nil, ErrNoMatchers } @@ -95,18 +89,18 @@ func (r *gRPCRegistrar) Register(ctx context.Context, matchers MatcherGroup) (Up defer close(updatech) defer close(errorch) - err := retry.Static(func() error { + _, err := retry.Static(func() (error, any) { err := r.tryRegister(ctx, request, updatech) if err == nil { - return nil + return nil, nil } // We can retry only due to connectivity issues - if s, ok := status.FromError(err.Unwrap()); ok && s.Code() == codes.Unavailable { - r.logger.Warn("gRPC registrar reconnecting stream", err.ZapFields()...) - return err + if retry.IsGRPCUnavailable(err.Unwrap()) { + r.logger.Info("gRPC registrar reconnecting stream", err.ZapFields()...) + return retry.Recoverable(err), nil } - return retry.Unrecoverable(err) + return retry.Unrecoverable(err), nil }) if err != nil { // Recovery failed, log and return the error @@ -124,5 +118,5 @@ func NewGRPCRegistrar(logger *zap.Logger, client genproto.MultiplexerServiceClie if client == nil { return nil, errors.New("gRPC client must not be nil") } - return &gRPCRegistrar{logger, client}, nil + return &gRPCRegistrar{logger.With(zap.Namespace("registrar")), client}, nil } diff --git a/internal/services/registrar_test.go b/internal/services/registrar_test.go index 9e10c1b..13c9064 100644 --- a/internal/services/registrar_test.go +++ b/internal/services/registrar_test.go @@ -10,6 +10,7 @@ import ( "github.com/bbralion/CTFloodBot/internal/genproto" "github.com/bbralion/CTFloodBot/internal/mockproto" + "github.com/bbralion/CTFloodBot/pkg/models" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" "github.com/golang/mock/gomock" "github.com/stretchr/testify/require" @@ -33,12 +34,12 @@ func TestGRPCRegistrar(t *testing.T) { // Registration without any matchers ctx := context.Background() - _, _, err = registrar.Register(ctx, MatcherGroup{}) + _, _, err = registrar.Register(ctx, models.MatcherGroup{}) req.ErrorIs(err, ErrNoMatchers, "shouldn't be able to register with no matchers") // Unrecoverable registration request fail mockMuxClient.EXPECT().RegisterHandler(gomock.Any(), gomock.Any()).Return(nil, status.Error(codes.Unauthenticated, "fake unauthenticated error")) - updatech, errorch, err := registrar.Register(ctx, MatcherGroup{regexp.MustCompile("/command")}) + updatech, errorch, err := registrar.Register(ctx, models.MatcherGroup{regexp.MustCompile("/command")}) req.NoError(err, "registration shouldn't fail") req.Eventually(func() bool { select { @@ -82,7 +83,7 @@ func TestGRPCRegistrar(t *testing.T) { mockUpdateStream.EXPECT().Recv().Return(&genproto.Update{Json: tgUpdateBytes}, nil) mockUpdateStream.EXPECT().Recv().Return(nil, status.FromContextError(context.Canceled).Err()) - updatech, errorch, err = registrar.Register(ctx, MatcherGroup{regexp.MustCompile("/command")}) + updatech, errorch, err = registrar.Register(ctx, models.MatcherGroup{regexp.MustCompile("/command")}) req.NoError(err, "registration shouldn't fail") var update tgbotapi.Update diff --git a/pkg/models/models.go b/pkg/models/models.go new file mode 100644 index 0000000..f448e74 --- /dev/null +++ b/pkg/models/models.go @@ -0,0 +1,23 @@ +package models + +import ( + "regexp" + + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" +) + +type ( + UpdateChan tgbotapi.UpdatesChannel + ErrorChan <-chan error +) + +type MatcherGroup []*regexp.Regexp + +func (g MatcherGroup) MatchString(s string) bool { + for _, m := range g { + if m.MatchString(s) { + return true + } + } + return false +} diff --git a/pkg/proxy/client.go b/pkg/proxy/client.go new file mode 100644 index 0000000..3a9730e --- /dev/null +++ b/pkg/proxy/client.go @@ -0,0 +1,201 @@ +package proxy + +import ( + "context" + "errors" + "fmt" + "net/http" + "time" + + "github.com/bbralion/CTFloodBot/internal/genproto" + "github.com/bbralion/CTFloodBot/internal/services" + "github.com/bbralion/CTFloodBot/pkg/auth" + "github.com/bbralion/CTFloodBot/pkg/models" + "github.com/bbralion/CTFloodBot/pkg/retry" + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" + "go.uber.org/zap" + "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" +) + +const defaultTimeout = time.Second * 5 + +// Context provides Handler's with often needed elements +type Context interface { + context.Context + API() *tgbotapi.BotAPI + Logger() *zap.Logger +} + +type proxyCtx struct { + context.Context + api *tgbotapi.BotAPI + logger *zap.Logger +} + +func (ctx *proxyCtx) API() *tgbotapi.BotAPI { + return ctx.api +} + +func (ctx *proxyCtx) Logger() *zap.Logger { + return ctx.logger +} + +// Handler is a proper handler of update requests received from the proxy +type Handler interface { + Serve(ctx Context, update tgbotapi.Update) +} + +// HandlerFunc is a util type for using functions as Handler's +type HandlerFunc func(ctx Context, update tgbotapi.Update) + +func (f HandlerFunc) Serve(ctx Context, update tgbotapi.Update) { + f(ctx, update) +} + +// Client is the proxy client implementation, it receives updates via gRPC and answers via HTTP +type Client struct { + Logger *zap.Logger + // Handler is the telegram update handler used + Handler Handler + // Matchers specify the matchers used to filter the requests which should be handled by this client + Matchers models.MatcherGroup + // Token is the auth token used to authorize this client + Token string + // GRPCEndpoint specifies the (currently insecure) gRPC endpoint to connect to + GRPCEndpoint string +} + +// Run runs the client. It starts by connecting to the gRPC proxy, from which it receives the HTTP +// proxy endpoint, as well as all of the following updates from telegram. +func (c *Client) Run(ctx context.Context) (err error) { + if c.Logger == nil || c.Handler == nil || len(c.Matchers) < 1 { + return errors.New("logger, handler and matchers must be specified for client") + } + c.Logger = c.Logger.With(zap.Namespace("client")) + logErr := func(msg string) { + c.Logger.Error(msg, zap.Error(err)) + } + + // Begin by setting up a proper grpc connection + conn, err := c.connectGRPC() + if err != nil { + logErr("failed to connect to specified gRPC endpoint") + return + } + defer conn.Close() + gc := genproto.NewMultiplexerServiceClient(conn) + + // And immediately test it out by requesting the HTTP endpoint + cfg, err := c.getConfig(ctx, gc) + if err != nil { + logErr("failed to get config") + return + } + + // Connect to the telegram bot proxy + api, err := c.connectHTTP(cfg.GetProxyEndpoint()) + if err != nil { + logErr("failed to connect to telegram http proxy") + return + } + + // Configure registrar and start listening for updates + registrar, err := services.NewGRPCRegistrar(c.Logger, gc) + if err != nil { + logErr("failed to setup registrar") + return + } + + updatech, errorch, err := registrar.Register(ctx, c.Matchers) + if err != nil { + logErr("failed to register client") + return + } + + // Make sure to cancel all requests when we die + ctx, cancel := context.WithCancel(ctx) + defer cancel() + for { + select { + case err = <-errorch: + logErr("critical error while receiving updates") + return + case update := <-updatech: + c.Handler.Serve(&proxyCtx{ctx, api, c.Logger}, update) + } + } +} + +func (c *Client) connectGRPC() (*grpc.ClientConn, error) { + if c.GRPCEndpoint == "" || c.Token == "" { + return nil, errors.New("endpoint and token must not be empty") + } + + interceptor := auth.NewGRPCClientInterceptor(c.Token) + conn, err := grpc.Dial(c.GRPCEndpoint, + grpc.WithTransportCredentials(insecure.NewCredentials()), + grpc.WithUnaryInterceptor(interceptor.Unary()), + grpc.WithStreamInterceptor(interceptor.Stream())) + if err != nil { + return nil, fmt.Errorf("failed to dial proxy gRPC endpoint: %w", err) + } + return conn, nil +} + +func (c *Client) getConfig(ctx context.Context, gc genproto.MultiplexerServiceClient) (*genproto.Config, error) { + config, err := retry.Backoff(func() (error, *genproto.Config) { + ctx, cancel := context.WithTimeout(ctx, defaultTimeout) + defer cancel() + + resp, err := gc.GetConfig(ctx, &genproto.ConfigRequest{}) + if err == nil { + return nil, resp.GetConfig() + } else if retry.IsGRPCUnavailable(err) { + c.Logger.Info("retrying connection to gRPC server", zap.Error(err)) + return retry.Recoverable(err), nil + } + return retry.Unrecoverable(err), nil + }) + if err != nil { + return nil, fmt.Errorf("request to gRPC server failed: %w", err) + } + return config, nil +} + +// internalHTTPTransport is a roundtripper for the http proxy which adds authorization +type internalHTTPTransport struct { + http.RoundTripper + logger *zap.Logger + token string +} + +func (t *internalHTTPTransport) RoundTrip(r *http.Request) (*http.Response, error) { + r.Header.Set("Authorization", t.token) + resp, err := t.RoundTripper.RoundTrip(r) + if err != nil { + t.logger.Warn("roundtripping request to tg proxy failed", zap.Error(err)) + return nil, fmt.Errorf("tg proxy roundtrip failed: %w", err) + } + + if resp.StatusCode == http.StatusForbidden || resp.StatusCode == http.StatusUnauthorized { + return nil, errors.New("request to tg proxy failed: unauthorized") + } + return resp, nil +} + +func (c *Client) connectHTTP(endpoint string) (*tgbotapi.BotAPI, error) { + httpC := &internalHTTPTransport{ + http.DefaultTransport, + c.Logger, + c.Token, + } + + api, err := tgbotapi.NewBotAPIWithClient(c.Token, endpoint, &http.Client{ + Transport: httpC, + }) + if err != nil { + return nil, fmt.Errorf("failed to setup telegram bot api: %w", err) + } + return api, nil +} diff --git a/pkg/retry/retry.go b/pkg/retry/retry.go index e58cf15..72ed146 100644 --- a/pkg/retry/retry.go +++ b/pkg/retry/retry.go @@ -4,71 +4,99 @@ import ( "errors" "fmt" "time" -) -const ( - DefaultBackoffMinDelay = time.Millisecond * 50 - DefaultBackoffMaxDelay = time.Minute * 10 - DefaultBackoffFactor = 2 + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" ) -type RecoverFunc func() error +type ( + DelayScheduler func() time.Duration + ErrTransformer func(error) error +) -type unrecoverableError struct { - wrapped error +type recoverError struct { + wrapped error + recoverable bool } -func (e unrecoverableError) Error() string { - return fmt.Sprintf("unrecoverable error: %s", e.wrapped.Error()) +func (e recoverError) Error() string { + s := "recoverable error: %s" + if !e.recoverable { + s = "un" + s + } + return fmt.Sprintf(s, e.wrapped.Error()) } -func (e unrecoverableError) Unwrap() error { +func (e recoverError) Unwrap() error { return e.wrapped } // Recoverable is used to explicitly mark an error as recoverable func Recoverable(err error) error { - return err + return recoverError{err, true} } // Unrecoverable wraps an error to indicate that it is not recoverable from, // after which retries will be stopped and it will be returned func Unrecoverable(err error) error { - return unrecoverableError{err} + return recoverError{err, false} } -// Backoff runs the function using the backoff retry algorithm -func Backoff(f RecoverFunc) error { - delay := DefaultBackoffMinDelay +// Recover runs the function using a custom delay scheduler +func Recover[T any](f func() (error, T), s DelayScheduler, et ...ErrTransformer) (T, error) { for { - var ue unrecoverableError - if err := f(); err == nil { - return nil - } else if errors.As(err, &ue) { - return ue.Unwrap() + err, ret := f() + for _, t := range et { + err = t(err) } - time.Sleep(delay) - - delay *= DefaultBackoffFactor - if delay > DefaultBackoffMaxDelay { - delay = DefaultBackoffMaxDelay + var re recoverError + if err == nil { + return ret, nil + } else if errors.As(err, &re) && !re.recoverable { + return ret, re.Unwrap() } + + time.Sleep(s()) } } +const ( + DefaultBackoffMinDelay = time.Millisecond * 50 + DefaultBackoffMaxDelay = time.Minute * 10 + DefaultBackoffFactor = 2 +) + +// Backoff runs the function using the backoff retry algorithm +func Backoff[T any](f func() (error, T), et ...ErrTransformer) (T, error) { + delay, next := time.Duration(0), DefaultBackoffMinDelay + return Recover(f, func() time.Duration { + delay, next = next, next*DefaultBackoffFactor + if next > DefaultBackoffMaxDelay { + next = DefaultBackoffMaxDelay + } + return delay + }, et...) +} + const DefaultStaticDelay = time.Second // Static runs the function using a static retry delay -func Static(f RecoverFunc) error { - for { - var ue unrecoverableError - if err := f(); err == nil { - return nil - } else if errors.As(err, &ue) { - return ue.Unwrap() - } +func Static[T any](f func() (error, T), et ...ErrTransformer) (T, error) { + return Recover(f, func() time.Duration { + return DefaultStaticDelay + }, et...) +} - time.Sleep(DefaultStaticDelay) - } +// IsGRPCUnavailable is a helper for testing whether the error resembles a gRPC Unavailable status +func IsGRPCUnavailable(err error) bool { + s, ok := status.FromError(err) + return ok && s.Code() == codes.Unavailable +} + +// IsGRPCUnavailable is a helper for testing whether the error +// resembles a gRPC Canceled or DeadlineExceeded status +func IsGRPCCanceled(err error) bool { + s, ok := status.FromError(err) + return ok && (s.Code() == codes.Canceled || s.Code() == codes.DeadlineExceeded) } diff --git a/pkg/retry/retry_test.go b/pkg/retry/retry_test.go index 72ddb50..1afe262 100644 --- a/pkg/retry/retry_test.go +++ b/pkg/retry/retry_test.go @@ -7,29 +7,31 @@ import ( "github.com/stretchr/testify/require" ) -func assertNumCallsFunc(req *require.Assertions, n int, err error) RecoverFunc { +func assertNumCallsFunc(req *require.Assertions, n int, tmpErr, finalErr error) func() (error, any) { ctr := 0 - return func() error { + return func() (error, any) { req.Less(ctr, n, "should be called %d times at most", n) ctr++ if ctr == n { - return err + return finalErr, nil } - return errors.New("fake recoverable error") + return tmpErr, nil } } -func testStrategy(req *require.Assertions, n int, strategy func(RecoverFunc) error) { - req.NoError(strategy(assertNumCallsFunc(req, n, nil))) +func testStrategy(req *require.Assertions, n int, strategy func(func() (error, any), ...ErrTransformer) (any, error)) { + _, err := strategy(assertNumCallsFunc(req, n, errors.New("fake recoverable error"), nil)) + req.NoError(err) e := errors.New("fake unrecoverable error") - req.ErrorIs(e, strategy(assertNumCallsFunc(req, n, Unrecoverable(e)))) + _, err = strategy(assertNumCallsFunc(req, n, errors.New("fake recoverable error"), Unrecoverable(e))) + req.ErrorIs(e, err) } func TestRetry(t *testing.T) { req := require.New(t) for i := 1; i < 4; i++ { - testStrategy(req, i, Backoff) - testStrategy(req, i, Static) + testStrategy(req, i, Backoff[any]) + testStrategy(req, i, Static[any]) } } From 040dd044e5c7feb29b9e4be87df51a00d643a28b Mon Sep 17 00:00:00 2001 From: renbou Date: Thu, 28 Jul 2022 21:21:15 +0300 Subject: [PATCH 08/17] feat: proxy http server --- go.mod | 3 +- go.sum | 6 +- internal/proxy/http.go | 167 +++++++++++++++++++++++++++++++++ internal/proxy/proxy.go | 1 + internal/services/allowlist.go | 24 +++++ internal/services/registrar.go | 2 +- pkg/proxy/client.go | 37 +------- pkg/services/updateprovider.go | 35 +++++++ 8 files changed, 239 insertions(+), 36 deletions(-) create mode 100644 internal/proxy/http.go create mode 100644 internal/proxy/proxy.go create mode 100644 internal/services/allowlist.go create mode 100644 pkg/services/updateprovider.go diff --git a/go.mod b/go.mod index 6292021..d29956d 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/go-telegram-bot-api/telegram-bot-api v1.0.1-0.20201107014523-54104a08f947 github.com/golang/mock v1.6.0 github.com/jinzhu/configor v1.2.1 + github.com/justinas/alice v1.2.0 github.com/stretchr/testify v1.8.0 go.uber.org/zap v1.21.0 google.golang.org/grpc v1.48.0 @@ -24,7 +25,7 @@ require ( go.uber.org/atomic v1.9.0 // indirect go.uber.org/multierr v1.8.0 // indirect golang.org/x/net v0.0.0-20220726230323-06994584191e // indirect - golang.org/x/sys v0.0.0-20220727055044-e65921a090b8 // indirect + golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 // indirect golang.org/x/text v0.3.7 // indirect google.golang.org/genproto v0.0.0-20220725144611-272f38e5d71b // indirect gopkg.in/yaml.v2 v2.4.0 // indirect diff --git a/go.sum b/go.sum index 8d29136..db33b85 100644 --- a/go.sum +++ b/go.sum @@ -64,6 +64,8 @@ github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+ github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= github.com/jinzhu/configor v1.2.1 h1:OKk9dsR8i6HPOCZR8BcMtcEImAFjIhbJFZNyn5GCZko= github.com/jinzhu/configor v1.2.1/go.mod h1:nX89/MOmDba7ZX7GCyU/VIaQ2Ar2aizBl2d3JLF/rDc= +github.com/justinas/alice v1.2.0 h1:+MHSA/vccVCF4Uq37S42jwlkvI2Xzl7zTPCN5BnZNVo= +github.com/justinas/alice v1.2.0/go.mod h1:fN5HRH/reO/zrUflLfTN43t3vXvKzvZIENsNEe7i7qA= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= @@ -137,8 +139,8 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220727055044-e65921a090b8 h1:dyU22nBWzrmTQxtNrr4dzVOvaw35nUYE279vF9UmsI8= -golang.org/x/sys v0.0.0-20220727055044-e65921a090b8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 h1:WIoqL4EROvwiPdUtaip4VcDdpZ4kha7wBWZrbVKCIZg= +golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= diff --git a/internal/proxy/http.go b/internal/proxy/http.go new file mode 100644 index 0000000..2c1a488 --- /dev/null +++ b/internal/proxy/http.go @@ -0,0 +1,167 @@ +package proxy + +import ( + "context" + "errors" + "fmt" + "net/http" + "net/http/httputil" + "net/url" + "regexp" + "sync/atomic" + "time" + + internal "github.com/bbralion/CTFloodBot/internal/services" + "github.com/bbralion/CTFloodBot/pkg/services" + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" + "github.com/justinas/alice" + "go.uber.org/zap" +) + +// DefaultRequestTimeout is the default timeout to be used for making requests to the telegram API +const DefaultRequestTimeout = time.Second * 30 + +// 16 megabytes should be enough for most usecases +const DefaultMaxBodyBytes = 16_000_000 + +// HTTP is the telegram API HTTP proxy +type HTTP struct { + http.Server + Logger *zap.Logger + AuthProvider services.AuthProvider + Allowlist internal.Allowlist + // Transport is the transport to use for making requests to the telegram API. + // http.DefaultTransport will be used by default + Transport *http.Transport + // Telegram API token + Token string + // Telegram API endpoint to use, may be another proxy + Endpoint string + requestCounter int64 +} + +var pathRe = regexp.MustCompile("^/proxy(\\w+)(/.+)$") + +// Path returns the HTTP proxy path format string suitable for use with tgbotapi +func (p *HTTP) Path() string { + return "/proxy%s/%s" +} + +func (p *HTTP) ListenAndServe() error { + if p.Logger == nil || p.AuthProvider == nil || p.Allowlist == nil { + return errors.New( + "logger, auth provider and allow list must be specified for the http proxy server") + } else if p.Token == "" { + return errors.New("telegram API token must be specified") + } + + endpointUrl, err := url.Parse(p.Endpoint) + if err != nil { + return fmt.Errorf("invalid endpoint url specified: %w", err) + } + + p.Logger = p.Logger.Named("http") + p.setDefaults() + + handler := httputil.ReverseProxy{ + Director: func(r *http.Request) { + // Route requests using the telegram API token and with a limited body + reqUrl := *endpointUrl + reqUrl.User = nil + reqUrl.Path = fmt.Sprintf(p.Endpoint, p.Token, r.URL.Path[1:]) + r.URL = &reqUrl + r.Body = http.MaxBytesReader(nil, r.Body, DefaultMaxBodyBytes) + }, + ErrorHandler: func(w http.ResponseWriter, r *http.Request, err error) { + p.Logger.Warn("request to telegram API failed", zap.Error(err), zap.Int64("request_id", requestID(r))) + w.WriteHeader(http.StatusBadGateway) + return + }, + Transport: p.Transport, + } + + p.Handler = alice.New(p.PanicMiddleware, p.RequestIDMiddleware, p.LoggingMiddleware, p.AuthMiddleware).Then(&handler) + return p.ListenAndServe() +} + +func (p *HTTP) setDefaults() { + if p.Transport == nil { + p.Transport = &http.Transport{} + } + if p.Transport.ResponseHeaderTimeout == 0 { + p.Transport.ResponseHeaderTimeout = DefaultRequestTimeout + } + if p.Endpoint == "" { + p.Endpoint = tgbotapi.APIEndpoint + } +} + +type requestIDCtxKey struct{} + +func requestID(r *http.Request) int64 { + return r.Context().Value(requestIDCtxKey{}).(int64) +} + +func (p *HTTP) RequestIDMiddleware(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + id := atomic.AddInt64(&p.requestCounter, 1) + next.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), requestIDCtxKey{}, id))) + }) +} + +func (p *HTTP) PanicMiddleware(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + defer func() { + if v := recover(); v != nil { + p.Logger.Error("recovered from panic", + zap.Any("recover", v), + zap.Int64("request_id", requestID(r))) + } + }() + + next.ServeHTTP(w, r) + }) +} + +type clientCtxKey struct{} + +func (p *HTTP) AuthMiddleware(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + groups := pathRe.FindStringSubmatch(r.URL.Path) + if len(groups) != 3 { + w.WriteHeader(http.StatusBadRequest) + return + } + + client, err := p.AuthProvider.Authenticate(groups[1]) + if err != nil { + w.WriteHeader(http.StatusUnauthorized) + return + } + + authenticatedReq := r.WithContext(context.WithValue(r.Context(), clientCtxKey{}, client)) + authenticatedReq.URL.Path = groups[2] + next.ServeHTTP(w, authenticatedReq) + }) +} + +func (p *HTTP) LoggingMiddleware(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + start := time.Now() + rcopy := *r + + // Always log, even on panic + defer func() { + latency := time.Since(start) + p.Logger.Info("handled request", + zap.String("uri", rcopy.RequestURI), + zap.String("method", rcopy.Method), + zap.Duration("latency", latency), + zap.String("remote_addr", rcopy.RemoteAddr), + zap.Int64("request_id", requestID(r)), + zap.Any("client", r.Context().Value(clientCtxKey{}))) + }() + + next.ServeHTTP(w, r) + }) +} diff --git a/internal/proxy/proxy.go b/internal/proxy/proxy.go new file mode 100644 index 0000000..943b369 --- /dev/null +++ b/internal/proxy/proxy.go @@ -0,0 +1 @@ +package proxy diff --git a/internal/services/allowlist.go b/internal/services/allowlist.go new file mode 100644 index 0000000..232da8a --- /dev/null +++ b/internal/services/allowlist.go @@ -0,0 +1,24 @@ +package services + +// Allowlist specifies values that are explicitly allowed to be used +type Allowlist interface { + Allowed(key string) bool +} + +type staticAllowList struct { + allowed map[string]struct{} +} + +func (l *staticAllowList) Allowed(key string) bool { + _, ok := l.allowed[key] + return ok +} + +// NewStaticAllowlist returns an allowlist that allows only the values specified +func NewStaticAllowlist(allowed []string) Allowlist { + m := make(map[string]struct{}, len(allowed)) + for _, s := range allowed { + m[s] = struct{}{} + } + return &staticAllowList{m} +} diff --git a/internal/services/registrar.go b/internal/services/registrar.go index 6c99322..f44f64e 100644 --- a/internal/services/registrar.go +++ b/internal/services/registrar.go @@ -118,5 +118,5 @@ func NewGRPCRegistrar(logger *zap.Logger, client genproto.MultiplexerServiceClie if client == nil { return nil, errors.New("gRPC client must not be nil") } - return &gRPCRegistrar{logger.With(zap.Namespace("registrar")), client}, nil + return &gRPCRegistrar{logger.Named("registrar"), client}, nil } diff --git a/pkg/proxy/client.go b/pkg/proxy/client.go index 3a9730e..8de4099 100644 --- a/pkg/proxy/client.go +++ b/pkg/proxy/client.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "net/http" "time" "github.com/bbralion/CTFloodBot/internal/genproto" @@ -72,7 +71,7 @@ func (c *Client) Run(ctx context.Context) (err error) { if c.Logger == nil || c.Handler == nil || len(c.Matchers) < 1 { return errors.New("logger, handler and matchers must be specified for client") } - c.Logger = c.Logger.With(zap.Namespace("client")) + c.Logger = c.Logger.Named("client") logErr := func(msg string) { c.Logger.Error(msg, zap.Error(err)) } @@ -123,6 +122,9 @@ func (c *Client) Run(ctx context.Context) (err error) { return case update := <-updatech: c.Handler.Serve(&proxyCtx{ctx, api, c.Logger}, update) + case <-ctx.Done(): + c.Logger.Info("shutting down") + return } } } @@ -163,37 +165,8 @@ func (c *Client) getConfig(ctx context.Context, gc genproto.MultiplexerServiceCl return config, nil } -// internalHTTPTransport is a roundtripper for the http proxy which adds authorization -type internalHTTPTransport struct { - http.RoundTripper - logger *zap.Logger - token string -} - -func (t *internalHTTPTransport) RoundTrip(r *http.Request) (*http.Response, error) { - r.Header.Set("Authorization", t.token) - resp, err := t.RoundTripper.RoundTrip(r) - if err != nil { - t.logger.Warn("roundtripping request to tg proxy failed", zap.Error(err)) - return nil, fmt.Errorf("tg proxy roundtrip failed: %w", err) - } - - if resp.StatusCode == http.StatusForbidden || resp.StatusCode == http.StatusUnauthorized { - return nil, errors.New("request to tg proxy failed: unauthorized") - } - return resp, nil -} - func (c *Client) connectHTTP(endpoint string) (*tgbotapi.BotAPI, error) { - httpC := &internalHTTPTransport{ - http.DefaultTransport, - c.Logger, - c.Token, - } - - api, err := tgbotapi.NewBotAPIWithClient(c.Token, endpoint, &http.Client{ - Transport: httpC, - }) + api, err := tgbotapi.NewBotAPIWithAPIEndpoint(c.Token, endpoint) if err != nil { return nil, fmt.Errorf("failed to setup telegram bot api: %w", err) } diff --git a/pkg/services/updateprovider.go b/pkg/services/updateprovider.go new file mode 100644 index 0000000..073b3e5 --- /dev/null +++ b/pkg/services/updateprovider.go @@ -0,0 +1,35 @@ +package services + +import ( + "fmt" + + "github.com/bbralion/CTFloodBot/pkg/models" + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" +) + +// UpdateProvider represents a telegram update provider +type UpdateProvider interface { + Updates() (models.UpdateChan, error) +} + +const DefaultLongPollTimeoutS = 60 + +type pollingUpdateProvider struct { + api *tgbotapi.BotAPI +} + +func (p *pollingUpdateProvider) Updates() (models.UpdateChan, error) { + updates, err := p.api.GetUpdatesChan(tgbotapi.UpdateConfig{ + Offset: 0, + Limit: p.api.Buffer, + Timeout: DefaultLongPollTimeoutS, + }) + if err != nil { + return nil, fmt.Errorf("failed to get tgbotapi update channel: %w", err) + } + return models.UpdateChan(updates), nil +} + +func NewPollingUpdateProvider(api *tgbotapi.BotAPI) UpdateProvider { + return &pollingUpdateProvider{api} +} From caed447d6f2a88a5063616c3e09fd7a199bf5a74 Mon Sep 17 00:00:00 2001 From: renbou Date: Thu, 28 Jul 2022 21:27:41 +0300 Subject: [PATCH 09/17] chore: add tests to ci, fix linter errors --- .github/workflows/test.yml | 13 +++++++++++++ internal/proxy/http.go | 15 +++++++-------- 2 files changed, 20 insertions(+), 8 deletions(-) create mode 100644 .github/workflows/test.yml diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..e47e4b8 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,13 @@ +name: lint +on: + - pull_request +jobs: + code: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Set up Go + uses: actions/setup-go@v3 + - name: Run tests + uses: make test diff --git a/internal/proxy/http.go b/internal/proxy/http.go index 2c1a488..cb84f48 100644 --- a/internal/proxy/http.go +++ b/internal/proxy/http.go @@ -40,7 +40,7 @@ type HTTP struct { requestCounter int64 } -var pathRe = regexp.MustCompile("^/proxy(\\w+)(/.+)$") +var pathRe = regexp.MustCompile(`^/proxy(\w+)(/.+)$`) // Path returns the HTTP proxy path format string suitable for use with tgbotapi func (p *HTTP) Path() string { @@ -55,7 +55,7 @@ func (p *HTTP) ListenAndServe() error { return errors.New("telegram API token must be specified") } - endpointUrl, err := url.Parse(p.Endpoint) + endpointURL, err := url.Parse(p.Endpoint) if err != nil { return fmt.Errorf("invalid endpoint url specified: %w", err) } @@ -66,16 +66,15 @@ func (p *HTTP) ListenAndServe() error { handler := httputil.ReverseProxy{ Director: func(r *http.Request) { // Route requests using the telegram API token and with a limited body - reqUrl := *endpointUrl - reqUrl.User = nil - reqUrl.Path = fmt.Sprintf(p.Endpoint, p.Token, r.URL.Path[1:]) - r.URL = &reqUrl + reqURL := *endpointURL + reqURL.User = nil + reqURL.Path = fmt.Sprintf(p.Endpoint, p.Token, r.URL.Path[1:]) + r.URL = &reqURL r.Body = http.MaxBytesReader(nil, r.Body, DefaultMaxBodyBytes) }, ErrorHandler: func(w http.ResponseWriter, r *http.Request, err error) { p.Logger.Warn("request to telegram API failed", zap.Error(err), zap.Int64("request_id", requestID(r))) w.WriteHeader(http.StatusBadGateway) - return }, Transport: p.Transport, } @@ -128,7 +127,7 @@ type clientCtxKey struct{} func (p *HTTP) AuthMiddleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { groups := pathRe.FindStringSubmatch(r.URL.Path) - if len(groups) != 3 { + if groups == nil { w.WriteHeader(http.StatusBadRequest) return } From 68e0725546695e9fc15909027e856f9d07d71c99 Mon Sep 17 00:00:00 2001 From: renbou Date: Thu, 28 Jul 2022 21:35:07 +0300 Subject: [PATCH 10/17] chore: attempt to fix ci --- .github/workflows/lint.yml | 2 ++ .github/workflows/test.yml | 2 +- .golangci.yml | 2 ++ 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 133bdb5..783eacf 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -11,3 +11,5 @@ jobs: uses: actions/setup-go@v3 - name: golangci-lint uses: golangci/golangci-lint-action@v3 + with: + version: latest diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index e47e4b8..38c2d1f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,4 +1,4 @@ -name: lint +name: test on: - pull_request jobs: diff --git a/.golangci.yml b/.golangci.yml index 314eef8..c8236c7 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -52,3 +52,5 @@ linters-settings: - status.Error( - retry.Recoverable( - retry.Unrecoverable( + +modules-download-mode: readonly From 9983588448be03b707dafc4a49e218657c98bbc0 Mon Sep 17 00:00:00 2001 From: renbou Date: Thu, 28 Jul 2022 22:04:14 +0300 Subject: [PATCH 11/17] chore: attempt to fix ci #2 --- .github/workflows/lint.yml | 2 +- .github/workflows/test.yml | 4 ++-- go.mod | 4 ++-- go.sum | 8 ++++---- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 783eacf..87bdd84 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -2,7 +2,7 @@ name: lint on: - pull_request jobs: - code: + lint: runs-on: ubuntu-latest steps: - name: Checkout diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 38c2d1f..cf6a254 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -2,7 +2,7 @@ name: test on: - pull_request jobs: - code: + test: runs-on: ubuntu-latest steps: - name: Checkout @@ -10,4 +10,4 @@ jobs: - name: Set up Go uses: actions/setup-go@v3 - name: Run tests - uses: make test + run: make test diff --git a/go.mod b/go.mod index d29956d..16bbeb1 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,7 @@ require ( github.com/stretchr/testify v1.8.0 go.uber.org/zap v1.21.0 google.golang.org/grpc v1.48.0 - google.golang.org/protobuf v1.28.0 + google.golang.org/protobuf v1.28.1 ) require ( @@ -24,7 +24,7 @@ require ( github.com/technoweenie/multipartstreamer v1.0.1 // indirect go.uber.org/atomic v1.9.0 // indirect go.uber.org/multierr v1.8.0 // indirect - golang.org/x/net v0.0.0-20220726230323-06994584191e // indirect + golang.org/x/net v0.0.0-20220728181054-f92ba40d432d // indirect golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 // indirect golang.org/x/text v0.3.7 // indirect google.golang.org/genproto v0.0.0-20220725144611-272f38e5d71b // indirect diff --git a/go.sum b/go.sum index db33b85..a20dfcd 100644 --- a/go.sum +++ b/go.sum @@ -121,8 +121,8 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLL golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= -golang.org/x/net v0.0.0-20220726230323-06994584191e h1:wOQNKh1uuDGRnmgF0jDxh7ctgGy/3P4rYWQRVJD4/Yg= -golang.org/x/net v0.0.0-20220726230323-06994584191e/go.mod h1:AaygXjzTFtRAg2ttMY5RMuhpJ3cNnI0XpyFJD1iQRSM= +golang.org/x/net v0.0.0-20220728181054-f92ba40d432d h1:3iMzhioG3w6/URLOo7X7eZRkWoLdz9iWE/UsnXHNTfY= +golang.org/x/net v0.0.0-20220728181054-f92ba40d432d/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -187,8 +187,8 @@ google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlba google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= -google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw= -google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w= +google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= From 5ed5b4117c97c1c82d2f85eaf5f6010ae4f859b8 Mon Sep 17 00:00:00 2001 From: renbou Date: Thu, 28 Jul 2022 22:07:09 +0300 Subject: [PATCH 12/17] chore: attempt to fix ci #3 --- .github/workflows/lint.yml | 2 ++ .github/workflows/test.yml | 2 ++ 2 files changed, 4 insertions(+) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 87bdd84..c297841 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -9,6 +9,8 @@ jobs: uses: actions/checkout@v3 - name: Set up Go uses: actions/setup-go@v3 + with: + go-version: ">=1.18.0" - name: golangci-lint uses: golangci/golangci-lint-action@v3 with: diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index cf6a254..29749fa 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -9,5 +9,7 @@ jobs: uses: actions/checkout@v3 - name: Set up Go uses: actions/setup-go@v3 + with: + go-version: ">=1.18.0" - name: Run tests run: make test From a51f4044fc16a839e26bc5dd20d3f3d4031baf9d Mon Sep 17 00:00:00 2001 From: renbou Date: Fri, 29 Jul 2022 19:28:41 +0300 Subject: [PATCH 13/17] huge refactor --- .golangci.yml | 6 + Makefile | 24 +- api/proto/mux.proto | 2 +- cmd/aboba-handler/main.go | 43 --- cmd/core/main.go | 77 ----- cmd/debug-handler/main.go | 29 -- cmd/shake-cat-handler/main.go | 44 --- config_core.yaml | 4 - config_handler.yaml | 4 - docker/docker-compose-redis-only.yml | 13 - go.mod | 13 +- go.sum | 44 +-- internal/config/config.go | 28 ++ internal/genproto/mux.pb.go | 8 +- internal/mockproto/mux_mock.go | 419 -------------------------- internal/mocks/mux_mock.go | 201 ++++++++++++ internal/mocks/tgbotapi_mock.go | 50 +++ {pkg => internal}/models/models.go | 10 +- internal/proxy/grpc.go | 51 ++++ internal/proxy/http.go | 73 +++-- internal/proxy/proxy.go | 1 - internal/services/allowlist_test.go | 63 ++++ internal/services/error.go | 30 -- internal/services/models.go | 10 - internal/services/multiplexer.go | 9 +- internal/services/multiplexer_test.go | 6 +- internal/services/registrar.go | 88 ++---- internal/services/registrar_test.go | 235 +++++++++------ pkg/auth/auth.go | 67 +--- pkg/core/answer.go | 11 - pkg/core/config.go | 24 -- pkg/core/constant.go | 6 - pkg/handlers/config.go | 9 - pkg/handlers/simple.go | 64 ---- pkg/proxy/client.go | 96 +++--- pkg/retry/retry.go | 30 +- pkg/retry/retry_test.go | 10 +- pkg/services/authenticator.go | 36 +++ pkg/services/authenticator_test.go | 64 ++++ pkg/services/authprovider.go | 32 -- pkg/services/updateprovider.go | 27 +- pkg/services/updateprovider_test.go | 63 ++++ pkg/utils/logger.go | 25 -- pkg/utils/telegram.go | 28 -- 44 files changed, 911 insertions(+), 1266 deletions(-) delete mode 100644 cmd/aboba-handler/main.go delete mode 100644 cmd/core/main.go delete mode 100644 cmd/debug-handler/main.go delete mode 100644 cmd/shake-cat-handler/main.go delete mode 100644 config_core.yaml delete mode 100644 config_handler.yaml delete mode 100644 docker/docker-compose-redis-only.yml create mode 100644 internal/config/config.go delete mode 100644 internal/mockproto/mux_mock.go create mode 100644 internal/mocks/mux_mock.go create mode 100644 internal/mocks/tgbotapi_mock.go rename {pkg => internal}/models/models.go (71%) create mode 100644 internal/proxy/grpc.go delete mode 100644 internal/proxy/proxy.go create mode 100644 internal/services/allowlist_test.go delete mode 100644 internal/services/error.go delete mode 100644 internal/services/models.go delete mode 100644 pkg/core/answer.go delete mode 100644 pkg/core/config.go delete mode 100644 pkg/core/constant.go delete mode 100644 pkg/handlers/config.go delete mode 100644 pkg/handlers/simple.go create mode 100644 pkg/services/authenticator.go create mode 100644 pkg/services/authenticator_test.go delete mode 100644 pkg/services/authprovider.go create mode 100644 pkg/services/updateprovider_test.go delete mode 100644 pkg/utils/logger.go delete mode 100644 pkg/utils/telegram.go diff --git a/.golangci.yml b/.golangci.yml index c8236c7..e2a88ea 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -10,22 +10,26 @@ linters: - dogsled - durationcheck - dupl + - errcheck - errchkjson - errname - errorlint - exhaustive - exportloopref + - gocognit - gocritic - gofumpt - goimports - gomnd - gomoddirectives + - gosec - gosimple - govet - ifshort - ineffassign - importas - misspell + - nilerr - noctx - prealloc - predeclared @@ -45,6 +49,8 @@ linters-settings: check-exported: false unused: check-exported: false + gocognit: + min-complexity: 20 wrapcheck: ignoreSigs: - .Errorf( diff --git a/Makefile b/Makefile index 4612645..95e41e0 100644 --- a/Makefile +++ b/Makefile @@ -1,26 +1,40 @@ .PHONY: proto proto: cd api/proto && \ - protoc \ + protoc \ --go_opt=Mmux.proto=github.com/bbralion/CTFloodBot/internal/genproto \ --go-grpc_opt=Mmux.proto=github.com/bbralion/CTFloodBot/internal/genproto \ --go_opt=paths=source_relative \ --go-grpc_opt=paths=source_relative \ --go_out=../../internal/genproto \ --go-grpc_out=../../internal/genproto \ - mux.proto + mux.proto .PHONY: mocks mocks: mockgen \ - -package=mockproto \ - -destination=internal/mockproto/mux_mock.go \ - -source=internal/genproto/mux_grpc.pb.go MultiplexerServiceClient + -package=mocks \ + -destination=internal/mocks/mux_mock.go \ + github.com/bbralion/CTFloodBot/internal/genproto MultiplexerServiceClient,MultiplexerService_RegisterHandlerClient + mockgen \ + -package mocks \ + -destination=internal/mocks/tgbotapi_mock.go \ + --mock_names HttpClient=MockTGBotAPIHTTPClient \ + github.com/go-telegram-bot-api/telegram-bot-api HttpClient .PHONY: test test: go test -v -race -count=1 ./... +.PHONY: cover +cover: + go test -race -count=1 -covermode=atomic -coverprofile cover.tmp.out -coverpkg=./... -v ./... && \ + grep -v 'genproto\|mocks' cover.tmp.out > cover.out + go tool cover -func cover.out && \ + go tool cover -html cover.out -o cover.html && \ + open cover.html && sleep 1 && \ + rm -f cover.tmp.out cover.out cover.html + .PHONY: lint lint: golangci-lint run -v \ No newline at end of file diff --git a/api/proto/mux.proto b/api/proto/mux.proto index 18d4a28..7a45297 100644 --- a/api/proto/mux.proto +++ b/api/proto/mux.proto @@ -8,7 +8,7 @@ message Config { // Update is a single update received by the proxy, passed as the actual stringified update object. message Update { - bytes json = 1; + string json = 1; } message ConfigRequest {} diff --git a/cmd/aboba-handler/main.go b/cmd/aboba-handler/main.go deleted file mode 100644 index 523cc0f..0000000 --- a/cmd/aboba-handler/main.go +++ /dev/null @@ -1,43 +0,0 @@ -package main - -import ( - "github.com/bbralion/CTFloodBot/pkg/core" - "github.com/bbralion/CTFloodBot/pkg/handlers" - "github.com/bbralion/CTFloodBot/pkg/utils" - telegramapi "github.com/go-telegram-bot-api/telegram-bot-api" - "github.com/jinzhu/configor" - "go.uber.org/zap" -) - -var config handlers.HandlerConfig - -const ( - TelegramTextCommand = "aboba" - TelegramTextAnswer = "abobus" -) - -func main() { - logger := utils.GetLogger() - - err := configor.Load(&config, "config_handler.yaml") - if err != nil { - logger.Fatal("Failed to parse app config", zap.Error(err)) - } - - handler := handlers.SimpleHandler{ - Handler: func(logger *zap.Logger, update *telegramapi.Update, answerChan handlers.AnswerChan) { - message := update.Message - if message == nil { - return - } - if message.Text == TelegramTextCommand { - msg := telegramapi.NewMessage(message.Chat.ID, TelegramTextAnswer) - msg.ReplyToMessageID = message.MessageID - answerChan <- core.HandlerAnswer{Type: "message:message_config", MessageConfig: &msg} - } - }, - Logger: logger, - Config: config, - } - handler.Run() -} diff --git a/cmd/core/main.go b/cmd/core/main.go deleted file mode 100644 index dec4bb8..0000000 --- a/cmd/core/main.go +++ /dev/null @@ -1,77 +0,0 @@ -package main - -import ( - "context" - "encoding/json" - - "github.com/bbralion/CTFloodBot/pkg/core" - "github.com/bbralion/CTFloodBot/pkg/utils" - telegramapi "github.com/go-telegram-bot-api/telegram-bot-api" - "github.com/jinzhu/configor" - "go.uber.org/zap" -) - -var config core.BotCoreConfig - -func main() { - logger := utils.GetLogger() - ctx := context.Background() - - err := configor.Load(&config, "config_core.yaml") - if err != nil { - logger.Fatal("Failed to parse app config", zap.Error(err)) - } - - botAPI, err := telegramapi.NewBotAPI(config.TelegramToken) - if err != nil { - logger.Fatal("Failed to create telegram api", zap.Error(err)) - } - - redisClient := core.GetRedisClientByConfig(config.Redis) - - tgUpdatesChan, err := botAPI.GetUpdatesChan(telegramapi.NewUpdate(0)) - if err != nil { - logger.Fatal("Failed to get telegram updates chanel", zap.Error(err)) - } - - answerSubscriber := redisClient.Subscribe(ctx, core.RedisAnswersChanel) - answerChan := answerSubscriber.Channel() - for { - select { - case update := <-tgUpdatesChan: - logger.Info("New update", zap.String("update_type", utils.GetTelegramUpdateType(&update))) - byteMessage, err := json.Marshal(update) - if err != nil { - logger.Error("Failed to marshal telegram update", zap.Error(err)) - continue - } - - command := redisClient.Publish(ctx, core.RedisUpdateChanel, byteMessage) - if command.Err() != nil { - logger.Error("Failed to send telegram update in redis", zap.Error(err)) - } - - case answerSubscriber := <-answerChan: - message := answerSubscriber.Payload - var answer core.HandlerAnswer - err := json.Unmarshal([]byte(message), &answer) - if err != nil { - logger.Error("Failed to unmarshal handler answer", zap.Error(err), zap.String("answer", message)) - continue - } - var telegramSend telegramapi.Chattable - switch answer.Type { - case "message:message_config": - telegramSend = answer.MessageConfig - case "message:sticker_config": - telegramSend = answer.StickerConfig - } - if telegramSend != nil { - _, err = botAPI.Send(telegramSend) - if err != nil { - logger.Error("Failed to send in telegram", zap.String("type", answer.Type), zap.Error(err)) - } - } - } - } -} diff --git a/cmd/debug-handler/main.go b/cmd/debug-handler/main.go deleted file mode 100644 index d9dbee7..0000000 --- a/cmd/debug-handler/main.go +++ /dev/null @@ -1,29 +0,0 @@ -package main - -import ( - "github.com/bbralion/CTFloodBot/pkg/handlers" - "github.com/bbralion/CTFloodBot/pkg/utils" - telegramapi "github.com/go-telegram-bot-api/telegram-bot-api" - "github.com/jinzhu/configor" - "go.uber.org/zap" -) - -var config handlers.HandlerConfig - -func main() { - logger := utils.GetLogger() - - err := configor.Load(&config, "config_handler.yaml") - if err != nil { - logger.Fatal("Failed to parse app config", zap.Error(err)) - } - - handler := handlers.SimpleHandler{ - Handler: func(logger *zap.Logger, update *telegramapi.Update, _ handlers.AnswerChan) { - logger.Info("Received update", zap.Any("update", update)) - }, - Logger: logger, - Config: config, - } - handler.Run() -} diff --git a/cmd/shake-cat-handler/main.go b/cmd/shake-cat-handler/main.go deleted file mode 100644 index 792b112..0000000 --- a/cmd/shake-cat-handler/main.go +++ /dev/null @@ -1,44 +0,0 @@ -package main - -import ( - "strings" - - "github.com/bbralion/CTFloodBot/pkg/core" - "github.com/bbralion/CTFloodBot/pkg/handlers" - "github.com/bbralion/CTFloodBot/pkg/utils" - telegramapi "github.com/go-telegram-bot-api/telegram-bot-api" - "github.com/jinzhu/configor" - "go.uber.org/zap" -) - -var config handlers.HandlerConfig - -const ( - TelegramTextCommand = "/shake_cat_stick" - StickerID = "CAACAgIAAxkBAAIBiGLfzvi09zcCIPcPc6pu4_GsC3nwAAJVHQACm9J4Sf-ATjduPn5eKQQ" -) - -func main() { - logger := utils.GetLogger() - - err := configor.Load(&config, "config_handler.yaml") - if err != nil { - logger.Fatal("Failed to parse app config", zap.Error(err)) - } - - handler := handlers.SimpleHandler{ - Handler: func(logger *zap.Logger, update *telegramapi.Update, answerChan handlers.AnswerChan) { - message := update.Message - if message == nil { - return - } - if strings.HasPrefix(message.Text, TelegramTextCommand) { - sticker := telegramapi.NewStickerShare(message.Chat.ID, StickerID) - answerChan <- core.HandlerAnswer{Type: "message:sticker_config", StickerConfig: &sticker} - } - }, - Logger: logger, - Config: config, - } - handler.Run() -} diff --git a/config_core.yaml b/config_core.yaml deleted file mode 100644 index 7427275..0000000 --- a/config_core.yaml +++ /dev/null @@ -1,4 +0,0 @@ -redis: - host: localhost:6379 - username: - password: aboba \ No newline at end of file diff --git a/config_handler.yaml b/config_handler.yaml deleted file mode 100644 index 7427275..0000000 --- a/config_handler.yaml +++ /dev/null @@ -1,4 +0,0 @@ -redis: - host: localhost:6379 - username: - password: aboba \ No newline at end of file diff --git a/docker/docker-compose-redis-only.yml b/docker/docker-compose-redis-only.yml deleted file mode 100644 index 02e4ada..0000000 --- a/docker/docker-compose-redis-only.yml +++ /dev/null @@ -1,13 +0,0 @@ -version: '3.8' -services: - cache: - image: redis:6.2-alpine - restart: always - ports: - - '6379:6379' - command: redis-server --save 20 1 --loglevel warning --requirepass aboba - volumes: - - cache:/data -volumes: - cache: - driver: local \ No newline at end of file diff --git a/go.mod b/go.mod index 16bbeb1..90e7cb8 100644 --- a/go.mod +++ b/go.mod @@ -3,31 +3,26 @@ module github.com/bbralion/CTFloodBot go 1.18 require ( - github.com/go-redis/redis/v8 v8.11.5 + github.com/go-logr/logr v1.2.3 github.com/go-telegram-bot-api/telegram-bot-api v1.0.1-0.20201107014523-54104a08f947 github.com/golang/mock v1.6.0 - github.com/jinzhu/configor v1.2.1 github.com/justinas/alice v1.2.0 github.com/stretchr/testify v1.8.0 - go.uber.org/zap v1.21.0 + go.uber.org/goleak v1.1.12 google.golang.org/grpc v1.48.0 google.golang.org/protobuf v1.28.1 ) require ( - github.com/BurntSushi/toml v1.2.0 // indirect - github.com/cespare/xxhash/v2 v2.1.2 // indirect github.com/davecgh/go-spew v1.1.1 // indirect - github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/golang/protobuf v1.5.2 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/technoweenie/multipartstreamer v1.0.1 // indirect - go.uber.org/atomic v1.9.0 // indirect - go.uber.org/multierr v1.8.0 // indirect golang.org/x/net v0.0.0-20220728181054-f92ba40d432d // indirect golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 // indirect golang.org/x/text v0.3.7 // indirect + golang.org/x/tools v0.1.12 // indirect + golang.org/x/xerrors v0.0.0-20220609144429-65e65417b02f // indirect google.golang.org/genproto v0.0.0-20220725144611-272f38e5d71b // indirect - gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index a20dfcd..6c27ec6 100644 --- a/go.sum +++ b/go.sum @@ -1,15 +1,9 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= -github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0= -github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= -github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= -github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= -github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE= -github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= @@ -20,18 +14,15 @@ github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWH github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= -github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk= github.com/envoyproxy/go-control-plane v0.10.2-0.20220325020618-49ff273808a1/go.mod h1:KJwIaB5Mv44NWtYuAOFCVOjcI94vtpEz2JU/D2v6IjE= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= -github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= -github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI= -github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo= +github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0= +github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-telegram-bot-api/telegram-bot-api v1.0.1-0.20201107014523-54104a08f947 h1:CguiLTREMSU5GMaHMlAUAVb2cT8M+IpZVhgRK1te6Ds= github.com/go-telegram-bot-api/telegram-bot-api v1.0.1-0.20201107014523-54104a08f947/go.mod h1:lDm2E64X4OjFdBUA4hlN4mEvbSitvhJdKw7rsA8KHgI= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= @@ -62,8 +53,6 @@ github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= -github.com/jinzhu/configor v1.2.1 h1:OKk9dsR8i6HPOCZR8BcMtcEImAFjIhbJFZNyn5GCZko= -github.com/jinzhu/configor v1.2.1/go.mod h1:nX89/MOmDba7ZX7GCyU/VIaQ2Ar2aizBl2d3JLF/rDc= github.com/justinas/alice v1.2.0 h1:+MHSA/vccVCF4Uq37S42jwlkvI2Xzl7zTPCN5BnZNVo= github.com/justinas/alice v1.2.0/go.mod h1:fN5HRH/reO/zrUflLfTN43t3vXvKzvZIENsNEe7i7qA= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= @@ -71,18 +60,12 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= -github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= -github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= -github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE= -github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= -github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= @@ -92,16 +75,8 @@ github.com/technoweenie/multipartstreamer v1.0.1 h1:XRztA5MXiR1TIRHxH2uNxXxaIkKQ github.com/technoweenie/multipartstreamer v1.0.1/go.mod h1:jNVxdtShOxzAsukZwTSw6MDx5eUJoiEBsSvzDU9uzog= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI= -go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= -go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= -go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= -go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI= -go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ= -go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= -go.uber.org/multierr v1.8.0 h1:dg6GjLku4EH+249NNmoIciG9N/jURbDG+pFlTkhzIC8= -go.uber.org/multierr v1.8.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak= -go.uber.org/zap v1.21.0 h1:WefMeulhovoZ2sYXz7st6K0sLj7bBhpiFaud4r4zST8= -go.uber.org/zap v1.21.0/go.mod h1:wjWOCqI0f2ZZrJF/UufIOkiC8ii6tm1iqIsLo76RfJw= +go.uber.org/goleak v1.1.12 h1:gZAh5/EyT/HQwlpkCy6wTpqfH9H8Lz8zbm3dZh+OyzA= +go.uber.org/goleak v1.1.12/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= @@ -109,6 +84,7 @@ golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/lint v0.0.0-20190930215403-16217165b5de h1:5hukYrvBGR8/eNkX5mdUezrA6JiaEZDtJb9Ei+1LlBs= golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -154,11 +130,14 @@ golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBn golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= +golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20220609144429-65e65417b02f h1:uF6paiQQebLeSXkrTqHqz0MXhXXS1KgF41eUdBNvxK0= +golang.org/x/xerrors v0.0.0-20220609144429-65e65417b02f/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= @@ -192,14 +171,9 @@ google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqw gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= -gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 0000000..414d864 --- /dev/null +++ b/internal/config/config.go @@ -0,0 +1,28 @@ +package config + +type TelegramAPI struct { + Token string + Endpoint string +} + +type HTTPProxy struct { + AdvertisedEndpoint string `mapstructure:"advertised_endpoint"` + Listen string + Allow []string +} + +type GRPCProxy struct { + Listen string +} + +type Client struct { + Name string + Token string +} + +type Config struct { + TelegramAPI TelegramAPI `mapstructure:"telegram"` + HTTPProxy HTTPProxy `mapstructure:"http"` + GRPCProxy GRPCProxy `mapstructure:"grpc"` + Clients []Client +} diff --git a/internal/genproto/mux.pb.go b/internal/genproto/mux.pb.go index c20e285..deb2ecd 100644 --- a/internal/genproto/mux.pb.go +++ b/internal/genproto/mux.pb.go @@ -74,7 +74,7 @@ type Update struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Json []byte `protobuf:"bytes,1,opt,name=json,proto3" json:"json,omitempty"` + Json string `protobuf:"bytes,1,opt,name=json,proto3" json:"json,omitempty"` } func (x *Update) Reset() { @@ -109,11 +109,11 @@ func (*Update) Descriptor() ([]byte, []int) { return file_mux_proto_rawDescGZIP(), []int{1} } -func (x *Update) GetJson() []byte { +func (x *Update) GetJson() string { if x != nil { return x.Json } - return nil + return "" } type ConfigRequest struct { @@ -256,7 +256,7 @@ var file_mux_proto_rawDesc = []byte{ 0x6f, 0x78, 0x79, 0x5f, 0x65, 0x6e, 0x64, 0x70, 0x6f, 0x69, 0x6e, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0d, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x45, 0x6e, 0x64, 0x70, 0x6f, 0x69, 0x6e, 0x74, 0x22, 0x1c, 0x0a, 0x06, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x6a, - 0x73, 0x6f, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x04, 0x6a, 0x73, 0x6f, 0x6e, 0x22, + 0x73, 0x6f, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6a, 0x73, 0x6f, 0x6e, 0x22, 0x0f, 0x0a, 0x0d, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x22, 0x35, 0x0a, 0x0e, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x23, 0x0a, 0x06, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x18, 0x01, 0x20, 0x01, diff --git a/internal/mockproto/mux_mock.go b/internal/mockproto/mux_mock.go deleted file mode 100644 index 433471c..0000000 --- a/internal/mockproto/mux_mock.go +++ /dev/null @@ -1,419 +0,0 @@ -// Code generated by MockGen. DO NOT EDIT. -// Source: internal/genproto/mux_grpc.pb.go - -// Package mockproto is a generated GoMock package. -package mockproto - -import ( - context "context" - reflect "reflect" - - genproto "github.com/bbralion/CTFloodBot/internal/genproto" - gomock "github.com/golang/mock/gomock" - grpc "google.golang.org/grpc" - metadata "google.golang.org/grpc/metadata" -) - -// MockMultiplexerServiceClient is a mock of MultiplexerServiceClient interface. -type MockMultiplexerServiceClient struct { - ctrl *gomock.Controller - recorder *MockMultiplexerServiceClientMockRecorder -} - -// MockMultiplexerServiceClientMockRecorder is the mock recorder for MockMultiplexerServiceClient. -type MockMultiplexerServiceClientMockRecorder struct { - mock *MockMultiplexerServiceClient -} - -// NewMockMultiplexerServiceClient creates a new mock instance. -func NewMockMultiplexerServiceClient(ctrl *gomock.Controller) *MockMultiplexerServiceClient { - mock := &MockMultiplexerServiceClient{ctrl: ctrl} - mock.recorder = &MockMultiplexerServiceClientMockRecorder{mock} - return mock -} - -// EXPECT returns an object that allows the caller to indicate expected use. -func (m *MockMultiplexerServiceClient) EXPECT() *MockMultiplexerServiceClientMockRecorder { - return m.recorder -} - -// GetConfig mocks base method. -func (m *MockMultiplexerServiceClient) GetConfig(ctx context.Context, in *genproto.ConfigRequest, opts ...grpc.CallOption) (*genproto.ConfigResponse, error) { - m.ctrl.T.Helper() - varargs := []interface{}{ctx, in} - for _, a := range opts { - varargs = append(varargs, a) - } - ret := m.ctrl.Call(m, "GetConfig", varargs...) - ret0, _ := ret[0].(*genproto.ConfigResponse) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// GetConfig indicates an expected call of GetConfig. -func (mr *MockMultiplexerServiceClientMockRecorder) GetConfig(ctx, in interface{}, opts ...interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - varargs := append([]interface{}{ctx, in}, opts...) - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetConfig", reflect.TypeOf((*MockMultiplexerServiceClient)(nil).GetConfig), varargs...) -} - -// RegisterHandler mocks base method. -func (m *MockMultiplexerServiceClient) RegisterHandler(ctx context.Context, in *genproto.RegisterRequest, opts ...grpc.CallOption) (genproto.MultiplexerService_RegisterHandlerClient, error) { - m.ctrl.T.Helper() - varargs := []interface{}{ctx, in} - for _, a := range opts { - varargs = append(varargs, a) - } - ret := m.ctrl.Call(m, "RegisterHandler", varargs...) - ret0, _ := ret[0].(genproto.MultiplexerService_RegisterHandlerClient) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// RegisterHandler indicates an expected call of RegisterHandler. -func (mr *MockMultiplexerServiceClientMockRecorder) RegisterHandler(ctx, in interface{}, opts ...interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - varargs := append([]interface{}{ctx, in}, opts...) - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RegisterHandler", reflect.TypeOf((*MockMultiplexerServiceClient)(nil).RegisterHandler), varargs...) -} - -// MockMultiplexerService_RegisterHandlerClient is a mock of MultiplexerService_RegisterHandlerClient interface. -type MockMultiplexerService_RegisterHandlerClient struct { - ctrl *gomock.Controller - recorder *MockMultiplexerService_RegisterHandlerClientMockRecorder -} - -// MockMultiplexerService_RegisterHandlerClientMockRecorder is the mock recorder for MockMultiplexerService_RegisterHandlerClient. -type MockMultiplexerService_RegisterHandlerClientMockRecorder struct { - mock *MockMultiplexerService_RegisterHandlerClient -} - -// NewMockMultiplexerService_RegisterHandlerClient creates a new mock instance. -func NewMockMultiplexerService_RegisterHandlerClient(ctrl *gomock.Controller) *MockMultiplexerService_RegisterHandlerClient { - mock := &MockMultiplexerService_RegisterHandlerClient{ctrl: ctrl} - mock.recorder = &MockMultiplexerService_RegisterHandlerClientMockRecorder{mock} - return mock -} - -// EXPECT returns an object that allows the caller to indicate expected use. -func (m *MockMultiplexerService_RegisterHandlerClient) EXPECT() *MockMultiplexerService_RegisterHandlerClientMockRecorder { - return m.recorder -} - -// CloseSend mocks base method. -func (m *MockMultiplexerService_RegisterHandlerClient) CloseSend() error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "CloseSend") - ret0, _ := ret[0].(error) - return ret0 -} - -// CloseSend indicates an expected call of CloseSend. -func (mr *MockMultiplexerService_RegisterHandlerClientMockRecorder) CloseSend() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CloseSend", reflect.TypeOf((*MockMultiplexerService_RegisterHandlerClient)(nil).CloseSend)) -} - -// Context mocks base method. -func (m *MockMultiplexerService_RegisterHandlerClient) Context() context.Context { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Context") - ret0, _ := ret[0].(context.Context) - return ret0 -} - -// Context indicates an expected call of Context. -func (mr *MockMultiplexerService_RegisterHandlerClientMockRecorder) Context() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Context", reflect.TypeOf((*MockMultiplexerService_RegisterHandlerClient)(nil).Context)) -} - -// Header mocks base method. -func (m *MockMultiplexerService_RegisterHandlerClient) Header() (metadata.MD, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Header") - ret0, _ := ret[0].(metadata.MD) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// Header indicates an expected call of Header. -func (mr *MockMultiplexerService_RegisterHandlerClientMockRecorder) Header() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Header", reflect.TypeOf((*MockMultiplexerService_RegisterHandlerClient)(nil).Header)) -} - -// Recv mocks base method. -func (m *MockMultiplexerService_RegisterHandlerClient) Recv() (*genproto.Update, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Recv") - ret0, _ := ret[0].(*genproto.Update) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// Recv indicates an expected call of Recv. -func (mr *MockMultiplexerService_RegisterHandlerClientMockRecorder) Recv() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Recv", reflect.TypeOf((*MockMultiplexerService_RegisterHandlerClient)(nil).Recv)) -} - -// RecvMsg mocks base method. -func (m_2 *MockMultiplexerService_RegisterHandlerClient) RecvMsg(m interface{}) error { - m_2.ctrl.T.Helper() - ret := m_2.ctrl.Call(m_2, "RecvMsg", m) - ret0, _ := ret[0].(error) - return ret0 -} - -// RecvMsg indicates an expected call of RecvMsg. -func (mr *MockMultiplexerService_RegisterHandlerClientMockRecorder) RecvMsg(m interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RecvMsg", reflect.TypeOf((*MockMultiplexerService_RegisterHandlerClient)(nil).RecvMsg), m) -} - -// SendMsg mocks base method. -func (m_2 *MockMultiplexerService_RegisterHandlerClient) SendMsg(m interface{}) error { - m_2.ctrl.T.Helper() - ret := m_2.ctrl.Call(m_2, "SendMsg", m) - ret0, _ := ret[0].(error) - return ret0 -} - -// SendMsg indicates an expected call of SendMsg. -func (mr *MockMultiplexerService_RegisterHandlerClientMockRecorder) SendMsg(m interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SendMsg", reflect.TypeOf((*MockMultiplexerService_RegisterHandlerClient)(nil).SendMsg), m) -} - -// Trailer mocks base method. -func (m *MockMultiplexerService_RegisterHandlerClient) Trailer() metadata.MD { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Trailer") - ret0, _ := ret[0].(metadata.MD) - return ret0 -} - -// Trailer indicates an expected call of Trailer. -func (mr *MockMultiplexerService_RegisterHandlerClientMockRecorder) Trailer() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Trailer", reflect.TypeOf((*MockMultiplexerService_RegisterHandlerClient)(nil).Trailer)) -} - -// MockMultiplexerServiceServer is a mock of MultiplexerServiceServer interface. -type MockMultiplexerServiceServer struct { - ctrl *gomock.Controller - recorder *MockMultiplexerServiceServerMockRecorder -} - -// MockMultiplexerServiceServerMockRecorder is the mock recorder for MockMultiplexerServiceServer. -type MockMultiplexerServiceServerMockRecorder struct { - mock *MockMultiplexerServiceServer -} - -// NewMockMultiplexerServiceServer creates a new mock instance. -func NewMockMultiplexerServiceServer(ctrl *gomock.Controller) *MockMultiplexerServiceServer { - mock := &MockMultiplexerServiceServer{ctrl: ctrl} - mock.recorder = &MockMultiplexerServiceServerMockRecorder{mock} - return mock -} - -// EXPECT returns an object that allows the caller to indicate expected use. -func (m *MockMultiplexerServiceServer) EXPECT() *MockMultiplexerServiceServerMockRecorder { - return m.recorder -} - -// GetConfig mocks base method. -func (m *MockMultiplexerServiceServer) GetConfig(arg0 context.Context, arg1 *genproto.ConfigRequest) (*genproto.ConfigResponse, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "GetConfig", arg0, arg1) - ret0, _ := ret[0].(*genproto.ConfigResponse) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// GetConfig indicates an expected call of GetConfig. -func (mr *MockMultiplexerServiceServerMockRecorder) GetConfig(arg0, arg1 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetConfig", reflect.TypeOf((*MockMultiplexerServiceServer)(nil).GetConfig), arg0, arg1) -} - -// RegisterHandler mocks base method. -func (m *MockMultiplexerServiceServer) RegisterHandler(arg0 *genproto.RegisterRequest, arg1 genproto.MultiplexerService_RegisterHandlerServer) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "RegisterHandler", arg0, arg1) - ret0, _ := ret[0].(error) - return ret0 -} - -// RegisterHandler indicates an expected call of RegisterHandler. -func (mr *MockMultiplexerServiceServerMockRecorder) RegisterHandler(arg0, arg1 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RegisterHandler", reflect.TypeOf((*MockMultiplexerServiceServer)(nil).RegisterHandler), arg0, arg1) -} - -// mustEmbedUnimplementedMultiplexerServiceServer mocks base method. -func (m *MockMultiplexerServiceServer) mustEmbedUnimplementedMultiplexerServiceServer() { - m.ctrl.T.Helper() - m.ctrl.Call(m, "mustEmbedUnimplementedMultiplexerServiceServer") -} - -// mustEmbedUnimplementedMultiplexerServiceServer indicates an expected call of mustEmbedUnimplementedMultiplexerServiceServer. -func (mr *MockMultiplexerServiceServerMockRecorder) mustEmbedUnimplementedMultiplexerServiceServer() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "mustEmbedUnimplementedMultiplexerServiceServer", reflect.TypeOf((*MockMultiplexerServiceServer)(nil).mustEmbedUnimplementedMultiplexerServiceServer)) -} - -// MockUnsafeMultiplexerServiceServer is a mock of UnsafeMultiplexerServiceServer interface. -type MockUnsafeMultiplexerServiceServer struct { - ctrl *gomock.Controller - recorder *MockUnsafeMultiplexerServiceServerMockRecorder -} - -// MockUnsafeMultiplexerServiceServerMockRecorder is the mock recorder for MockUnsafeMultiplexerServiceServer. -type MockUnsafeMultiplexerServiceServerMockRecorder struct { - mock *MockUnsafeMultiplexerServiceServer -} - -// NewMockUnsafeMultiplexerServiceServer creates a new mock instance. -func NewMockUnsafeMultiplexerServiceServer(ctrl *gomock.Controller) *MockUnsafeMultiplexerServiceServer { - mock := &MockUnsafeMultiplexerServiceServer{ctrl: ctrl} - mock.recorder = &MockUnsafeMultiplexerServiceServerMockRecorder{mock} - return mock -} - -// EXPECT returns an object that allows the caller to indicate expected use. -func (m *MockUnsafeMultiplexerServiceServer) EXPECT() *MockUnsafeMultiplexerServiceServerMockRecorder { - return m.recorder -} - -// mustEmbedUnimplementedMultiplexerServiceServer mocks base method. -func (m *MockUnsafeMultiplexerServiceServer) mustEmbedUnimplementedMultiplexerServiceServer() { - m.ctrl.T.Helper() - m.ctrl.Call(m, "mustEmbedUnimplementedMultiplexerServiceServer") -} - -// mustEmbedUnimplementedMultiplexerServiceServer indicates an expected call of mustEmbedUnimplementedMultiplexerServiceServer. -func (mr *MockUnsafeMultiplexerServiceServerMockRecorder) mustEmbedUnimplementedMultiplexerServiceServer() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "mustEmbedUnimplementedMultiplexerServiceServer", reflect.TypeOf((*MockUnsafeMultiplexerServiceServer)(nil).mustEmbedUnimplementedMultiplexerServiceServer)) -} - -// MockMultiplexerService_RegisterHandlerServer is a mock of MultiplexerService_RegisterHandlerServer interface. -type MockMultiplexerService_RegisterHandlerServer struct { - ctrl *gomock.Controller - recorder *MockMultiplexerService_RegisterHandlerServerMockRecorder -} - -// MockMultiplexerService_RegisterHandlerServerMockRecorder is the mock recorder for MockMultiplexerService_RegisterHandlerServer. -type MockMultiplexerService_RegisterHandlerServerMockRecorder struct { - mock *MockMultiplexerService_RegisterHandlerServer -} - -// NewMockMultiplexerService_RegisterHandlerServer creates a new mock instance. -func NewMockMultiplexerService_RegisterHandlerServer(ctrl *gomock.Controller) *MockMultiplexerService_RegisterHandlerServer { - mock := &MockMultiplexerService_RegisterHandlerServer{ctrl: ctrl} - mock.recorder = &MockMultiplexerService_RegisterHandlerServerMockRecorder{mock} - return mock -} - -// EXPECT returns an object that allows the caller to indicate expected use. -func (m *MockMultiplexerService_RegisterHandlerServer) EXPECT() *MockMultiplexerService_RegisterHandlerServerMockRecorder { - return m.recorder -} - -// Context mocks base method. -func (m *MockMultiplexerService_RegisterHandlerServer) Context() context.Context { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Context") - ret0, _ := ret[0].(context.Context) - return ret0 -} - -// Context indicates an expected call of Context. -func (mr *MockMultiplexerService_RegisterHandlerServerMockRecorder) Context() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Context", reflect.TypeOf((*MockMultiplexerService_RegisterHandlerServer)(nil).Context)) -} - -// RecvMsg mocks base method. -func (m_2 *MockMultiplexerService_RegisterHandlerServer) RecvMsg(m interface{}) error { - m_2.ctrl.T.Helper() - ret := m_2.ctrl.Call(m_2, "RecvMsg", m) - ret0, _ := ret[0].(error) - return ret0 -} - -// RecvMsg indicates an expected call of RecvMsg. -func (mr *MockMultiplexerService_RegisterHandlerServerMockRecorder) RecvMsg(m interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RecvMsg", reflect.TypeOf((*MockMultiplexerService_RegisterHandlerServer)(nil).RecvMsg), m) -} - -// Send mocks base method. -func (m *MockMultiplexerService_RegisterHandlerServer) Send(arg0 *genproto.Update) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Send", arg0) - ret0, _ := ret[0].(error) - return ret0 -} - -// Send indicates an expected call of Send. -func (mr *MockMultiplexerService_RegisterHandlerServerMockRecorder) Send(arg0 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Send", reflect.TypeOf((*MockMultiplexerService_RegisterHandlerServer)(nil).Send), arg0) -} - -// SendHeader mocks base method. -func (m *MockMultiplexerService_RegisterHandlerServer) SendHeader(arg0 metadata.MD) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "SendHeader", arg0) - ret0, _ := ret[0].(error) - return ret0 -} - -// SendHeader indicates an expected call of SendHeader. -func (mr *MockMultiplexerService_RegisterHandlerServerMockRecorder) SendHeader(arg0 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SendHeader", reflect.TypeOf((*MockMultiplexerService_RegisterHandlerServer)(nil).SendHeader), arg0) -} - -// SendMsg mocks base method. -func (m_2 *MockMultiplexerService_RegisterHandlerServer) SendMsg(m interface{}) error { - m_2.ctrl.T.Helper() - ret := m_2.ctrl.Call(m_2, "SendMsg", m) - ret0, _ := ret[0].(error) - return ret0 -} - -// SendMsg indicates an expected call of SendMsg. -func (mr *MockMultiplexerService_RegisterHandlerServerMockRecorder) SendMsg(m interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SendMsg", reflect.TypeOf((*MockMultiplexerService_RegisterHandlerServer)(nil).SendMsg), m) -} - -// SetHeader mocks base method. -func (m *MockMultiplexerService_RegisterHandlerServer) SetHeader(arg0 metadata.MD) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "SetHeader", arg0) - ret0, _ := ret[0].(error) - return ret0 -} - -// SetHeader indicates an expected call of SetHeader. -func (mr *MockMultiplexerService_RegisterHandlerServerMockRecorder) SetHeader(arg0 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetHeader", reflect.TypeOf((*MockMultiplexerService_RegisterHandlerServer)(nil).SetHeader), arg0) -} - -// SetTrailer mocks base method. -func (m *MockMultiplexerService_RegisterHandlerServer) SetTrailer(arg0 metadata.MD) { - m.ctrl.T.Helper() - m.ctrl.Call(m, "SetTrailer", arg0) -} - -// SetTrailer indicates an expected call of SetTrailer. -func (mr *MockMultiplexerService_RegisterHandlerServerMockRecorder) SetTrailer(arg0 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetTrailer", reflect.TypeOf((*MockMultiplexerService_RegisterHandlerServer)(nil).SetTrailer), arg0) -} diff --git a/internal/mocks/mux_mock.go b/internal/mocks/mux_mock.go new file mode 100644 index 0000000..0b1f7f1 --- /dev/null +++ b/internal/mocks/mux_mock.go @@ -0,0 +1,201 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: github.com/bbralion/CTFloodBot/internal/genproto (interfaces: MultiplexerServiceClient,MultiplexerService_RegisterHandlerClient) + +// Package mocks is a generated GoMock package. +package mocks + +import ( + context "context" + reflect "reflect" + + genproto "github.com/bbralion/CTFloodBot/internal/genproto" + gomock "github.com/golang/mock/gomock" + grpc "google.golang.org/grpc" + metadata "google.golang.org/grpc/metadata" +) + +// MockMultiplexerServiceClient is a mock of MultiplexerServiceClient interface. +type MockMultiplexerServiceClient struct { + ctrl *gomock.Controller + recorder *MockMultiplexerServiceClientMockRecorder +} + +// MockMultiplexerServiceClientMockRecorder is the mock recorder for MockMultiplexerServiceClient. +type MockMultiplexerServiceClientMockRecorder struct { + mock *MockMultiplexerServiceClient +} + +// NewMockMultiplexerServiceClient creates a new mock instance. +func NewMockMultiplexerServiceClient(ctrl *gomock.Controller) *MockMultiplexerServiceClient { + mock := &MockMultiplexerServiceClient{ctrl: ctrl} + mock.recorder = &MockMultiplexerServiceClientMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockMultiplexerServiceClient) EXPECT() *MockMultiplexerServiceClientMockRecorder { + return m.recorder +} + +// GetConfig mocks base method. +func (m *MockMultiplexerServiceClient) GetConfig(arg0 context.Context, arg1 *genproto.ConfigRequest, arg2 ...grpc.CallOption) (*genproto.ConfigResponse, error) { + m.ctrl.T.Helper() + varargs := []interface{}{arg0, arg1} + for _, a := range arg2 { + varargs = append(varargs, a) + } + ret := m.ctrl.Call(m, "GetConfig", varargs...) + ret0, _ := ret[0].(*genproto.ConfigResponse) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetConfig indicates an expected call of GetConfig. +func (mr *MockMultiplexerServiceClientMockRecorder) GetConfig(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + varargs := append([]interface{}{arg0, arg1}, arg2...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetConfig", reflect.TypeOf((*MockMultiplexerServiceClient)(nil).GetConfig), varargs...) +} + +// RegisterHandler mocks base method. +func (m *MockMultiplexerServiceClient) RegisterHandler(arg0 context.Context, arg1 *genproto.RegisterRequest, arg2 ...grpc.CallOption) (genproto.MultiplexerService_RegisterHandlerClient, error) { + m.ctrl.T.Helper() + varargs := []interface{}{arg0, arg1} + for _, a := range arg2 { + varargs = append(varargs, a) + } + ret := m.ctrl.Call(m, "RegisterHandler", varargs...) + ret0, _ := ret[0].(genproto.MultiplexerService_RegisterHandlerClient) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// RegisterHandler indicates an expected call of RegisterHandler. +func (mr *MockMultiplexerServiceClientMockRecorder) RegisterHandler(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + varargs := append([]interface{}{arg0, arg1}, arg2...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RegisterHandler", reflect.TypeOf((*MockMultiplexerServiceClient)(nil).RegisterHandler), varargs...) +} + +// MockMultiplexerService_RegisterHandlerClient is a mock of MultiplexerService_RegisterHandlerClient interface. +type MockMultiplexerService_RegisterHandlerClient struct { + ctrl *gomock.Controller + recorder *MockMultiplexerService_RegisterHandlerClientMockRecorder +} + +// MockMultiplexerService_RegisterHandlerClientMockRecorder is the mock recorder for MockMultiplexerService_RegisterHandlerClient. +type MockMultiplexerService_RegisterHandlerClientMockRecorder struct { + mock *MockMultiplexerService_RegisterHandlerClient +} + +// NewMockMultiplexerService_RegisterHandlerClient creates a new mock instance. +func NewMockMultiplexerService_RegisterHandlerClient(ctrl *gomock.Controller) *MockMultiplexerService_RegisterHandlerClient { + mock := &MockMultiplexerService_RegisterHandlerClient{ctrl: ctrl} + mock.recorder = &MockMultiplexerService_RegisterHandlerClientMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockMultiplexerService_RegisterHandlerClient) EXPECT() *MockMultiplexerService_RegisterHandlerClientMockRecorder { + return m.recorder +} + +// CloseSend mocks base method. +func (m *MockMultiplexerService_RegisterHandlerClient) CloseSend() error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "CloseSend") + ret0, _ := ret[0].(error) + return ret0 +} + +// CloseSend indicates an expected call of CloseSend. +func (mr *MockMultiplexerService_RegisterHandlerClientMockRecorder) CloseSend() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CloseSend", reflect.TypeOf((*MockMultiplexerService_RegisterHandlerClient)(nil).CloseSend)) +} + +// Context mocks base method. +func (m *MockMultiplexerService_RegisterHandlerClient) Context() context.Context { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Context") + ret0, _ := ret[0].(context.Context) + return ret0 +} + +// Context indicates an expected call of Context. +func (mr *MockMultiplexerService_RegisterHandlerClientMockRecorder) Context() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Context", reflect.TypeOf((*MockMultiplexerService_RegisterHandlerClient)(nil).Context)) +} + +// Header mocks base method. +func (m *MockMultiplexerService_RegisterHandlerClient) Header() (metadata.MD, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Header") + ret0, _ := ret[0].(metadata.MD) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Header indicates an expected call of Header. +func (mr *MockMultiplexerService_RegisterHandlerClientMockRecorder) Header() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Header", reflect.TypeOf((*MockMultiplexerService_RegisterHandlerClient)(nil).Header)) +} + +// Recv mocks base method. +func (m *MockMultiplexerService_RegisterHandlerClient) Recv() (*genproto.Update, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Recv") + ret0, _ := ret[0].(*genproto.Update) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Recv indicates an expected call of Recv. +func (mr *MockMultiplexerService_RegisterHandlerClientMockRecorder) Recv() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Recv", reflect.TypeOf((*MockMultiplexerService_RegisterHandlerClient)(nil).Recv)) +} + +// RecvMsg mocks base method. +func (m *MockMultiplexerService_RegisterHandlerClient) RecvMsg(arg0 interface{}) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "RecvMsg", arg0) + ret0, _ := ret[0].(error) + return ret0 +} + +// RecvMsg indicates an expected call of RecvMsg. +func (mr *MockMultiplexerService_RegisterHandlerClientMockRecorder) RecvMsg(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RecvMsg", reflect.TypeOf((*MockMultiplexerService_RegisterHandlerClient)(nil).RecvMsg), arg0) +} + +// SendMsg mocks base method. +func (m *MockMultiplexerService_RegisterHandlerClient) SendMsg(arg0 interface{}) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "SendMsg", arg0) + ret0, _ := ret[0].(error) + return ret0 +} + +// SendMsg indicates an expected call of SendMsg. +func (mr *MockMultiplexerService_RegisterHandlerClientMockRecorder) SendMsg(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SendMsg", reflect.TypeOf((*MockMultiplexerService_RegisterHandlerClient)(nil).SendMsg), arg0) +} + +// Trailer mocks base method. +func (m *MockMultiplexerService_RegisterHandlerClient) Trailer() metadata.MD { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Trailer") + ret0, _ := ret[0].(metadata.MD) + return ret0 +} + +// Trailer indicates an expected call of Trailer. +func (mr *MockMultiplexerService_RegisterHandlerClientMockRecorder) Trailer() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Trailer", reflect.TypeOf((*MockMultiplexerService_RegisterHandlerClient)(nil).Trailer)) +} diff --git a/internal/mocks/tgbotapi_mock.go b/internal/mocks/tgbotapi_mock.go new file mode 100644 index 0000000..8bce3e1 --- /dev/null +++ b/internal/mocks/tgbotapi_mock.go @@ -0,0 +1,50 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: github.com/go-telegram-bot-api/telegram-bot-api (interfaces: HttpClient) + +// Package mocks is a generated GoMock package. +package mocks + +import ( + http "net/http" + reflect "reflect" + + gomock "github.com/golang/mock/gomock" +) + +// MockTGBotAPIHTTPClient is a mock of HttpClient interface. +type MockTGBotAPIHTTPClient struct { + ctrl *gomock.Controller + recorder *MockTGBotAPIHTTPClientMockRecorder +} + +// MockTGBotAPIHTTPClientMockRecorder is the mock recorder for MockTGBotAPIHTTPClient. +type MockTGBotAPIHTTPClientMockRecorder struct { + mock *MockTGBotAPIHTTPClient +} + +// NewMockTGBotAPIHTTPClient creates a new mock instance. +func NewMockTGBotAPIHTTPClient(ctrl *gomock.Controller) *MockTGBotAPIHTTPClient { + mock := &MockTGBotAPIHTTPClient{ctrl: ctrl} + mock.recorder = &MockTGBotAPIHTTPClientMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockTGBotAPIHTTPClient) EXPECT() *MockTGBotAPIHTTPClientMockRecorder { + return m.recorder +} + +// Do mocks base method. +func (m *MockTGBotAPIHTTPClient) Do(arg0 *http.Request) (*http.Response, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Do", arg0) + ret0, _ := ret[0].(*http.Response) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Do indicates an expected call of Do. +func (mr *MockTGBotAPIHTTPClientMockRecorder) Do(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Do", reflect.TypeOf((*MockTGBotAPIHTTPClient)(nil).Do), arg0) +} diff --git a/pkg/models/models.go b/internal/models/models.go similarity index 71% rename from pkg/models/models.go rename to internal/models/models.go index f448e74..bd54388 100644 --- a/pkg/models/models.go +++ b/internal/models/models.go @@ -6,10 +6,12 @@ import ( tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" ) -type ( - UpdateChan tgbotapi.UpdatesChannel - ErrorChan <-chan error -) +type PossibleUpdate struct { + Update tgbotapi.Update + Error error +} + +type UpdateChan <-chan PossibleUpdate type MatcherGroup []*regexp.Regexp diff --git a/internal/proxy/grpc.go b/internal/proxy/grpc.go new file mode 100644 index 0000000..0ed5950 --- /dev/null +++ b/internal/proxy/grpc.go @@ -0,0 +1,51 @@ +package proxy + +import ( + "context" + "errors" + + "github.com/bbralion/CTFloodBot/internal/genproto" + "github.com/bbralion/CTFloodBot/pkg/auth" + "github.com/bbralion/CTFloodBot/pkg/services" + "github.com/go-logr/logr" + "google.golang.org/grpc" +) + +type GRPC struct { + genproto.UnimplementedMultiplexerServiceServer + AdvertisedHTTPEndpoint string + Addr string + Logger logr.Logger + AuthProvider services.Authenticator +} + +func (p *GRPC) ListenAndServe() error { + if p.AuthProvider == nil || p.AdvertisedHTTPEndpoint == "" { + return errors.New("logger, auth provider and the advertised http endpoint must be set") + } + + return nil +} + +func (p *GRPC) GetConfig(context.Context, *genproto.ConfigRequest) (*genproto.ConfigResponse, error) { + return &genproto.ConfigResponse{ + Config: &genproto.Config{ + ProxyEndpoint: p.AdvertisedHTTPEndpoint, + }, + }, nil +} + +func (p *GRPC) RegisterHandler(*genproto.RegisterRequest, genproto.MultiplexerService_RegisterHandlerServer) error { + return nil +} + +func (p *GRPC) setupGRPC() *grpc.Server { + interceptor := auth.NewGRPCServerInterceptor(p.Logger, p.AuthProvider) + server := grpc.NewServer( + grpc.UnaryInterceptor(interceptor.Unary()), + grpc.StreamInterceptor(interceptor.Stream()), + ) + + genproto.RegisterMultiplexerServiceServer(server, p) + return server +} diff --git a/internal/proxy/http.go b/internal/proxy/http.go index cb84f48..c80fe02 100644 --- a/internal/proxy/http.go +++ b/internal/proxy/http.go @@ -2,7 +2,6 @@ package proxy import ( "context" - "errors" "fmt" "net/http" "net/http/httputil" @@ -13,9 +12,9 @@ import ( internal "github.com/bbralion/CTFloodBot/internal/services" "github.com/bbralion/CTFloodBot/pkg/services" + "github.com/go-logr/logr" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" "github.com/justinas/alice" - "go.uber.org/zap" ) // DefaultRequestTimeout is the default timeout to be used for making requests to the telegram API @@ -27,9 +26,11 @@ const DefaultMaxBodyBytes = 16_000_000 // HTTP is the telegram API HTTP proxy type HTTP struct { http.Server - Logger *zap.Logger - AuthProvider services.AuthProvider - Allowlist internal.Allowlist + Logger logr.Logger + // If set will be used to authenticate clients via a token in the Authorization header + AuthProvider services.Authenticator + // If set only paths in the allowlist will be allowed + Allowlist internal.Allowlist // Transport is the transport to use for making requests to the telegram API. // http.DefaultTransport will be used by default Transport *http.Transport @@ -48,21 +49,15 @@ func (p *HTTP) Path() string { } func (p *HTTP) ListenAndServe() error { - if p.Logger == nil || p.AuthProvider == nil || p.Allowlist == nil { - return errors.New( - "logger, auth provider and allow list must be specified for the http proxy server") - } else if p.Token == "" { - return errors.New("telegram API token must be specified") - } - endpointURL, err := url.Parse(p.Endpoint) if err != nil { return fmt.Errorf("invalid endpoint url specified: %w", err) } - p.Logger = p.Logger.Named("http") + p.Logger = p.Logger.WithName("http") p.setDefaults() + // TODO: implement proper handling of special commands such as setMyCommands handler := httputil.ReverseProxy{ Director: func(r *http.Request) { // Route requests using the telegram API token and with a limited body @@ -73,13 +68,13 @@ func (p *HTTP) ListenAndServe() error { r.Body = http.MaxBytesReader(nil, r.Body, DefaultMaxBodyBytes) }, ErrorHandler: func(w http.ResponseWriter, r *http.Request, err error) { - p.Logger.Warn("request to telegram API failed", zap.Error(err), zap.Int64("request_id", requestID(r))) + p.Logger.Error(err, "request to telegram API failed", "request_id", requestID(r)) w.WriteHeader(http.StatusBadGateway) }, Transport: p.Transport, } - p.Handler = alice.New(p.PanicMiddleware, p.RequestIDMiddleware, p.LoggingMiddleware, p.AuthMiddleware).Then(&handler) + p.Handler = alice.New(p.PanicMiddleware, p.RequestIDMiddleware, p.LoggingMiddleware, p.AuthMiddleware, p.AllowPathMiddleware).Then(&handler) return p.ListenAndServe() } @@ -90,6 +85,7 @@ func (p *HTTP) setDefaults() { if p.Transport.ResponseHeaderTimeout == 0 { p.Transport.ResponseHeaderTimeout = DefaultRequestTimeout } + if p.Endpoint == "" { p.Endpoint = tgbotapi.APIEndpoint } @@ -112,9 +108,7 @@ func (p *HTTP) PanicMiddleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { defer func() { if v := recover(); v != nil { - p.Logger.Error("recovered from panic", - zap.Any("recover", v), - zap.Int64("request_id", requestID(r))) + p.Logger.Info("recovered from panic", "recover", v, "request_id", requestID(r)) } }() @@ -132,35 +126,48 @@ func (p *HTTP) AuthMiddleware(next http.Handler) http.Handler { return } - client, err := p.AuthProvider.Authenticate(groups[1]) - if err != nil { - w.WriteHeader(http.StatusUnauthorized) - return + if p.AuthProvider != nil { + client, err := p.AuthProvider.Authenticate(groups[1]) + if err != nil { + w.WriteHeader(http.StatusUnauthorized) + return + } + authenticatedReq := r.WithContext(context.WithValue(r.Context(), clientCtxKey{}, client)) + authenticatedReq.URL.Path = groups[2] + next.ServeHTTP(w, authenticatedReq) + } else { + next.ServeHTTP(w, r) } - - authenticatedReq := r.WithContext(context.WithValue(r.Context(), clientCtxKey{}, client)) - authenticatedReq.URL.Path = groups[2] - next.ServeHTTP(w, authenticatedReq) }) } func (p *HTTP) LoggingMiddleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { start := time.Now() - rcopy := *r + rcopy := r.Clone(context.Background()) // Always log, even on panic defer func() { latency := time.Since(start) p.Logger.Info("handled request", - zap.String("uri", rcopy.RequestURI), - zap.String("method", rcopy.Method), - zap.Duration("latency", latency), - zap.String("remote_addr", rcopy.RemoteAddr), - zap.Int64("request_id", requestID(r)), - zap.Any("client", r.Context().Value(clientCtxKey{}))) + "uri", rcopy.RequestURI, + "method", rcopy.Method, + "latency", latency, + "remote_addr", rcopy.RemoteAddr, + "request_id", requestID(r), + "client", r.Context().Value(clientCtxKey{})) }() next.ServeHTTP(w, r) }) } + +func (p *HTTP) AllowPathMiddleware(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if p.Allowlist != nil && !p.Allowlist.Allowed(r.URL.Path) { + w.WriteHeader(http.StatusForbidden) + return + } + next.ServeHTTP(w, r) + }) +} diff --git a/internal/proxy/proxy.go b/internal/proxy/proxy.go deleted file mode 100644 index 943b369..0000000 --- a/internal/proxy/proxy.go +++ /dev/null @@ -1 +0,0 @@ -package proxy diff --git a/internal/services/allowlist_test.go b/internal/services/allowlist_test.go new file mode 100644 index 0000000..d92d5ec --- /dev/null +++ b/internal/services/allowlist_test.go @@ -0,0 +1,63 @@ +package services + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func Test_staticAllowList_Allowed(t *testing.T) { + type args struct { + key string + } + tests := []struct { + name string + allowed []string + args args + want bool + }{ + { + name: "nil static allowlist", + allowed: nil, + args: args{ + key: "test-key", + }, + want: false, + }, + { + name: "empty static allowlist", + allowed: []string{}, + args: args{ + key: "test-key", + }, + want: false, + }, + { + name: "allowed value in allowlist", + allowed: []string{"allowed"}, + args: args{ + key: "allowed", + }, + want: true, + }, + { + name: "unallowed value in allowlist", + allowed: []string{"allowed"}, + args: args{ + key: "unallowed", + }, + want: false, + }, + } + + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + req := require.New(t) + + l := NewStaticAllowlist(tt.allowed) + req.Equal(tt.want, l.Allowed(tt.args.key)) + }) + } +} diff --git a/internal/services/error.go b/internal/services/error.go deleted file mode 100644 index eb3f770..0000000 --- a/internal/services/error.go +++ /dev/null @@ -1,30 +0,0 @@ -package services - -import "go.uber.org/zap" - -// error is the shared wrapper to be used for errors returned by services -type svcError struct { - Wrapped error - Info string - Message string -} - -func (e *svcError) Unwrap() error { - return e.Wrapped -} - -func (e *svcError) Error() string { - return e.Message -} - -func (e *svcError) ZapFields() []zap.Field { - return []zap.Field{zap.Error(e.Unwrap()), zap.String("info", e.Info), zap.String("message", e.Message)} -} - -func wrap(e error, p, m string) *svcError { - return &svcError{ - Wrapped: e, - Info: p, - Message: m, - } -} diff --git a/internal/services/models.go b/internal/services/models.go deleted file mode 100644 index 18d3de0..0000000 --- a/internal/services/models.go +++ /dev/null @@ -1,10 +0,0 @@ -package services - -import ( - tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" -) - -type ( - UpdateChan tgbotapi.UpdatesChannel - ErrorChan <-chan error -) diff --git a/internal/services/multiplexer.go b/internal/services/multiplexer.go index cbbbe33..d5ac1d4 100644 --- a/internal/services/multiplexer.go +++ b/internal/services/multiplexer.go @@ -2,10 +2,11 @@ package services import ( "context" + "errors" "sync" "sync/atomic" - "github.com/bbralion/CTFloodBot/pkg/models" + "github.com/bbralion/CTFloodBot/internal/models" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" ) @@ -13,7 +14,7 @@ import ( type Multiplexer interface { // Register registers a new handler which will receive updates until the context is canceled. // Safe for concurrent use, so matchers can be registered from anywhere. - Register(ctx context.Context, matchers models.MatcherGroup) (models.UpdateChan, error) + Register(ctx context.Context, matchers models.MatcherGroup) (tgbotapi.UpdatesChannel, error) // Serve multiplexes the update across the registered handlers. // Isn't safe for concurrent use, so all calls to Serve must be from a single goroutine. Serve(update tgbotapi.Update) @@ -35,9 +36,9 @@ type mapMux struct { bufferLen int } -func (m *mapMux) Register(ctx context.Context, matchers models.MatcherGroup) (models.UpdateChan, error) { +func (m *mapMux) Register(ctx context.Context, matchers models.MatcherGroup) (tgbotapi.UpdatesChannel, error) { if len(matchers) < 1 { - return nil, ErrNoMatchers + return nil, errors.New("cannot register with zero matchers") } key := muxKey(atomic.AddUint64((*uint64)(&m.curKey), 1)) diff --git a/internal/services/multiplexer_test.go b/internal/services/multiplexer_test.go index c6d8045..029e557 100644 --- a/internal/services/multiplexer_test.go +++ b/internal/services/multiplexer_test.go @@ -7,7 +7,7 @@ import ( "testing" "time" - "github.com/bbralion/CTFloodBot/pkg/models" + "github.com/bbralion/CTFloodBot/internal/models" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" "github.com/stretchr/testify/require" ) @@ -47,6 +47,10 @@ func TestMultiplexer(t *testing.T) { var wg sync.WaitGroup mux := NewMultiplexer(1) + + _, err := mux.Register(context.Background(), nil) + req.Error(err, "registration without matchers") + startExpectingMuxClient(&wg, req, mux, updates[:2], models.MatcherGroup{regexp.MustCompile("^/[ab]$")}) startExpectingMuxClient(&wg, req, mux, updates[:6], models.MatcherGroup{regexp.MustCompile("^/[a-f]$")}) startExpectingMuxClient(&wg, req, mux, updates[:9], models.MatcherGroup{regexp.MustCompile("^/[a-j]$")}) diff --git a/internal/services/registrar.go b/internal/services/registrar.go index f44f64e..73a0125 100644 --- a/internal/services/registrar.go +++ b/internal/services/registrar.go @@ -4,76 +4,66 @@ import ( "context" "encoding/json" "errors" + "fmt" "github.com/bbralion/CTFloodBot/internal/genproto" - "github.com/bbralion/CTFloodBot/pkg/models" + "github.com/bbralion/CTFloodBot/internal/models" "github.com/bbralion/CTFloodBot/pkg/retry" + "github.com/go-logr/logr" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" - "go.uber.org/zap" ) -var ErrNoMatchers = errors.New("cannot register with zero matchers") - // Registrar allows registration of command handlers for subsequent receival of updates type Registrar interface { // Register registers a new command handler with the given matchers. // The context should span the lifetime of the registered handler and canceled when it dies. - Register(ctx context.Context, matchers models.MatcherGroup) (models.UpdateChan, models.ErrorChan, error) + Register(ctx context.Context, matchers models.MatcherGroup) (models.UpdateChan, error) } // gRPCRegistrar is an implementation of Registrar using grpc with retries type gRPCRegistrar struct { - logger *zap.Logger + logger logr.Logger client genproto.MultiplexerServiceClient } -func (r *gRPCRegistrar) tryRegister(ctx context.Context, request *genproto.RegisterRequest, updatech chan tgbotapi.Update) *svcError { - stream, err := retry.Backoff(func() (error, genproto.MultiplexerService_RegisterHandlerClient) { +func (r *gRPCRegistrar) tryRegister(ctx context.Context, request *genproto.RegisterRequest, updateCh chan models.PossibleUpdate) error { + stream, err := retry.Backoff(func() (genproto.MultiplexerService_RegisterHandlerClient, error) { stream, err := r.client.RegisterHandler(ctx, request) + if err == nil { + return stream, nil + } if retry.IsGRPCUnavailable(err) { - r.logger.Info("gRPC registrar retrying connection to server", zap.Error(err)) - return retry.Recoverable(err), nil + r.logger.Error(err, "gRPC registrar retrying connection to server") + return nil, retry.Recoverable() } - return retry.Unrecoverable(err), stream + return nil, retry.Unrecoverable(err) }) if err != nil { - if retry.IsGRPCCanceled(err) { - // Client disconnected before we managed to register - return nil - } - return wrap(err, "RegisterHandler request failed", "failed to register command handler") - } - - wrapUpdateErr := func(err error, info string) *svcError { - return wrap(err, info, "failed to receive updates") + return fmt.Errorf("registering handler: %w", err) } for { updatePB, err := stream.Recv() if err != nil { - if retry.IsGRPCCanceled(err) { - // If the updates are simply stopped, then no error has happened - return nil - } - return wrapUpdateErr(err, "unexpected error receiving next update") + return fmt.Errorf("receiving update: %w", err) } var update tgbotapi.Update - if err := json.Unmarshal(updatePB.GetJson(), &update); err != nil { - return wrapUpdateErr(err, "failed to unmarshal json update") + if err := json.Unmarshal([]byte(updatePB.GetJson()), &update); err != nil { + return fmt.Errorf("unmarshaling update json: %w", err) } select { - case updatech <- update: + case updateCh <- models.PossibleUpdate{Update: update}: case <-ctx.Done(): return nil } } } -func (r *gRPCRegistrar) Register(ctx context.Context, matchers models.MatcherGroup) (models.UpdateChan, models.ErrorChan, error) { +func (r *gRPCRegistrar) Register(ctx context.Context, matchers models.MatcherGroup) (models.UpdateChan, error) { if len(matchers) < 1 { - return nil, nil, ErrNoMatchers + return nil, errors.New("cannot register with zero matchers") } request := &genproto.RegisterRequest{ @@ -83,40 +73,28 @@ func (r *gRPCRegistrar) Register(ctx context.Context, matchers models.MatcherGro request.Matchers[i] = m.String() } - updatech := make(chan tgbotapi.Update) - errorch := make(chan error, 1) + updateCh := make(chan models.PossibleUpdate) go func() { - defer close(updatech) - defer close(errorch) + defer close(updateCh) - _, err := retry.Static(func() (error, any) { - err := r.tryRegister(ctx, request, updatech) - if err == nil { + _, err := retry.Static(func() (any, error) { + err := r.tryRegister(ctx, request, updateCh) + if uw := errors.Unwrap(err); uw == nil || retry.IsGRPCCanceled(uw) { return nil, nil + } else if retry.IsGRPCUnavailable(uw) { + r.logger.Error(err, "gRPC registrar reconnecting stream") + return nil, retry.Recoverable() } - - // We can retry only due to connectivity issues - if retry.IsGRPCUnavailable(err.Unwrap()) { - r.logger.Info("gRPC registrar reconnecting stream", err.ZapFields()...) - return retry.Recoverable(err), nil - } - return retry.Unrecoverable(err), nil + return nil, retry.Unrecoverable(err) }) if err != nil { - // Recovery failed, log and return the error - var serr *svcError - errors.As(err, &serr) - r.logger.Error("gRPC registrar unable to reconnect", serr.ZapFields()...) - errorch <- serr + updateCh <- models.PossibleUpdate{Error: err} } }() - return updatech, errorch, nil + return updateCh, nil } // NewGRPCRegistrar creates a Registrar based on the gRPC API client with preconfigured retries -func NewGRPCRegistrar(logger *zap.Logger, client genproto.MultiplexerServiceClient) (Registrar, error) { - if client == nil { - return nil, errors.New("gRPC client must not be nil") - } - return &gRPCRegistrar{logger.Named("registrar"), client}, nil +func NewGRPCRegistrar(logger logr.Logger, client genproto.MultiplexerServiceClient) Registrar { + return &gRPCRegistrar{logger.WithName("registrar"), client} } diff --git a/internal/services/registrar_test.go b/internal/services/registrar_test.go index 13c9064..9024392 100644 --- a/internal/services/registrar_test.go +++ b/internal/services/registrar_test.go @@ -2,119 +2,164 @@ package services import ( "context" - "encoding/json" - "errors" "regexp" "testing" "time" "github.com/bbralion/CTFloodBot/internal/genproto" - "github.com/bbralion/CTFloodBot/internal/mockproto" - "github.com/bbralion/CTFloodBot/pkg/models" + "github.com/bbralion/CTFloodBot/internal/mocks" + "github.com/bbralion/CTFloodBot/internal/models" + "github.com/go-logr/logr" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" "github.com/golang/mock/gomock" "github.com/stretchr/testify/require" - "go.uber.org/zap" + "go.uber.org/goleak" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" ) -func TestGRPCRegistrar(t *testing.T) { - ctrl := gomock.NewController(t) - req := require.New(t) - logcfg := zap.NewDevelopmentConfig() - logcfg.DisableStacktrace = true - logger, err := logcfg.Build() - req.NoError(err, "should be able to create logger") - - // Creation of registrar - mockMuxClient := mockproto.NewMockMultiplexerServiceClient(ctrl) - registrar, err := NewGRPCRegistrar(logger, mockMuxClient) - req.NoError(err, "registrar creation shouldn't fail") - - // Registration without any matchers - ctx := context.Background() - _, _, err = registrar.Register(ctx, models.MatcherGroup{}) - req.ErrorIs(err, ErrNoMatchers, "shouldn't be able to register with no matchers") - - // Unrecoverable registration request fail - mockMuxClient.EXPECT().RegisterHandler(gomock.Any(), gomock.Any()).Return(nil, status.Error(codes.Unauthenticated, "fake unauthenticated error")) - updatech, errorch, err := registrar.Register(ctx, models.MatcherGroup{regexp.MustCompile("/command")}) - req.NoError(err, "registration shouldn't fail") - req.Eventually(func() bool { - select { - case e, ok := <-errorch: - req.True(ok, "error channel should not be empty") - req.Error(e, "should receive proper error on channel") - return true - default: - return false - } - }, time.Second, time.Millisecond*50) - req.Eventually(func() bool { - select { - case _, ok := <-updatech: - req.False(ok, "update channel should close without update") - return true - default: - return false - } - }, time.Second, time.Millisecond*50) +func Test_gRPCRegistrar_Register(t *testing.T) { + type args struct { + matchers models.MatcherGroup + } + type streamUpdate struct { + update *genproto.Update + err error + } + type registerResponse struct { + stream []streamUpdate + err error + } + type possibleUpdate struct { + update tgbotapi.Update + err bool + } - // Two failed registration requests followed by a successful registration with single update - var tgUpdate tgbotapi.Update - tgUpdate.Message = &tgbotapi.Message{ - Text: "message text", + tests := []struct { + name string + args args + registerResponses []registerResponse + want []possibleUpdate + wantErr bool + }{ + { + name: "registration with nil matchers", + args: args{matchers: nil}, + want: nil, + wantErr: true, + }, + { + name: "registration with no matchers", + args: args{matchers: models.MatcherGroup{}}, + want: nil, + wantErr: true, + }, + { + name: "unrecoverable registration error", + args: args{matchers: models.MatcherGroup{regexp.MustCompile("/command")}}, + registerResponses: []registerResponse{{err: status.Error(codes.Unauthenticated, "unauthenticated")}}, + want: []possibleUpdate{{err: true}}, + }, + { + name: "retries during registration", + args: args{matchers: models.MatcherGroup{regexp.MustCompile("^/command"), regexp.MustCompile("^.*$")}}, + registerResponses: []registerResponse{ + {err: status.Error(codes.Unavailable, "temporarily unavailable")}, + {err: status.Error(codes.Unavailable, "starting")}, + {err: nil, stream: []streamUpdate{ + {update: &genproto.Update{Json: `{"update_id":1}`}}, + {update: &genproto.Update{Json: `{"update_id":2}`}}, + {err: status.FromContextError(context.Canceled).Err()}, + }}, + }, + want: []possibleUpdate{ + {update: tgbotapi.Update{UpdateID: 1}}, + {update: tgbotapi.Update{UpdateID: 2}}, + }, + }, + { + name: "invalid json in update", + args: args{matchers: models.MatcherGroup{regexp.MustCompile("^/command"), regexp.MustCompile("^.*$")}}, + registerResponses: []registerResponse{ + {err: nil, stream: []streamUpdate{ + {update: &genproto.Update{Json: `{bad}`}}, + }}, + }, + want: []possibleUpdate{ + {err: true}, + }, + }, + { + name: "reconnect after stream fail", + args: args{matchers: models.MatcherGroup{regexp.MustCompile("^/aboba$")}}, + registerResponses: []registerResponse{ + {err: nil, stream: []streamUpdate{ + {update: &genproto.Update{Json: `{"update_id":1}`}}, + {err: status.Error(codes.Unavailable, "stream broken")}, + }}, + {err: nil, stream: []streamUpdate{ + {update: &genproto.Update{Json: `{"update_id":2}`}}, + {err: status.FromContextError(context.DeadlineExceeded).Err()}, + }}, + }, + want: []possibleUpdate{ + {update: tgbotapi.Update{UpdateID: 1}}, + {update: tgbotapi.Update{UpdateID: 2}}, + }, + }, } - tgUpdateBytes, err := json.Marshal(tgUpdate) - req.NoError(err, "should be able to marshal telegram update") - ctx, cancel := context.WithCancel(ctx) - mockUpdateStream := mockproto.NewMockMultiplexerService_RegisterHandlerClient(ctrl) - mockMuxClient.EXPECT().RegisterHandler(ctx, &genproto.RegisterRequest{ - Matchers: []string{"/command"}, - }).Return(nil, status.Error(codes.Unavailable, "fake network error 1")) - mockMuxClient.EXPECT().RegisterHandler(ctx, &genproto.RegisterRequest{ - Matchers: []string{"/command"}, - }).Return(nil, status.Error(codes.Unavailable, "fake network error 2")) - mockMuxClient.EXPECT().RegisterHandler(ctx, &genproto.RegisterRequest{ - Matchers: []string{"/command"}, - }).Return(mockUpdateStream, nil) - mockUpdateStream.EXPECT().Recv().Return(&genproto.Update{Json: tgUpdateBytes}, nil) - mockUpdateStream.EXPECT().Recv().Return(nil, status.FromContextError(context.Canceled).Err()) + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + t.Parallel() - updatech, errorch, err = registrar.Register(ctx, models.MatcherGroup{regexp.MustCompile("/command")}) - req.NoError(err, "registration shouldn't fail") + ctrl, req := gomock.NewController(t), require.New(t) + defer ctrl.Finish() - var update tgbotapi.Update - req.Eventually(func() bool { - select { - case update = <-updatech: - return true - default: - return false - } - }, time.Second, time.Millisecond*50, "expected update on channel") - req.Equal(tgUpdate, update, "received incorrect update") - cancel() - req.Eventually(func() bool { - select { - case update, ok := <-updatech: - if ok { - req.Fail("received unexpected update on channel", update) + mockMuxClient := mocks.NewMockMultiplexerServiceClient(ctrl) + r := NewGRPCRegistrar(logr.Discard(), mockMuxClient) + + reqMatchers := make([]string, len(tt.args.matchers)) + for i, m := range tt.args.matchers { + reqMatchers[i] = m.String() } - default: - return false - } - select { - case err, ok := <-errorch: - if ok { - req.Fail("received unexpected error on channel", `errs: "%v" "%v"`, err, errors.Unwrap(err)) + ctx := context.Background() + for i := range tt.registerResponses { + resp := tt.registerResponses[i] + stream := mocks.NewMockMultiplexerService_RegisterHandlerClient(ctrl) + for _, u := range resp.stream { + stream.EXPECT().Recv().Return(u.update, u.err) + } + mockMuxClient.EXPECT().RegisterHandler(ctx, &genproto.RegisterRequest{Matchers: reqMatchers}).Return(stream, resp.err) } - default: - return false - } - return true - }, time.Second, time.Millisecond*50, "expected updater goroutine to shut down and close channels") + + updateCh, err := r.Register(ctx, tt.args.matchers) + req.Equal(tt.wantErr, err != nil) + + left := len(tt.want) + req.Eventually(func() bool { + select { + case update, ok := <-updateCh: + if !ok { + req.Zero(left, "less updates on channel than wanted") + return true + } + + req.NotZero(left, "more updates on channel than wanted") + want := &tt.want[len(tt.want)-left] + req.Equal(want.err, update.Error != nil) + req.Equal(want.update, update.Update) + left-- + default: + } + return updateCh == nil + }, time.Second*5, time.Millisecond*50) + }) + } +} + +func TestMain(m *testing.M) { + goleak.VerifyTestMain(m) } diff --git a/pkg/auth/auth.go b/pkg/auth/auth.go index 05926cb..5bf5883 100644 --- a/pkg/auth/auth.go +++ b/pkg/auth/auth.go @@ -2,22 +2,16 @@ package auth import ( "context" - "encoding/json" - "errors" - "fmt" "github.com/bbralion/CTFloodBot/pkg/services" - "go.uber.org/zap" + "github.com/go-logr/logr" "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/metadata" "google.golang.org/grpc/status" ) -const ( - tokenKey = "authorization" - clientKey = "authenticated_client" -) +const tokenKey = "authorization" type GRPCClientInterceptor string @@ -60,36 +54,29 @@ func (t GRPCClientInterceptor) Stream() grpc.StreamClientInterceptor { // GRPCServerInterceptor is a Unary and Stream interceptor provider which // uses an underlying AuthProvider for authentication of clients type GRPCServerInterceptor struct { - logger *zap.Logger - provider services.AuthProvider + logger logr.Logger + provider services.Authenticator } // NewGRPCServerInterceptor returns a new gRPC server interceptor // which authenticates clients using the specified provider. -func NewGRPCServerInterceptor(logger *zap.Logger, provider services.AuthProvider) *GRPCServerInterceptor { +func NewGRPCServerInterceptor(logger logr.Logger, provider services.Authenticator) *GRPCServerInterceptor { return &GRPCServerInterceptor{logger, provider} } -func (i *GRPCServerInterceptor) authorize(ctx context.Context) (context.Context, error) { +func (i *GRPCServerInterceptor) authorize(ctx context.Context, method string) error { md, ok := metadata.FromIncomingContext(ctx) if !ok || len(md[tokenKey]) != 1 { - return nil, status.Error(codes.Unauthenticated, "must contain metadata with single auth token") - } - if len(md[clientKey]) != 0 { - return nil, status.Error(codes.Unauthenticated, "illegal metadata contained in request") + return status.Error(codes.Unauthenticated, "must contain metadata with single auth token") } client, err := i.provider.Authenticate(md[tokenKey][0]) if err != nil { - return nil, status.Error(codes.Unauthenticated, err.Error()) + return status.Error(codes.Unauthenticated, err.Error()) } - data, err := json.Marshal(client) - if err != nil { - i.logger.Error("failed to marshal client struct for storing in gRPC metadata", zap.Error(err)) - return nil, status.Error(codes.Internal, "internal error while authenticating client") - } - return metadata.NewIncomingContext(ctx, metadata.Join(md, metadata.Pairs(clientKey, string(data)))), nil + i.logger.Info("gRPC request from authenticated client", "client", client, "method", method) + return nil } // Unary returns a unary gRPC server interceptor for authentication @@ -100,26 +87,13 @@ func (i *GRPCServerInterceptor) Unary() grpc.UnaryServerInterceptor { info *grpc.UnaryServerInfo, handler grpc.UnaryHandler, ) (interface{}, error) { - ctx, err := i.authorize(ctx) - if err != nil { + if err := i.authorize(ctx, info.FullMethod); err != nil { return nil, err } return handler(ctx, req) } } -type authenticatedStream struct { - grpc.ServerStream - ctx context.Context -} - -func (s authenticatedStream) Context() context.Context { return s.ctx } -func (s authenticatedStream) RecvMsg(msg interface{}) error { return s.ServerStream.RecvMsg(msg) } //nolint:wrapcheck -func (s authenticatedStream) SendMsg(msg interface{}) error { return s.ServerStream.SendMsg(msg) } //nolint:wrapcheck -func (s authenticatedStream) SendHeader(md metadata.MD) error { return s.ServerStream.SendHeader(md) } //nolint:wrapcheck -func (s authenticatedStream) SetHeader(md metadata.MD) error { return s.ServerStream.SetHeader(md) } //nolint:wrapcheck -func (s authenticatedStream) SetTrailer(md metadata.MD) { s.ServerStream.SetTrailer(md) } - // Stream returns a stream gRPC server interceptor for authentication func (i *GRPCServerInterceptor) Stream() grpc.StreamServerInterceptor { return func( @@ -128,24 +102,9 @@ func (i *GRPCServerInterceptor) Stream() grpc.StreamServerInterceptor { info *grpc.StreamServerInfo, handler grpc.StreamHandler, ) error { - ctx, err := i.authorize(stream.Context()) - if err != nil { + if err := i.authorize(stream.Context(), info.FullMethod); err != nil { return err } - return handler(srv, authenticatedStream{stream, ctx}) - } -} - -// ClientFromCtx returns the client from an authenticated context -func ClientFromCtx(ctx context.Context) (*services.Client, error) { - md, ok := metadata.FromIncomingContext(ctx) - if !ok || len(md[clientKey]) != 1 { - return nil, errors.New("ClientFromCtx must only be called with an authenticated context") - } - - var client services.Client - if err := json.Unmarshal([]byte(md[clientKey][0]), &client); err != nil { - return nil, fmt.Errorf("failed to unmarshal stored client: %w", err) + return handler(srv, stream) } - return &client, nil } diff --git a/pkg/core/answer.go b/pkg/core/answer.go deleted file mode 100644 index 3dc739e..0000000 --- a/pkg/core/answer.go +++ /dev/null @@ -1,11 +0,0 @@ -package core - -import ( - telegramapi "github.com/go-telegram-bot-api/telegram-bot-api" -) - -type HandlerAnswer struct { - Type string `json:"type"` // one of message:message_config, message:sticker_config - MessageConfig *telegramapi.MessageConfig `json:"message_config"` - StickerConfig *telegramapi.StickerConfig `json:"sticker_config"` -} diff --git a/pkg/core/config.go b/pkg/core/config.go deleted file mode 100644 index dab465e..0000000 --- a/pkg/core/config.go +++ /dev/null @@ -1,24 +0,0 @@ -package core - -import ( - "github.com/go-redis/redis/v8" -) - -type BotRedisConfig struct { - Host string `yaml:"host" env:"DB_HOST"` - Username string `yaml:"user" env:"DB_USER"` - Password string `yaml:"password" env:"DB_PASSWORD"` -} - -type BotCoreConfig struct { - TelegramToken string `yaml:"auth_token" env:"BOT_AUTH_TOKEN"` - Redis BotRedisConfig `yaml:"redis"` -} - -func GetRedisClientByConfig(config BotRedisConfig) *redis.Client { - return redis.NewClient(&redis.Options{ - Addr: config.Host, - Username: config.Username, - Password: config.Password, - }) -} diff --git a/pkg/core/constant.go b/pkg/core/constant.go deleted file mode 100644 index bc7c397..0000000 --- a/pkg/core/constant.go +++ /dev/null @@ -1,6 +0,0 @@ -package core - -const ( - RedisUpdateChanel = "TelegramUpdates" - RedisAnswersChanel = "TelegramAnswers" -) diff --git a/pkg/handlers/config.go b/pkg/handlers/config.go deleted file mode 100644 index 4d55112..0000000 --- a/pkg/handlers/config.go +++ /dev/null @@ -1,9 +0,0 @@ -package handlers - -import ( - "github.com/bbralion/CTFloodBot/pkg/core" -) - -type HandlerConfig struct { - Redis core.BotRedisConfig `yaml:"redis"` -} diff --git a/pkg/handlers/simple.go b/pkg/handlers/simple.go deleted file mode 100644 index 66daae7..0000000 --- a/pkg/handlers/simple.go +++ /dev/null @@ -1,64 +0,0 @@ -package handlers - -import ( - "context" - "encoding/json" - - "github.com/bbralion/CTFloodBot/pkg/core" - telegramapi "github.com/go-telegram-bot-api/telegram-bot-api" - "go.uber.org/zap" -) - -type AnswerChan chan<- core.HandlerAnswer - -type SimpleHandler struct { - Handler func(logger *zap.Logger, update *telegramapi.Update, answerChan AnswerChan) - Logger *zap.Logger - Config HandlerConfig -} - -func (h *SimpleHandler) Run() { - ctx := context.Background() - - redisClient := core.GetRedisClientByConfig(h.Config.Redis) - - publish := func(message interface{}) { - command := redisClient.Publish(ctx, core.RedisAnswersChanel, message) - if command.Err() != nil { - h.Logger.Error("Failed to send answer", zap.Error(command.Err())) - } - } - - h.Logger.Info("Handler is ready to start") - - subscriber := redisClient.Subscribe(ctx, core.RedisUpdateChanel) - for message := range subscriber.Channel() { - var update telegramapi.Update - err := json.Unmarshal([]byte(message.Payload), &update) - if err != nil { - h.Logger.Fatal("Failed to unmarshal received update", zap.Error(err), zap.String("message", message.Payload)) - } - - go h.processUpdate(&update, publish) - } -} - -func (h *SimpleHandler) createAnswerChan(publish func(message interface{})) AnswerChan { - ch := make(chan core.HandlerAnswer) - go func() { - for v := range ch { - marshal, err := json.Marshal(v) - if err != nil { - h.Logger.Error("Failed to marshal answer", zap.Error(err)) - } - publish(marshal) - } - }() - return ch -} - -func (h *SimpleHandler) processUpdate(update *telegramapi.Update, publish func(message interface{})) { - ch := h.createAnswerChan(publish) - h.Handler(h.Logger, update, ch) - close(ch) -} diff --git a/pkg/proxy/client.go b/pkg/proxy/client.go index 8de4099..d66bd1b 100644 --- a/pkg/proxy/client.go +++ b/pkg/proxy/client.go @@ -4,61 +4,41 @@ import ( "context" "errors" "fmt" + "regexp" "time" "github.com/bbralion/CTFloodBot/internal/genproto" + "github.com/bbralion/CTFloodBot/internal/models" "github.com/bbralion/CTFloodBot/internal/services" "github.com/bbralion/CTFloodBot/pkg/auth" - "github.com/bbralion/CTFloodBot/pkg/models" "github.com/bbralion/CTFloodBot/pkg/retry" + "github.com/go-logr/logr" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" - "go.uber.org/zap" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" ) const defaultTimeout = time.Second * 5 -// Context provides Handler's with often needed elements -type Context interface { - context.Context - API() *tgbotapi.BotAPI - Logger() *zap.Logger -} - -type proxyCtx struct { - context.Context - api *tgbotapi.BotAPI - logger *zap.Logger -} - -func (ctx *proxyCtx) API() *tgbotapi.BotAPI { - return ctx.api -} - -func (ctx *proxyCtx) Logger() *zap.Logger { - return ctx.logger -} - // Handler is a proper handler of update requests received from the proxy type Handler interface { - Serve(ctx Context, update tgbotapi.Update) + Serve(api *tgbotapi.BotAPI, update tgbotapi.Update) } // HandlerFunc is a util type for using functions as Handler's -type HandlerFunc func(ctx Context, update tgbotapi.Update) +type HandlerFunc func(api *tgbotapi.BotAPI, update tgbotapi.Update) -func (f HandlerFunc) Serve(ctx Context, update tgbotapi.Update) { - f(ctx, update) +func (f HandlerFunc) Serve(api *tgbotapi.BotAPI, update tgbotapi.Update) { + f(api, update) } // Client is the proxy client implementation, it receives updates via gRPC and answers via HTTP type Client struct { - Logger *zap.Logger + Logger logr.Logger // Handler is the telegram update handler used Handler Handler // Matchers specify the matchers used to filter the requests which should be handled by this client - Matchers models.MatcherGroup + Matchers []string // Token is the auth token used to authorize this client Token string // GRPCEndpoint specifies the (currently insecure) gRPC endpoint to connect to @@ -67,20 +47,24 @@ type Client struct { // Run runs the client. It starts by connecting to the gRPC proxy, from which it receives the HTTP // proxy endpoint, as well as all of the following updates from telegram. -func (c *Client) Run(ctx context.Context) (err error) { - if c.Logger == nil || c.Handler == nil || len(c.Matchers) < 1 { +func (c *Client) Run(ctx context.Context) error { + if c.Handler == nil || len(c.Matchers) < 1 { return errors.New("logger, handler and matchers must be specified for client") } - c.Logger = c.Logger.Named("client") - logErr := func(msg string) { - c.Logger.Error(msg, zap.Error(err)) + c.Logger = c.Logger.WithName("client") + + matchers := make(models.MatcherGroup, len(c.Matchers)) + for i, m := range c.Matchers { + var err error + if matchers[i], err = regexp.Compile(m); err != nil { + return fmt.Errorf("invalid matcher specified: %w", err) + } } // Begin by setting up a proper grpc connection conn, err := c.connectGRPC() if err != nil { - logErr("failed to connect to specified gRPC endpoint") - return + return fmt.Errorf("connecting to gRPC server: %w", err) } defer conn.Close() gc := genproto.NewMultiplexerServiceClient(conn) @@ -88,28 +72,20 @@ func (c *Client) Run(ctx context.Context) (err error) { // And immediately test it out by requesting the HTTP endpoint cfg, err := c.getConfig(ctx, gc) if err != nil { - logErr("failed to get config") - return + return fmt.Errorf("getting config via gRPC: %w", err) } // Connect to the telegram bot proxy api, err := c.connectHTTP(cfg.GetProxyEndpoint()) if err != nil { - logErr("failed to connect to telegram http proxy") - return + return fmt.Errorf("connecting to telegram HTTP proxy: %w", err) } // Configure registrar and start listening for updates - registrar, err := services.NewGRPCRegistrar(c.Logger, gc) - if err != nil { - logErr("failed to setup registrar") - return - } - - updatech, errorch, err := registrar.Register(ctx, c.Matchers) + registrar := services.NewGRPCRegistrar(c.Logger, gc) + updateCh, err := registrar.Register(ctx, matchers) if err != nil { - logErr("failed to register client") - return + return fmt.Errorf("registering client: %w", err) } // Make sure to cancel all requests when we die @@ -117,14 +93,14 @@ func (c *Client) Run(ctx context.Context) (err error) { defer cancel() for { select { - case err = <-errorch: - logErr("critical error while receiving updates") - return - case update := <-updatech: - c.Handler.Serve(&proxyCtx{ctx, api, c.Logger}, update) + case update := <-updateCh: + if update.Error != nil { + return fmt.Errorf("receiving updates: %w", err) + } + c.Handler.Serve(api, update.Update) case <-ctx.Done(): c.Logger.Info("shutting down") - return + return nil } } } @@ -146,18 +122,18 @@ func (c *Client) connectGRPC() (*grpc.ClientConn, error) { } func (c *Client) getConfig(ctx context.Context, gc genproto.MultiplexerServiceClient) (*genproto.Config, error) { - config, err := retry.Backoff(func() (error, *genproto.Config) { + config, err := retry.Backoff(func() (*genproto.Config, error) { ctx, cancel := context.WithTimeout(ctx, defaultTimeout) defer cancel() resp, err := gc.GetConfig(ctx, &genproto.ConfigRequest{}) if err == nil { - return nil, resp.GetConfig() + return resp.GetConfig(), nil } else if retry.IsGRPCUnavailable(err) { - c.Logger.Info("retrying connection to gRPC server", zap.Error(err)) - return retry.Recoverable(err), nil + c.Logger.Error(err, "retrying connection to gRPC server") + return nil, retry.Recoverable() } - return retry.Unrecoverable(err), nil + return nil, retry.Unrecoverable(err) }) if err != nil { return nil, fmt.Errorf("request to gRPC server failed: %w", err) diff --git a/pkg/retry/retry.go b/pkg/retry/retry.go index 72ed146..be6ab81 100644 --- a/pkg/retry/retry.go +++ b/pkg/retry/retry.go @@ -15,37 +15,35 @@ type ( ) type recoverError struct { - wrapped error - recoverable bool + wrapped error } func (e recoverError) Error() string { - s := "recoverable error: %s" - if !e.recoverable { - s = "un" + s + if e.wrapped == nil { + return "temporary recoverable error" } - return fmt.Sprintf(s, e.wrapped.Error()) + return fmt.Sprintf("unrecoverable error: %s", e.wrapped.Error()) } func (e recoverError) Unwrap() error { return e.wrapped } -// Recoverable is used to explicitly mark an error as recoverable -func Recoverable(err error) error { - return recoverError{err, true} +// Recoverable is used to explicitly mark a recovery +func Recoverable() error { + return recoverError{nil} } // Unrecoverable wraps an error to indicate that it is not recoverable from, // after which retries will be stopped and it will be returned func Unrecoverable(err error) error { - return recoverError{err, false} + return recoverError{err} } // Recover runs the function using a custom delay scheduler -func Recover[T any](f func() (error, T), s DelayScheduler, et ...ErrTransformer) (T, error) { +func Recover[T any](f func() (T, error), s DelayScheduler, et ...ErrTransformer) (T, error) { for { - err, ret := f() + ret, err := f() for _, t := range et { err = t(err) } @@ -53,8 +51,8 @@ func Recover[T any](f func() (error, T), s DelayScheduler, et ...ErrTransformer) var re recoverError if err == nil { return ret, nil - } else if errors.As(err, &re) && !re.recoverable { - return ret, re.Unwrap() + } else if errors.As(err, &re) && re.wrapped != nil { + return ret, re.wrapped } time.Sleep(s()) @@ -68,7 +66,7 @@ const ( ) // Backoff runs the function using the backoff retry algorithm -func Backoff[T any](f func() (error, T), et ...ErrTransformer) (T, error) { +func Backoff[T any](f func() (T, error), et ...ErrTransformer) (T, error) { delay, next := time.Duration(0), DefaultBackoffMinDelay return Recover(f, func() time.Duration { delay, next = next, next*DefaultBackoffFactor @@ -82,7 +80,7 @@ func Backoff[T any](f func() (error, T), et ...ErrTransformer) (T, error) { const DefaultStaticDelay = time.Second // Static runs the function using a static retry delay -func Static[T any](f func() (error, T), et ...ErrTransformer) (T, error) { +func Static[T any](f func() (T, error), et ...ErrTransformer) (T, error) { return Recover(f, func() time.Duration { return DefaultStaticDelay }, et...) diff --git a/pkg/retry/retry_test.go b/pkg/retry/retry_test.go index 1afe262..509a04d 100644 --- a/pkg/retry/retry_test.go +++ b/pkg/retry/retry_test.go @@ -7,19 +7,19 @@ import ( "github.com/stretchr/testify/require" ) -func assertNumCallsFunc(req *require.Assertions, n int, tmpErr, finalErr error) func() (error, any) { +func assertNumCallsFunc(req *require.Assertions, n int, tmpErr, finalErr error) func() (any, error) { ctr := 0 - return func() (error, any) { + return func() (any, error) { req.Less(ctr, n, "should be called %d times at most", n) ctr++ if ctr == n { - return finalErr, nil + return nil, finalErr } - return tmpErr, nil + return nil, tmpErr } } -func testStrategy(req *require.Assertions, n int, strategy func(func() (error, any), ...ErrTransformer) (any, error)) { +func testStrategy(req *require.Assertions, n int, strategy func(func() (any, error), ...ErrTransformer) (any, error)) { _, err := strategy(assertNumCallsFunc(req, n, errors.New("fake recoverable error"), nil)) req.NoError(err) e := errors.New("fake unrecoverable error") diff --git a/pkg/services/authenticator.go b/pkg/services/authenticator.go new file mode 100644 index 0000000..8204504 --- /dev/null +++ b/pkg/services/authenticator.go @@ -0,0 +1,36 @@ +package services + +import "errors" + +// Client is an identification of a single client of a service +type Client struct { + Name string +} + +var ErrInvalidToken = errors.New("invalid authentication token provided") + +// Authenticator represents a token-based authentication provider +type Authenticator interface { + Authenticate(token string) (Client, error) +} + +type staticAuthenticator struct { + clients map[string]Client +} + +func (p *staticAuthenticator) Authenticate(token string) (Client, error) { + if c, ok := p.clients[token]; ok { + return c, nil + } + return Client{}, ErrInvalidToken +} + +// NewStaticAuthenticator returns an authenticator which authenticates clients +// using a static token->Client map specified at creation time +func NewStaticAuthenticator(clients map[string]Client) Authenticator { + a := &staticAuthenticator{make(map[string]Client, len(clients))} + for k, v := range clients { + a.clients[k] = v + } + return a +} diff --git a/pkg/services/authenticator_test.go b/pkg/services/authenticator_test.go new file mode 100644 index 0000000..9d1a705 --- /dev/null +++ b/pkg/services/authenticator_test.go @@ -0,0 +1,64 @@ +package services + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func Test_staticAuthenticator_Authenticate(t *testing.T) { + type args struct { + token string + } + tests := []struct { + name string + clients map[string]Client + args args + want Client + wantErr bool + }{ + { + name: "nil client map", + clients: nil, + args: args{"faketoken"}, + wantErr: true, + }, + { + name: "empty client map", + clients: map[string]Client{}, + args: args{"faketoken"}, + wantErr: true, + }, + { + name: "valid token", + clients: map[string]Client{ + "goodtoken": {Name: "client1"}, + }, + args: args{"goodtoken"}, + want: Client{Name: "client1"}, + wantErr: false, + }, + { + name: "invalid token", + clients: map[string]Client{ + "goodtoken": {Name: "client1"}, + }, + args: args{"badtoken"}, + wantErr: true, + }, + } + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + req := require.New(t) + + p := NewStaticAuthenticator(tt.clients) + got, err := p.Authenticate(tt.args.token) + if tt.wantErr { + req.Error(err) + } + req.Equal(tt.want, got) + }) + } +} diff --git a/pkg/services/authprovider.go b/pkg/services/authprovider.go deleted file mode 100644 index a086f91..0000000 --- a/pkg/services/authprovider.go +++ /dev/null @@ -1,32 +0,0 @@ -package services - -import "errors" - -// Client is an identification of a single client of a service -type Client struct { - Name string -} - -var ErrInvalidToken = errors.New("invalid authentication token provided") - -// AuthProvider represents a token-based authentication provider -type AuthProvider interface { - Authenticate(token string) (*Client, error) -} - -type staticAuthProvider struct { - clients map[string]*Client -} - -func (p *staticAuthProvider) Authenticate(token string) (*Client, error) { - if c, ok := p.clients[token]; ok { - return c, nil - } - return nil, ErrInvalidToken -} - -// NewStaticAuthProvider returns an auth provider which authenticates clients -// using a static token->Client map specified at creation time -func NewStaticAuthProvider(clients map[string]*Client) AuthProvider { - return &staticAuthProvider{clients} -} diff --git a/pkg/services/updateprovider.go b/pkg/services/updateprovider.go index 073b3e5..a0909e2 100644 --- a/pkg/services/updateprovider.go +++ b/pkg/services/updateprovider.go @@ -1,35 +1,38 @@ package services import ( - "fmt" + "context" - "github.com/bbralion/CTFloodBot/pkg/models" + "github.com/go-logr/logr" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" ) // UpdateProvider represents a telegram update provider type UpdateProvider interface { - Updates() (models.UpdateChan, error) + Updates(ctx context.Context) tgbotapi.UpdatesChannel } const DefaultLongPollTimeoutS = 60 type pollingUpdateProvider struct { - api *tgbotapi.BotAPI + logger logr.Logger + api *tgbotapi.BotAPI } -func (p *pollingUpdateProvider) Updates() (models.UpdateChan, error) { - updates, err := p.api.GetUpdatesChan(tgbotapi.UpdateConfig{ +func (p *pollingUpdateProvider) Updates(ctx context.Context) tgbotapi.UpdatesChannel { + updates, _ := p.api.GetUpdatesChan(tgbotapi.UpdateConfig{ Offset: 0, Limit: p.api.Buffer, Timeout: DefaultLongPollTimeoutS, }) - if err != nil { - return nil, fmt.Errorf("failed to get tgbotapi update channel: %w", err) - } - return models.UpdateChan(updates), nil + + go func() { + <-ctx.Done() + p.api.StopReceivingUpdates() + }() + return updates } -func NewPollingUpdateProvider(api *tgbotapi.BotAPI) UpdateProvider { - return &pollingUpdateProvider{api} +func NewPollingUpdateProvider(logger logr.Logger, api *tgbotapi.BotAPI) UpdateProvider { + return &pollingUpdateProvider{logger, api} } diff --git a/pkg/services/updateprovider_test.go b/pkg/services/updateprovider_test.go new file mode 100644 index 0000000..03163f2 --- /dev/null +++ b/pkg/services/updateprovider_test.go @@ -0,0 +1,63 @@ +package services + +import ( + "bytes" + "context" + "io" + "net/http" + "testing" + "time" + + "github.com/bbralion/CTFloodBot/internal/mocks" + "github.com/go-logr/logr" + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/require" + "go.uber.org/goleak" +) + +func TestPollingUpdateProvider(t *testing.T) { + ctrl, req := gomock.NewController(t), require.New(t) + + mockClient := mocks.NewMockTGBotAPIHTTPClient(ctrl) + mockClient.EXPECT().Do(gomock.Any()).Return(&http.Response{Body: io.NopCloser(bytes.NewBuffer([]byte(`{"ok":true,"result":{}}`)))}, nil) + api, err := tgbotapi.NewBotAPIWithClient("fake-token", "http://endpoint/fake%s/%s", mockClient) + req.NoError(err) + + up := NewPollingUpdateProvider(logr.Discard(), api) + ctx, cancel := context.WithCancel(context.Background()) + + mockClient.EXPECT().Do(gomock.Any()).DoAndReturn(func(r *http.Request) (*http.Response, error) { + cancel() + return &http.Response{Body: io.NopCloser(bytes.NewBuffer([]byte(`{"ok":true,"result":[{"update_id":1}]}`)))}, nil + }).AnyTimes() + updates := up.Updates(ctx) + <-ctx.Done() + + // Update received + req.Eventually(func() bool { + select { + case update, ok := <-updates: + req.True(ok, "one update must be received") + req.Equal(1, update.UpdateID) + return true + default: + } + return false + }, time.Second, time.Millisecond*50) + + // Channel closed + req.Eventually(func() bool { + select { + case _, ok := <-updates: + req.False(ok, "channel must be closed") + return true + default: + } + return false + }, time.Second, time.Millisecond*50) +} + +func TestMain(m *testing.M) { + goleak.VerifyTestMain(m) +} diff --git a/pkg/utils/logger.go b/pkg/utils/logger.go deleted file mode 100644 index 3f21554..0000000 --- a/pkg/utils/logger.go +++ /dev/null @@ -1,25 +0,0 @@ -package utils - -import ( - "go.uber.org/zap" - "go.uber.org/zap/zapcore" -) - -func GetLogger() *zap.Logger { - loggerConfig := zap.Config{ - Level: zap.NewAtomicLevelAt(zap.DebugLevel), - Encoding: "json", - OutputPaths: []string{"stdout"}, - ErrorOutputPaths: []string{"stderr"}, - EncoderConfig: zapcore.EncoderConfig{ - MessageKey: "message", - LevelKey: "level", - EncodeLevel: zapcore.LowercaseLevelEncoder, - }, - } - logger, err := loggerConfig.Build() - if err != nil { - panic(err) - } - return logger -} diff --git a/pkg/utils/telegram.go b/pkg/utils/telegram.go deleted file mode 100644 index 16fed2d..0000000 --- a/pkg/utils/telegram.go +++ /dev/null @@ -1,28 +0,0 @@ -package utils - -import ( - "strings" - - telegramapi "github.com/go-telegram-bot-api/telegram-bot-api" -) - -func addIfNotNil(slice []string, name string, objectIsNil bool) []string { - if !objectIsNil { - return append(slice, name) - } - return slice -} - -func GetTelegramUpdateType(update *telegramapi.Update) string { - var contents []string - contents = addIfNotNil(contents, "message", update.Message == nil) - contents = addIfNotNil(contents, "edited_message", update.EditedMessage == nil) - contents = addIfNotNil(contents, "channel_post", update.ChannelPost == nil) - contents = addIfNotNil(contents, "edited_channel_post", update.EditedChannelPost == nil) - contents = addIfNotNil(contents, "inline_query", update.InlineQuery == nil) - contents = addIfNotNil(contents, "chosen_inline_result", update.ChosenInlineResult == nil) - contents = addIfNotNil(contents, "callback_query", update.CallbackQuery == nil) - contents = addIfNotNil(contents, "shipping_query", update.ShippingQuery == nil) - contents = addIfNotNil(contents, "pre_checkout_query", update.PreCheckoutQuery == nil) - return strings.Join(contents, ",") -} From 341f9235aef87fe9a905d17dfd1133cdf9051241 Mon Sep 17 00:00:00 2001 From: renbou Date: Mon, 1 Aug 2022 15:46:35 +0300 Subject: [PATCH 14/17] feat: custom long poll streamer --- go.mod | 3 + go.sum | 8 + pkg/proxy/client.go | 145 ++++++----------- pkg/services/longpoll.go | 159 +++++++++++++++++++ {internal => pkg}/services/registrar.go | 5 +- {internal => pkg}/services/registrar_test.go | 2 +- pkg/services/streams.go | 130 +++++++++++++++ pkg/services/streams_test.go | 73 +++++++++ pkg/services/updateprovider.go | 38 ----- pkg/services/updateprovider_test.go | 63 -------- test.go | 25 +++ 11 files changed, 448 insertions(+), 203 deletions(-) create mode 100644 pkg/services/longpoll.go rename {internal => pkg}/services/registrar.go (95%) rename {internal => pkg}/services/registrar_test.go (98%) create mode 100644 pkg/services/streams.go create mode 100644 pkg/services/streams_test.go delete mode 100644 pkg/services/updateprovider.go delete mode 100644 pkg/services/updateprovider_test.go create mode 100644 test.go diff --git a/go.mod b/go.mod index 90e7cb8..4df0150 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/go-logr/logr v1.2.3 github.com/go-telegram-bot-api/telegram-bot-api v1.0.1-0.20201107014523-54104a08f947 github.com/golang/mock v1.6.0 + github.com/json-iterator/go v1.1.12 github.com/justinas/alice v1.2.0 github.com/stretchr/testify v1.8.0 go.uber.org/goleak v1.1.12 @@ -16,6 +17,8 @@ require ( require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/golang/protobuf v1.5.2 // indirect + github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/technoweenie/multipartstreamer v1.0.1 // indirect golang.org/x/net v0.0.0-20220728181054-f92ba40d432d // indirect diff --git a/go.sum b/go.sum index 6c27ec6..6da6c69 100644 --- a/go.sum +++ b/go.sum @@ -51,8 +51,11 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/justinas/alice v1.2.0 h1:+MHSA/vccVCF4Uq37S42jwlkvI2Xzl7zTPCN5BnZNVo= github.com/justinas/alice v1.2.0/go.mod h1:fN5HRH/reO/zrUflLfTN43t3vXvKzvZIENsNEe7i7qA= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= @@ -60,12 +63,17 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= diff --git a/pkg/proxy/client.go b/pkg/proxy/client.go index d66bd1b..f5b1e41 100644 --- a/pkg/proxy/client.go +++ b/pkg/proxy/client.go @@ -7,51 +7,27 @@ import ( "regexp" "time" - "github.com/bbralion/CTFloodBot/internal/genproto" "github.com/bbralion/CTFloodBot/internal/models" - "github.com/bbralion/CTFloodBot/internal/services" - "github.com/bbralion/CTFloodBot/pkg/auth" - "github.com/bbralion/CTFloodBot/pkg/retry" - "github.com/go-logr/logr" + "github.com/bbralion/CTFloodBot/pkg/services" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" - "google.golang.org/grpc" - "google.golang.org/grpc/credentials/insecure" ) const defaultTimeout = time.Second * 5 -// Handler is a proper handler of update requests received from the proxy -type Handler interface { - Serve(api *tgbotapi.BotAPI, update tgbotapi.Update) -} - -// HandlerFunc is a util type for using functions as Handler's -type HandlerFunc func(api *tgbotapi.BotAPI, update tgbotapi.Update) - -func (f HandlerFunc) Serve(api *tgbotapi.BotAPI, update tgbotapi.Update) { - f(api, update) -} - -// Client is the proxy client implementation, it receives updates via gRPC and answers via HTTP +// Client is the proxy client implementation type Client struct { - Logger logr.Logger - // Handler is the telegram update handler used + // Handler is the telegram update handler to use Handler Handler // Matchers specify the matchers used to filter the requests which should be handled by this client Matchers []string - // Token is the auth token used to authorize this client - Token string - // GRPCEndpoint specifies the (currently insecure) gRPC endpoint to connect to - GRPCEndpoint string } -// Run runs the client. It starts by connecting to the gRPC proxy, from which it receives the HTTP -// proxy endpoint, as well as all of the following updates from telegram. -func (c *Client) Run(ctx context.Context) error { +// RegisterAndRun registers the client using the given registrar, +// then handles the received updates by responding to them using the api +func (c *Client) RegisterAndRun(ctx context.Context, registrar services.Registrar, api *tgbotapi.BotAPI) error { if c.Handler == nil || len(c.Matchers) < 1 { - return errors.New("logger, handler and matchers must be specified for client") + return errors.New("handler and matchers must be specified for client") } - c.Logger = c.Logger.WithName("client") matchers := make(models.MatcherGroup, len(c.Matchers)) for i, m := range c.Matchers { @@ -61,36 +37,11 @@ func (c *Client) Run(ctx context.Context) error { } } - // Begin by setting up a proper grpc connection - conn, err := c.connectGRPC() - if err != nil { - return fmt.Errorf("connecting to gRPC server: %w", err) - } - defer conn.Close() - gc := genproto.NewMultiplexerServiceClient(conn) - - // And immediately test it out by requesting the HTTP endpoint - cfg, err := c.getConfig(ctx, gc) - if err != nil { - return fmt.Errorf("getting config via gRPC: %w", err) - } - - // Connect to the telegram bot proxy - api, err := c.connectHTTP(cfg.GetProxyEndpoint()) - if err != nil { - return fmt.Errorf("connecting to telegram HTTP proxy: %w", err) - } - - // Configure registrar and start listening for updates - registrar := services.NewGRPCRegistrar(c.Logger, gc) updateCh, err := registrar.Register(ctx, matchers) if err != nil { return fmt.Errorf("registering client: %w", err) } - // Make sure to cancel all requests when we die - ctx, cancel := context.WithCancel(ctx) - defer cancel() for { select { case update := <-updateCh: @@ -99,52 +50,46 @@ func (c *Client) Run(ctx context.Context) error { } c.Handler.Serve(api, update.Update) case <-ctx.Done(): - c.Logger.Info("shutting down") return nil } } } -func (c *Client) connectGRPC() (*grpc.ClientConn, error) { - if c.GRPCEndpoint == "" || c.Token == "" { - return nil, errors.New("endpoint and token must not be empty") - } - - interceptor := auth.NewGRPCClientInterceptor(c.Token) - conn, err := grpc.Dial(c.GRPCEndpoint, - grpc.WithTransportCredentials(insecure.NewCredentials()), - grpc.WithUnaryInterceptor(interceptor.Unary()), - grpc.WithStreamInterceptor(interceptor.Stream())) - if err != nil { - return nil, fmt.Errorf("failed to dial proxy gRPC endpoint: %w", err) - } - return conn, nil -} - -func (c *Client) getConfig(ctx context.Context, gc genproto.MultiplexerServiceClient) (*genproto.Config, error) { - config, err := retry.Backoff(func() (*genproto.Config, error) { - ctx, cancel := context.WithTimeout(ctx, defaultTimeout) - defer cancel() - - resp, err := gc.GetConfig(ctx, &genproto.ConfigRequest{}) - if err == nil { - return resp.GetConfig(), nil - } else if retry.IsGRPCUnavailable(err) { - c.Logger.Error(err, "retrying connection to gRPC server") - return nil, retry.Recoverable() - } - return nil, retry.Unrecoverable(err) - }) - if err != nil { - return nil, fmt.Errorf("request to gRPC server failed: %w", err) - } - return config, nil -} - -func (c *Client) connectHTTP(endpoint string) (*tgbotapi.BotAPI, error) { - api, err := tgbotapi.NewBotAPIWithAPIEndpoint(c.Token, endpoint) - if err != nil { - return nil, fmt.Errorf("failed to setup telegram bot api: %w", err) - } - return api, nil -} +// func (c *Client) connectGRPC() (*grpc.ClientConn, error) { +// if c.GRPCEndpoint == "" || c.Token == "" { +// return nil, errors.New("endpoint and token must not be empty") +// } + +// interceptor := auth.NewGRPCClientInterceptor(c.Token) +// conn, err := grpc.Dial(c.GRPCEndpoint, +// grpc.WithTransportCredentials(insecure.NewCredentials()), +// grpc.WithUnaryInterceptor(interceptor.Unary()), +// grpc.WithStreamInterceptor(interceptor.Stream())) +// if err != nil { +// return nil, fmt.Errorf("failed to dial proxy gRPC endpoint: %w", err) +// } +// return conn, nil +// } + +// func (c *Client) getConfig(ctx context.Context, gc genproto.MultiplexerServiceClient) (*genproto.Config, error) { +// ctx, cancel := context.WithTimeout(ctx, defaultTimeout) +// defer cancel() + +// config, err := gc.GetConfig(ctx, &genproto.ConfigRequest{}) +// if err != nil { +// return nil, fmt.Errorf("requesting config from gRPC server: %w", err) +// } +// return config.Config, nil +// } + +// func (c *Client) connectHTTP(endpoint string) (*tgbotapi.BotAPI, error) { +// api, err := tgbotapi.NewBotAPIWithClient(c.Token, endpoint, &http.Client{ +// Transport: &http.Transport{ +// ResponseHeaderTimeout: defaultTimeout, +// }, +// }) +// if err != nil { +// return nil, fmt.Errorf("failed to setup telegram bot api: %w", err) +// } +// return api, nil +// } diff --git a/pkg/services/longpoll.go b/pkg/services/longpoll.go new file mode 100644 index 0000000..7a12e7d --- /dev/null +++ b/pkg/services/longpoll.go @@ -0,0 +1,159 @@ +package services + +import ( + "context" + "errors" + "fmt" + "io" + "net/http" + "net/url" + "path" + "strconv" + "time" + + jsoniter "github.com/json-iterator/go" +) + +// DefaultLongPollTimeout is the default timeout used for long polling +const DefaultLongPollTimeout = time.Second * 60 + +// LongPollOptions specifies various options to use inside the long poll streamer. +// Offset, Limit and Timeout specify the values to send to Telegram's getUpdates method. +// By default a Timeout of DefaultLongPollTimeout is used. +type LongPollOptions struct { + Offset int + Limit int + Timeout time.Duration + // Telegram Bot API endpoint + Endpoint string + // Telegram Bot API token + Token string + // The HTTP client to use for requests + Client *http.Client +} + +type longPollStreamer struct { + opts LongPollOptions + endpointURL *url.URL + params url.Values + iterator *jsoniter.Iterator +} + +func (s *longPollStreamer) poll(ctx context.Context) (*http.Response, error) { + s.endpointURL.RawQuery = s.params.Encode() + ctx, cancel := context.WithTimeout(ctx, s.opts.Timeout) + defer cancel() + + req, err := http.NewRequestWithContext(ctx, "GET", s.endpointURL.String(), nil) + if err != nil { + return nil, fmt.Errorf("preparing request: %w", err) + } + + resp, err := s.opts.Client.Do(req) + if err != nil { + // Unwrap url.Error returned from do to avoid leaking url with bot token + return nil, fmt.Errorf("doing poll request: %w", errors.Unwrap(err)) + } + if resp.StatusCode != http.StatusOK { + return nil, fmt.Errorf("bad response code while polling: %s", resp.Status) + } + return resp, nil +} + +func (s *longPollStreamer) parseUpdates(stream chan Maybe[RawUpdate], body io.ReadCloser) error { + s.iterator.Reset(body) + defer body.Close() + defer s.iterator.Reset(nil) + + // Parse API response wrapper + var resp struct { + Ok bool + Description string + Result []jsoniter.RawMessage + } + if s.iterator.ReadVal(&resp); s.iterator.Error != nil { + return fmt.Errorf("parsing getUpdates response: %w", s.iterator.Error) + } + if !resp.Ok { + return fmt.Errorf("getUpdates response.Ok is false: %s", resp.Description) + } + + for _, u := range resp.Result { + stream <- Maybe[RawUpdate]{Value: RawUpdate(u)} + } + if len(resp.Result) > 0 { + val := jsoniter.Get(resp.Result[len(resp.Result)-1], "update_id") + updateID := val.ToInt() + if err := val.LastError(); err != nil { + return fmt.Errorf("retrieving update_id: %w", err) + } + s.params.Set("offset", strconv.Itoa(updateID+1)) + } + return nil +} + +func (s *longPollStreamer) Stream(ctx context.Context) RawStream { + stream := make(chan Maybe[RawUpdate]) + go func() { + defer close(stream) + + for { + resp, err := s.poll(ctx) + if err != nil { + // If the context is finished, then the error we received is *probably* a timeout/canceled + select { + case <-ctx.Done(): + return + default: + } + + // Global context isn't finished, which means that this is a temporary timeout + if errors.Is(err, context.DeadlineExceeded) { + stream <- Maybe[RawUpdate]{Error: fmt.Errorf("temporary timeout while polling: %w", err)} + continue + } + stream <- Maybe[RawUpdate]{Error: err} + return + } + + if err := s.parseUpdates(stream, resp.Body); err != nil { + stream <- Maybe[RawUpdate]{Error: err} + return + } + } + }() + return stream +} + +// NewLongPollStreamer starts a long polling streamer on the given endpoint in the form +// "https://api.telegram.org" using the specified token for authorization. +// Long poll requests will be made to "{endpoint}/bot{token}/getUpdates". +func NewLongPollStreamer(endpoint, token string, opts LongPollOptions) (RawStreamer, error) { + endpointURL, err := url.Parse(endpoint) + if err != nil { + return nil, fmt.Errorf("invalid long poll endpoint: %w", err) + } + + // Set proper defaults in options and client + if opts.Timeout == 0 { + opts.Timeout = DefaultLongPollTimeout + } + if opts.Client == nil { + opts.Client = http.DefaultClient + } + + // Timeout is always set to avoid short polling, other opts are used only if set + endpointURL.Path = path.Join(endpointURL.Path, "bot"+token, "getUpdates") + params := make(url.Values) + params.Set("timeout", strconv.Itoa(int(opts.Timeout.Seconds()))) + if opts.Offset != 0 { + params.Set("offset", strconv.Itoa(opts.Offset)) + } + if opts.Limit != 0 { + params.Set("limit", strconv.Itoa(opts.Limit)) + } + return &longPollStreamer{ + opts, endpointURL, params, + jsoniter.ParseBytes(jsoniter.ConfigFastest, make([]byte, DefaultDecodeBufferSize)), + }, nil +} diff --git a/internal/services/registrar.go b/pkg/services/registrar.go similarity index 95% rename from internal/services/registrar.go rename to pkg/services/registrar.go index 73a0125..56c3466 100644 --- a/internal/services/registrar.go +++ b/pkg/services/registrar.go @@ -49,7 +49,7 @@ func (r *gRPCRegistrar) tryRegister(ctx context.Context, request *genproto.Regis } var update tgbotapi.Update - if err := json.Unmarshal([]byte(updatePB.GetJson()), &update); err != nil { + if err := json.Unmarshal([]byte(updatePB.Json), &update); err != nil { return fmt.Errorf("unmarshaling update json: %w", err) } @@ -96,5 +96,8 @@ func (r *gRPCRegistrar) Register(ctx context.Context, matchers models.MatcherGro // NewGRPCRegistrar creates a Registrar based on the gRPC API client with preconfigured retries func NewGRPCRegistrar(logger logr.Logger, client genproto.MultiplexerServiceClient) Registrar { + if logger == (logr.Logger{}) { + logger = logr.Discard() + } return &gRPCRegistrar{logger.WithName("registrar"), client} } diff --git a/internal/services/registrar_test.go b/pkg/services/registrar_test.go similarity index 98% rename from internal/services/registrar_test.go rename to pkg/services/registrar_test.go index 9024392..741b384 100644 --- a/internal/services/registrar_test.go +++ b/pkg/services/registrar_test.go @@ -118,7 +118,7 @@ func Test_gRPCRegistrar_Register(t *testing.T) { defer ctrl.Finish() mockMuxClient := mocks.NewMockMultiplexerServiceClient(ctrl) - r := NewGRPCRegistrar(logr.Discard(), mockMuxClient) + r := NewGRPCRegistrar(logr.Logger{}, mockMuxClient) reqMatchers := make([]string, len(tt.args.matchers)) for i, m := range tt.args.matchers { diff --git a/pkg/services/streams.go b/pkg/services/streams.go new file mode 100644 index 0000000..64ffb19 --- /dev/null +++ b/pkg/services/streams.go @@ -0,0 +1,130 @@ +package services + +import ( + "context" + "runtime" + "sync" + + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" + jsoniter "github.com/json-iterator/go" +) + +// DefaultCapacity is the default capacity which should be passed into Map* and As* functions. +// 100 is selected because Telegram's long polling API limits number of updates received to 100. +const DefaultCapacity = 100 + +// DefaultDecodeBufferSize specifies the default buffer size used for decoding responses +// in various streamers. Can be changed to, well, change the default buffer size. +var DefaultDecodeBufferSize = 256 << 10 + +// Maybe defines a type that either contains a value or an error. +type Maybe[T any] struct { + Value T + Error error +} + +// Stream is a readonly channel of some type. +type Stream[T any] <-chan T + +// Drain drains the stream completely. +func (s Stream[T]) Drain() { + for { + _, ok := <-s + if !ok { + return + } + } +} + +// TODO: place inside MappedStream once type decs inside generic functions are supported +type mapJob[T, K any] struct { + in Maybe[T] + out chan Maybe[K] +} + +// MappedStream maps a stream to a channel in parallel using the given mapper. +// runtime.NumCPU() goroutines are used for mapping, and the returned channel +// will have the capacity specified in arguments. +// +// Make sure to call stream.Drain if you suddenly stop reading from the returned stream. +func MappedStream[T, K any](in Stream[Maybe[T]], mapper func(T) (K, error), capacity int, +) Stream[Maybe[K]] { + n := runtime.NumCPU() + + // These channels are closed by the parallelizing goroutine + outOrder := make(chan Stream[Maybe[K]], n) + jobs := make(chan mapJob[T, K], n) + + // Pool of channels, should be ok to use here since the goroutines here should be longlived + chpool := sync.Pool{ + New: func() any { + return make(chan Maybe[K]) + }, + } + // Parallelize input + go func() { + defer close(outOrder) + defer close(jobs) + + for { + v, ok := <-in + if !ok { + return + } + + ch := chpool.Get().(chan Maybe[K]) + jobs <- mapJob[T, K]{in: v, out: ch} + outOrder <- ch + } + }() + + mapF := func() { + for { + job, ok := <-jobs + if !ok { + return + } + + result := Maybe[K]{ + Error: job.in.Error, + } + if result.Error == nil { + result.Value, result.Error = mapper(job.in.Value) + } + job.out <- result + } + } + + out := make(chan Maybe[K], capacity) + // Synchronize output + go func() { + defer close(out) + for in := range outOrder { + out <- <-in + } + }() + + for i := 0; i < n; i++ { + go mapF() + } + return out +} + +// RawUpdate is either the raw JSON representation of an Update message. +type RawUpdate jsoniter.RawMessage + +// RawStream is a stream of raw updates. +type RawStream Stream[Maybe[RawUpdate]] + +// AsTgBotAPI converts a RawStream into a stream of tgbotapi-style updates. +func (s RawStream) AsTgBotAPI(capacity int) Stream[Maybe[tgbotapi.Update]] { + return MappedStream(Stream[Maybe[RawUpdate]](s), func(u RawUpdate) (tu tgbotapi.Update, err error) { + err = jsoniter.Unmarshal([]byte(u), &tu) + return + }, capacity) +} + +// RawStreamer is a provider of RawUpdate's updates via an unbuffered stream. +type RawStreamer interface { + Stream(ctx context.Context) RawStream +} diff --git a/pkg/services/streams_test.go b/pkg/services/streams_test.go new file mode 100644 index 0000000..2114003 --- /dev/null +++ b/pkg/services/streams_test.go @@ -0,0 +1,73 @@ +package services + +import ( + "errors" + "testing" + "time" + + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" + "github.com/stretchr/testify/require" +) + +func streamIs[T any](req *require.Assertions, s Stream[T], want []T) { + got := 0 + req.Eventually(func() bool { + select { + case val, ok := <-s: + if !ok { + req.Equal(len(want), got, "less values on stream than wanted") + return true + } + req.Less(got, len(want), "more updates on stream than wanted") + req.Equal(want[got], val) + got++ + default: + } + return false + }, time.Hour*5, time.Millisecond*50) +} + +func TestRawStream_AsTgBotApi(t *testing.T) { + tests := []struct { + name string + updates []Maybe[RawUpdate] + want []Maybe[tgbotapi.Update] + }{ + { + name: "no values", + updates: nil, + want: nil, + }, + { + name: "values and error", + updates: []Maybe[RawUpdate]{ + {Value: RawUpdate(`{"update_id":1,"inline_query":{"query":"inline-query-test"}}`)}, + {Value: RawUpdate(`{"update_id":2,"message":{"text":"message-test"}}`)}, + {Error: errors.New("connection error")}, + }, + want: []Maybe[tgbotapi.Update]{ + {Value: tgbotapi.Update{UpdateID: 1, InlineQuery: &tgbotapi.InlineQuery{Query: "inline-query-test"}}}, + {Value: tgbotapi.Update{UpdateID: 2, Message: &tgbotapi.Message{Text: "message-test"}}}, + {Error: errors.New("connection error")}, + }, + }, + } + + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + req := require.New(t) + + stream := make(chan Maybe[RawUpdate]) + go func() { + defer close(stream) + for _, u := range tt.updates { + stream <- u + } + }() + + streamIs(req, RawStream(stream).AsTgBotAPI(DefaultCapacity), tt.want) + }) + } +} diff --git a/pkg/services/updateprovider.go b/pkg/services/updateprovider.go deleted file mode 100644 index a0909e2..0000000 --- a/pkg/services/updateprovider.go +++ /dev/null @@ -1,38 +0,0 @@ -package services - -import ( - "context" - - "github.com/go-logr/logr" - tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" -) - -// UpdateProvider represents a telegram update provider -type UpdateProvider interface { - Updates(ctx context.Context) tgbotapi.UpdatesChannel -} - -const DefaultLongPollTimeoutS = 60 - -type pollingUpdateProvider struct { - logger logr.Logger - api *tgbotapi.BotAPI -} - -func (p *pollingUpdateProvider) Updates(ctx context.Context) tgbotapi.UpdatesChannel { - updates, _ := p.api.GetUpdatesChan(tgbotapi.UpdateConfig{ - Offset: 0, - Limit: p.api.Buffer, - Timeout: DefaultLongPollTimeoutS, - }) - - go func() { - <-ctx.Done() - p.api.StopReceivingUpdates() - }() - return updates -} - -func NewPollingUpdateProvider(logger logr.Logger, api *tgbotapi.BotAPI) UpdateProvider { - return &pollingUpdateProvider{logger, api} -} diff --git a/pkg/services/updateprovider_test.go b/pkg/services/updateprovider_test.go deleted file mode 100644 index 03163f2..0000000 --- a/pkg/services/updateprovider_test.go +++ /dev/null @@ -1,63 +0,0 @@ -package services - -import ( - "bytes" - "context" - "io" - "net/http" - "testing" - "time" - - "github.com/bbralion/CTFloodBot/internal/mocks" - "github.com/go-logr/logr" - tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" - "github.com/golang/mock/gomock" - "github.com/stretchr/testify/require" - "go.uber.org/goleak" -) - -func TestPollingUpdateProvider(t *testing.T) { - ctrl, req := gomock.NewController(t), require.New(t) - - mockClient := mocks.NewMockTGBotAPIHTTPClient(ctrl) - mockClient.EXPECT().Do(gomock.Any()).Return(&http.Response{Body: io.NopCloser(bytes.NewBuffer([]byte(`{"ok":true,"result":{}}`)))}, nil) - api, err := tgbotapi.NewBotAPIWithClient("fake-token", "http://endpoint/fake%s/%s", mockClient) - req.NoError(err) - - up := NewPollingUpdateProvider(logr.Discard(), api) - ctx, cancel := context.WithCancel(context.Background()) - - mockClient.EXPECT().Do(gomock.Any()).DoAndReturn(func(r *http.Request) (*http.Response, error) { - cancel() - return &http.Response{Body: io.NopCloser(bytes.NewBuffer([]byte(`{"ok":true,"result":[{"update_id":1}]}`)))}, nil - }).AnyTimes() - updates := up.Updates(ctx) - <-ctx.Done() - - // Update received - req.Eventually(func() bool { - select { - case update, ok := <-updates: - req.True(ok, "one update must be received") - req.Equal(1, update.UpdateID) - return true - default: - } - return false - }, time.Second, time.Millisecond*50) - - // Channel closed - req.Eventually(func() bool { - select { - case _, ok := <-updates: - req.False(ok, "channel must be closed") - return true - default: - } - return false - }, time.Second, time.Millisecond*50) -} - -func TestMain(m *testing.M) { - goleak.VerifyTestMain(m) -} diff --git a/test.go b/test.go new file mode 100644 index 0000000..1d4641b --- /dev/null +++ b/test.go @@ -0,0 +1,25 @@ +package main + +import ( + "context" + "log" + "time" + + "github.com/bbralion/CTFloodBot/pkg/services" +) + +func main() { + streamer, err := services.NewLongPollStreamer( + "https://api.telegram.org", "5417655434:AAH18hyT_Jz1GSD0ITS1kI7_oBBiGLAbwXE", + services.LongPollOptions{}) + if err != nil { + log.Println(err) + } + + ctx, cancel := context.WithTimeout(context.Background(), time.Minute*10) + defer cancel() + stream := streamer.Stream(ctx) + for x := range stream.AsTgBotAPI(services.DefaultCapacity) { + log.Println(x.Error, x.Value.Message.Text) + } +} From a8ba72a7077a9bca657838ddba9fdb4fc034991a Mon Sep 17 00:00:00 2001 From: renbou Date: Tue, 2 Aug 2022 01:39:52 +0300 Subject: [PATCH 15/17] test: stream performance test --- pkg/services/longpoll.go | 4 - pkg/services/{streams.go => stream.go} | 0 pkg/services/stream_test.go | 211 +++++++++++++++++++++++++ pkg/services/streams_test.go | 73 --------- 4 files changed, 211 insertions(+), 77 deletions(-) rename pkg/services/{streams.go => stream.go} (100%) create mode 100644 pkg/services/stream_test.go delete mode 100644 pkg/services/streams_test.go diff --git a/pkg/services/longpoll.go b/pkg/services/longpoll.go index 7a12e7d..2adbd20 100644 --- a/pkg/services/longpoll.go +++ b/pkg/services/longpoll.go @@ -24,10 +24,6 @@ type LongPollOptions struct { Offset int Limit int Timeout time.Duration - // Telegram Bot API endpoint - Endpoint string - // Telegram Bot API token - Token string // The HTTP client to use for requests Client *http.Client } diff --git a/pkg/services/streams.go b/pkg/services/stream.go similarity index 100% rename from pkg/services/streams.go rename to pkg/services/stream.go diff --git a/pkg/services/stream_test.go b/pkg/services/stream_test.go new file mode 100644 index 0000000..55904a4 --- /dev/null +++ b/pkg/services/stream_test.go @@ -0,0 +1,211 @@ +package services + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "io" + "net/http" + "strings" + "sync" + "testing" + "time" + + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" + "github.com/stretchr/testify/require" +) + +func streamIs[T any](req *require.Assertions, s Stream[T], want []T) { + got := 0 + req.Eventually(func() bool { + select { + case val, ok := <-s: + if !ok { + req.Equal(len(want), got, "less values on stream than wanted") + return true + } + req.Less(got, len(want), "more updates on stream than wanted") + req.Equal(want[got], val) + got++ + default: + } + return false + }, time.Hour*5, time.Millisecond*50) +} + +func TestRawStream_AsTgBotApi(t *testing.T) { + tests := []struct { + name string + updates []Maybe[RawUpdate] + want []Maybe[tgbotapi.Update] + }{ + { + name: "no values", + updates: nil, + want: nil, + }, + { + name: "values and error", + updates: []Maybe[RawUpdate]{ + {Value: RawUpdate(`{"update_id":1,"inline_query":{"query":"inline-query-test"}}`)}, + {Value: RawUpdate(`{"update_id":2,"message":{"text":"message-test"}}`)}, + {Error: errors.New("connection error")}, + }, + want: []Maybe[tgbotapi.Update]{ + {Value: tgbotapi.Update{UpdateID: 1, InlineQuery: &tgbotapi.InlineQuery{Query: "inline-query-test"}}}, + {Value: tgbotapi.Update{UpdateID: 2, Message: &tgbotapi.Message{Text: "message-test"}}}, + {Error: errors.New("connection error")}, + }, + }, + } + + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + req := require.New(t) + + stream := make(chan Maybe[RawUpdate]) + go func() { + defer close(stream) + for _, u := range tt.updates { + stream <- u + } + }() + + streamIs(req, RawStream(stream).AsTgBotAPI(DefaultCapacity), tt.want) + }) + } +} + +const ( + longPollTestMessagesTotal = 10000 + longPollTestMessagesPerResponse = 100 +) + +func encodeAPIResponse(data any) ([]byte, error) { + response := struct { + Ok bool + Result any + }{ + Ok: true, + Result: data, + } + return json.Marshal(response) +} + +type longPollTestRoundTripper struct { + data [][]byte + mu sync.Mutex + i int +} + +func (rt *longPollTestRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { + select { + case <-req.Context().Done(): + return nil, req.Context().Err() + default: + } + + if strings.HasSuffix(req.URL.Path, "getMe") { + b, err := encodeAPIResponse(tgbotapi.User{ID: 1}) + if err != nil { + return nil, err + } + return &http.Response{StatusCode: 200, Body: io.NopCloser(bytes.NewBuffer(b))}, nil + } + + var cur int + rt.mu.Lock() + cur, rt.i = rt.i, (rt.i+1)%len(rt.data) + rt.mu.Unlock() + return &http.Response{ + StatusCode: 200, + Body: io.NopCloser(bytes.NewBuffer(rt.data[cur])), + }, nil +} + +func newLongPollTestRoundTripper(b *testing.B) *longPollTestRoundTripper { + data := make([][]byte, b.N*longPollTestMessagesTotal/longPollTestMessagesPerResponse) + for i := range data { + updates := make([]tgbotapi.Update, longPollTestMessagesPerResponse) + for j := range updates { + updates[j] = tgbotapi.Update{UpdateID: i*(longPollTestMessagesPerResponse) + j} + } + + raw, err := encodeAPIResponse(updates) + if err != nil { + b.Error(err) + b.FailNow() + return nil + } + data[i] = raw + } + return &longPollTestRoundTripper{data: data} +} + +func longPollTestValidate(b *testing.B, s Stream[Maybe[tgbotapi.Update]]) { + end := longPollTestMessagesTotal * b.N + i, cnt := 0, 0 + for u := range s { + if u.Error != nil { + b.Error(u.Error) + b.FailNow() + } else if u.Value.UpdateID != i { + b.Errorf("expected %d but got %d", i, u.Value.UpdateID) + b.FailNow() + } + + i = (i + 1) % (b.N * longPollTestMessagesTotal) + cnt++ + if cnt == end { + break + } + } +} + +// Benchmark of simple long polling using a single goroutine receiving messages and decoding them +func BenchmarkNaiveLongPoll(b *testing.B) { + b.StopTimer() + b.Logf("Decoding %d messages %d times", longPollTestMessagesTotal, b.N) + api, err := tgbotapi.NewBotAPIWithClient("aboba", tgbotapi.APIEndpoint, &http.Client{Transport: newLongPollTestRoundTripper(b)}) + if err != nil { + b.Error(err) + b.FailNow() + } + + b.StartTimer() + updateCh, _ := api.GetUpdatesChan(tgbotapi.UpdateConfig{}) + stream := make(chan Maybe[tgbotapi.Update]) + go func() { + defer close(stream) + for { + v, ok := <-updateCh + if !ok { + return + } + + stream <- Maybe[tgbotapi.Update]{Value: v} + } + }() + longPollTestValidate(b, stream) + api.StopReceivingUpdates() +} + +// Benchmark of long poll using parallelized json decoding +func BenchmarkOptimizedLongPoll(b *testing.B) { + b.StopTimer() + b.Logf("Decoding %d messages %d times", longPollTestMessagesTotal, b.N) + streamer, err := NewLongPollStreamer("https://api.telegram.org", "aboba", LongPollOptions{Client: &http.Client{Transport: newLongPollTestRoundTripper(b)}}) + if err != nil { + b.Error(err) + b.FailNow() + } + ctx, cancel := context.WithCancel(context.Background()) + + b.StartTimer() + stream := streamer.Stream(ctx) + longPollTestValidate(b, stream.AsTgBotAPI(DefaultCapacity)) + cancel() +} diff --git a/pkg/services/streams_test.go b/pkg/services/streams_test.go deleted file mode 100644 index 2114003..0000000 --- a/pkg/services/streams_test.go +++ /dev/null @@ -1,73 +0,0 @@ -package services - -import ( - "errors" - "testing" - "time" - - tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" - "github.com/stretchr/testify/require" -) - -func streamIs[T any](req *require.Assertions, s Stream[T], want []T) { - got := 0 - req.Eventually(func() bool { - select { - case val, ok := <-s: - if !ok { - req.Equal(len(want), got, "less values on stream than wanted") - return true - } - req.Less(got, len(want), "more updates on stream than wanted") - req.Equal(want[got], val) - got++ - default: - } - return false - }, time.Hour*5, time.Millisecond*50) -} - -func TestRawStream_AsTgBotApi(t *testing.T) { - tests := []struct { - name string - updates []Maybe[RawUpdate] - want []Maybe[tgbotapi.Update] - }{ - { - name: "no values", - updates: nil, - want: nil, - }, - { - name: "values and error", - updates: []Maybe[RawUpdate]{ - {Value: RawUpdate(`{"update_id":1,"inline_query":{"query":"inline-query-test"}}`)}, - {Value: RawUpdate(`{"update_id":2,"message":{"text":"message-test"}}`)}, - {Error: errors.New("connection error")}, - }, - want: []Maybe[tgbotapi.Update]{ - {Value: tgbotapi.Update{UpdateID: 1, InlineQuery: &tgbotapi.InlineQuery{Query: "inline-query-test"}}}, - {Value: tgbotapi.Update{UpdateID: 2, Message: &tgbotapi.Message{Text: "message-test"}}}, - {Error: errors.New("connection error")}, - }, - }, - } - - for _, tt := range tests { - tt := tt - t.Run(tt.name, func(t *testing.T) { - t.Parallel() - req := require.New(t) - - stream := make(chan Maybe[RawUpdate]) - go func() { - defer close(stream) - for _, u := range tt.updates { - stream <- u - } - }() - - streamIs(req, RawStream(stream).AsTgBotAPI(DefaultCapacity), tt.want) - }) - } -} From 772215d0fbe8b7cd6181a2e4d948fc81ddbc4a69 Mon Sep 17 00:00:00 2001 From: renbou Date: Tue, 2 Aug 2022 04:59:09 +0300 Subject: [PATCH 16/17] refactor: don't use jsoniter, disable parallelized computation order synchronizaztion --- go.mod | 3 - go.sum | 8 --- pkg/services/longpoll.go | 81 +++++++++++++++------------ pkg/services/stream.go | 108 +++++++++++++----------------------- pkg/services/stream_test.go | 98 ++++++++++++++++---------------- test.go | 25 --------- 6 files changed, 135 insertions(+), 188 deletions(-) delete mode 100644 test.go diff --git a/go.mod b/go.mod index 4df0150..90e7cb8 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,6 @@ require ( github.com/go-logr/logr v1.2.3 github.com/go-telegram-bot-api/telegram-bot-api v1.0.1-0.20201107014523-54104a08f947 github.com/golang/mock v1.6.0 - github.com/json-iterator/go v1.1.12 github.com/justinas/alice v1.2.0 github.com/stretchr/testify v1.8.0 go.uber.org/goleak v1.1.12 @@ -17,8 +16,6 @@ require ( require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/golang/protobuf v1.5.2 // indirect - github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect - github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/technoweenie/multipartstreamer v1.0.1 // indirect golang.org/x/net v0.0.0-20220728181054-f92ba40d432d // indirect diff --git a/go.sum b/go.sum index 6da6c69..6c27ec6 100644 --- a/go.sum +++ b/go.sum @@ -51,11 +51,8 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= -github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= -github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/justinas/alice v1.2.0 h1:+MHSA/vccVCF4Uq37S42jwlkvI2Xzl7zTPCN5BnZNVo= github.com/justinas/alice v1.2.0/go.mod h1:fN5HRH/reO/zrUflLfTN43t3vXvKzvZIENsNEe7i7qA= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= @@ -63,17 +60,12 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= -github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= -github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= -github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= -github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= diff --git a/pkg/services/longpoll.go b/pkg/services/longpoll.go index 2adbd20..787740d 100644 --- a/pkg/services/longpoll.go +++ b/pkg/services/longpoll.go @@ -2,6 +2,7 @@ package services import ( "context" + "encoding/json" "errors" "fmt" "io" @@ -10,8 +11,6 @@ import ( "path" "strconv" "time" - - jsoniter "github.com/json-iterator/go" ) // DefaultLongPollTimeout is the default timeout used for long polling @@ -19,20 +18,18 @@ const DefaultLongPollTimeout = time.Second * 60 // LongPollOptions specifies various options to use inside the long poll streamer. // Offset, Limit and Timeout specify the values to send to Telegram's getUpdates method. -// By default a Timeout of DefaultLongPollTimeout is used. +// By default a Timeout of DefaultLongPollTimeout is used, and DefaultCapacity will be used as the default Limit. type LongPollOptions struct { Offset int Limit int Timeout time.Duration - // The HTTP client to use for requests - Client *http.Client + Client *http.Client } type longPollStreamer struct { opts LongPollOptions endpointURL *url.URL params url.Values - iterator *jsoniter.Iterator } func (s *longPollStreamer) poll(ctx context.Context) (*http.Response, error) { @@ -49,47 +46,61 @@ func (s *longPollStreamer) poll(ctx context.Context) (*http.Response, error) { if err != nil { // Unwrap url.Error returned from do to avoid leaking url with bot token return nil, fmt.Errorf("doing poll request: %w", errors.Unwrap(err)) - } - if resp.StatusCode != http.StatusOK { + } else if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("bad response code while polling: %s", resp.Status) } return resp, nil } -func (s *longPollStreamer) parseUpdates(stream chan Maybe[RawUpdate], body io.ReadCloser) error { - s.iterator.Reset(body) - defer body.Close() - defer s.iterator.Reset(nil) +// readRespones tries to read the response in the fastest way possible +func (s *longPollStreamer) readResponse(resp *http.Response) (buf []byte, err error) { + defer resp.Body.Close() + + if resp.ContentLength != -1 { + buf = make([]byte, resp.ContentLength) + _, err = io.ReadFull(resp.Body, buf) + } else { + buf, err = io.ReadAll(resp.Body) + } + return +} + +func (s *longPollStreamer) parseUpdates(stream chan<- Maybe[RawUpdate], resp *http.Response) error { + buf, err := s.readResponse(resp) + if err != nil { + return fmt.Errorf("reading getUpdates response: %w", err) + } // Parse API response wrapper - var resp struct { + var apiResp struct { Ok bool Description string - Result []jsoniter.RawMessage + Result []RawUpdate } - if s.iterator.ReadVal(&resp); s.iterator.Error != nil { - return fmt.Errorf("parsing getUpdates response: %w", s.iterator.Error) + if err := json.Unmarshal(buf, &apiResp); err != nil { + return fmt.Errorf("parsing getUpdates response: %w", err) } - if !resp.Ok { - return fmt.Errorf("getUpdates response.Ok is false: %s", resp.Description) + if !apiResp.Ok { + return fmt.Errorf("getUpdates response.Ok is false: %s", apiResp.Description) } - for _, u := range resp.Result { - stream <- Maybe[RawUpdate]{Value: RawUpdate(u)} + for _, u := range apiResp.Result { + stream <- Maybe[RawUpdate]{Value: u} } - if len(resp.Result) > 0 { - val := jsoniter.Get(resp.Result[len(resp.Result)-1], "update_id") - updateID := val.ToInt() - if err := val.LastError(); err != nil { + if len(apiResp.Result) > 0 { + var updateWrapper struct { + UpdateID int `json:"update_id"` + } + if err := json.Unmarshal(apiResp.Result[len(apiResp.Result)-1], &updateWrapper); err != nil { return fmt.Errorf("retrieving update_id: %w", err) } - s.params.Set("offset", strconv.Itoa(updateID+1)) + s.params.Set("offset", strconv.Itoa(updateWrapper.UpdateID+1)) } return nil } func (s *longPollStreamer) Stream(ctx context.Context) RawStream { - stream := make(chan Maybe[RawUpdate]) + stream := make(chan Maybe[RawUpdate], s.opts.Limit) go func() { defer close(stream) @@ -112,7 +123,7 @@ func (s *longPollStreamer) Stream(ctx context.Context) RawStream { return } - if err := s.parseUpdates(stream, resp.Body); err != nil { + if err := s.parseUpdates(stream, resp); err != nil { stream <- Maybe[RawUpdate]{Error: err} return } @@ -134,6 +145,9 @@ func NewLongPollStreamer(endpoint, token string, opts LongPollOptions) (RawStrea if opts.Timeout == 0 { opts.Timeout = DefaultLongPollTimeout } + if opts.Limit == 0 { + opts.Limit = DefaultCapacity + } if opts.Client == nil { opts.Client = http.DefaultClient } @@ -142,14 +156,7 @@ func NewLongPollStreamer(endpoint, token string, opts LongPollOptions) (RawStrea endpointURL.Path = path.Join(endpointURL.Path, "bot"+token, "getUpdates") params := make(url.Values) params.Set("timeout", strconv.Itoa(int(opts.Timeout.Seconds()))) - if opts.Offset != 0 { - params.Set("offset", strconv.Itoa(opts.Offset)) - } - if opts.Limit != 0 { - params.Set("limit", strconv.Itoa(opts.Limit)) - } - return &longPollStreamer{ - opts, endpointURL, params, - jsoniter.ParseBytes(jsoniter.ConfigFastest, make([]byte, DefaultDecodeBufferSize)), - }, nil + params.Set("limit", strconv.Itoa(opts.Limit)) + params.Set("offset", strconv.Itoa(opts.Offset)) + return &longPollStreamer{opts, endpointURL, params}, nil } diff --git a/pkg/services/stream.go b/pkg/services/stream.go index 64ffb19..661748b 100644 --- a/pkg/services/stream.go +++ b/pkg/services/stream.go @@ -2,11 +2,11 @@ package services import ( "context" + "encoding/json" "runtime" "sync" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" - jsoniter "github.com/json-iterator/go" ) // DefaultCapacity is the default capacity which should be passed into Map* and As* functions. @@ -36,92 +36,64 @@ func (s Stream[T]) Drain() { } } -// TODO: place inside MappedStream once type decs inside generic functions are supported -type mapJob[T, K any] struct { - in Maybe[T] - out chan Maybe[K] -} - -// MappedStream maps a stream to a channel in parallel using the given mapper. -// runtime.NumCPU() goroutines are used for mapping, and the returned channel -// will have the capacity specified in arguments. +// MappedStream maps a stream to a stream in parallel using the given mapper. runtime.NumCPU() goroutines +// are used for mapping, and the returned stream will have the same capacity as the input stream. +// The output order after processing is not synchronized or defined. // // Make sure to call stream.Drain if you suddenly stop reading from the returned stream. -func MappedStream[T, K any](in Stream[Maybe[T]], mapper func(T) (K, error), capacity int, -) Stream[Maybe[K]] { +func MappedStream[T, K any](in Stream[Maybe[T]], mapper func(T) (K, error)) Stream[Maybe[K]] { + var wg sync.WaitGroup n := runtime.NumCPU() + out := make(chan Maybe[K], cap(in)) - // These channels are closed by the parallelizing goroutine - outOrder := make(chan Stream[Maybe[K]], n) - jobs := make(chan mapJob[T, K], n) - - // Pool of channels, should be ok to use here since the goroutines here should be longlived - chpool := sync.Pool{ - New: func() any { - return make(chan Maybe[K]) - }, - } - // Parallelize input - go func() { - defer close(outOrder) - defer close(jobs) - - for { - v, ok := <-in - if !ok { - return - } - - ch := chpool.Get().(chan Maybe[K]) - jobs <- mapJob[T, K]{in: v, out: ch} - outOrder <- ch - } - }() - - mapF := func() { - for { - job, ok := <-jobs - if !ok { - return - } - - result := Maybe[K]{ - Error: job.in.Error, - } - if result.Error == nil { - result.Value, result.Error = mapper(job.in.Value) + wg.Add(n) + for i := 0; i < n; i++ { + go func() { + defer wg.Done() + for { + job, ok := <-in + if !ok { + return + } + + result := Maybe[K]{ + Error: job.Error, + } + if result.Error == nil { + result.Value, result.Error = mapper(job.Value) + } + out <- result } - job.out <- result - } + }() } - out := make(chan Maybe[K], capacity) - // Synchronize output go func() { - defer close(out) - for in := range outOrder { - out <- <-in - } + wg.Wait() + close(out) }() - - for i := 0; i < n; i++ { - go mapF() - } return out } -// RawUpdate is either the raw JSON representation of an Update message. -type RawUpdate jsoniter.RawMessage +// RawUpdate is the raw JSON representation of an Update message. +type RawUpdate []byte + +// UnmarshalJSON is like json.RawMessage's UnmarshalJSON, however instead of +// copying the data it simply assigns it. Specifically, this means that +// the data used during decoding should not be reused elsewhere afterwards (i.e. no sync.Pool) +func (r *RawUpdate) UnmarshalJSON(m []byte) error { + *r = m + return nil +} // RawStream is a stream of raw updates. type RawStream Stream[Maybe[RawUpdate]] // AsTgBotAPI converts a RawStream into a stream of tgbotapi-style updates. -func (s RawStream) AsTgBotAPI(capacity int) Stream[Maybe[tgbotapi.Update]] { +func (s RawStream) AsTgBotAPI() Stream[Maybe[tgbotapi.Update]] { return MappedStream(Stream[Maybe[RawUpdate]](s), func(u RawUpdate) (tu tgbotapi.Update, err error) { - err = jsoniter.Unmarshal([]byte(u), &tu) + err = json.Unmarshal([]byte(u), &tu) return - }, capacity) + }) } // RawStreamer is a provider of RawUpdate's updates via an unbuffered stream. diff --git a/pkg/services/stream_test.go b/pkg/services/stream_test.go index 55904a4..ee462d3 100644 --- a/pkg/services/stream_test.go +++ b/pkg/services/stream_test.go @@ -5,8 +5,10 @@ import ( "context" "encoding/json" "errors" + "fmt" "io" "net/http" + "reflect" "strings" "sync" "testing" @@ -16,18 +18,27 @@ import ( "github.com/stretchr/testify/require" ) -func streamIs[T any](req *require.Assertions, s Stream[T], want []T) { - got := 0 +// streamContains validates that a stream contains the wanted values in any order +func streamContains[T any](req *require.Assertions, s Stream[T], want []T) { + got := make([]bool, len(want)) req.Eventually(func() bool { select { case val, ok := <-s: if !ok { - req.Equal(len(want), got, "less values on stream than wanted") + for _, v := range got { + req.True(v, "not all wanted values found on stream") + } return true } - req.Less(got, len(want), "more updates on stream than wanted") - req.Equal(want[got], val) - got++ + + var foundUnused bool + for i, v := range got { + if !v && reflect.DeepEqual(want[i], val) { + got[i], foundUnused = true, true + break + } + } + req.True(foundUnused, "redundant value found on stream: %q", val) default: } return false @@ -66,7 +77,7 @@ func TestRawStream_AsTgBotApi(t *testing.T) { t.Parallel() req := require.New(t) - stream := make(chan Maybe[RawUpdate]) + stream := make(chan Maybe[RawUpdate], DefaultCapacity) go func() { defer close(stream) for _, u := range tt.updates { @@ -74,7 +85,7 @@ func TestRawStream_AsTgBotApi(t *testing.T) { } }() - streamIs(req, RawStream(stream).AsTgBotAPI(DefaultCapacity), tt.want) + streamContains(req, RawStream(stream).AsTgBotAPI(), tt.want) }) } } @@ -92,7 +103,12 @@ func encodeAPIResponse(data any) ([]byte, error) { Ok: true, Result: data, } - return json.Marshal(response) + + b, err := json.Marshal(response) + if err != nil { + return nil, fmt.Errorf("marshaling api response: %w", err) + } + return b, nil } type longPollTestRoundTripper struct { @@ -104,30 +120,34 @@ type longPollTestRoundTripper struct { func (rt *longPollTestRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { select { case <-req.Context().Done(): - return nil, req.Context().Err() + return nil, fmt.Errorf("context done: %w", req.Context().Err()) default: } + response := func(b []byte) *http.Response { + return &http.Response{ + StatusCode: http.StatusOK, + ContentLength: int64(len(b)), + Body: io.NopCloser(bytes.NewBuffer(b)), + } + } if strings.HasSuffix(req.URL.Path, "getMe") { b, err := encodeAPIResponse(tgbotapi.User{ID: 1}) if err != nil { return nil, err } - return &http.Response{StatusCode: 200, Body: io.NopCloser(bytes.NewBuffer(b))}, nil + return response(b), nil } var cur int rt.mu.Lock() cur, rt.i = rt.i, (rt.i+1)%len(rt.data) rt.mu.Unlock() - return &http.Response{ - StatusCode: 200, - Body: io.NopCloser(bytes.NewBuffer(rt.data[cur])), - }, nil + return response(rt.data[cur]), nil } -func newLongPollTestRoundTripper(b *testing.B) *longPollTestRoundTripper { - data := make([][]byte, b.N*longPollTestMessagesTotal/longPollTestMessagesPerResponse) +func newLongPollTestClient(req *require.Assertions, n int) *http.Client { + data := make([][]byte, n*longPollTestMessagesTotal/longPollTestMessagesPerResponse) for i := range data { updates := make([]tgbotapi.Update, longPollTestMessagesPerResponse) for j := range updates { @@ -135,29 +155,16 @@ func newLongPollTestRoundTripper(b *testing.B) *longPollTestRoundTripper { } raw, err := encodeAPIResponse(updates) - if err != nil { - b.Error(err) - b.FailNow() - return nil - } + req.NoError(err) data[i] = raw } - return &longPollTestRoundTripper{data: data} + return &http.Client{Transport: &longPollTestRoundTripper{data: data}} } func longPollTestValidate(b *testing.B, s Stream[Maybe[tgbotapi.Update]]) { - end := longPollTestMessagesTotal * b.N - i, cnt := 0, 0 - for u := range s { - if u.Error != nil { - b.Error(u.Error) - b.FailNow() - } else if u.Value.UpdateID != i { - b.Errorf("expected %d but got %d", i, u.Value.UpdateID) - b.FailNow() - } - - i = (i + 1) % (b.N * longPollTestMessagesTotal) + b.Helper() + cnt, end := 0, longPollTestMessagesTotal*b.N + for range s { cnt++ if cnt == end { break @@ -167,13 +174,11 @@ func longPollTestValidate(b *testing.B, s Stream[Maybe[tgbotapi.Update]]) { // Benchmark of simple long polling using a single goroutine receiving messages and decoding them func BenchmarkNaiveLongPoll(b *testing.B) { + req := require.New(b) b.StopTimer() b.Logf("Decoding %d messages %d times", longPollTestMessagesTotal, b.N) - api, err := tgbotapi.NewBotAPIWithClient("aboba", tgbotapi.APIEndpoint, &http.Client{Transport: newLongPollTestRoundTripper(b)}) - if err != nil { - b.Error(err) - b.FailNow() - } + api, err := tgbotapi.NewBotAPIWithClient("aboba", tgbotapi.APIEndpoint, newLongPollTestClient(req, b.N)) + req.NoError(err) b.StartTimer() updateCh, _ := api.GetUpdatesChan(tgbotapi.UpdateConfig{}) @@ -195,17 +200,16 @@ func BenchmarkNaiveLongPoll(b *testing.B) { // Benchmark of long poll using parallelized json decoding func BenchmarkOptimizedLongPoll(b *testing.B) { + req := require.New(b) b.StopTimer() b.Logf("Decoding %d messages %d times", longPollTestMessagesTotal, b.N) - streamer, err := NewLongPollStreamer("https://api.telegram.org", "aboba", LongPollOptions{Client: &http.Client{Transport: newLongPollTestRoundTripper(b)}}) - if err != nil { - b.Error(err) - b.FailNow() - } + streamer, err := NewLongPollStreamer("https://api.telegram.org", "aboba", LongPollOptions{Client: newLongPollTestClient(req, b.N)}) + req.NoError(err) ctx, cancel := context.WithCancel(context.Background()) b.StartTimer() - stream := streamer.Stream(ctx) - longPollTestValidate(b, stream.AsTgBotAPI(DefaultCapacity)) + stream := streamer.Stream(ctx).AsTgBotAPI() + longPollTestValidate(b, stream) cancel() + stream.Drain() } diff --git a/test.go b/test.go deleted file mode 100644 index 1d4641b..0000000 --- a/test.go +++ /dev/null @@ -1,25 +0,0 @@ -package main - -import ( - "context" - "log" - "time" - - "github.com/bbralion/CTFloodBot/pkg/services" -) - -func main() { - streamer, err := services.NewLongPollStreamer( - "https://api.telegram.org", "5417655434:AAH18hyT_Jz1GSD0ITS1kI7_oBBiGLAbwXE", - services.LongPollOptions{}) - if err != nil { - log.Println(err) - } - - ctx, cancel := context.WithTimeout(context.Background(), time.Minute*10) - defer cancel() - stream := streamer.Stream(ctx) - for x := range stream.AsTgBotAPI(services.DefaultCapacity) { - log.Println(x.Error, x.Value.Message.Text) - } -} From 32cc6057ea28073ca8036b509f3129a24e95aa6f Mon Sep 17 00:00:00 2001 From: renbou Date: Tue, 2 Aug 2022 05:08:10 +0300 Subject: [PATCH 17/17] fix: streamer should close/drain channel when context is done --- pkg/services/longpoll.go | 15 +++++++++++---- pkg/services/stream.go | 14 ++------------ pkg/services/stream_test.go | 1 - 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/pkg/services/longpoll.go b/pkg/services/longpoll.go index 787740d..2a86f70 100644 --- a/pkg/services/longpoll.go +++ b/pkg/services/longpoll.go @@ -52,7 +52,8 @@ func (s *longPollStreamer) poll(ctx context.Context) (*http.Response, error) { return resp, nil } -// readRespones tries to read the response in the fastest way possible +// readRespones tries to read the response in the fastest way possible. That is, if ContentLength +// is set, then we can use it in order to allocate a buffer wit the correct size from the get-go. func (s *longPollStreamer) readResponse(resp *http.Response) (buf []byte, err error) { defer resp.Body.Close() @@ -65,13 +66,14 @@ func (s *longPollStreamer) readResponse(resp *http.Response) (buf []byte, err er return } -func (s *longPollStreamer) parseUpdates(stream chan<- Maybe[RawUpdate], resp *http.Response) error { +func (s *longPollStreamer) parseUpdates(ctx context.Context, stream chan<- Maybe[RawUpdate], resp *http.Response) error { buf, err := s.readResponse(resp) if err != nil { return fmt.Errorf("reading getUpdates response: %w", err) } - // Parse API response wrapper + // Parse API response wrapper. The updates here are parsed as RawUpdate's, which simply set + // their value to the correct portion of the prepared buffer, bypassing an extra copy. var apiResp struct { Ok bool Description string @@ -85,6 +87,11 @@ func (s *longPollStreamer) parseUpdates(stream chan<- Maybe[RawUpdate], resp *ht } for _, u := range apiResp.Result { + select { + case <-ctx.Done(): + return nil + default: + } stream <- Maybe[RawUpdate]{Value: u} } if len(apiResp.Result) > 0 { @@ -123,7 +130,7 @@ func (s *longPollStreamer) Stream(ctx context.Context) RawStream { return } - if err := s.parseUpdates(stream, resp); err != nil { + if err := s.parseUpdates(ctx, stream, resp); err != nil { stream <- Maybe[RawUpdate]{Error: err} return } diff --git a/pkg/services/stream.go b/pkg/services/stream.go index 661748b..3c67f20 100644 --- a/pkg/services/stream.go +++ b/pkg/services/stream.go @@ -26,21 +26,9 @@ type Maybe[T any] struct { // Stream is a readonly channel of some type. type Stream[T any] <-chan T -// Drain drains the stream completely. -func (s Stream[T]) Drain() { - for { - _, ok := <-s - if !ok { - return - } - } -} - // MappedStream maps a stream to a stream in parallel using the given mapper. runtime.NumCPU() goroutines // are used for mapping, and the returned stream will have the same capacity as the input stream. // The output order after processing is not synchronized or defined. -// -// Make sure to call stream.Drain if you suddenly stop reading from the returned stream. func MappedStream[T, K any](in Stream[Maybe[T]], mapper func(T) (K, error)) Stream[Maybe[K]] { var wg sync.WaitGroup n := runtime.NumCPU() @@ -98,5 +86,7 @@ func (s RawStream) AsTgBotAPI() Stream[Maybe[tgbotapi.Update]] { // RawStreamer is a provider of RawUpdate's updates via an unbuffered stream. type RawStreamer interface { + // Stream launches a single instance of the streamer. In general, it isn't safe to use this concurrently. + // On context cancelation/deadline the streamer must stop streaming and close the stream. Stream(ctx context.Context) RawStream } diff --git a/pkg/services/stream_test.go b/pkg/services/stream_test.go index ee462d3..ebf949f 100644 --- a/pkg/services/stream_test.go +++ b/pkg/services/stream_test.go @@ -211,5 +211,4 @@ func BenchmarkOptimizedLongPoll(b *testing.B) { stream := streamer.Stream(ctx).AsTgBotAPI() longPollTestValidate(b, stream) cancel() - stream.Drain() }