2019-11-14 10:15:58 +00:00
|
|
|
package mailservers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
2021-09-01 15:57:12 +00:00
|
|
|
multiaddr "github.com/multiformats/go-multiaddr"
|
|
|
|
|
2019-11-14 10:15:58 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/enr"
|
|
|
|
"github.com/status-im/status-go/rtt"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PingQuery struct {
|
|
|
|
Addresses []string `json:"addresses"`
|
|
|
|
TimeoutMs int `json:"timeoutMs"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type PingResult struct {
|
2021-09-01 15:57:12 +00:00
|
|
|
Address string `json:"address"`
|
|
|
|
RTTMs *int `json:"rttMs"`
|
|
|
|
Err *string `json:"error"`
|
2019-11-14 10:15:58 +00:00
|
|
|
}
|
|
|
|
|
2021-09-01 15:57:12 +00:00
|
|
|
type parseFn func(string) (string, error)
|
|
|
|
|
2019-11-14 10:15:58 +00:00
|
|
|
func (pr *PingResult) Update(rttMs int, err error) {
|
|
|
|
if err != nil {
|
|
|
|
errStr := err.Error()
|
|
|
|
pr.Err = &errStr
|
|
|
|
}
|
2022-01-13 14:30:00 +00:00
|
|
|
if rttMs >= 0 {
|
2019-11-25 14:05:49 +00:00
|
|
|
pr.RTTMs = &rttMs
|
|
|
|
} else {
|
|
|
|
pr.RTTMs = nil
|
|
|
|
}
|
2019-11-14 10:15:58 +00:00
|
|
|
}
|
|
|
|
|
2022-01-12 16:02:01 +00:00
|
|
|
func EnodeToAddr(node *enode.Node) (string, error) {
|
2019-11-14 10:15:58 +00:00
|
|
|
var ip4 enr.IPv4
|
2022-01-12 16:02:01 +00:00
|
|
|
err := node.Load(&ip4)
|
2019-11-14 10:15:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
var tcp enr.TCP
|
|
|
|
err = node.Load(&tcp)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s:%d", net.IP(ip4).String(), tcp), nil
|
|
|
|
}
|
|
|
|
|
2022-01-12 16:02:01 +00:00
|
|
|
func EnodeStringToAddr(enodeAddr string) (string, error) {
|
|
|
|
node, err := enode.ParseV4(enodeAddr)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return EnodeToAddr(node)
|
|
|
|
}
|
|
|
|
|
2021-09-01 15:57:12 +00:00
|
|
|
func parse(addresses []string, fn parseFn) (map[string]*PingResult, []string) {
|
|
|
|
results := make(map[string]*PingResult, len(addresses))
|
2019-11-14 10:15:58 +00:00
|
|
|
var toPing []string
|
|
|
|
|
2021-09-01 15:57:12 +00:00
|
|
|
for i := range addresses {
|
|
|
|
addr, err := fn(addresses[i])
|
2019-11-14 10:15:58 +00:00
|
|
|
if err != nil {
|
|
|
|
errStr := err.Error()
|
2021-09-01 15:57:12 +00:00
|
|
|
results[addresses[i]] = &PingResult{Address: addresses[i], Err: &errStr}
|
2019-11-14 10:15:58 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-09-01 15:57:12 +00:00
|
|
|
results[addr] = &PingResult{Address: addresses[i]}
|
2019-11-14 10:15:58 +00:00
|
|
|
toPing = append(toPing, addr)
|
|
|
|
}
|
|
|
|
return results, toPing
|
|
|
|
}
|
|
|
|
|
|
|
|
func mapValues(m map[string]*PingResult) []*PingResult {
|
|
|
|
rval := make([]*PingResult, 0, len(m))
|
|
|
|
for _, value := range m {
|
|
|
|
rval = append(rval, value)
|
|
|
|
}
|
|
|
|
return rval
|
|
|
|
}
|
|
|
|
|
2022-01-12 16:02:01 +00:00
|
|
|
func DoPing(ctx context.Context, addresses []string, timeoutMs int, p parseFn) ([]*PingResult, error) {
|
|
|
|
timeout := time.Duration(timeoutMs) * time.Millisecond
|
2019-11-14 10:15:58 +00:00
|
|
|
|
2022-01-12 16:02:01 +00:00
|
|
|
resultsMap, toPing := parse(addresses, p)
|
2019-11-14 10:15:58 +00:00
|
|
|
|
|
|
|
// run the checks concurrently
|
|
|
|
results, err := rtt.CheckHosts(toPing, timeout)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// set ping results
|
|
|
|
for i := range results {
|
|
|
|
r := results[i]
|
|
|
|
pr := resultsMap[r.Addr]
|
|
|
|
if pr == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
pr.Update(r.RTTMs, r.Err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return mapValues(resultsMap), nil
|
|
|
|
}
|
2021-09-01 15:57:12 +00:00
|
|
|
|
|
|
|
func (a *API) Ping(ctx context.Context, pq PingQuery) ([]*PingResult, error) {
|
2022-01-12 16:02:01 +00:00
|
|
|
return DoPing(ctx, pq.Addresses, pq.TimeoutMs, EnodeStringToAddr)
|
2021-09-01 15:57:12 +00:00
|
|
|
}
|
|
|
|
|
2022-01-12 16:02:01 +00:00
|
|
|
func MultiAddressToAddress(multiAddr string) (string, error) {
|
2021-09-01 15:57:12 +00:00
|
|
|
|
|
|
|
ma, err := multiaddr.NewMultiaddr(multiAddr)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2022-05-19 14:23:25 +00:00
|
|
|
dns4, err := ma.ValueForProtocol(multiaddr.P_DNS4)
|
|
|
|
if err != nil && err != multiaddr.ErrProtocolNotFound {
|
2021-09-01 15:57:12 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2022-05-19 14:23:25 +00:00
|
|
|
ip4 := ""
|
|
|
|
if dns4 != "" {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
|
|
defer cancel()
|
|
|
|
ip4, err = net.DefaultResolver.LookupCNAME(ctx, dns4)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ip4, err = ma.ValueForProtocol(multiaddr.P_IP4)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-01 15:57:12 +00:00
|
|
|
tcp, err := ma.ValueForProtocol(multiaddr.P_TCP)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s:%s", ip4, tcp), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) MultiAddressPing(ctx context.Context, pq PingQuery) ([]*PingResult, error) {
|
2022-01-12 16:02:01 +00:00
|
|
|
return DoPing(ctx, pq.Addresses, pq.TimeoutMs, MultiAddressToAddress)
|
2021-09-01 15:57:12 +00:00
|
|
|
}
|