Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ist17002 committed Apr 22, 2022
2 parents 065771c + 60b6261 commit 9652186
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 74 deletions.
13 changes: 13 additions & 0 deletions CTng/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@
"config/test/localhost:8081.crypto.json"
],
},
{
"name": "Launch Monitor",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceRoot}/ctng.go",
"args": [
"monitor",
"testData/monitorNetworkTest/monitor_pub_config.json",
"testData/monitorNetworkTest/1/monitor_priv_config.json",
"testData/gossiperNetworkTest/1/gossiperCrypto.json"
],
},
{
"name": "Launch testData Gossiper 3",
"type": "go",
Expand Down
4 changes: 3 additions & 1 deletion CTng/ctng.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ func main() {
fmt.Println(helpText)
panic(err)
}
storage := new(gossip.Gossip_Storage)
*storage = make(gossip.Gossip_Storage)
ctx := monitor.MonitorContext{
Config: &conf,
Storage: new(gossip.Gossip_Storage),
Storage: storage,
StorageFile: "monitor_data.json",
}
ctx.Config = &conf
Expand Down
2 changes: 1 addition & 1 deletion CTng/gossip/gossiper.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func SendToOwner(c *GossiperContext, obj Gossip_object) {
fmt.Println(err)
}
// Send the gossip object to the owner.
resp, postErr := c.Client.Post("http://"+c.Config.Owner_URL+"/monitor/recieve-gossip", "application/json", bytes.NewBuffer(msg))
resp, postErr := c.Client.Post("http://"+c.Config.Owner_URL+"/recieve-gossip", "application/json", bytes.NewBuffer(msg))
if postErr != nil {
fmt.Errorf("Error sending object to owner: " + postErr.Error())
} else {
Expand Down
84 changes: 46 additions & 38 deletions CTng/monitor/monitor.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package monitor

import (
"CTng/crypto"
"CTng/gossip"
"CTng/util"
"bytes"
"encoding/json"
"errors"
Expand All @@ -13,63 +13,73 @@ import (
"time"
)

func QueryLoggers(loggerURLs []string) {
for _, logger := range loggerURLs {
const PROTOCOL = "http://"

func QueryLoggers(c *MonitorContext) {
for _, logger := range c.Config.Logger_URLs {

// Get today's STH from logger.

// Get today's date in format YYYY-MM-DD
var today = time.Now().UTC().Format(time.RFC3339)[0:10]

sthResp, err := http.Get(logger + "ctng/v1/get-sth/" + today)
sthResp, err := http.Get(PROTOCOL + logger + "/ctng/v1/get-sth/" + today)
if err != nil {
log.Fatalln(err)
log.Println(err)
AccuseEntity(c, logger)
continue
}

sthBody, err := ioutil.ReadAll(sthResp.Body)
var STH gossip.Gossip_object
err = json.Unmarshal(sthBody, &STH)
if err != nil {
log.Fatalln(err)
}
STH := string(sthBody)
fmt.Printf("STH from logger " + logger + ": " + STH + "\n") //temp

// Get today's entries from logger
entriesResp, err := http.Get(logger + "ctng/v1/get-entries/" + today)
if err != nil {
log.Fatalln(err)
log.Println(util.RED+err.Error(), util.RESET)
AccuseEntity(c, logger)
continue
}

entiresBody, err := ioutil.ReadAll(entriesResp.Body)
err = STH.Verify(c.Config.Crypto)
if err != nil {
log.Fatalln(err)
log.Println(util.RED+"STH signature verification failed", err.Error(), util.RESET)
AccuseEntity(c, logger)
} else {
Process_valid_object(c, STH)
}
entries := string(entiresBody)
fmt.Printf("Entries from logger " + logger + ": " + entries + "\n") //temp

// TODO - process STH and entries
// Get today's entries from logger
// entriesResp, err := http.Get(logger + "/ctng/v1/get-entries/" + today)
// if err != nil {
// log.Println(util.RED+err.Error(), util.RESET)
// }

// entiresBody, err := ioutil.ReadAll(entriesResp.Body)
// if err != nil {
// log.Println(util.RED+err.Error(), util.RESET)
// }
// entries := string(entiresBody)
// fmt.Printf("Entries from logger " + logger + ": " + entries + "\n") //temp
}

}

func QueryAuthorities(authURLs []string) {
for _, CA := range authURLs {
func QueryAuthorities(c *MonitorContext) {
for _, CA := range c.Config.CA_URLs {

// Get today's revocation information from CA.

// Get today's date in format YYYY-MM-DD
var today = time.Now().UTC().Format(time.RFC3339)[0:10]

revResp, err := http.Get(CA + "/ctng/v1/get-revocations/" + today)
revResp, err := http.Get(PROTOCOL + CA + "/ctng/v1/get-revocations/" + today)
if err != nil {
log.Fatalln(err)
log.Println(util.RED+err.Error(), util.RESET)
}

revBody, err := ioutil.ReadAll(revResp.Body)
if err != nil {
log.Fatalln(err)
log.Println(util.RED+err.Error(), util.RESET)
}
rev := string(revBody)
fmt.Printf("Revocation information from CA " + CA + ": " + rev + "\n")
fmt.Println("Revocation information from CA " + CA + ": " + rev + "\n")

// TODO - process revocation data
}
Expand All @@ -78,29 +88,27 @@ 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) {
func AccuseEntity(c *MonitorContext, Accused string) {
// 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
}
signature, _ := c.Config.Crypto.ThresholdSign(msg)

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(),
Application: "CTng",
Type: gossip.ACCUSATION_FRAG,
Signer: c.Config.Crypto.SelfID.String(),
Signature: sigarray,
Timestamp: gossip.GetCurrentTimestamp(),
Payload: payloadarray,
}
return accusation, nil
Send_to_gossiper(c, accusation)
}

func Send_to_gossiper(c *MonitorContext, g gossip.Gossip_object) {
Expand All @@ -110,9 +118,9 @@ func Send_to_gossiper(c *MonitorContext, g gossip.Gossip_object) {
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))
resp, postErr := c.Client.Post(PROTOCOL+c.Config.Gossiper_URL+"/gossip/gossip-data", "application/json", bytes.NewBuffer(msg))
if postErr != nil {
fmt.Printf("Error sending object to Gossiper: " + postErr.Error())
fmt.Println("Error sending object to Gossiper: ", postErr.Error())
} else {
// Close the response, mentioned by http.Post
// Alernatively, we could return the response from this function.
Expand Down
4 changes: 2 additions & 2 deletions CTng/monitor/monitor_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func Process_valid_object(c *MonitorContext, g gossip.Gossip_object) {
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())
fmt.Println(err.Error())
}
Send_to_gossiper(c, g)
f := func() {
Expand All @@ -35,7 +35,7 @@ func Process_valid_object(c *MonitorContext, g gossip.Gossip_object) {
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())
fmt.Println(err.Error())
}
Send_to_gossiper(c, g)
f := func() {
Expand Down
4 changes: 2 additions & 2 deletions CTng/monitorTest.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#/bin/bash

go run . monitor \
testData/monitorNetworkTest/gossiper_pub_config.json \
testData/monitorNetworkTest/$1/gossiper_priv_config.json \
testData/monitorNetworkTest/monitor_pub_config.json \
testData/monitorNetworkTest/$1/monitor_priv_config.json \
testData/gossiperNetworkTest/$1/gossiperCrypto.json
2 changes: 2 additions & 0 deletions CTng/server/Gossiper_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ func handleOwnerGossip(c *gossip.GossiperContext, w http.ResponseWriter, r *http
err := json.NewDecoder(r.Body).Decode(&gossip_obj)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

err = gossip_obj.Verify(c.Config.Crypto)
Expand All @@ -169,6 +170,7 @@ func handleOwnerGossip(c *gossip.GossiperContext, w http.ResponseWriter, r *http
// gossip.ProcessInvalidObject(gossip_obj, err)
fmt.Println(util.RED+"Owner sent invalid object.", util.RESET)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
stored_obj, found := c.GetObject(gossip_obj.GetID(c.Config.Public.Period_interval))
if found {
Expand Down
48 changes: 20 additions & 28 deletions CTng/server/Monitor_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package server
import (
"CTng/gossip"
"CTng/monitor"
"CTng/util"
"encoding/json"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -82,7 +83,7 @@ func receivePOM(c *monitor.MonitorContext, w http.ResponseWriter, r *http.Reques
}

PoM := string(body)
fmt.Printf("PoM Received: " + PoM + "\n") //temp
fmt.Println("PoM Received: " + PoM) //temp

// TODO - Validate, process and save PoM
}
Expand All @@ -99,7 +100,7 @@ func getRevocation(c *monitor.MonitorContext, w http.ResponseWriter, r *http.Req

// Get {date} from the end of the URL
date := path.Base(r.URL.Path)
fmt.Printf(date, '\n') //temp
fmt.Println(date) //temp

// if no revocation data found for specified day, return a 404
http.Error(w, "Revocation information not found.", 404)
Expand All @@ -111,7 +112,7 @@ func getSTH(c *monitor.MonitorContext, w http.ResponseWriter, r *http.Request) {

// Get {date} from the end of the URL
date := path.Base(r.URL.Path)
fmt.Printf(date, '\n') //temp
fmt.Println(date) //temp

// if no STH found for specified day, return a 404
http.Error(w, "STH object not found.", 404)
Expand All @@ -123,7 +124,7 @@ func getPOM(c *monitor.MonitorContext, w http.ResponseWriter, r *http.Request) {

// Get {date} from the end of the URL
date := path.Base(r.URL.Path)
fmt.Printf(date, '\n') //temp
fmt.Println(date)

// if no POM found for specified day, return a 404
http.Error(w, "PoM not found.", 404)
Expand All @@ -143,8 +144,7 @@ func handle_gossip(c *monitor.MonitorContext, w http.ResponseWriter, r *http.Req
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)
monitor.AccuseEntity(c, gossip_obj.Signer)
http.Error(w, err.Error(), http.StatusOK)
return
}
Expand Down Expand Up @@ -179,28 +179,20 @@ func StartMonitorServer(c *monitor.MonitorContext) {
panic(err)
}
}

// Start HTTP server loop
// Execute as goroutine so server and client
// can operate concurrently
go handleMonitorRequests(c)

// Query loggers and authorities once per day
queryInterval := time.Hour * 24

// Perform startup queries
monitor.QueryLoggers(c.Config.Logger_URLs)
monitor.QueryAuthorities(c.Config.CA_URLs)
queryTime := time.Now()

// Monitor client loop
for {
// Check if enough time has passed to query loggers and CAs again
if time.Since(queryTime) >= queryInterval {
monitor.QueryLoggers(c.Config.Logger_URLs)
monitor.QueryAuthorities(c.Config.CA_URLs)
queryTime = time.Now()
tr := &http.Transport{}
c.Client = &http.Client{
Transport: tr,
}
// Run a go routine to handle tasks that must occur every MMD
f := func() {
for {
fmt.Println(util.GREEN + "Querying Loggers+CAs" + util.RESET)
monitor.QueryLoggers(c)
// monitor.QueryAuthorities(c)
time.Sleep(time.Duration(c.Config.Public.MMD) * time.Second)
}
time.Sleep(time.Second)
}
go f()
// Start HTTP server loop
handleMonitorRequests(c)
}
4 changes: 2 additions & 2 deletions CTng/testData/monitorNetworkTest/1/monitor_priv_config.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"Crypto_config_path": "testData/monitorNetworkTest/1/monitorCrypto.json",
"CA_URLs": ["192.168.1.1", "1.1.1.1", "255.255.255.255"],
"Logger_URLs": ["192.168.1.1", "1.1.1.1", "255.255.255.255"],
"CA_URLs": ["localhost:9000"],
"Logger_URLs": ["localhost:8080", "localhost:8081"],
"Gossiper_URL": "localhost:8080",
"Port": "8180"
}

0 comments on commit 9652186

Please sign in to comment.