add only legit machine addresses to the PoPSelection process (#404)

add only legit machine addresses to the PoPSelection process

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
This commit is contained in:
Jürgen Eckel 2024-06-06 14:37:09 +02:00 committed by GitHub
parent dd2621afde
commit 1462b6ee88
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 91 additions and 0 deletions

View File

@ -1,10 +1,14 @@
package monitor package monitor
import ( import (
"context"
"crypto/tls" "crypto/tls"
"encoding/json"
"io"
"log" "log"
"math/rand" "math/rand"
"net" "net"
"net/http"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
@ -196,6 +200,11 @@ func (mms *MqttMonitor) MqttMsgHandler(_ mqtt.Client, msg mqtt.Message) {
return return
} }
active, err := IsLegitMachineAddress(address)
if err != nil || !active {
return
}
unixTime := time.Now().Unix() unixTime := time.Now().Unix()
err = mms.AddParticipant(address, unixTime) err = mms.AddParticipant(address, unixTime)
@ -206,6 +215,81 @@ func (mms *MqttMonitor) MqttMsgHandler(_ mqtt.Client, msg mqtt.Message) {
} }
} }
func IsLegitMachineAddress(address string) (active bool, err error) {
url := "http://localhost:1317/planetmint/machine/address/" + address
// Create a new HTTP client
client := &http.Client{}
// Create a new request
ctx := context.Background()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
log.Println("[app] [Monitor] cannot send machine query request " + err.Error())
return
}
// Set the header
req.Header.Set("Accept", "application/json")
// Send the request
resp, err := client.Do(req)
if err != nil {
log.Println("[app] [Monitor] cannot connect to server: " + err.Error())
return
}
// Close the response body
defer resp.Body.Close()
// Read the response body
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Println("[app] [Monitor] cannot read response: " + err.Error())
return
}
// Check the status code
if resp.StatusCode != http.StatusOK {
log.Printf("[app] [Monitor] Error: unexpected status code: " + string(body))
return
}
// Unmarshal the response body into a map
var data map[string]interface{}
err = json.Unmarshal(body, &data)
if err != nil {
log.Println("[app] [Monitor] cannot unmarshal response " + err.Error())
return
}
// Check if the "info" key exists
machineValue, ok := data["machine"]
if !ok {
log.Println("[app] [Monitor] response does not contain the required machine")
return
}
machineMap, ok := machineValue.(map[string]interface{})
if !ok {
log.Println("[app] [Monitor] cannot convert machine map")
return
}
addressMap, ok := machineMap["address"]
if !ok {
log.Println("[app] [Monitor] response does not contain the required name")
return
}
value, ok := addressMap.(string)
if !ok || value != address {
log.Println("[app] [Monitor] return machine is not the required one")
return
}
err = nil
active = true
return
}
func (mms *MqttMonitor) onConnectionLost(_ mqtt.Client, err error) { func (mms *MqttMonitor) onConnectionLost(_ mqtt.Client, err error) {
log.Println("[app] [Monitor] Connection lost: " + err.Error()) log.Println("[app] [Monitor] Connection lost: " + err.Error())
// Handle connection loss here (e.g., reconnect attempts, logging) // Handle connection loss here (e.g., reconnect attempts, logging)

View File

@ -99,3 +99,10 @@ func TestCleanupPrecisionTest(t *testing.T) {
assert.Contains(t, challengee, "plmnt") assert.Contains(t, challengee, "plmnt")
mqttMonitor.Terminate() mqttMonitor.Terminate()
} }
func TestIsLegitMachineAddress(t *testing.T) {
t.SkipNow()
active, err := monitor.IsLegitMachineAddress("plmnt1z6xmwqfnn9mvean9gsd57segawgjykpxw8hq5t")
assert.NoError(t, err)
assert.Equal(t, active, true)
}