Simon 2 years ago
parent
commit
fc6ce5de35
4 changed files with 52 additions and 26 deletions
  1. 33 8
      client/client.go
  2. 5 5
      follower/follower.go
  3. 11 10
      leader/leader.go
  4. 3 3
      lib/databaseRead.go

+ 33 - 8
client/client.go

@@ -42,11 +42,11 @@ const leader string = "127.0.0.1:4441"
 //needs to be changed at leader/follower/client at the same time
 const neededSubscriptions = 1
 const numClients = 1
-const dataLength int = 64
+const dataLength int = 128
 const numThreads int = 12
 
-var dbWriteSize int = 4
-
+var dbWriteSize int = 100
+var round int
 var topicList []string
 var archiveTopicList []string
 
@@ -121,7 +121,7 @@ func client(clientNumber int) {
 		//gets current phase
 		phase := readFrom(leaderConn, 1)
 
-		fmt.Println("Phase: ", phase[0])
+		fmt.Println("Phase ", phase[0])
 
 		if phase[0] == 1 {
 
@@ -137,6 +137,8 @@ func client(clientNumber int) {
 				panic(err)
 			}
 
+			round = byteToInt(roundAsBytes)
+
 			//request virtualAddress from leader via pirQuery
 			encryptedQueryLeader, encryptedQueryFollower := createAuditPIRQuery(clientNumber)
 			sendQuerys(encryptedQueryLeader, encryptedQueryFollower, leaderConn, false)
@@ -413,9 +415,13 @@ func receiveTweets(sharedSecret [2][32]byte, leaderConn net.Conn, getArchive boo
 			lib.Xor(expandedSharedSecrets[i][:], tweets)
 		}
 
-		index := strings.Index(string(tweets), ";;")
-		text := string(tweets)[:index]
-		fmt.Println(text)
+		index := strings.Index(string(tweets), ";;;")
+		if index != -1 {
+			//text := string(tweets)[:index]
+			//fmt.Println(text)
+		} else {
+			fmt.Println("received text not of correct format", round, string(tweets))
+		}
 	}
 }
 
@@ -554,7 +560,26 @@ func getTweet(clientNumber int) []byte {
 
 	r := mr.New(mr.NewSource(time.Now().UnixNano()))
 
-	topics := []byte("house, mouse;")
+	maxTopics := r.Intn(4)
+	if maxTopics == 0 {
+		maxTopics = 1
+	}
+	maxInt := 10
+	topics := make([]byte, maxTopics)
+	//fills the array with unique random ascending values ranging from 0 to max
+	for i := 0; i < maxTopics; i++ {
+		topics[i] = byte(mr.Intn(maxInt))
+
+		for j := 0; j < i; j++ {
+			if topics[i] == topics[j] {
+				i--
+				break
+			}
+		}
+	}
+
+	fmt.Println("topics", topics)
+
 	text := []byte("I am a house in a mouse " + strconv.Itoa(r.Intn(100)) + ";")
 	tweet = append(tweet, topics...)
 	tweet = append(tweet, text...)

+ 5 - 5
follower/follower.go

@@ -47,10 +47,10 @@ var leaderPublicKey *[32]byte
 
 //needs to be changed at leader/follower/client at the same time
 const neededSubscriptions = 1
-const dataLength = 64
+const dataLength = 128
 const numThreads = 12
 
-var dbWriteSize int = 4
+var dbWriteSize int = 100
 var maxTimePerRound time.Duration = 5 * time.Second
 
 var round int = 1
@@ -145,7 +145,7 @@ func main() {
 
 	for {
 
-		fmt.Println("Phase 1")
+		fmt.Println("Phase 1 Round", round)
 
 		//create write db for this round
 		for i := 0; i < dbWriteSize; i++ {
@@ -586,12 +586,12 @@ func handlePirQuery(clientKeys clientKeys, leaderWorkerConnection net.Conn, subP
 	copy(decryptNonce[:], message[:24])
 	decrypted, ok := box.Open(nil, message[24:], &decryptNonce, &clientPublicKey, followerPrivateKey)
 	if !ok {
-		panic("pirQuery decryption not ok")
+		fmt.Println("pirQuery decryption not ok")
+		return clientKeys, nil, true
 	}
 
 	//gets sharedSecret
 	if subPhase == 0 {
-		//bs!
 		var newSharedSecret [32]byte
 		for index := 0; index < 32; index++ {
 			newSharedSecret[index] = decrypted[index]

+ 11 - 10
leader/leader.go

@@ -60,9 +60,9 @@ var archiveTopicAmount int
 const roundsBeforeUpdate = 5
 const neededSubscriptions = 1
 const numThreads = 12
-const dataLength = 64
+const dataLength = 128
 
-var dbWriteSize int = 4
+var dbWriteSize int = 100
 var maxTimePerRound time.Duration = 5 * time.Second
 
 //counts the number of rounds
@@ -169,7 +169,7 @@ func main() {
 		for {
 			clientConnection, err := lnClients.Accept()
 			if err != nil {
-				fmt.Println(err)
+				fmt.Println("error", err)
 			}
 			clientConnection.SetDeadline(time.Time{})
 
@@ -182,7 +182,7 @@ func main() {
 			//send leader publicKey
 			_, err = clientConnection.Write(leaderPublicKey[:])
 			if err != nil {
-				fmt.Println(err)
+				fmt.Println("error", err)
 				clientConnection.Close()
 				break
 			}
@@ -190,7 +190,7 @@ func main() {
 			//send follower publicKey
 			_, err = clientConnection.Write(followerPublicKey[:])
 			if err != nil {
-				fmt.Println(err)
+				fmt.Println("error", err)
 				clientConnection.Close()
 				break
 			}
@@ -200,7 +200,7 @@ func main() {
 			//gets publicKey from client
 			_, err = clientConnection.Read(tmpClientPublicKey[:])
 			if err != nil {
-				fmt.Println(err)
+				fmt.Println("error", err)
 				clientConnection.Close()
 				break
 			}
@@ -233,7 +233,7 @@ func main() {
 		startTime = time.Now()
 		phase[0] = 1
 
-		fmt.Println("Phase 1")
+		fmt.Println("Phase 1 Round", round)
 
 		//creates a new write Db for this round
 		for i := 0; i < dbWriteSize; i++ {
@@ -1051,7 +1051,8 @@ func handlePirQuery(clientKeys clientKeys, clientConnection net.Conn, followerCo
 	copy(decryptNonce[:], leaderBox[:24])
 	decrypted, ok := box.Open(nil, leaderBox[24:], &decryptNonce, clientPublicKey, leaderPrivateKey)
 	if !ok {
-		panic("pirQuery decryption not ok")
+		fmt.Println("pirQuery decryption not ok")
+		return clientKeys, nil, true
 	}
 
 	//if sharedSecret is send
@@ -1164,7 +1165,7 @@ func writeToWError(connection net.Conn, array []byte, followerConnection net.Con
 	if err != nil {
 		//lets follower know that client has disconnected unexpectedly
 		if connection.RemoteAddr().String() != follower {
-			fmt.Println(err)
+			fmt.Println("error", err)
 			array := make([]byte, size)
 			array[0] = 1
 			_, err = followerConnection.Write(array)
@@ -1187,7 +1188,7 @@ func readFrom(connection net.Conn, size int, followerConnection net.Conn, sizeEr
 	if err != nil {
 		//lets follower know that client has disconnected unexpectedly
 		if connection.RemoteAddr().String() != follower {
-			fmt.Println(err)
+			fmt.Println("error", err)
 			array := make([]byte, sizeError)
 			array[0] = 1
 			_, err = followerConnection.Write(array)

+ 3 - 3
lib/databaseRead.go

@@ -134,14 +134,14 @@ func tweetsToByteArray(tweetsToReturn [][]Tweet) []byte {
 				tweetToAppend = append(tweetToAppend, []byte(topic)...)
 				tweetToAppend = append(tweetToAppend, ","...)
 			}
-			//replaces last "," with ";" bc there is text following and not another topic
+			//replaces last "," with ";;" bc there is text following and not another topic
 			tweetToAppend = tweetToAppend[:len(tweetToAppend)-1]
-			tweetToAppend = append(tweetToAppend, []byte(";")[0])
+			tweetToAppend = append(tweetToAppend, []byte(";;")[:]...)
 			tweetToAppend = append(tweetToAppend, []byte(tweet.Text)...)
 			tweetToAppend = append(tweetToAppend, []byte(";")[0])
 		}
 		//adds padding
-		tweetToAppend = append(tweetToAppend, []byte(";")[0])
+		tweetToAppend = append(tweetToAppend, []byte(";;")[:]...)
 		length := minimumBlockSize - len(tweetToAppend)
 		padding := make([]byte, length)
 		rand.Read(padding)