2024-06-29 21:27:48 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-06-30 23:20:52 +02:00
|
|
|
"bytes"
|
2024-07-05 03:08:35 +02:00
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
2024-06-30 23:20:52 +02:00
|
|
|
"encoding/json"
|
2024-06-29 21:27:48 +02:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2024-07-05 03:08:35 +02:00
|
|
|
"log"
|
2024-06-29 21:27:48 +02:00
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2024-06-30 23:20:52 +02:00
|
|
|
var (
|
2024-07-05 03:08:35 +02:00
|
|
|
authCode string
|
2024-06-30 23:20:52 +02:00
|
|
|
peers []string
|
2024-07-05 03:08:35 +02:00
|
|
|
authMutex sync.Mutex
|
|
|
|
authenticated = make(map[string]bool)
|
2024-06-29 21:27:48 +02:00
|
|
|
)
|
|
|
|
|
2024-06-30 23:20:52 +02:00
|
|
|
type Message struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
Content string `json:"content"`
|
|
|
|
}
|
|
|
|
|
2024-07-05 03:08:35 +02:00
|
|
|
type CrawlerConfig struct {
|
|
|
|
ID string
|
|
|
|
Host string
|
|
|
|
Port int
|
|
|
|
AuthCode string
|
|
|
|
}
|
|
|
|
|
2024-06-30 23:20:52 +02:00
|
|
|
func loadNodeConfig() {
|
|
|
|
config := loadConfig()
|
2024-07-05 03:08:35 +02:00
|
|
|
authCode = config.AuthCode // nuh uh
|
2024-06-30 23:20:52 +02:00
|
|
|
peers = config.Peers
|
|
|
|
}
|
2024-06-29 21:27:48 +02:00
|
|
|
|
|
|
|
func handleNodeRequest(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != http.MethodPost {
|
|
|
|
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "Error reading request body", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer r.Body.Close()
|
|
|
|
|
2024-06-30 23:20:52 +02:00
|
|
|
var msg Message
|
|
|
|
if err := json.Unmarshal(body, &msg); err != nil {
|
|
|
|
http.Error(w, "Error parsing JSON", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2024-06-29 21:27:48 +02:00
|
|
|
|
2024-07-05 03:08:35 +02:00
|
|
|
if !isAuthenticated(msg.ID) {
|
|
|
|
http.Error(w, "Authentication required", http.StatusUnauthorized)
|
2024-06-29 21:27:48 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-06-30 23:20:52 +02:00
|
|
|
interpretMessage(msg)
|
|
|
|
fmt.Fprintln(w, "Message received")
|
|
|
|
}
|
|
|
|
|
2024-07-05 03:08:35 +02:00
|
|
|
func handleAuth(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != http.MethodPost {
|
|
|
|
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "Error reading request body", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer r.Body.Close()
|
|
|
|
|
|
|
|
var authRequest CrawlerConfig
|
|
|
|
if err := json.Unmarshal(body, &authRequest); err != nil {
|
|
|
|
http.Error(w, "Error parsing JSON", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedCode := GenerateRegistrationCode(authRequest.Host, authRequest.Port, authCode)
|
|
|
|
if authRequest.AuthCode != expectedCode {
|
|
|
|
http.Error(w, "Invalid auth code", http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
authMutex.Lock()
|
|
|
|
authenticated[authRequest.ID] = true
|
|
|
|
authMutex.Unlock()
|
|
|
|
|
|
|
|
fmt.Fprintln(w, "Authenticated successfully")
|
|
|
|
}
|
|
|
|
|
|
|
|
func isAuthenticated(id string) bool {
|
|
|
|
authMutex.Lock()
|
|
|
|
defer authMutex.Unlock()
|
|
|
|
return authenticated[id]
|
|
|
|
}
|
|
|
|
|
2024-06-30 23:20:52 +02:00
|
|
|
func interpretMessage(msg Message) {
|
|
|
|
switch msg.Type {
|
|
|
|
case "test":
|
|
|
|
fmt.Println("Received test message:", msg.Content)
|
|
|
|
case "update":
|
|
|
|
fmt.Println("Received update message:", msg.Content)
|
|
|
|
go update()
|
2024-07-05 03:08:35 +02:00
|
|
|
case "heartbeat":
|
|
|
|
handleHeartbeat(msg.Content)
|
|
|
|
case "election":
|
|
|
|
handleElection(msg.Content)
|
2024-06-30 23:20:52 +02:00
|
|
|
default:
|
|
|
|
fmt.Println("Received unknown message type:", msg.Type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendMessage(address, id, msgType, content string) error {
|
|
|
|
msg := Message{
|
|
|
|
ID: id,
|
|
|
|
Type: msgType,
|
|
|
|
Content: content,
|
|
|
|
}
|
|
|
|
msgBytes, err := json.Marshal(msg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/node", address), bytes.NewBuffer(msgBytes))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
client := &http.Client{}
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
body, _ := ioutil.ReadAll(resp.Body)
|
|
|
|
return fmt.Errorf("failed to send message: %s", body)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2024-06-29 21:27:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func startNodeClient(addresses []string) {
|
|
|
|
for _, address := range addresses {
|
|
|
|
go func(addr string) {
|
|
|
|
for {
|
2024-07-05 03:08:35 +02:00
|
|
|
err := sendMessage(addr, authCode, "test", "This is a test message")
|
2024-06-29 21:27:48 +02:00
|
|
|
if err != nil {
|
2024-06-30 23:20:52 +02:00
|
|
|
fmt.Println("Error sending test message to", addr, ":", err)
|
2024-06-29 21:27:48 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
}
|
|
|
|
}(address)
|
|
|
|
}
|
|
|
|
}
|
2024-07-05 03:08:35 +02:00
|
|
|
|
|
|
|
func GenerateRegistrationCode(host string, port int, authCode string) string {
|
|
|
|
data := fmt.Sprintf("%s:%d:%s", host, port, authCode)
|
|
|
|
hash := sha256.Sum256([]byte(data))
|
|
|
|
return hex.EncodeToString(hash[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseRegistrationCode(code string, host string, port int, authCode string) (string, int, string, error) {
|
|
|
|
data := fmt.Sprintf("%s:%d:%s", host, port, authCode)
|
|
|
|
hash := sha256.Sum256([]byte(data))
|
|
|
|
expectedCode := hex.EncodeToString(hash[:])
|
|
|
|
|
|
|
|
log.Printf("Parsing registration code: %s", code)
|
|
|
|
log.Printf("Expected registration code: %s", expectedCode)
|
|
|
|
|
|
|
|
if expectedCode != code {
|
|
|
|
return "", 0, "", fmt.Errorf("invalid registration code")
|
|
|
|
}
|
|
|
|
|
|
|
|
return host, port, authCode, nil
|
|
|
|
}
|