mirror of
https://github.com/ditatompel/xmr-remote-nodes.git
synced 2025-01-08 05:52:10 +07:00
feat: Allow user to specify endpoint from CLI flag
`--no-tor` also added to `probe` CLI flags to force probing clearnet nodes only.
This commit is contained in:
parent
c0885f5e5a
commit
0321006eb3
2 changed files with 45 additions and 19 deletions
|
@ -33,38 +33,62 @@ func (err errProber) Error() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
type proberClient struct {
|
type proberClient struct {
|
||||||
config *config.App
|
endpoint string // server endpoint
|
||||||
message string // message to include when reporting back to server
|
apiKey string // prober api key
|
||||||
|
acceptTor bool // accept tor
|
||||||
|
torSOCKS string // IP:Port of tor socks
|
||||||
|
message string // message to include when reporting back to server
|
||||||
}
|
}
|
||||||
|
|
||||||
func newProber() *proberClient {
|
func newProber() *proberClient {
|
||||||
return &proberClient{config: config.AppCfg()}
|
cfg := config.AppCfg()
|
||||||
|
return &proberClient{
|
||||||
|
endpoint: cfg.ServerEndpoint,
|
||||||
|
apiKey: cfg.APIKey,
|
||||||
|
acceptTor: cfg.AcceptTor,
|
||||||
|
torSOCKS: cfg.TorSOCKS,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
if err := RunProber(); err != nil {
|
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 {
|
||||||
slog.Error(fmt.Sprintf("[PROBE] %s", err.Error()))
|
slog.Error(fmt.Sprintf("[PROBE] %s", err.Error()))
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *proberClient) SetEndpoint(endpoint string) {
|
||||||
|
p.endpoint = endpoint
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *proberClient) SetAcceptTor(acceptTor bool) {
|
||||||
|
p.acceptTor = acceptTor
|
||||||
|
}
|
||||||
|
|
||||||
// Fetch a new job from the server, fetches node info, and sends it to the server
|
// Fetch a new job from the server, fetches node info, and sends it to the server
|
||||||
func RunProber() error {
|
func (p *proberClient) Run() error {
|
||||||
if err := validateConfig(); err != nil {
|
if err := p.validateConfig(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
prober := newProber()
|
|
||||||
|
|
||||||
node, err := prober.fetchJob()
|
node, err := p.fetchJob()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
fetchNode, err := prober.fetchNode(node)
|
fetchNode, err := p.fetchNode(node)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -73,11 +97,11 @@ func RunProber() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// checks if all required environment variables are set
|
// checks if all required environment variables are set
|
||||||
func validateConfig() error {
|
func (p *proberClient) validateConfig() error {
|
||||||
if config.AppCfg().ServerEndpoint == "" {
|
if p.endpoint == "" {
|
||||||
return errEnvNoEndpoint
|
return errEnvNoEndpoint
|
||||||
}
|
}
|
||||||
if config.AppCfg().AcceptTor && config.AppCfg().TorSocks == "" {
|
if p.acceptTor && p.torSOCKS == "" {
|
||||||
return errEnvNoTorSocks
|
return errEnvNoTorSocks
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -86,20 +110,20 @@ func validateConfig() error {
|
||||||
// Get monero node info to fetch from the server
|
// Get monero node info to fetch from the server
|
||||||
func (p *proberClient) fetchJob() (monero.Node, error) {
|
func (p *proberClient) fetchJob() (monero.Node, error) {
|
||||||
queryParams := ""
|
queryParams := ""
|
||||||
if p.config.AcceptTor {
|
if p.acceptTor {
|
||||||
queryParams = "?accept_tor=1"
|
queryParams = "?accept_tor=1"
|
||||||
}
|
}
|
||||||
|
|
||||||
var node monero.Node
|
var node monero.Node
|
||||||
|
|
||||||
uri := fmt.Sprintf("%s/api/v1/job%s", p.config.ServerEndpoint, queryParams)
|
uri := fmt.Sprintf("%s/api/v1/job%s", p.endpoint, queryParams)
|
||||||
slog.Info(fmt.Sprintf("[PROBE] Getting node from %s", uri))
|
slog.Info(fmt.Sprintf("[PROBE] Getting node from %s", uri))
|
||||||
|
|
||||||
req, err := http.NewRequest(http.MethodGet, uri, nil)
|
req, err := http.NewRequest(http.MethodGet, uri, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return node, err
|
return node, err
|
||||||
}
|
}
|
||||||
req.Header.Add(monero.ProberAPIKey, p.config.ApiKey)
|
req.Header.Add(monero.ProberAPIKey, p.apiKey)
|
||||||
req.Header.Set("User-Agent", RPCUserAgent)
|
req.Header.Set("User-Agent", RPCUserAgent)
|
||||||
|
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
|
@ -144,8 +168,8 @@ func (p *proberClient) fetchNode(node monero.Node) (monero.Node, error) {
|
||||||
req.Header.Set("Origin", "https://xmr.ditatompel.com")
|
req.Header.Set("Origin", "https://xmr.ditatompel.com")
|
||||||
|
|
||||||
var client http.Client
|
var client http.Client
|
||||||
if p.config.AcceptTor && node.IsTor {
|
if p.acceptTor && node.IsTor {
|
||||||
dialer, err := proxy.SOCKS5("tcp", p.config.TorSocks, nil, proxy.Direct)
|
dialer, err := proxy.SOCKS5("tcp", p.torSOCKS, nil, proxy.Direct)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return node, err
|
return node, err
|
||||||
}
|
}
|
||||||
|
@ -290,12 +314,12 @@ func (p *proberClient) reportResult(node monero.Node, tookTime float64) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
endpoint := fmt.Sprintf("%s/api/v1/job", p.config.ServerEndpoint)
|
endpoint := fmt.Sprintf("%s/api/v1/job", p.endpoint)
|
||||||
req, err := http.NewRequest(http.MethodPost, endpoint, bytes.NewBuffer(jsonData))
|
req, err := http.NewRequest(http.MethodPost, endpoint, bytes.NewBuffer(jsonData))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
req.Header.Add(monero.ProberAPIKey, p.config.ApiKey)
|
req.Header.Add(monero.ProberAPIKey, p.apiKey)
|
||||||
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
|
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
|
||||||
req.Header.Set("User-Agent", RPCUserAgent)
|
req.Header.Set("User-Agent", RPCUserAgent)
|
||||||
|
|
||||||
|
|
|
@ -29,6 +29,8 @@ func init() {
|
||||||
cobra.OnInitialize(initConfig)
|
cobra.OnInitialize(initConfig)
|
||||||
Root.PersistentFlags().StringVarP(&configFile, "config-file", "c", "", "Default to .env")
|
Root.PersistentFlags().StringVarP(&configFile, "config-file", "c", "", "Default to .env")
|
||||||
Root.AddCommand(client.ProbeCmd)
|
Root.AddCommand(client.ProbeCmd)
|
||||||
|
client.ProbeCmd.Flags().StringP("endpoint", "e", "", "Server endpoint")
|
||||||
|
client.ProbeCmd.Flags().Bool("no-tor", false, "Only probe clearnet nodes")
|
||||||
}
|
}
|
||||||
|
|
||||||
func initConfig() {
|
func initConfig() {
|
||||||
|
|
Loading…
Reference in a new issue