123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- #include <mutex>
- #include <condition_variable>
- #include <chrono>
- #include <iostream>
- #include <fstream>
- #include <cstring>
- #include <cstdlib>
- #include <vector>
- #include <string>
- #include "networkClient.hpp"
- #define EPOCH_URI "/epoch"
- #define EXIT_URI "/exit"
- #define BGN_URI "/ws?c"
- #define BLIND_GEN_URI "/ws?_"
- #define BGN_TMP_FILE "~/tmp/bgn"
- #define GEN_TMP_FILE "~/tmp/generator"
- using namespace std;
- struct synchronization_tool exitSync, bgnSync, generatorSync;
- size_t epochNum = 0;
- // Initialize the classes we use
- void initialize_prsona_classes()
- {
- Scalar::init();
- PrsonaBase::init();
- PrsonaBase::set_client_malicious();
- }
- PrsonaClient *create_client_from_files(size_t numServers)
- {
- unique_lock<mutex> lck1(bgnSync.mtx);
- ifstream bgnFile(BGN_TMP_FILE);
- BGNPublicKey publicKey;
- bgnFile >> publicKey;
- lck1.unlock();
- unique_lock<mutex> lck2(generatorSync.mtx);
- ifstream genFile(GEN_TMP_FILE);
- vector<Proof> pi;
- Twistpoint generator;
- size_t sizeOfPi;
- genFile >> sizeOfPi;
- for (size_t i = 0; i < sizeOfPi; i++)
- {
- Proof currProof;
- genFile >> currProof;
- pi.push_back(currProof);
- }
- genFile >> generator;
- return new PrsonaClient(pi, generator, publicKey, numServers);
- }
- PrsonaClient *create_client(size_t numServers)
- {
- struct mg_connection *conn = mg_connect_websocket_client(
- serverIPs[0].c_str(),
- PRSONA_PORT,
- USE_SSL,
- NULL,
- 0,
- BGN_URI,
- "null",
- bgn_websocket_data_handler,
- bgn_websocket_close_handler,
- NULL);
- if (!conn)
- {
- cerr << "Couldn't obtain BGN details" << endl;
- return 1;
- }
- unique_lock<mutex> lck1(bgnSync.mtx);
- remove(BGN_TMP_FILE);
- bgnSync.val = 0;
- mg_websocket_client_write(
- conn,
- MG_WEBSOCKET_OPCODE_DATACOMPLETE,
- "",
- 0);
- while (!bgnSync.val)
- bgnSync.cv.wait(lck1);
- lck1.unlock();
- struct mg_connection *conn = mg_connect_websocket_client(
- serverIPs[0].c_str(),
- PRSONA_PORT,
- USE_SSL,
- NULL,
- 0,
- BLIND_GEN_URI,
- "null",
- blind_gen_websocket_data_handler,
- blind_gen_websocket_close_handler,
- NULL);
- if (!conn)
- {
- cerr << "Couldn't obtain BGN details" << endl;
- return 1;
- }
- unique_lock<mutex> lck2(generatorSync.mtx);
- remove(GEN_TMP_FILE);
- generatorSync.val = 0;
- mg_websocket_client_write(
- conn,
- MG_WEBSOCKET_OPCODE_DATACOMPLETE,
- "",
- 0);
- while (!generatorSync.val)
- generatorSync.cv.wait(lck2);
- lck2.unlock();
- return create_client_from_files(numServers);
- }
- int main(int argc, char *argv[])
- {
- initialize_prsona_classes();
- #if USE_SSL
- mg_init_library(0);
- #else
- mg_init_library(MG_FEATURES_SSL);
- #endif
-
- const char *options[] = {"listening_ports", PRSONA_PORT, 0};
- vector<string> serverIPs, clientIPs;
- string selfIP;
- char buffer[40];
- ifstream serverConfig("serverIPs.cfg");
- while (!serverConfig.eof())
- {
- serverConfig.getline(buffer, 40);
- if (strlen(buffer) > 0)
- serverIPs.push_back(string(buffer));
- }
- ifstream clientConfig("clientIPs.cfg");
- while (!clientConfig.eof())
- {
- clientConfig.getline(buffer, 40);
- if (strlen(buffer) > 0)
- clientIPs.push_back(string(buffer));
- }
- ifstream selfConfig("selfIP.cfg");
- while (!selfConfig.eof())
- {
- selfConfig.getline(buffer, 40);
- if (strlen(buffer) > 0)
- selfIP = buffer;
- }
- // Defaults
- size_t numServers = serverIPs.size();
- size_t numClients = clientIPs.size();
- bool maliciousServers = true;
- if (argc > 1)
- {
- bool setting = argv[1][0] == 't' || argv[1][0] == 'T';
- maliciousServers = setting;
- }
- cout << "Establishing PRSONA client with the following parameters: " << endl;
- cout << numServers << " PRSONA servers" << endl;
- cout << numClients << " PRSONA clients" << endl;
- cout << "Servers are set to " << (maliciousServers ? "MALICIOUS" : "HBC") << " security" << endl;
- cout << "This client is at IP address: " << selfIP << endl;
- cout << endl;
- // Set malicious flags where necessary
- if (maliciousServers)
- PrsonaBase::set_server_malicious();
- // Entities we operate with
- PrsonaClient *prsonaClient = create_client(numServers);
- CivetServer server(options);
- PrsonaClientWebSocketHandler wsHandler(prsonaClient, &updateMtx, &epochNum, serverIPs, selfIP);
- server.addWebSocketHandler("/ws", wsHandler);
- ExitHandler exitHandler;
- server.addHandler(EXIT_URI, exitHandler);
- unique_lock<mutex> lck(exitSync.mtx);
- while (!exitSync.val)
- exitSync.cv.wait(lck);
- mg_exit_library();
- delete prsonaClient;
- return 0;
- }
|