2024-05-18 10:59:54 +00:00
|
|
|
package client
|
2024-05-04 15:36:57 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2024-05-04 18:42:47 +00:00
|
|
|
"errors"
|
2024-05-04 15:36:57 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2024-05-13 11:40:01 +00:00
|
|
|
"log/slog"
|
2024-05-04 15:36:57 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"time"
|
2024-07-06 18:28:44 +00:00
|
|
|
|
|
|
|
"github.com/ditatompel/xmr-remote-nodes/internal/config"
|
|
|
|
"github.com/ditatompel/xmr-remote-nodes/internal/monero"
|
2024-05-04 15:36:57 +00:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"golang.org/x/net/proxy"
|
|
|
|
)
|
|
|
|
|
2024-05-13 07:56:29 +00:00
|
|
|
const RPCUserAgent = "ditatombot/0.0.1 (Monero RPC Monitoring; https://github.com/ditatompel/xmr-remote-nodes)"
|
2024-05-04 15:36:57 +00:00
|
|
|
|
2024-05-22 17:40:13 +00:00
|
|
|
const (
|
2024-06-19 11:46:12 +00:00
|
|
|
errNoEndpoint = errProber("no SERVER_ENDPOINT was provided")
|
|
|
|
errNoTorSocks = errProber("no TOR_SOCKS was provided")
|
|
|
|
errNoAPIKey = errProber("no API_KEY was provided")
|
|
|
|
errInvalidCredentials = errProber("invalid API_KEY credentials")
|
2024-05-22 17:40:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type errProber string
|
|
|
|
|
|
|
|
func (err errProber) Error() string {
|
|
|
|
return string(err)
|
|
|
|
}
|
|
|
|
|
2024-05-04 15:36:57 +00:00
|
|
|
type proberClient struct {
|
2024-09-05 17:08:59 +00:00
|
|
|
endpoint string // server endpoint
|
|
|
|
apiKey string // prober api key
|
|
|
|
acceptTor bool // accept tor
|
|
|
|
torSOCKS string // IP:Port of tor socks
|
|
|
|
acceptIPv6 bool // accept ipv6
|
|
|
|
message string // message to include when reporting back to server
|
2024-05-04 15:36:57 +00:00
|
|
|
}
|
|
|
|
|
2024-05-22 17:40:13 +00:00
|
|
|
func newProber() *proberClient {
|
2024-06-19 09:32:40 +00:00
|
|
|
cfg := config.AppCfg()
|
|
|
|
return &proberClient{
|
2024-09-05 17:08:59 +00:00
|
|
|
endpoint: cfg.ServerEndpoint,
|
|
|
|
apiKey: cfg.APIKey,
|
|
|
|
acceptTor: cfg.AcceptTor,
|
|
|
|
torSOCKS: cfg.TorSOCKS,
|
|
|
|
acceptIPv6: cfg.IPv6Capable,
|
2024-06-19 09:32:40 +00:00
|
|
|
}
|
2024-05-04 15:36:57 +00:00
|
|
|
}
|
|
|
|
|
2024-05-18 10:59:54 +00:00
|
|
|
var ProbeCmd = &cobra.Command{
|
2024-05-04 15:36:57 +00:00
|
|
|
Use: "probe",
|
2024-05-18 10:59:54 +00:00
|
|
|
Short: "Probe remote nodes",
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2024-06-19 09:32:40 +00:00
|
|
|
prober := newProber()
|
|
|
|
if e, _ := cmd.Flags().GetString("endpoint"); e != "" {
|
|
|
|
prober.SetEndpoint(e)
|
|
|
|
}
|
|
|
|
if t, _ := cmd.Flags().GetBool("no-tor"); t {
|
|
|
|
prober.SetAcceptTor(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := prober.Run(); err != nil {
|
2024-06-19 11:46:12 +00:00
|
|
|
switch err.(type) {
|
|
|
|
case errProber:
|
|
|
|
slog.Error(fmt.Sprintf("[PROBE] %s", err.Error()))
|
|
|
|
os.Exit(1)
|
|
|
|
default:
|
|
|
|
slog.Warn(fmt.Sprintf("[PROBE] %s", err.Error()))
|
|
|
|
}
|
2024-05-22 17:40:13 +00:00
|
|
|
}
|
2024-05-04 15:36:57 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-06-19 09:32:40 +00:00
|
|
|
func (p *proberClient) SetEndpoint(endpoint string) {
|
|
|
|
p.endpoint = endpoint
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *proberClient) SetAcceptTor(acceptTor bool) {
|
|
|
|
p.acceptTor = acceptTor
|
|
|
|
}
|
|
|
|
|
2024-09-05 17:08:59 +00:00
|
|
|
func (p *proberClient) SetAcceptIPv6(acceptIPv6 bool) {
|
|
|
|
p.acceptIPv6 = acceptIPv6
|
|
|
|
}
|
|
|
|
|
2024-05-22 17:40:13 +00:00
|
|
|
// Fetch a new job from the server, fetches node info, and sends it to the server
|
2024-06-19 09:32:40 +00:00
|
|
|
func (p *proberClient) Run() error {
|
|
|
|
if err := p.validateConfig(); err != nil {
|
2024-05-22 17:40:13 +00:00
|
|
|
return err
|
2024-05-04 15:36:57 +00:00
|
|
|
}
|
|
|
|
|
2024-06-19 09:32:40 +00:00
|
|
|
node, err := p.fetchJob()
|
2024-05-04 15:36:57 +00:00
|
|
|
if err != nil {
|
2024-05-22 17:40:13 +00:00
|
|
|
return err
|
2024-05-04 15:36:57 +00:00
|
|
|
}
|
|
|
|
|
2024-06-19 09:32:40 +00:00
|
|
|
fetchNode, err := p.fetchNode(node)
|
2024-05-04 15:36:57 +00:00
|
|
|
if err != nil {
|
2024-05-22 17:40:13 +00:00
|
|
|
return err
|
2024-05-04 15:36:57 +00:00
|
|
|
}
|
2024-05-13 11:40:01 +00:00
|
|
|
slog.Debug(fmt.Sprintf("[PROBE] fetchNode: %s", prettyPrint(fetchNode)))
|
2024-05-22 17:40:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// checks if all required environment variables are set
|
2024-06-19 09:32:40 +00:00
|
|
|
func (p *proberClient) validateConfig() error {
|
|
|
|
if p.endpoint == "" {
|
2024-06-19 11:46:12 +00:00
|
|
|
return errNoEndpoint
|
|
|
|
}
|
|
|
|
if p.apiKey == "" {
|
|
|
|
return errNoAPIKey
|
2024-05-22 17:40:13 +00:00
|
|
|
}
|
2024-06-19 09:32:40 +00:00
|
|
|
if p.acceptTor && p.torSOCKS == "" {
|
2024-06-19 11:46:12 +00:00
|
|
|
return errNoTorSocks
|
2024-05-22 17:40:13 +00:00
|
|
|
}
|
2024-06-19 11:46:12 +00:00
|
|
|
|
2024-05-22 17:40:13 +00:00
|
|
|
return nil
|
2024-05-04 15:36:57 +00:00
|
|
|
}
|
|
|
|
|
2024-05-22 17:40:13 +00:00
|
|
|
// Get monero node info to fetch from the server
|
|
|
|
func (p *proberClient) fetchJob() (monero.Node, error) {
|
2024-09-05 17:08:59 +00:00
|
|
|
acceptTor := 0
|
2024-06-19 09:32:40 +00:00
|
|
|
if p.acceptTor {
|
2024-09-05 17:08:59 +00:00
|
|
|
acceptTor = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
acceptIPv6 := 0
|
|
|
|
if p.acceptIPv6 {
|
|
|
|
acceptIPv6 = 1
|
2024-05-04 15:36:57 +00:00
|
|
|
}
|
|
|
|
|
2024-05-22 15:45:38 +00:00
|
|
|
var node monero.Node
|
2024-05-04 15:36:57 +00:00
|
|
|
|
2024-09-05 17:08:59 +00:00
|
|
|
uri := fmt.Sprintf("%s/api/v1/job?accept_tor=%d&accept_ipv6=%d", p.endpoint, acceptTor, acceptIPv6)
|
2024-05-22 17:40:13 +00:00
|
|
|
slog.Info(fmt.Sprintf("[PROBE] Getting node from %s", uri))
|
2024-05-04 15:36:57 +00:00
|
|
|
|
2024-05-22 17:40:13 +00:00
|
|
|
req, err := http.NewRequest(http.MethodGet, uri, nil)
|
2024-05-04 15:36:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return node, err
|
|
|
|
}
|
2024-09-05 17:08:59 +00:00
|
|
|
|
2024-06-19 09:32:40 +00:00
|
|
|
req.Header.Add(monero.ProberAPIKey, p.apiKey)
|
2024-05-04 15:36:57 +00:00
|
|
|
req.Header.Set("User-Agent", RPCUserAgent)
|
|
|
|
|
|
|
|
client := &http.Client{}
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return node, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2024-06-19 11:46:12 +00:00
|
|
|
switch resp.StatusCode {
|
|
|
|
case 200:
|
|
|
|
break
|
|
|
|
case 401:
|
|
|
|
return node, errInvalidCredentials
|
|
|
|
default:
|
2024-05-04 15:36:57 +00:00
|
|
|
return node, fmt.Errorf("status code: %d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
response := struct {
|
2024-05-22 15:45:38 +00:00
|
|
|
Data monero.Node `json:"data"`
|
2024-05-04 15:36:57 +00:00
|
|
|
}{}
|
|
|
|
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&response)
|
|
|
|
if err != nil {
|
|
|
|
return node, err
|
|
|
|
}
|
|
|
|
|
|
|
|
node = response.Data
|
2024-05-13 11:40:01 +00:00
|
|
|
slog.Info(fmt.Sprintf("[PROBE] Got node: %s://%s:%d", node.Protocol, node.Hostname, node.Port))
|
2024-05-04 15:36:57 +00:00
|
|
|
|
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
|
2024-05-22 15:45:38 +00:00
|
|
|
func (p *proberClient) fetchNode(node monero.Node) (monero.Node, error) {
|
2024-05-04 15:36:57 +00:00
|
|
|
startTime := time.Now()
|
|
|
|
endpoint := fmt.Sprintf("%s://%s:%d/json_rpc", node.Protocol, node.Hostname, node.Port)
|
|
|
|
rpcParam := []byte(`{"jsonrpc": "2.0","id": "0","method": "get_info"}`)
|
2024-05-13 11:40:01 +00:00
|
|
|
slog.Info(fmt.Sprintf("[PROBE] Fetching node info from %s", endpoint))
|
|
|
|
slog.Debug(fmt.Sprintf("[PROBE] RPC param: %s", string(rpcParam)))
|
2024-05-04 15:36:57 +00:00
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodPost, endpoint, bytes.NewBuffer(rpcParam))
|
|
|
|
if err != nil {
|
|
|
|
return node, err
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
|
|
|
|
req.Header.Set("User-Agent", RPCUserAgent)
|
|
|
|
req.Header.Set("Origin", "https://xmr.ditatompel.com")
|
|
|
|
|
|
|
|
var client http.Client
|
2024-06-19 09:32:40 +00:00
|
|
|
if p.acceptTor && node.IsTor {
|
|
|
|
dialer, err := proxy.SOCKS5("tcp", p.torSOCKS, nil, proxy.Direct)
|
2024-05-04 15:36:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return node, err
|
|
|
|
}
|
|
|
|
dialContext := func(ctx context.Context, network, addr string) (net.Conn, error) {
|
|
|
|
return dialer.Dial(network, addr)
|
|
|
|
}
|
|
|
|
transport := &http.Transport{
|
|
|
|
DialContext: dialContext,
|
|
|
|
DisableKeepAlives: true,
|
|
|
|
}
|
|
|
|
client.Transport = transport
|
|
|
|
client.Timeout = 60 * time.Second
|
|
|
|
}
|
|
|
|
|
|
|
|
// reset the default node struct
|
|
|
|
node.IsAvailable = false
|
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
2024-05-04 18:42:47 +00:00
|
|
|
p.message = err.Error()
|
2024-05-23 22:37:27 +00:00
|
|
|
if err := p.reportResult(node, time.Since(startTime).Seconds()); err != nil {
|
|
|
|
return node, err
|
|
|
|
}
|
2024-05-04 15:36:57 +00:00
|
|
|
return node, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != 200 {
|
2024-05-04 18:42:47 +00:00
|
|
|
p.message = fmt.Sprintf("status code: %d", resp.StatusCode)
|
2024-05-23 22:37:27 +00:00
|
|
|
if err := p.reportResult(node, time.Since(startTime).Seconds()); err != nil {
|
|
|
|
return node, err
|
|
|
|
}
|
2024-05-04 18:42:47 +00:00
|
|
|
return node, errors.New(p.message)
|
2024-05-04 15:36:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2024-05-04 18:42:47 +00:00
|
|
|
p.message = err.Error()
|
2024-05-23 22:37:27 +00:00
|
|
|
if err := p.reportResult(node, time.Since(startTime).Seconds()); err != nil {
|
|
|
|
return node, err
|
|
|
|
}
|
2024-05-04 15:36:57 +00:00
|
|
|
return node, err
|
|
|
|
}
|
|
|
|
|
|
|
|
reportNode := struct {
|
2024-05-22 15:45:38 +00:00
|
|
|
monero.Node `json:"result"`
|
2024-05-04 15:36:57 +00:00
|
|
|
}{}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(body, &reportNode); err != nil {
|
2024-05-04 18:42:47 +00:00
|
|
|
p.message = err.Error()
|
2024-05-23 22:37:27 +00:00
|
|
|
if err := p.reportResult(node, time.Since(startTime).Seconds()); err != nil {
|
|
|
|
return node, err
|
|
|
|
}
|
2024-05-04 15:36:57 +00:00
|
|
|
}
|
2024-05-04 15:53:03 +00:00
|
|
|
if reportNode.Status == "OK" {
|
|
|
|
node.IsAvailable = true
|
|
|
|
}
|
2024-05-22 15:45:38 +00:00
|
|
|
node.Nettype = reportNode.Nettype
|
2024-05-04 15:36:57 +00:00
|
|
|
node.AdjustedTime = reportNode.AdjustedTime
|
|
|
|
node.DatabaseSize = reportNode.DatabaseSize
|
|
|
|
node.Difficulty = reportNode.Difficulty
|
2024-05-04 18:42:47 +00:00
|
|
|
node.Height = reportNode.Height
|
2024-05-04 15:53:03 +00:00
|
|
|
node.Version = reportNode.Version
|
|
|
|
|
2024-05-04 15:36:57 +00:00
|
|
|
if resp.Header.Get("Access-Control-Allow-Origin") == "*" || resp.Header.Get("Access-Control-Allow-Origin") == "https://xmr.ditatompel.com" {
|
2024-05-22 15:45:38 +00:00
|
|
|
node.CORSCapable = true
|
2024-05-04 15:36:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !node.IsTor {
|
|
|
|
hostIp, err := net.LookupIP(node.Hostname)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Warning: Could not resolve hostname: " + node.Hostname)
|
|
|
|
} else {
|
2024-05-22 15:45:38 +00:00
|
|
|
node.IP = hostIp[0].String()
|
2024-05-04 15:36:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sleeping 1 second to avoid too many request on host behind CloudFlare
|
|
|
|
// time.Sleep(1 * time.Second)
|
|
|
|
|
|
|
|
// check fee
|
2024-06-03 06:10:35 +00:00
|
|
|
fee, err := p.fetchFee(client, endpoint)
|
2024-05-04 15:36:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return node, err
|
|
|
|
}
|
2024-06-03 06:10:35 +00:00
|
|
|
node.EstimateFee = fee
|
2024-05-04 15:36:57 +00:00
|
|
|
|
2024-06-03 06:10:35 +00:00
|
|
|
tookTime := time.Since(startTime).Seconds()
|
|
|
|
|
|
|
|
slog.Info(fmt.Sprintf("[PROBE] Took %f seconds", tookTime))
|
|
|
|
if err := p.reportResult(node, tookTime); err != nil {
|
2024-05-04 15:36:57 +00:00
|
|
|
return node, err
|
|
|
|
}
|
2024-06-03 06:10:35 +00:00
|
|
|
return node, nil
|
|
|
|
}
|
2024-05-04 15:36:57 +00:00
|
|
|
|
2024-06-03 06:10:35 +00:00
|
|
|
// get estimate fee from remote node
|
|
|
|
func (p *proberClient) fetchFee(client http.Client, endpoint string) (uint, error) {
|
|
|
|
rpcParam := []byte(`{"jsonrpc": "2.0","id": "0","method": "get_fee_estimate"}`)
|
|
|
|
req, err := http.NewRequest(http.MethodPost, endpoint, bytes.NewBuffer(rpcParam))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2024-05-04 15:36:57 +00:00
|
|
|
}
|
2024-06-03 06:10:35 +00:00
|
|
|
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
|
|
|
|
req.Header.Set("User-Agent", RPCUserAgent)
|
2024-05-04 15:36:57 +00:00
|
|
|
|
2024-06-03 06:10:35 +00:00
|
|
|
res, err := client.Do(req)
|
2024-05-04 15:36:57 +00:00
|
|
|
if err != nil {
|
2024-06-03 06:10:35 +00:00
|
|
|
return 0, err
|
2024-05-04 15:36:57 +00:00
|
|
|
}
|
2024-06-03 06:10:35 +00:00
|
|
|
defer res.Body.Close()
|
2024-05-04 15:36:57 +00:00
|
|
|
|
2024-06-03 06:10:35 +00:00
|
|
|
if res.StatusCode != 200 {
|
|
|
|
return 0, fmt.Errorf("status code: %d", res.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := io.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
f := struct {
|
2024-05-04 15:36:57 +00:00
|
|
|
Result struct {
|
|
|
|
Fee uint `json:"fee"`
|
|
|
|
} `json:"result"`
|
|
|
|
}{}
|
|
|
|
|
2024-06-03 06:10:35 +00:00
|
|
|
if err := json.Unmarshal(body, &f); err != nil {
|
|
|
|
return 0, err
|
2024-05-04 15:36:57 +00:00
|
|
|
}
|
|
|
|
|
2024-06-03 06:10:35 +00:00
|
|
|
return f.Result.Fee, nil
|
2024-05-04 15:36:57 +00:00
|
|
|
}
|
|
|
|
|
2024-05-22 15:45:38 +00:00
|
|
|
func (p *proberClient) reportResult(node monero.Node, tookTime float64) error {
|
|
|
|
jsonData, err := json.Marshal(monero.ProbeReport{
|
2024-05-04 18:42:47 +00:00
|
|
|
TookTime: tookTime,
|
|
|
|
Message: p.message,
|
2024-06-03 15:15:32 +00:00
|
|
|
Node: node,
|
2024-05-04 18:42:47 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-06-19 09:32:40 +00:00
|
|
|
endpoint := fmt.Sprintf("%s/api/v1/job", p.endpoint)
|
2024-05-04 18:42:47 +00:00
|
|
|
req, err := http.NewRequest(http.MethodPost, endpoint, bytes.NewBuffer(jsonData))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-06-19 09:32:40 +00:00
|
|
|
req.Header.Add(monero.ProberAPIKey, p.apiKey)
|
2024-05-04 18:42:47 +00:00
|
|
|
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
|
|
|
|
req.Header.Set("User-Agent", RPCUserAgent)
|
|
|
|
|
|
|
|
client := &http.Client{Timeout: 60 * time.Second}
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
return fmt.Errorf("status code: %d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-04 15:36:57 +00:00
|
|
|
// for debug purposes
|
|
|
|
func prettyPrint(i interface{}) string {
|
|
|
|
s, _ := json.MarshalIndent(i, "", "\t")
|
|
|
|
return string(s)
|
|
|
|
}
|