mirror of
https://github.com/ditatompel/xmr-remote-nodes.git
synced 2025-01-08 05:52:10 +07:00
Compare commits
No commits in common. "2576d53c35dd6f99ae592eeaff140e77ac47f9d0" and "e9cacb478c8e2b30a84341d8e9a3c615212fbbc7" have entirely different histories.
2576d53c35
...
e9cacb478c
5 changed files with 34 additions and 80 deletions
|
@ -6,18 +6,18 @@ LOG_LEVEL=INFO # can be DEBUG, INFO, WARN, ERROR
|
||||||
# #############
|
# #############
|
||||||
SERVER_ENDPOINT="http://127.0.0.1:18901"
|
SERVER_ENDPOINT="http://127.0.0.1:18901"
|
||||||
API_KEY=
|
API_KEY=
|
||||||
ACCEPT_TOR=false
|
ACCEPT_TOR=true
|
||||||
TOR_SOCKS="127.0.0.1:9050"
|
TOR_SOCKS="127.0.0.1:9050"
|
||||||
|
|
||||||
# Server Config
|
# Server Config
|
||||||
# #############
|
# #############
|
||||||
|
|
||||||
# Fiber Config
|
# Fiber Config
|
||||||
APP_PREFORK=false
|
APP_PREFORK=true
|
||||||
APP_HOST="127.0.0.1"
|
APP_HOST="127.0.0.1"
|
||||||
APP_PORT=18090
|
APP_PORT=18090
|
||||||
APP_PROXY_HEADER="X-Real-Ip" # `CF-Connecting-IP` if using Cloudflare
|
APP_PROXY_HEADER="X-Real-Ip" # CF-Connecting-IP
|
||||||
APP_ALLOW_ORIGIN="http://localhost:5173,http://127.0.0.1:5173,https://xmr.ditatompel.com"
|
APP_ALLOW_ORIGIN="http://localhost:5173,http://127.0.0.1:5173,https://ditatompel.com"
|
||||||
|
|
||||||
#DB settings:
|
#DB settings:
|
||||||
DB_HOST=127.0.0.1
|
DB_HOST=127.0.0.1
|
||||||
|
|
|
@ -1,7 +1,3 @@
|
||||||
linters-settings:
|
|
||||||
errcheck:
|
|
||||||
ignore: ""
|
|
||||||
|
|
||||||
issues:
|
issues:
|
||||||
exclude-rules:
|
exclude-rules:
|
||||||
- path: frontend/embed.go
|
- path: frontend/embed.go
|
||||||
|
|
|
@ -22,10 +22,8 @@ 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)"
|
||||||
|
|
||||||
const (
|
const (
|
||||||
errNoEndpoint = errProber("no SERVER_ENDPOINT was provided")
|
errEnvNoEndpoint = errProber("please set SERVER_ENDPOINT in .env")
|
||||||
errNoTorSocks = errProber("no TOR_SOCKS was provided")
|
errEnvNoTorSocks = errProber("please set TOR_SOCKS in .env")
|
||||||
errNoAPIKey = errProber("no API_KEY was provided")
|
|
||||||
errInvalidCredentials = errProber("invalid API_KEY credentials")
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type errProber string
|
type errProber string
|
||||||
|
@ -35,67 +33,38 @@ func (err errProber) Error() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
type proberClient struct {
|
type proberClient struct {
|
||||||
endpoint string // server endpoint
|
config *config.App
|
||||||
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
|
message string // message to include when reporting back to server
|
||||||
}
|
}
|
||||||
|
|
||||||
func newProber() *proberClient {
|
func newProber() *proberClient {
|
||||||
cfg := config.AppCfg()
|
return &proberClient{config: 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) {
|
||||||
prober := newProber()
|
if err := RunProber(); err != nil {
|
||||||
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 {
|
|
||||||
switch err.(type) {
|
|
||||||
case errProber:
|
|
||||||
slog.Error(fmt.Sprintf("[PROBE] %s", err.Error()))
|
slog.Error(fmt.Sprintf("[PROBE] %s", err.Error()))
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
default:
|
|
||||||
slog.Warn(fmt.Sprintf("[PROBE] %s", err.Error()))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
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 (p *proberClient) Run() error {
|
func RunProber() error {
|
||||||
if err := p.validateConfig(); err != nil {
|
if err := validateConfig(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
prober := newProber()
|
||||||
|
|
||||||
node, err := p.fetchJob()
|
node, err := prober.fetchJob()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
fetchNode, err := p.fetchNode(node)
|
fetchNode, err := prober.fetchNode(node)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -104,37 +73,33 @@ func (p *proberClient) Run() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// checks if all required environment variables are set
|
// checks if all required environment variables are set
|
||||||
func (p *proberClient) validateConfig() error {
|
func validateConfig() error {
|
||||||
if p.endpoint == "" {
|
if config.AppCfg().ServerEndpoint == "" {
|
||||||
return errNoEndpoint
|
return errEnvNoEndpoint
|
||||||
}
|
}
|
||||||
if p.apiKey == "" {
|
if config.AppCfg().AcceptTor && config.AppCfg().TorSocks == "" {
|
||||||
return errNoAPIKey
|
return errEnvNoTorSocks
|
||||||
}
|
}
|
||||||
if p.acceptTor && p.torSOCKS == "" {
|
|
||||||
return errNoTorSocks
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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.acceptTor {
|
if p.config.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.endpoint, queryParams)
|
uri := fmt.Sprintf("%s/api/v1/job%s", p.config.ServerEndpoint, 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.apiKey)
|
req.Header.Add(monero.ProberAPIKey, p.config.ApiKey)
|
||||||
req.Header.Set("User-Agent", RPCUserAgent)
|
req.Header.Set("User-Agent", RPCUserAgent)
|
||||||
|
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
|
@ -144,12 +109,7 @@ func (p *proberClient) fetchJob() (monero.Node, error) {
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
switch resp.StatusCode {
|
if resp.StatusCode != 200 {
|
||||||
case 200:
|
|
||||||
break
|
|
||||||
case 401:
|
|
||||||
return node, errInvalidCredentials
|
|
||||||
default:
|
|
||||||
return node, fmt.Errorf("status code: %d", resp.StatusCode)
|
return node, fmt.Errorf("status code: %d", resp.StatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -184,8 +144,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.acceptTor && node.IsTor {
|
if p.config.AcceptTor && node.IsTor {
|
||||||
dialer, err := proxy.SOCKS5("tcp", p.torSOCKS, nil, proxy.Direct)
|
dialer, err := proxy.SOCKS5("tcp", p.config.TorSocks, nil, proxy.Direct)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return node, err
|
return node, err
|
||||||
}
|
}
|
||||||
|
@ -330,12 +290,12 @@ func (p *proberClient) reportResult(node monero.Node, tookTime float64) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
endpoint := fmt.Sprintf("%s/api/v1/job", p.endpoint)
|
endpoint := fmt.Sprintf("%s/api/v1/job", p.config.ServerEndpoint)
|
||||||
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.apiKey)
|
req.Header.Add(monero.ProberAPIKey, p.config.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,8 +29,6 @@ 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() {
|
||||||
|
|
|
@ -19,9 +19,9 @@ type App struct {
|
||||||
|
|
||||||
// configuration for prober (client)
|
// configuration for prober (client)
|
||||||
ServerEndpoint string
|
ServerEndpoint string
|
||||||
APIKey string
|
ApiKey string
|
||||||
AcceptTor bool
|
AcceptTor bool
|
||||||
TorSOCKS string
|
TorSocks string
|
||||||
}
|
}
|
||||||
|
|
||||||
var app = &App{}
|
var app = &App{}
|
||||||
|
@ -54,7 +54,7 @@ func LoadApp() {
|
||||||
|
|
||||||
// prober configuration
|
// prober configuration
|
||||||
app.ServerEndpoint = os.Getenv("SERVER_ENDPOINT")
|
app.ServerEndpoint = os.Getenv("SERVER_ENDPOINT")
|
||||||
app.APIKey = os.Getenv("API_KEY")
|
app.ApiKey = os.Getenv("API_KEY")
|
||||||
app.AcceptTor, _ = strconv.ParseBool(os.Getenv("ACCEPT_TOR"))
|
app.AcceptTor, _ = strconv.ParseBool(os.Getenv("ACCEPT_TOR"))
|
||||||
app.TorSOCKS = os.Getenv("TOR_SOCKS")
|
app.TorSocks = os.Getenv("TOR_SOCKS")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue