From 5079a84bf47924c4bf1fa50bfcdd44f3ccbb8ba0 Mon Sep 17 00:00:00 2001 From: Chris Marslender Date: Mon, 9 Sep 2024 17:16:39 -0500 Subject: [PATCH] Generate certs + config if it doesn't already exist --- cmd/crawl.go | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 4 ++-- go.sum | 8 +++---- 3 files changed, 73 insertions(+), 6 deletions(-) diff --git a/cmd/crawl.go b/cmd/crawl.go index a345206..728fa5e 100644 --- a/cmd/crawl.go +++ b/cmd/crawl.go @@ -12,8 +12,10 @@ import ( "sync" "time" + "github.com/chia-network/go-chia-libs/pkg/config" "github.com/chia-network/go-chia-libs/pkg/peerprotocol" "github.com/chia-network/go-chia-libs/pkg/protocols" + "github.com/chia-network/go-chia-libs/pkg/tls" wrappedPrometheus "github.com/chia-network/go-modules/pkg/prometheus" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" @@ -21,6 +23,7 @@ import ( "github.com/spf13/cobra" "github.com/spf13/viper" "gopkg.in/go-playground/pool.v3" + "gopkg.in/yaml.v3" ) const poolSize = 1000 @@ -53,6 +56,9 @@ var crawlCmd = &cobra.Command{ Use: "crawl", Short: "Runs the crawler and optional metrics server", Run: func(cmd *cobra.Command, args []string) { + // Checking for certs/chia root/etc + ensureChiaRoot() + // All IPs we know about, and the best timestamp we've seen (from us or a peer) // map[ip]timestamp hostTimestamps = map[string]uint64{} @@ -153,6 +159,64 @@ func init() { rootCmd.AddCommand(crawlCmd) } +func ensureChiaRoot() { + chiaRoot, err := config.GetChiaRootPath() + if err != nil { + log.Fatalln(err.Error()) + } + + // Check if the directory exists + _, err = os.Stat(chiaRoot) + if os.IsNotExist(err) { + // Directory doesn't exist, create it + err = os.MkdirAll(chiaRoot, os.ModePerm) + if err != nil { + log.Fatalln("Failed to create chia root directory:", err.Error()) + } + } + + // Generate certs if the config/ssl directory doesn't exist + sslDir := path.Join(chiaRoot, "config", "ssl") + _, err = os.Stat(sslDir) + if os.IsNotExist(err) { + // Directory doesn't exist, generate the certs + err = tls.GenerateAllCerts(sslDir) + if err != nil { + log.Fatalln("Error generating chia certs:", err.Error()) + } + } else if err != nil { + log.Fatalln("Error checking ssl directory:", err.Error()) + } + + configPath := path.Join(chiaRoot, "config", "config.yaml") + + // Check if config.yaml exists + _, err = os.Stat(configPath) + if os.IsNotExist(err) { + // Config file doesn't exist, create it + cfg, err := config.LoadDefaultConfig() + if err != nil { + log.Fatalln("Error loading default config:", err.Error()) + } + + // Marshal the config struct to YAML + data, err := yaml.Marshal(cfg) + if err != nil { + log.Fatalln("Error marshalling config to YAML:", err.Error()) + } + + // Write the YAML data to config.yaml + err = os.WriteFile(configPath, data, 0644) + if err != nil { + log.Fatalln("Error writing config.yaml:", err.Error()) + } + + log.Println("Config.yaml successfully created") + } else if err != nil { + log.Fatalln("Error checking config.yaml:", err.Error()) + } +} + func stats() { last5Days := 0 v6count := 0 @@ -276,6 +340,9 @@ func processPeers(host string, bar *progressbar.ProgressBar) pool.WorkFunc { if bar != nil { _ = bar.Add(1) } + if err != nil { + log.Println(err.Error()) + } return nil, err } } diff --git a/go.mod b/go.mod index d56d79c..944affc 100644 --- a/go.mod +++ b/go.mod @@ -3,9 +3,9 @@ module github.com/cmmarslender/go-chia-crawler go 1.21 require ( - github.com/chia-network/go-chia-libs v0.8.6 + github.com/chia-network/go-chia-libs v0.9.1 github.com/chia-network/go-modules v0.0.5 - github.com/prometheus/client_golang v1.20.2 + github.com/prometheus/client_golang v1.20.3 github.com/schollz/progressbar/v3 v3.14.6 github.com/spf13/cobra v1.8.1 github.com/spf13/viper v1.19.0 diff --git a/go.sum b/go.sum index e5eb18f..b9df3eb 100644 --- a/go.sum +++ b/go.sum @@ -2,8 +2,8 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= -github.com/chia-network/go-chia-libs v0.8.6 h1:7EzDO/TiQD3L981a28ArbsKb621vOVsml5kkpFitoFk= -github.com/chia-network/go-chia-libs v0.8.6/go.mod h1:npTqaFSjTdMxE7hc0LOmWJmWGqcs+IERarK5fDxXk/I= +github.com/chia-network/go-chia-libs v0.9.1 h1:RAoCnnAl0hccUw6ue/9OsoYxFLlUiXpNy5OPDUDqUQQ= +github.com/chia-network/go-chia-libs v0.9.1/go.mod h1:npTqaFSjTdMxE7hc0LOmWJmWGqcs+IERarK5fDxXk/I= github.com/chia-network/go-modules v0.0.5 h1:5luTVlP6RgBXodnFcWFBk2sLdJn+6vQ4wObim683C7c= github.com/chia-network/go-modules v0.0.5/go.mod h1:5AiYBxQSvf2aFSOizTqFXXSeb9AucZWrWmRCVwUMO3A= github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= @@ -48,8 +48,8 @@ github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/prometheus/client_golang v1.20.2 h1:5ctymQzZlyOON1666svgwn3s6IKWgfbjsejTMiXIyjg= -github.com/prometheus/client_golang v1.20.2/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE= +github.com/prometheus/client_golang v1.20.3 h1:oPksm4K8B+Vt35tUhw6GbSNSgVlVSBH0qELP/7u83l4= +github.com/prometheus/client_golang v1.20.3/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE= github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E= github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY= github.com/prometheus/common v0.55.0 h1:KEi6DK7lXW/m7Ig5i47x0vRzuBsHuvJdi5ee6Y3G1dc=