add encryption
This commit is contained in:
@@ -71,6 +71,15 @@ int PIClientServer::Client::write(const void * d, const size_t s) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void PIClientServer::Client::enableSymmetricEncryption(const PIByteArray & key) {
|
||||||
|
if (key.isNotEmpty()) {
|
||||||
|
stream.setCryptEnabled(true);
|
||||||
|
stream.setCryptKey(key);
|
||||||
|
} else
|
||||||
|
stream.setCryptEnabled(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void PIClientServer::Client::createForServer(PIEthernet * tcp_) {
|
void PIClientServer::Client::createForServer(PIEthernet * tcp_) {
|
||||||
tcp = tcp_;
|
tcp = tcp_;
|
||||||
tcp->setParameter(PIEthernet::KeepConnection, false);
|
tcp->setParameter(PIEthernet::KeepConnection, false);
|
||||||
|
|||||||
@@ -91,6 +91,11 @@ void PIClientServer::Server::setMaxClients(int new_max_clients) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void PIClientServer::Server::enableSymmetricEncryption(const PIByteArray & key) {
|
||||||
|
crypt_key = key;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void PIClientServer::Server::stopServer() {
|
void PIClientServer::Server::stopServer() {
|
||||||
if (!tcp_server) return;
|
if (!tcp_server) return;
|
||||||
tcp_server->stopAndWait();
|
tcp_server->stopAndWait();
|
||||||
@@ -98,8 +103,9 @@ void PIClientServer::Server::stopServer() {
|
|||||||
|
|
||||||
|
|
||||||
void PIClientServer::Server::newClient(Client * c) {
|
void PIClientServer::Server::newClient(Client * c) {
|
||||||
c->readed_func = [this, c](PIByteArray ba) { readed(c, ba); };
|
|
||||||
clients << c;
|
clients << c;
|
||||||
|
c->enableSymmetricEncryption(crypt_key);
|
||||||
|
c->readed_func = [this, c](PIByteArray ba) { readed(c, ba); };
|
||||||
c->tcp->startThreadedRead();
|
c->tcp->startThreadedRead();
|
||||||
c->connected();
|
c->connected();
|
||||||
piCout << "New client";
|
piCout << "New client";
|
||||||
|
|||||||
@@ -54,6 +54,8 @@ public:
|
|||||||
int write(const void * d, const size_t s);
|
int write(const void * d, const size_t s);
|
||||||
int write(const PIByteArray & ba) { return write(ba.data(), ba.size()); }
|
int write(const PIByteArray & ba) { return write(ba.data(), ba.size()); }
|
||||||
|
|
||||||
|
void enableSymmetricEncryption(const PIByteArray & key);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void readed(PIByteArray data) {}
|
virtual void readed(PIByteArray data) {}
|
||||||
virtual void connected() {}
|
virtual void connected() {}
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ public:
|
|||||||
|
|
||||||
void setClientFactory(std::function<Client *()> f) { client_factory = f; }
|
void setClientFactory(std::function<Client *()> f) { client_factory = f; }
|
||||||
|
|
||||||
void write(Client * c, const PIByteArray & data);
|
void enableSymmetricEncryption(const PIByteArray & key);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void readed(Client * c, PIByteArray data) {}
|
virtual void readed(Client * c, PIByteArray data) {}
|
||||||
@@ -61,6 +61,7 @@ private:
|
|||||||
std::function<Client *()> client_factory;
|
std::function<Client *()> client_factory;
|
||||||
PIEthernet * tcp_server = nullptr;
|
PIEthernet * tcp_server = nullptr;
|
||||||
PIThread * clean_thread = nullptr;
|
PIThread * clean_thread = nullptr;
|
||||||
|
PIByteArray crypt_key;
|
||||||
PIVector<Client *> clients;
|
PIVector<Client *> clients;
|
||||||
PIMutex clients_mutex;
|
PIMutex clients_mutex;
|
||||||
|
|
||||||
|
|||||||
9
main.cpp
9
main.cpp
@@ -28,7 +28,7 @@ protected:
|
|||||||
send_thread.start(
|
send_thread.start(
|
||||||
[this] {
|
[this] {
|
||||||
// write((PIString::fromNumber(++counter)).toUTF8());
|
// write((PIString::fromNumber(++counter)).toUTF8());
|
||||||
PIByteArray ba(64_MiB);
|
PIByteArray ba(64_KiB);
|
||||||
write(ba);
|
write(ba);
|
||||||
},
|
},
|
||||||
2_Hz);
|
2_Hz);
|
||||||
@@ -41,20 +41,21 @@ protected:
|
|||||||
int main(int argc, char * argv[]) {
|
int main(int argc, char * argv[]) {
|
||||||
kbd.enableExitCapture();
|
kbd.enableExitCapture();
|
||||||
|
|
||||||
piCout << argc;
|
|
||||||
|
|
||||||
PIClientServer::Server * s = nullptr;
|
PIClientServer::Server * s = nullptr;
|
||||||
PIClientServer::Client * c = nullptr;
|
PIClientServer::Client * c = nullptr;
|
||||||
|
|
||||||
if (argc > 1) {
|
if (argc > 1) {
|
||||||
// server
|
piCout << "Server";
|
||||||
s = new PIClientServer::Server();
|
s = new PIClientServer::Server();
|
||||||
s->setClientFactory([] { return new MyClient(); });
|
s->setClientFactory([] { return new MyClient(); });
|
||||||
|
s->enableSymmetricEncryption("1122334455667788"_hex);
|
||||||
s->listenAll(12345);
|
s->listenAll(12345);
|
||||||
} else {
|
} else {
|
||||||
// client
|
piCout << "Client";
|
||||||
c = new MyClient();
|
c = new MyClient();
|
||||||
c->createNew();
|
c->createNew();
|
||||||
|
c->enableSymmetricEncryption("1122334455667788"_hex);
|
||||||
c->connect(PINetworkAddress::resolve("127.0.0.1", 12345));
|
c->connect(PINetworkAddress::resolve("127.0.0.1", 12345));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user