mirror of
https://github.com/ditatompel/xmr-remote-nodes.git
synced 2025-01-19 17:24:32 +00:00
5496692c5d
I hope it will be less discoverable by other users and less likely to be used unintentionally in other projects.
96 lines
2.2 KiB
Go
96 lines
2.2 KiB
Go
package cmd
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"xmr-remote-nodes/internal/database"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
type importClient struct {
|
|
db *database.DB
|
|
}
|
|
|
|
func newImport(db *database.DB) *importClient {
|
|
return &importClient{db: db}
|
|
}
|
|
|
|
type importData struct {
|
|
Hostname string `json:"hostname"`
|
|
Port int `json:"port"`
|
|
Protocol string `json:"protocol"`
|
|
IsTor bool `json:"is_tor"`
|
|
}
|
|
|
|
var importCmd = &cobra.Command{
|
|
Use: "import",
|
|
Short: "Import Monero nodes from old API",
|
|
Long: `Import Monero nodes from old API.
|
|
This command only available during migration process and will be removed in future versions.`,
|
|
Run: func(_ *cobra.Command, _ []string) {
|
|
if err := database.ConnectDB(); err != nil {
|
|
panic(err)
|
|
}
|
|
req, err := http.NewRequest(http.MethodGet, "https://api.ditatompel.com/monero/remote-node?limit=500", nil)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
client := &http.Client{}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != 200 {
|
|
panic(fmt.Errorf("status code: %d", resp.StatusCode))
|
|
}
|
|
|
|
response := struct {
|
|
Data []importData `json:"data"`
|
|
}{}
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&response)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
action := newImport(database.GetDB())
|
|
|
|
for _, node := range response.Data {
|
|
action.processData(node)
|
|
}
|
|
|
|
fmt.Println("Total Source Data: ", len(response.Data))
|
|
|
|
fmt.Println("Done!")
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(importCmd)
|
|
}
|
|
|
|
func (i *importClient) processData(node importData) error {
|
|
query := `SELECT id FROM tbl_node WHERE hostname = ? AND port = ? AND protocol = ?`
|
|
row := i.db.QueryRow(query, node.Hostname, node.Port, node.Protocol)
|
|
var id int
|
|
err := row.Scan(&id)
|
|
if err == nil {
|
|
// fmt.Printf("Skipping %s://%s:%d\n", node.Protocol, node.Hostname, node.Port)
|
|
return fmt.Errorf("node already exists")
|
|
}
|
|
|
|
// insert
|
|
query = `INSERT INTO tbl_node (hostname, port, protocol, is_tor, nettype, ip_addr, last_check_status) VALUES (?, ?, ?, ?, ?, ?, ?)`
|
|
_, err = i.db.Exec(query, node.Hostname, node.Port, node.Protocol, node.IsTor, "", "", "[2,2,2,2,2]")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
fmt.Printf("Imported %s://%s:%d\n", node.Protocol, node.Hostname, node.Port)
|
|
|
|
return nil
|
|
}
|