Skip to content

Commit

Permalink
Moved functions and simplified some logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tfn18001 committed Apr 20, 2022
1 parent 5d5356a commit 12d7972
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 167 deletions.
4 changes: 2 additions & 2 deletions CTng/config/test/monitor_pub_config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"CA_URLs": ["192.168.1.1", "1.0.0.1", "192.168.1.9"],
"Logger_URLs": ["192.168.1.1", "1.0.0.1", "192.168.1.9"],
"All_CA_URLs": ["192.168.1.1", "1.0.0.1", "192.168.1.9"],
"All_Logger_URLs": ["192.168.1.1", "1.0.0.1", "192.168.1.9"],
"Gossip_wait_time": 3000,
"MMD": 3000,
"MRD": 3000,
Expand Down
4 changes: 2 additions & 2 deletions CTng/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (

// The structs that are read/written to files.
type Monitor_public_config struct {
CA_URLs []string
Logger_URLs []string
All_CA_URLs []string
All_Logger_URLs []string
Gossip_wait_time int
MMD int
MRD int
Expand Down
80 changes: 80 additions & 0 deletions CTng/monitor/monitor.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package monitor

import (
"CTng/crypto"
"CTng/gossip"
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
Expand Down Expand Up @@ -70,3 +75,78 @@ func QueryAuthorities(authURLs []string) {
}

}

//Accused = Domain name of the accused entity (logger etc.)
//should be a monitor functionality
func AccuseEntity(c *crypto.CryptoConfig, Accused string) (gossip.Gossip_object, error) {
// this should be a method for the monitor
// psedo code for now
msg := Accused
signature, err := c.ThresholdSign(msg)
if err != nil {
return gossip.Gossip_object{}, err
}
var sigarray [2]string
sigarray[0] = signature.String()
sigarray[1] = ""
var payloadarray [2]string
payloadarray[0] = msg
payloadarray[1] = ""
accusation := gossip.Gossip_object{
Application: "accsuation",
Type: "http://ctng.uconn.edu/203",
Signer: c.SelfID.String(),
Signature: sigarray,
Timestamp: gossip.GetCurrentTimestamp(),
Payload: payloadarray,
}
return accusation, nil
}

func Send_to_gossiper(c *MonitorContext, g gossip.Gossip_object) {
// Convert gossip object to JSON
msg, err := json.Marshal(g)
if err != nil {
fmt.Println(err)
}
// Send the gossip object to the gossiper.
resp, postErr := c.Client.Post("http://"+c.Config.Gossiper_URL+"/monitor/recieve-gossip", "application/json", bytes.NewBuffer(msg))
if postErr != nil {
fmt.Printf("Error sending object to Gossiper: " + postErr.Error())
} else {
// Close the response, mentioned by http.Post
// Alernatively, we could return the response from this function.
defer resp.Body.Close()
fmt.Println("Owner responded with " + resp.Status)
}

}

//this function takes the name of the entity as input and check if there is a POM against it
//this should be invoked after the monitor receives the information from its loggers and CAs prior to threshold signning it
func Check_entity_pom(c *MonitorContext, name string) error {
for _, v := range *c.Storage {
if v.Payload[0] == name && (v.Type == gossip.ACCUSATION_POM || v.Type == gossip.APPLICATION_POM || v.Type == gossip.GOSSIP_POM) {
return errors.New("There exists a proof of misbehavior against this entity")
}
}
return nil
}

func IsLogger(c *MonitorContext, loggerURL string) bool {
for _, url := range c.Config.Public.All_Logger_URLs {
if url == loggerURL {
return true
}
}
return false
}

func IsAuthority(c *MonitorContext, authURL string) bool {
for _, url := range c.Config.Public.All_CA_URLs {
if url == authURL {
return true
}
}
return false
}
60 changes: 60 additions & 0 deletions CTng/monitor/monitor_process.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package monitor

import (
"CTng/gossip"
"fmt"
"time"
)

//Need to discuss about this ******************************************************
func Process_valid_object(c *MonitorContext, g gossip.Gossip_object) {
//if the valid object is from the logger in the monitor config logger URL list
//This handles the STHS
if IsLogger(c, g.Signer) && g.Type == gossip.STH {
sig_frag, err := c.Config.Crypto.ThresholdSign(g.Payload[0])
if err != nil {
fmt.Printf(err.Error())
}
Send_to_gossiper(c, g)
f := func() {
pom_err := Check_entity_pom(c, g.Signer)
//if there is no conflicting information/PoM send the Threshold signed version to the gossiper
if pom_err == nil {
g.Type = gossip.STH_FRAG
g.Signature[0] = sig_frag.String()
g.Signer = c.Config.Crypto.SelfID.String()
Send_to_gossiper(c, g)
}

}
time.AfterFunc(time.Duration(c.Config.Public.Gossip_wait_time), f)
return
}
//if the object is from a CA, revocation information
//this handles revocation information
if IsAuthority(c, g.Signer) && g.Type == gossip.REVOCATION {
sig_frag, err := c.Config.Crypto.ThresholdSign(g.Payload[0])
if err != nil {
fmt.Printf(err.Error())
}
Send_to_gossiper(c, g)
f := func() {
pom_err := Check_entity_pom(c, g.Signer)
if pom_err == nil {
g.Type = gossip.REVOCATION_FRAG
g.Signature[0] = sig_frag.String()
g.Signer = c.Config.Crypto.SelfID.String()
Send_to_gossiper(c, g)
}

}
time.AfterFunc(time.Duration(c.Config.Public.Gossip_wait_time), f)
return
}
//if the object is from its own gossiper
// Note didn't implement the directory separation here***************************************************************
if g.Type == gossip.ACCUSATION_POM || g.Type == gossip.GOSSIP_POM || g.Type == gossip.APPLICATION_POM || g.Type == gossip.REVOCATION_FULL || g.Type == gossip.STH_FULL {
c.StoreObject(g)
return
}
}
162 changes: 0 additions & 162 deletions CTng/server/Monitor_ng.go

This file was deleted.

35 changes: 34 additions & 1 deletion CTng/server/Monitor_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func handleMonitorRequests(c *monitor.MonitorContext) {
gorillaRouter := mux.NewRouter().StrictSlash(true)

// POST functions
gorillaRouter.HandleFunc("/gossip/push-data", bindMonitorContext(c, receiveGossip)).Methods("POST")
gorillaRouter.HandleFunc("/gossip/push-data", bindMonitorContext(c, handle_gossip)).Methods("POST")
gorillaRouter.HandleFunc("/submit-pom", bindMonitorContext(c, receivePOM)).Methods("POST")

// GET functions
Expand Down Expand Up @@ -131,6 +131,39 @@ func getPOM(c *monitor.MonitorContext, w http.ResponseWriter, r *http.Request) {
// if POM found, send to requester
}

func handle_gossip(c *monitor.MonitorContext, w http.ResponseWriter, r *http.Request) {
// Parse sent object.
// Converts JSON passed in the body of a POST to a Gossip_object.
var gossip_obj gossip.Gossip_object
err := json.NewDecoder(r.Body).Decode(&gossip_obj)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
// Verify the object is valid.
err = gossip_obj.Verify(c.Config.Crypto)
if err != nil {
fmt.Println("Recieved invalid object from " + getSenderURL(r) + ".")
obj, err := monitor.AccuseEntity(c.Config.Crypto, gossip_obj.Signer)
monitor.Send_to_gossiper(c, obj)
http.Error(w, err.Error(), http.StatusOK)
return
}
// Check for duplicate object.
_, found := c.GetObject(gossip_obj.GetID(int64(c.Config.Public.Gossip_wait_time)))
if found {
// If the object is already stored, still return OK.{
fmt.Println("Duplicate:", gossip_obj.Type, getSenderURL(r)+".")
http.Error(w, "Gossip object already stored.", http.StatusOK)
// processDuplicateObject(c, gossip_obj, stored_obj)
return
} else {
fmt.Println("Recieved new, valid", gossip_obj.Type, "from "+getSenderURL(r)+".")
monitor.Process_valid_object(c, gossip_obj)
c.SaveStorage()
}
http.Error(w, "Gossip object Processed.", http.StatusOK)
}

func StartMonitorServer(c *monitor.MonitorContext) {
// Check if the storage file exists in this directory
err := c.LoadStorage()
Expand Down

0 comments on commit 12d7972

Please sign in to comment.