wip
This commit is contained in:
parent
8f913eca0d
commit
f6576a9134
5 changed files with 237 additions and 36 deletions
93
node.go
93
node.go
|
@ -2,18 +2,22 @@ package main
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
connCode string
|
||||
authCode string
|
||||
peers []string
|
||||
connCodeMutex sync.Mutex
|
||||
authMutex sync.Mutex
|
||||
authenticated = make(map[string]bool)
|
||||
)
|
||||
|
||||
type Message struct {
|
||||
|
@ -22,9 +26,16 @@ type Message struct {
|
|||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
type CrawlerConfig struct {
|
||||
ID string
|
||||
Host string
|
||||
Port int
|
||||
AuthCode string
|
||||
}
|
||||
|
||||
func loadNodeConfig() {
|
||||
config := loadConfig()
|
||||
connCode = config.ConnectionCode
|
||||
authCode = config.AuthCode // nuh uh
|
||||
peers = config.Peers
|
||||
}
|
||||
|
||||
|
@ -34,9 +45,6 @@ func handleNodeRequest(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
connCodeMutex.Lock()
|
||||
defer connCodeMutex.Unlock()
|
||||
|
||||
body, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
http.Error(w, "Error reading request body", http.StatusInternalServerError)
|
||||
|
@ -50,8 +58,8 @@ func handleNodeRequest(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
if msg.ID != connCode {
|
||||
http.Error(w, "Authentication failed", http.StatusUnauthorized)
|
||||
if !isAuthenticated(msg.ID) {
|
||||
http.Error(w, "Authentication required", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -59,17 +67,55 @@ func handleNodeRequest(w http.ResponseWriter, r *http.Request) {
|
|||
fmt.Fprintln(w, "Message received")
|
||||
}
|
||||
|
||||
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]
|
||||
}
|
||||
|
||||
func interpretMessage(msg Message) {
|
||||
switch msg.Type {
|
||||
case "test":
|
||||
fmt.Println("Received test message:", msg.Content)
|
||||
case "get-version":
|
||||
fmt.Println("Received get-version message")
|
||||
// Handle get-version logic here
|
||||
case "update":
|
||||
fmt.Println("Received update message:", msg.Content)
|
||||
// Handle update logic here
|
||||
go update()
|
||||
case "heartbeat":
|
||||
handleHeartbeat(msg.Content)
|
||||
case "election":
|
||||
handleElection(msg.Content)
|
||||
default:
|
||||
fmt.Println("Received unknown message type:", msg.Type)
|
||||
}
|
||||
|
@ -111,7 +157,7 @@ func startNodeClient(addresses []string) {
|
|||
for _, address := range addresses {
|
||||
go func(addr string) {
|
||||
for {
|
||||
err := sendMessage(addr, connCode, "test", "This is a test message")
|
||||
err := sendMessage(addr, authCode, "test", "This is a test message")
|
||||
if err != nil {
|
||||
fmt.Println("Error sending test message to", addr, ":", err)
|
||||
continue
|
||||
|
@ -121,3 +167,24 @@ func startNodeClient(addresses []string) {
|
|||
}(address)
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue