mirror of
https://github.com/ditatompel/xmr-remote-nodes.git
synced 2025-01-08 05:52:10 +07:00
Compare commits
5 commits
e9cacb478c
...
2576d53c35
Author | SHA1 | Date | |
---|---|---|---|
2576d53c35 | |||
aa8ecc6b61 | |||
0321006eb3 | |||
c0885f5e5a | |||
3f5c0c9472 |
5 changed files with 80 additions and 34 deletions
|
@ -6,18 +6,18 @@ LOG_LEVEL=INFO # can be DEBUG, INFO, WARN, ERROR
|
|||
# #############
|
||||
SERVER_ENDPOINT="http://127.0.0.1:18901"
|
||||
API_KEY=
|
||||
ACCEPT_TOR=true
|
||||
ACCEPT_TOR=false
|
||||
TOR_SOCKS="127.0.0.1:9050"
|
||||
|
||||
# Server Config
|
||||
# #############
|
||||
|
||||
# Fiber Config
|
||||
APP_PREFORK=true
|
||||
APP_PREFORK=false
|
||||
APP_HOST="127.0.0.1"
|
||||
APP_PORT=18090
|
||||
APP_PROXY_HEADER="X-Real-Ip" # CF-Connecting-IP
|
||||
APP_ALLOW_ORIGIN="http://localhost:5173,http://127.0.0.1:5173,https://ditatompel.com"
|
||||
APP_PROXY_HEADER="X-Real-Ip" # `CF-Connecting-IP` if using Cloudflare
|
||||
APP_ALLOW_ORIGIN="http://localhost:5173,http://127.0.0.1:5173,https://xmr.ditatompel.com"
|
||||
|
||||
#DB settings:
|
||||
DB_HOST=127.0.0.1
|
||||
|
|
|
@ -1,3 +1,7 @@
|
|||
linters-settings:
|
||||
errcheck:
|
||||
ignore: ""
|
||||
|
||||
issues:
|
||||
exclude-rules:
|
||||
- path: frontend/embed.go
|
||||
|
|
|
@ -22,8 +22,10 @@ import (
|
|||
const RPCUserAgent = "ditatombot/0.0.1 (Monero RPC Monitoring; https://github.com/ditatompel/xmr-remote-nodes)"
|
||||
|
||||
const (
|
||||
errEnvNoEndpoint = errProber("please set SERVER_ENDPOINT in .env")
|
||||
errEnvNoTorSocks = errProber("please set TOR_SOCKS in .env")
|
||||
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")
|
||||
)
|
||||
|
||||
type errProber string
|
||||
|
@ -33,38 +35,67 @@ func (err errProber) Error() string {
|
|||
}
|
||||
|
||||
type proberClient struct {
|
||||
config *config.App
|
||||
endpoint string // server endpoint
|
||||
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 {
|
||||
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{
|
||||
Use: "probe",
|
||||
Short: "Probe remote nodes",
|
||||
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 {
|
||||
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()))
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
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
|
||||
func RunProber() error {
|
||||
if err := validateConfig(); err != nil {
|
||||
func (p *proberClient) Run() error {
|
||||
if err := p.validateConfig(); err != nil {
|
||||
return err
|
||||
}
|
||||
prober := newProber()
|
||||
|
||||
node, err := prober.fetchJob()
|
||||
node, err := p.fetchJob()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fetchNode, err := prober.fetchNode(node)
|
||||
fetchNode, err := p.fetchNode(node)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -73,33 +104,37 @@ func RunProber() error {
|
|||
}
|
||||
|
||||
// checks if all required environment variables are set
|
||||
func validateConfig() error {
|
||||
if config.AppCfg().ServerEndpoint == "" {
|
||||
return errEnvNoEndpoint
|
||||
func (p *proberClient) validateConfig() error {
|
||||
if p.endpoint == "" {
|
||||
return errNoEndpoint
|
||||
}
|
||||
if config.AppCfg().AcceptTor && config.AppCfg().TorSocks == "" {
|
||||
return errEnvNoTorSocks
|
||||
if p.apiKey == "" {
|
||||
return errNoAPIKey
|
||||
}
|
||||
if p.acceptTor && p.torSOCKS == "" {
|
||||
return errNoTorSocks
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get monero node info to fetch from the server
|
||||
func (p *proberClient) fetchJob() (monero.Node, error) {
|
||||
queryParams := ""
|
||||
if p.config.AcceptTor {
|
||||
if p.acceptTor {
|
||||
queryParams = "?accept_tor=1"
|
||||
}
|
||||
|
||||
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))
|
||||
|
||||
req, err := http.NewRequest(http.MethodGet, uri, nil)
|
||||
if err != nil {
|
||||
return node, err
|
||||
}
|
||||
req.Header.Add(monero.ProberAPIKey, p.config.ApiKey)
|
||||
req.Header.Add(monero.ProberAPIKey, p.apiKey)
|
||||
req.Header.Set("User-Agent", RPCUserAgent)
|
||||
|
||||
client := &http.Client{}
|
||||
|
@ -109,7 +144,12 @@ func (p *proberClient) fetchJob() (monero.Node, error) {
|
|||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
switch resp.StatusCode {
|
||||
case 200:
|
||||
break
|
||||
case 401:
|
||||
return node, errInvalidCredentials
|
||||
default:
|
||||
return node, fmt.Errorf("status code: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
|
@ -144,8 +184,8 @@ func (p *proberClient) fetchNode(node monero.Node) (monero.Node, error) {
|
|||
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 p.acceptTor && node.IsTor {
|
||||
dialer, err := proxy.SOCKS5("tcp", p.torSOCKS, nil, proxy.Direct)
|
||||
if err != nil {
|
||||
return node, err
|
||||
}
|
||||
|
@ -290,12 +330,12 @@ func (p *proberClient) reportResult(node monero.Node, tookTime float64) error {
|
|||
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))
|
||||
if err != nil {
|
||||
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("User-Agent", RPCUserAgent)
|
||||
|
||||
|
|
|
@ -29,6 +29,8 @@ func init() {
|
|||
cobra.OnInitialize(initConfig)
|
||||
Root.PersistentFlags().StringVarP(&configFile, "config-file", "c", "", "Default to .env")
|
||||
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() {
|
||||
|
|
|
@ -19,9 +19,9 @@ type App struct {
|
|||
|
||||
// configuration for prober (client)
|
||||
ServerEndpoint string
|
||||
ApiKey string
|
||||
APIKey string
|
||||
AcceptTor bool
|
||||
TorSocks string
|
||||
TorSOCKS string
|
||||
}
|
||||
|
||||
var app = &App{}
|
||||
|
@ -54,7 +54,7 @@ func LoadApp() {
|
|||
|
||||
// prober configuration
|
||||
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.TorSocks = os.Getenv("TOR_SOCKS")
|
||||
app.TorSOCKS = os.Getenv("TOR_SOCKS")
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue