mirror of
https://github.com/ditatompel/xmr-remote-nodes.git
synced 2025-01-08 05:52:10 +07:00
Adding some const error in client probe cmd
This commit is contained in:
parent
d44cea7e7c
commit
c6efceb0ac
1 changed files with 46 additions and 28 deletions
|
@ -21,52 +21,70 @@ import (
|
||||||
|
|
||||||
const RPCUserAgent = "ditatombot/0.0.1 (Monero RPC Monitoring; https://github.com/ditatompel/xmr-remote-nodes)"
|
const RPCUserAgent = "ditatombot/0.0.1 (Monero RPC Monitoring; https://github.com/ditatompel/xmr-remote-nodes)"
|
||||||
|
|
||||||
type proberClient struct {
|
const (
|
||||||
config *config.App
|
errEnvNoEndpoint = errProber("please set SERVER_ENDPOINT in .env")
|
||||||
message string
|
errEnvNoTorSocks = errProber("please set TOR_SOCKS in .env")
|
||||||
|
)
|
||||||
|
|
||||||
|
type errProber string
|
||||||
|
|
||||||
|
func (err errProber) Error() string {
|
||||||
|
return string(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func newProber(cfg *config.App) *proberClient {
|
type proberClient struct {
|
||||||
return &proberClient{config: cfg}
|
config *config.App
|
||||||
|
message string // message to include when reporting back to server
|
||||||
|
}
|
||||||
|
|
||||||
|
func newProber() *proberClient {
|
||||||
|
return &proberClient{config: config.AppCfg()}
|
||||||
}
|
}
|
||||||
|
|
||||||
var ProbeCmd = &cobra.Command{
|
var ProbeCmd = &cobra.Command{
|
||||||
Use: "probe",
|
Use: "probe",
|
||||||
Short: "Probe remote nodes",
|
Short: "Probe remote nodes",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
RunProbe()
|
if err := RunProber(); err != nil {
|
||||||
|
slog.Error(fmt.Sprintf("[PROBE] %s", err.Error()))
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func RunProbe() {
|
// Fetch a new job from the server, fetches node info, and sends it to the server
|
||||||
cfg := config.AppCfg()
|
func RunProber() error {
|
||||||
if cfg.ServerEndpoint == "" {
|
if err := validateConfig(); err != nil {
|
||||||
fmt.Println("Please set SERVER_ENDPOINT in .env")
|
return err
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
prober := newProber()
|
||||||
|
|
||||||
if cfg.AcceptTor && cfg.TorSocks == "" {
|
node, err := prober.fetchJob()
|
||||||
fmt.Println("Please set TOR_SOCKS in .env")
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
probe := newProber(cfg)
|
|
||||||
|
|
||||||
node, err := probe.getJob()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error(fmt.Sprintf("[PROBE] getJob: %s", err.Error()))
|
return err
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fetchNode, err := probe.fetchNode(node)
|
fetchNode, err := prober.fetchNode(node)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error(fmt.Sprintf("[PROBE] fetchNode: %s", err.Error()))
|
return err
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
slog.Debug(fmt.Sprintf("[PROBE] fetchNode: %s", prettyPrint(fetchNode)))
|
slog.Debug(fmt.Sprintf("[PROBE] fetchNode: %s", prettyPrint(fetchNode)))
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *proberClient) getJob() (monero.Node, error) {
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get monero node info to fetch from the server
|
||||||
|
func (p *proberClient) fetchJob() (monero.Node, error) {
|
||||||
queryParams := ""
|
queryParams := ""
|
||||||
if p.config.AcceptTor {
|
if p.config.AcceptTor {
|
||||||
queryParams = "?accept_tor=1"
|
queryParams = "?accept_tor=1"
|
||||||
|
@ -74,10 +92,10 @@ func (p *proberClient) getJob() (monero.Node, error) {
|
||||||
|
|
||||||
var node monero.Node
|
var node monero.Node
|
||||||
|
|
||||||
endpoint := fmt.Sprintf("%s/api/v1/job%s", p.config.ServerEndpoint, queryParams)
|
uri := fmt.Sprintf("%s/api/v1/job%s", p.config.ServerEndpoint, queryParams)
|
||||||
slog.Info(fmt.Sprintf("[PROBE] Getting node from %s", endpoint))
|
slog.Info(fmt.Sprintf("[PROBE] Getting node from %s", uri))
|
||||||
|
|
||||||
req, err := http.NewRequest(http.MethodGet, endpoint, nil)
|
req, err := http.NewRequest(http.MethodGet, uri, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return node, err
|
return node, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue