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-05-08 14:35:04 +00:00
|
|
|
"xmr-remote-nodes/internal/config"
|
2024-05-22 15:45:38 +00:00
|
|
|
"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 (
|
|
|
|
errEnvNoEndpoint = errProber("please set SERVER_ENDPOINT in .env")
|
|
|
|
errEnvNoTorSocks = errProber("please set TOR_SOCKS in .env")
|
|
|
|
)
|
|
|
|
|
|
|
|
type errProber string
|
|
|
|
|
|
|
|
func (err errProber) Error() string {
|
|
|
|
return string(err)
|
|
|
|
}
|
|
|
|
|
2024-05-04 15:36:57 +00:00
|
|
|
type proberClient struct {
|
2024-05-04 18:42:47 +00:00
|
|
|
config *config.App
|
2024-05-22 17:40:13 +00:00
|
|
|
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 {
|
|
|
|
return &proberClient{config: config.AppCfg()}
|
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-05-22 17:40:13 +00:00
|
|
|
if err := RunProber(); err != nil {
|
|
|
|
slog.Error(fmt.Sprintf("[PROBE] %s", err.Error()))
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2024-05-04 15:36:57 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-05-22 17:40:13 +00:00
|
|
|
// Fetch a new job from the server, fetches node info, and sends it to the server
|
|
|
|
func RunProber() error {
|
|
|
|
if err := validateConfig(); err != nil {
|
|
|
|
return err
|
2024-05-04 15:36:57 +00:00
|
|
|
}
|
2024-05-22 17:40:13 +00:00
|
|
|
prober := newProber()
|
2024-05-04 15:36:57 +00:00
|
|
|
|
2024-05-22 17:40:13 +00:00
|
|
|
node, err := prober.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-05-22 17:40:13 +00:00
|
|
|
fetchNode, err := prober.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
|
|
|
|
func validateConfig() error {
|
|
|
|
if config.AppCfg().ServerEndpoint == "" {
|
|
|
|
return errEnvNoEndpoint
|
|
|
|
}
|
|
|
|
if config.AppCfg().AcceptTor && config.AppCfg().TorSocks == "" {
|
|
|
|
return errEnvNoTorSocks
|
|
|
|
}
|
|
|
|
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-05-04 15:36:57 +00:00
|
|
|
queryParams := ""
|
2024-05-06 15:53:23 +00:00
|
|
|
if p.config.AcceptTor {
|
|
|
|
queryParams = "?accept_tor=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-05-22 17:40:13 +00:00
|
|
|
uri := fmt.Sprintf("%s/api/v1/job%s", p.config.ServerEndpoint, queryParams)
|
|
|
|
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-05-30 06:40:57 +00:00
|
|
|
req.Header.Add(monero.ProberAPIKey, p.config.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()
|
|
|
|
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
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
|
|
|
|
if p.config.AcceptTor && node.IsTor {
|
|
|
|
dialer, err := proxy.SOCKS5("tcp", p.config.TorSocks, nil, proxy.Direct)
|
|
|
|
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,
|
|
|
|
NodeInfo: node,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoint := fmt.Sprintf("%s/api/v1/job", p.config.ServerEndpoint)
|
|
|
|
req, err := http.NewRequest(http.MethodPost, endpoint, bytes.NewBuffer(jsonData))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-05-30 06:40:57 +00:00
|
|
|
req.Header.Add(monero.ProberAPIKey, p.config.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)
|
|
|
|
}
|