2024-06-12 14:26:50 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2024-06-30 23:20:52 +02:00
|
|
|
"crypto/rand"
|
|
|
|
"encoding/base64"
|
2024-06-12 14:26:50 +02:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
2024-06-30 23:20:52 +02:00
|
|
|
"strings"
|
2024-07-05 03:08:35 +02:00
|
|
|
"sync"
|
2024-08-08 13:35:50 +02:00
|
|
|
"time"
|
2024-07-05 03:08:35 +02:00
|
|
|
|
|
|
|
"github.com/fsnotify/fsnotify"
|
2024-06-12 14:26:50 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2024-07-05 03:08:35 +02:00
|
|
|
Port int
|
|
|
|
AuthCode string
|
|
|
|
Peers []string
|
2024-08-08 13:35:50 +02:00
|
|
|
PeerID string
|
2024-07-05 03:08:35 +02:00
|
|
|
OpenSearch OpenSearchConfig
|
2024-06-12 14:26:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type OpenSearchConfig struct {
|
|
|
|
Domain string
|
|
|
|
}
|
|
|
|
|
|
|
|
var defaultConfig = Config{
|
|
|
|
Port: 5000,
|
|
|
|
OpenSearch: OpenSearchConfig{
|
|
|
|
Domain: "localhost",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
const configFilePath = "config.json"
|
|
|
|
|
2024-07-05 03:08:35 +02:00
|
|
|
var config Config
|
|
|
|
var configLock sync.RWMutex
|
|
|
|
|
2024-06-12 14:26:50 +02:00
|
|
|
func main() {
|
|
|
|
err := initConfig()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error during initialization:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-06-30 23:20:52 +02:00
|
|
|
loadNodeConfig()
|
2024-07-05 03:08:35 +02:00
|
|
|
go startFileWatcher()
|
|
|
|
go checkMasterHeartbeat()
|
|
|
|
|
|
|
|
if config.AuthCode == "" {
|
|
|
|
config.AuthCode = generateStrongRandomString(64)
|
|
|
|
fmt.Printf("Generated connection code: %s\n", config.AuthCode)
|
|
|
|
saveConfig(config)
|
|
|
|
}
|
|
|
|
|
2024-08-08 13:44:13 +02:00
|
|
|
// Generate Host ID
|
|
|
|
hostID, nodeErr := generateHostID()
|
2024-08-08 13:35:50 +02:00
|
|
|
if nodeErr != nil {
|
2024-08-08 13:44:13 +02:00
|
|
|
log.Fatalf("Failed to generate host ID: %v", nodeErr)
|
2024-08-08 13:35:50 +02:00
|
|
|
}
|
2024-08-08 13:44:13 +02:00
|
|
|
config.PeerID = hostID
|
2024-08-08 13:35:50 +02:00
|
|
|
|
2024-07-05 03:08:35 +02:00
|
|
|
if len(config.Peers) > 0 {
|
2024-08-08 13:35:50 +02:00
|
|
|
time.Sleep(2 * time.Second) // Give some time for connections to establish
|
2024-07-05 03:08:35 +02:00
|
|
|
startElection()
|
|
|
|
}
|
2024-06-30 23:20:52 +02:00
|
|
|
|
2024-08-08 13:35:50 +02:00
|
|
|
go startNodeClient()
|
|
|
|
|
2024-06-12 14:26:50 +02:00
|
|
|
runServer()
|
|
|
|
}
|
|
|
|
|
|
|
|
func initConfig() error {
|
|
|
|
if _, err := os.Stat(configFilePath); os.IsNotExist(err) {
|
|
|
|
return createConfig()
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Configuration file already exists.")
|
2024-07-05 03:08:35 +02:00
|
|
|
config = loadConfig()
|
2024-06-12 14:26:50 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createConfig() error {
|
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
|
|
|
|
fmt.Println("Configuration file not found.")
|
|
|
|
fmt.Print("Do you want to use default values? (yes/no): ")
|
|
|
|
useDefaults, _ := reader.ReadString('\n')
|
|
|
|
|
|
|
|
config := defaultConfig
|
|
|
|
if useDefaults != "yes\n" {
|
|
|
|
fmt.Print("Enter port (default 5000): ")
|
|
|
|
portStr, _ := reader.ReadString('\n')
|
|
|
|
if portStr != "\n" {
|
|
|
|
port, err := strconv.Atoi(portStr[:len(portStr)-1])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
config.Port = port
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Print("Enter your domain address (e.g., domain.com): ")
|
|
|
|
domain, _ := reader.ReadString('\n')
|
|
|
|
if domain != "\n" {
|
|
|
|
config.OpenSearch.Domain = domain[:len(domain)-1]
|
|
|
|
}
|
2024-06-30 23:20:52 +02:00
|
|
|
|
|
|
|
fmt.Print("Do you want to connect to other nodes? (yes/no): ")
|
|
|
|
connectNodes, _ := reader.ReadString('\n')
|
|
|
|
if strings.TrimSpace(connectNodes) == "yes" {
|
2024-08-08 13:35:50 +02:00
|
|
|
fmt.Println("Enter peer addresses (comma separated, e.g., /ip4/127.0.0.1/tcp/5000,/ip4/127.0.0.1/tcp/5001): ")
|
2024-06-30 23:20:52 +02:00
|
|
|
peersStr, _ := reader.ReadString('\n')
|
|
|
|
if peersStr != "\n" {
|
|
|
|
config.Peers = strings.Split(strings.TrimSpace(peersStr), ",")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-05 03:08:35 +02:00
|
|
|
if config.AuthCode == "" {
|
|
|
|
config.AuthCode = generateStrongRandomString(64)
|
|
|
|
fmt.Printf("Generated connection code: %s\n", config.AuthCode)
|
2024-06-12 14:26:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
saveConfig(config)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func saveConfig(config Config) {
|
|
|
|
file, err := os.Create(configFilePath)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error creating config file:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
configData, err := json.MarshalIndent(config, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error marshalling config data:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = file.Write(configData)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error writing to config file:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadConfig() Config {
|
|
|
|
configFile, err := os.Open(configFilePath)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error opening config file: %v", err)
|
|
|
|
}
|
|
|
|
defer configFile.Close()
|
|
|
|
|
|
|
|
var config Config
|
|
|
|
if err := json.NewDecoder(configFile).Decode(&config); err != nil {
|
|
|
|
log.Fatalf("Error decoding config file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return config
|
|
|
|
}
|
2024-06-30 23:20:52 +02:00
|
|
|
|
|
|
|
func generateStrongRandomString(length int) string {
|
|
|
|
bytes := make([]byte, length)
|
|
|
|
_, err := rand.Read(bytes)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error generating random string: %v", err)
|
|
|
|
}
|
|
|
|
return base64.URLEncoding.EncodeToString(bytes)[:length]
|
|
|
|
}
|
2024-07-05 03:08:35 +02:00
|
|
|
|
|
|
|
func startFileWatcher() {
|
|
|
|
watcher, err := fsnotify.NewWatcher()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer watcher.Close()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case event, ok := <-watcher.Events:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if event.Op&fsnotify.Write == fsnotify.Write {
|
|
|
|
log.Println("Modified file:", event.Name)
|
|
|
|
configLock.Lock()
|
|
|
|
config = loadConfig()
|
|
|
|
configLock.Unlock()
|
|
|
|
// Perform your logic here to handle the changes in the config file
|
|
|
|
}
|
|
|
|
case err, ok := <-watcher.Errors:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Println("Error:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = watcher.Add(configFilePath)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|