Skip to content

Commit

Permalink
refactor(reporter): refactoring TelegramWriter, GoogleChatWriter (#1628)
Browse files Browse the repository at this point in the history
* style: remove unnecessary line break

* style: use regexp.MatchString instead of regexp.Match

* refactor(reporter): refactoring TelegramWriter, GoogleChatWriter
  • Loading branch information
MaineK00n committed Apr 20, 2023
1 parent d4d33fc commit fb904f0
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 7 deletions.
7 changes: 3 additions & 4 deletions reporter/googlechat.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (w GoogleChatWriter) Write(rs ...models.ScanResult) (err error) {
re := regexp.MustCompile(w.Cnf.ServerNameRegexp)

for _, r := range rs {
if re.Match([]byte(r.FormatServerName())) {
if re.MatchString(r.FormatServerName()) {
continue
}
msgs := []string{fmt.Sprintf("*%s*\n%s\t%s\t%s",
Expand Down Expand Up @@ -73,11 +73,10 @@ func (w GoogleChatWriter) Write(rs ...models.ScanResult) (err error) {
}

func (w GoogleChatWriter) postMessage(message string) error {
uri := fmt.Sprintf("%s", w.Cnf.WebHookURL)
payload := `{"text": "` + message + `" }`

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, uri, bytes.NewBuffer([]byte(payload)))
req, err := http.NewRequestWithContext(ctx, http.MethodPost, w.Cnf.WebHookURL, bytes.NewBuffer([]byte(payload)))
defer cancel()
if err != nil {
return err
Expand All @@ -88,7 +87,7 @@ func (w GoogleChatWriter) postMessage(message string) error {
return err
}
resp, err := client.Do(req)
if checkResponse(resp) != nil && err != nil {
if w.checkResponse(resp) != nil && err != nil {
return err
}
defer resp.Body.Close()
Expand Down
4 changes: 2 additions & 2 deletions reporter/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ func (w TelegramWriter) sendMessage(chatID, token, message string) error {
return err
}
resp, err := client.Do(req)
if checkResponse(resp) != nil && err != nil {
if w.checkResponse(resp) != nil && err != nil {
return err
}
defer resp.Body.Close()
return nil
}

func checkResponse(r *http.Response) error {
func (w TelegramWriter) checkResponse(r *http.Response) error {
if c := r.StatusCode; 200 <= c && c <= 299 {
return nil
}
Expand Down
1 change: 0 additions & 1 deletion scanner/windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,6 @@ func parseRegistry(stdout, arch string) (osInfo, error) {
}

func detectOSName(osInfo osInfo) (string, error) {

osName, err := detectOSNameFromOSInfo(osInfo)
if err != nil {
return "", xerrors.Errorf("Failed to detect OS Name from OSInfo: %+v, err: %w", osInfo, err)
Expand Down

0 comments on commit fb904f0

Please sign in to comment.