storj/satellite/mailservice/simulate/linkclicker.go
Jeff Wendling 7999d24f81 all: use monkit v3
this commit updates our monkit dependency to the v3 version where
it outputs in an influx style. this makes discovery much easier
as many tools are built to look at it this way.

graphite and rothko will suffer some due to no longer being a tree
based on dots. hopefully time will exist to update rothko to
index based on the new metric format.

it adds an influx output for the statreceiver so that we can
write to influxdb v1 or v2 directly.

Change-Id: Iae9f9494a6d29cfbd1f932a5e71a891b490415ff
2020-02-05 23:53:17 +00:00

87 lines
1.8 KiB
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information
package simulate
import (
"context"
"net/http"
"regexp"
"strings"
"github.com/spacemonkeygo/monkit/v3"
"github.com/zeebo/errs"
"storj.io/storj/private/post"
"storj.io/storj/satellite/mailservice"
)
var mon = monkit.Package()
var _ mailservice.Sender = (*LinkClicker)(nil)
// LinkClicker is mailservice.Sender that click all links
// from html msg parts
//
// architecture: Service
type LinkClicker struct{}
// FromAddress return empty mail address
func (clicker *LinkClicker) FromAddress() post.Address {
return post.Address{}
}
// SendEmail click all links from email html parts
func (clicker *LinkClicker) SendEmail(ctx context.Context, msg *post.Message) (err error) {
defer mon.Task()(&ctx)(&err)
// dirty way to find links without pulling in a html dependency
regx := regexp.MustCompile(`href="([^\s])+"`)
// collect all links
var links []string
for _, part := range msg.Parts {
tags := findLinkTags(part.Content)
for _, tag := range tags {
href := regx.FindString(tag)
if href == "" {
continue
}
links = append(links, href[len(`href="`):len(href)-1])
}
}
// click all links
var sendError error
for _, link := range links {
response, err := http.Get(link)
if err != nil {
continue
}
sendError = errs.Combine(sendError, err, response.Body.Close())
}
return sendError
}
func findLinkTags(body string) []string {
var tags []string
Loop:
for {
stTag := strings.Index(body, "<a")
if stTag < 0 {
break Loop
}
stripped := body[stTag:]
endTag := strings.Index(stripped, "</a>")
if endTag < 0 {
break Loop
}
offset := endTag + len("</a>") + 1
body = stripped[offset:]
tags = append(tags, stripped[:offset])
}
return tags
}