File size: 4,019 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package twilio

import (
	"context"
	"database/sql"
	"encoding/json"
	"errors"
	"fmt"
	"io"
	"net/http"
	"net/url"
	"strings"
	"time"

	"github.com/target/goalert/config"
	"github.com/target/goalert/permission"
	"github.com/target/goalert/user/contactmethod"
	"github.com/target/goalert/util/log"
)

// CarrierInfo holds information about the carrier network for a particular number.
type CarrierInfo struct {
	Name              string `json:"name"`
	Type              string `json:"type"`
	MobileNetworkCode string `json:"mobile_network_code"`
	MobileCountryCode string `json:"mobile_country_code"`
}

// DefaultLookupURL is the value that will be used for lookup calls if Config.BaseURL is empty.
const DefaultLookupURL = "https://lookups.twilio.com"

// ErrCarrierStale is returned if the available carrier data is over a year old.
var ErrCarrierStale = errors.New("carrier data is stale")

// ErrCarrierUnavailable is returned if the carrier data is missing and fetch is disabled.
var ErrCarrierUnavailable = errors.New("carrier data is unavailable")

func (c *Config) dbCarrierInfo(ctx context.Context, number string) (*CarrierInfo, error) {
	m, err := c.CMStore.MetadataByDest(ctx, c.DB, NewSMSDest(number))
	if errors.Is(err, sql.ErrNoRows) {
		return nil, ErrCarrierUnavailable
	}
	if err != nil {
		return nil, err
	}
	info := &CarrierInfo{
		Name:              m.CarrierV1.Name,
		Type:              m.CarrierV1.Type,
		MobileCountryCode: m.CarrierV1.MobileCountryCode,
		MobileNetworkCode: m.CarrierV1.MobileNetworkCode,
	}
	if m.FetchedAt.Sub(m.CarrierV1.UpdatedAt) > 365*24*time.Hour {
		// over a year old
		return info, ErrCarrierStale
	}

	return info, nil
}

// CarrierInfo will return carrier information for the provided number. If fetch is true, it will fetch
// data from the Twilio API if it is not available from the DB.
func (c *Config) CarrierInfo(ctx context.Context, number string, fetch bool) (*CarrierInfo, error) {
	if c.CMStore == nil {
		return nil, nil
	}
	info, err := c.dbCarrierInfo(ctx, number)
	if !fetch || err == nil {
		return info, err
	}

	if !errors.Is(err, ErrCarrierStale) && !errors.Is(err, ErrCarrierUnavailable) {
		log.Log(ctx, err)
	}

	return c.FetchCarrierInfo(ctx, number)
}

// FetchCarrierInfo will lookup carrier information for the provided number using the Twilio API.
func (c Config) FetchCarrierInfo(ctx context.Context, number string) (*CarrierInfo, error) {
	if c.CMStore == nil {
		return nil, nil
	}
	// must be admin to fetch carrier info
	err := permission.LimitCheckAny(ctx, permission.Admin)
	if err != nil {
		return nil, err
	}

	cfg := config.FromContext(ctx)
	if c.BaseURL == "" {
		c.BaseURL = DefaultLookupURL
	}
	c.BaseURL = strings.TrimSuffix(c.BaseURL, "/")

	url := c.BaseURL + "/v1/PhoneNumbers/" + url.PathEscape(number) + "?Type=carrier"
	req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
	if err != nil {
		return nil, err
	}
	req.SetBasicAuth(cfg.Twilio.AccountSID, cfg.Twilio.AuthToken)

	resp, err := c.httpClient().Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	if resp.StatusCode != 200 {
		return nil, fmt.Errorf("non-200 response from Twilio: %s", resp.Status)
	}

	data, err := io.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}

	var result struct {
		Carrier CarrierInfo
	}
	err = json.Unmarshal(data, &result)
	if err != nil {
		return nil, err
	}

	// merge into existing metadata (if possible)
	var m contactmethod.Metadata
	m.CarrierV1.Name = result.Carrier.Name
	m.CarrierV1.Type = result.Carrier.Type
	m.CarrierV1.MobileCountryCode = result.Carrier.MobileCountryCode
	m.CarrierV1.MobileNetworkCode = result.Carrier.MobileNetworkCode

	err = c.CMStore.SetCarrierV1MetadataByDest(ctx, c.DB, NewSMSDest(number), &m)
	if err != nil && !errors.Is(err, sql.ErrNoRows) {
		log.Log(ctx, err)
	}
	err = c.CMStore.SetCarrierV1MetadataByDest(ctx, c.DB, NewVoiceDest(number), &m)
	if err != nil && !errors.Is(err, sql.ErrNoRows) {
		log.Log(ctx, err)
	}

	return &result.Carrier, nil
}