refactor(reporter): refactoring TelegramWriter, GoogleChatWriter (#1628)
* style: remove unnecessary line break * style: use regexp.MatchString instead of regexp.Match * refactor(reporter): refactoring TelegramWriter, GoogleChatWriter
This commit is contained in:
		@@ -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",
 | 
			
		||||
@@ -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
 | 
			
		||||
@@ -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()
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user