Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate certs + config if it doesn't already exist #56

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions cmd/crawl.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ 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"
"github.com/schollz/progressbar/v3"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gopkg.in/go-playground/pool.v3"
"gopkg.in/yaml.v3"
)

const poolSize = 1000
Expand Down Expand Up @@ -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{}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down