Search/node.go

152 lines
3.2 KiB
Go
Raw Normal View History

2024-06-29 19:27:48 +00:00
package main
import (
2024-06-30 21:20:52 +00:00
"bytes"
2024-08-08 11:35:50 +00:00
"crypto/rand"
2024-06-30 21:20:52 +00:00
"encoding/json"
2024-06-29 19:27:48 +00:00
"fmt"
"io/ioutil"
2024-07-05 01:08:35 +00:00
"log"
2024-06-29 19:27:48 +00:00
"net/http"
"sync"
"time"
2024-08-08 11:35:50 +00:00
libp2p "github.com/libp2p/go-libp2p"
crypto "github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/peer"
2024-06-29 19:27:48 +00:00
)
2024-06-30 21:20:52 +00:00
var (
2024-07-05 01:08:35 +00:00
authCode string
2024-06-30 21:20:52 +00:00
peers []string
2024-07-05 01:08:35 +00:00
authMutex sync.Mutex
authenticated = make(map[string]bool)
2024-08-08 11:35:50 +00:00
hostID peer.ID
2024-06-29 19:27:48 +00:00
)
2024-06-30 21:20:52 +00:00
type Message struct {
ID string `json:"id"`
Type string `json:"type"`
Content string `json:"content"`
}
2024-07-05 01:08:35 +00:00
type CrawlerConfig struct {
ID string
Host string
Port int
AuthCode string
}
2024-06-30 21:20:52 +00:00
func loadNodeConfig() {
config := loadConfig()
2024-08-08 11:35:50 +00:00
authCode = config.AuthCode
2024-06-30 21:20:52 +00:00
peers = config.Peers
}
2024-06-29 19:27:48 +00:00
2024-08-08 11:35:50 +00:00
func initP2P() (peer.ID, error) {
priv, _, err := crypto.GenerateKeyPairWithReader(crypto.Ed25519, 2048, rand.Reader)
if err != nil {
return "", fmt.Errorf("failed to generate key pair: %v", err)
2024-06-29 19:27:48 +00:00
}
2024-08-08 11:35:50 +00:00
h, err := libp2p.New(libp2p.Identity(priv))
2024-06-29 19:27:48 +00:00
if err != nil {
2024-08-08 11:35:50 +00:00
return "", fmt.Errorf("failed to create libp2p host: %v", err)
2024-06-29 19:27:48 +00:00
}
2024-08-08 11:35:50 +00:00
return h.ID(), nil
}
func sendMessage(serverAddr string, msg Message) error {
msgBytes, err := json.Marshal(msg)
if err != nil {
return fmt.Errorf("failed to marshal message: %v", err)
2024-06-30 21:20:52 +00:00
}
2024-06-29 19:27:48 +00:00
2024-08-08 11:35:50 +00:00
req, err := http.NewRequest("POST", serverAddr, bytes.NewBuffer(msgBytes))
if err != nil {
return fmt.Errorf("failed to create request: %v", err)
2024-06-29 19:27:48 +00:00
}
2024-08-08 11:35:50 +00:00
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", authCode)
2024-06-29 19:27:48 +00:00
2024-08-08 11:35:50 +00:00
client := &http.Client{
Timeout: time.Second * 10,
}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("failed to send request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := ioutil.ReadAll(resp.Body)
return fmt.Errorf("server error: %s", body)
}
return nil
2024-06-30 21:20:52 +00:00
}
2024-08-08 11:35:50 +00:00
func handleNodeRequest(w http.ResponseWriter, r *http.Request) {
2024-07-05 01:08:35 +00:00
if r.Method != http.MethodPost {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
return
}
2024-08-08 11:35:50 +00:00
auth := r.Header.Get("Authorization")
if auth != authCode {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
2024-07-05 01:08:35 +00:00
return
}
2024-08-08 11:35:50 +00:00
var msg Message
err := json.NewDecoder(r.Body).Decode(&msg)
if err != nil {
2024-07-05 01:08:35 +00:00
http.Error(w, "Error parsing JSON", http.StatusBadRequest)
return
}
2024-08-08 11:35:50 +00:00
defer r.Body.Close()
2024-07-05 01:08:35 +00:00
2024-08-08 11:35:50 +00:00
log.Printf("Received message: %+v\n", msg)
w.Write([]byte("Message received"))
2024-07-05 01:08:35 +00:00
2024-08-08 11:35:50 +00:00
interpretMessage(msg)
2024-07-05 01:08:35 +00:00
}
2024-08-08 11:35:50 +00:00
func startNodeClient() {
for {
for _, peerAddr := range peers {
msg := Message{
ID: hostID.Pretty(),
Type: "test",
Content: "This is a test message from the client node",
}
err := sendMessage(peerAddr, msg)
if err != nil {
log.Printf("Error sending message to %s: %v", peerAddr, err)
} else {
log.Println("Message sent successfully to", peerAddr)
}
}
time.Sleep(10 * time.Second)
}
2024-07-05 01:08:35 +00:00
}
2024-06-30 21:20:52 +00: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 01:08:35 +00:00
case "heartbeat":
handleHeartbeat(msg.Content)
case "election":
handleElection(msg.Content)
2024-06-30 21:20:52 +00:00
default:
fmt.Println("Received unknown message type:", msg.Type)
}
}