2019-03-02 15:22:20 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information
|
|
|
|
|
|
|
|
package simulate
|
|
|
|
|
|
|
|
import (
|
2019-06-04 12:55:38 +01:00
|
|
|
"context"
|
2019-03-02 15:22:20 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
2022-05-11 18:23:05 +01:00
|
|
|
"time"
|
2019-03-02 15:22:20 +00:00
|
|
|
|
2019-11-08 20:40:39 +00:00
|
|
|
"github.com/spacemonkeygo/monkit/v3"
|
2019-03-02 15:22:20 +00:00
|
|
|
"github.com/zeebo/errs"
|
2022-05-11 18:23:05 +01:00
|
|
|
"go.uber.org/zap"
|
2021-07-23 21:53:19 +01:00
|
|
|
"golang.org/x/net/html"
|
2019-03-02 15:22:20 +00:00
|
|
|
|
2019-11-14 19:46:15 +00:00
|
|
|
"storj.io/storj/private/post"
|
2019-09-10 14:24:16 +01:00
|
|
|
"storj.io/storj/satellite/mailservice"
|
2019-03-02 15:22:20 +00:00
|
|
|
)
|
|
|
|
|
2019-06-04 12:55:38 +01:00
|
|
|
var mon = monkit.Package()
|
|
|
|
|
2019-09-10 14:24:16 +01:00
|
|
|
var _ mailservice.Sender = (*LinkClicker)(nil)
|
|
|
|
|
2020-12-05 16:01:42 +00:00
|
|
|
// LinkClicker is mailservice.Sender that click all links from html msg parts.
|
2019-09-10 14:24:16 +01:00
|
|
|
//
|
|
|
|
// architecture: Service
|
2021-07-23 21:53:19 +01:00
|
|
|
type LinkClicker struct {
|
2022-05-11 18:23:05 +01:00
|
|
|
log *zap.Logger
|
|
|
|
|
2021-07-23 21:53:19 +01:00
|
|
|
// MarkerAttribute specifies the attribute every anchor element must have in order to be clicked.
|
|
|
|
// This prevents the link clicker from clicking links that it should not (such as the password reset cancellation link).
|
|
|
|
// Leaving this field empty will make it click every link.
|
2022-05-11 18:23:05 +01:00
|
|
|
markerAttribute string
|
2021-07-23 21:53:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDefaultLinkClicker returns a LinkClicker with the default marker attribute.
|
2022-05-11 18:23:05 +01:00
|
|
|
func NewDefaultLinkClicker(log *zap.Logger) *LinkClicker {
|
|
|
|
return &LinkClicker{
|
|
|
|
log: log,
|
|
|
|
markerAttribute: "data-simulate",
|
|
|
|
}
|
2021-07-23 21:53:19 +01:00
|
|
|
}
|
2019-03-02 15:22:20 +00:00
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// FromAddress return empty mail address.
|
2019-03-02 15:22:20 +00:00
|
|
|
func (clicker *LinkClicker) FromAddress() post.Address {
|
|
|
|
return post.Address{}
|
|
|
|
}
|
|
|
|
|
2021-07-23 21:53:19 +01:00
|
|
|
// SendEmail click all links belonging to properly attributed anchors from email html parts.
|
2019-06-04 12:55:38 +01:00
|
|
|
func (clicker *LinkClicker) SendEmail(ctx context.Context, msg *post.Message) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2021-07-23 21:53:19 +01:00
|
|
|
var body string
|
2019-03-02 15:22:20 +00:00
|
|
|
for _, part := range msg.Parts {
|
2021-07-23 21:53:19 +01:00
|
|
|
body += part.Content
|
2019-03-02 15:22:20 +00:00
|
|
|
}
|
2021-07-23 21:53:19 +01:00
|
|
|
|
2019-03-02 15:22:20 +00:00
|
|
|
// click all links
|
|
|
|
var sendError error
|
2021-07-23 21:53:19 +01:00
|
|
|
for _, link := range clicker.FindLinks(body) {
|
2021-05-14 16:05:42 +01:00
|
|
|
req, err := http.NewRequestWithContext(ctx, "GET", link, nil)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2022-05-11 18:23:05 +01:00
|
|
|
clicker.log.Debug("clicking", zap.String("url", link))
|
|
|
|
client := &http.Client{}
|
|
|
|
client.Timeout = 5 * time.Second
|
|
|
|
response, err := client.Do(req)
|
2019-10-29 14:24:16 +00:00
|
|
|
if err != nil {
|
2022-05-11 18:23:05 +01:00
|
|
|
clicker.log.Error("failed to click", zap.String("url", link), zap.Error(err))
|
2019-10-29 14:24:16 +00:00
|
|
|
continue
|
|
|
|
}
|
2019-08-22 12:40:15 +01:00
|
|
|
sendError = errs.Combine(sendError, err, response.Body.Close())
|
2019-03-02 15:22:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return sendError
|
|
|
|
}
|
|
|
|
|
2021-07-23 21:53:19 +01:00
|
|
|
// FindLinks returns a list of all links belonging to properly attributed anchors in the HTML body.
|
|
|
|
func (clicker *LinkClicker) FindLinks(body string) (links []string) {
|
|
|
|
tokens := html.NewTokenizer(strings.NewReader(body))
|
2019-03-02 15:22:20 +00:00
|
|
|
Loop:
|
|
|
|
for {
|
2021-07-23 21:53:19 +01:00
|
|
|
switch tokens.Next() {
|
|
|
|
case html.ErrorToken:
|
2019-03-02 15:22:20 +00:00
|
|
|
break Loop
|
2021-07-23 21:53:19 +01:00
|
|
|
case html.StartTagToken:
|
|
|
|
token := tokens.Token()
|
|
|
|
if strings.ToLower(token.Data) == "a" {
|
2022-05-11 18:23:05 +01:00
|
|
|
simulate := clicker.markerAttribute == ""
|
2021-07-23 21:53:19 +01:00
|
|
|
var href string
|
|
|
|
for _, attr := range token.Attr {
|
|
|
|
if strings.ToLower(attr.Key) == "href" {
|
|
|
|
href = attr.Val
|
2022-05-11 18:23:05 +01:00
|
|
|
} else if !simulate && attr.Key == clicker.markerAttribute {
|
2021-07-23 21:53:19 +01:00
|
|
|
simulate = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if simulate && href != "" {
|
|
|
|
links = append(links, href)
|
|
|
|
}
|
|
|
|
}
|
2019-03-02 15:22:20 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-23 21:53:19 +01:00
|
|
|
return links
|
2019-03-02 15:22:20 +00:00
|
|
|
}
|