mirror of
https://github.com/crowdsecurity/crowdsec.git
synced 2025-05-10 20:05:55 +02:00
Support WithUserAgent in cti client (#3542)
This commit is contained in:
parent
d64f196b3f
commit
78a6179566
1 changed files with 19 additions and 8 deletions
|
@ -9,9 +9,8 @@ import (
|
|||
"net/http"
|
||||
"strings"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/crowdsecurity/crowdsec/pkg/apiclient/useragent"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -21,17 +20,19 @@ const (
|
|||
)
|
||||
|
||||
var (
|
||||
ErrUnauthorized = errors.New("unauthorized")
|
||||
ErrLimit = errors.New("request quota exceeded, please reduce your request rate")
|
||||
ErrNotFound = errors.New("ip not found")
|
||||
ErrDisabled = errors.New("cti is disabled")
|
||||
ErrUnknown = errors.New("unknown error")
|
||||
ErrUnauthorized = errors.New("unauthorized")
|
||||
ErrLimit = errors.New("request quota exceeded, please reduce your request rate")
|
||||
ErrNotFound = errors.New("ip not found")
|
||||
ErrDisabled = errors.New("cti is disabled")
|
||||
ErrUnknown = errors.New("unknown error")
|
||||
defaultUserAgent = useragent.Default()
|
||||
)
|
||||
|
||||
type CrowdsecCTIClient struct {
|
||||
httpClient *http.Client
|
||||
apiKey string
|
||||
Logger *log.Entry
|
||||
UserAgent string
|
||||
}
|
||||
|
||||
func (c *CrowdsecCTIClient) doRequest(ctx context.Context, method string, endpoint string, params map[string]string) ([]byte, error) {
|
||||
|
@ -48,7 +49,7 @@ func (c *CrowdsecCTIClient) doRequest(ctx context.Context, method string, endpoi
|
|||
}
|
||||
|
||||
req.Header.Set("X-Api-Key", c.apiKey)
|
||||
req.Header.Set("User-Agent", useragent.Default())
|
||||
req.Header.Set("User-Agent", c.UserAgent)
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
|
@ -169,6 +170,10 @@ func NewCrowdsecCTIClient(options ...func(*CrowdsecCTIClient)) *CrowdsecCTIClien
|
|||
client.Logger = log.NewEntry(log.New())
|
||||
}
|
||||
|
||||
if client.UserAgent == "" {
|
||||
client.UserAgent = defaultUserAgent
|
||||
}
|
||||
|
||||
return client
|
||||
}
|
||||
|
||||
|
@ -189,3 +194,9 @@ func WithAPIKey(apiKey string) func(*CrowdsecCTIClient) {
|
|||
c.apiKey = apiKey
|
||||
}
|
||||
}
|
||||
|
||||
func WithUserAgent(userAgent string) func(*CrowdsecCTIClient) {
|
||||
return func(c *CrowdsecCTIClient) {
|
||||
c.UserAgent = userAgent
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue