git-svn-id: svn://db.shs.com.ru/pip@226 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5

This commit is contained in:
2016-08-17 15:40:37 +00:00
parent 2a7e45baff
commit f76a27d6cf
12 changed files with 253 additions and 65 deletions

View File

@@ -0,0 +1,5 @@
add_executable(picrypt "main.cpp")
target_link_libraries(picrypt pip)
if (DEFINED LIB)
install(TARGETS picrypt DESTINATION ${CMAKE_INSTALL_PREFIX}/bin)
endif ()

100
utils/crypt_tool/main.cpp Normal file
View File

@@ -0,0 +1,100 @@
#include "pip.h"
#include "picrypt.h"
using namespace PICoutManipulators;
void usage() {
piCout << Bold << "PIP Crypt Utility";
piCout << Cyan << "Version" << Bold << PIPVersion() << NewLine;
piCout << Green << Bold << "Usage:" << Default << "\"picrypt [-thr] [-g <pass>] [-o <out_file>] [{[-c <file>] [-u <file>]} {[-p <pass>] [-s <hash>] [-k <key_file>] [-x <hex_key>]}]\"" << NewLine;
piCout << Green << Bold << "Details:";
piCout << "-h --help " << Green << "- display this message and exit";
piCout << "-g --genhash " << Green << "- generate hash from <pass> string";
piCout << "-t --text " << Green << "- output in text base64";
piCout << "-o --out <out_file> " << Green << "- write out to file <out_file>";
piCout << "-c --crypt <file> " << Green << "- crypt file <file> using secret <pass> or <hash> or <key_file>";
piCout << "-u --uncrypt <file> " << Green << "- uncrypt file <file> using secret <pass> or <hash> or <key_file>";
piCout << "-p --pass <pass> " << Green << "- use secret from passphrase <pass>";
piCout << "-s --secret <hash> " << Green << "- use secret from hash <hash>";
piCout << "-k --key <key_file> " << Green << "- use secret from binary key_file <key_file>";
piCout << "-x --hex [<hex_key>] " << Green << "- use secret from hex hash <hex_key> or output in text hex";
piCout << "-r --random " << Green << "- generate random secret key";
}
int main (int argc, char * argv[]) {
PICrypt crypt;
PIByteArray bout;
PICLI cli(argc, argv);
cli.addArgument("genhash", true);
cli.addArgument("out", true);
cli.addArgument("crypt", true);
cli.addArgument("uncrypt", true);
cli.addArgument("pass", true);
cli.addArgument("secret", true);
cli.addArgument("key", true);
cli.addArgument("hex", 'x', "hex", true);
cli.addArgument("help");
cli.addArgument("text");
cli.addArgument("random");
if (!(cli.hasArgument("genhash") ||
cli.hasArgument("crypt") ||
cli.hasArgument("uncrypt") ||
cli.hasArgument("random")) ||
cli.hasArgument("help")) {
usage();
return 0;
}
PIString out_file = cli.argumentValue("out");
PIFile outf;
if (!out_file.isEmpty()) {
if (outf.open(out_file, PIIODevice::ReadWrite)) {
outf.resize(0);
} else piCout << "error: while open out file";
}
if (cli.hasArgument("genhash")) {
PIString s = cli.argumentValue("genhash");
bout = crypt.hash(s);
} else if (cli.hasArgument("random")) bout = crypt.generateKey();
if (cli.hasArgument("crypt") || cli.hasArgument("uncrypt")) {
PIByteArray secret;
if (cli.hasArgument("key")) {
PIFile keyf(cli.argumentValue("key"), PIIODevice::ReadOnly);
if (keyf.open()) secret = keyf.readAll();
else piCout << "error: while open key file";
}
if (cli.hasArgument("pass")) secret = crypt.hash(cli.argumentValue("pass"));
if (cli.hasArgument("secret")) secret = PIByteArray::fromBase64(cli.argumentValue("secret").toByteArray());
if (cli.hasArgument("hex")) secret = PIByteArray::fromHex(cli.argumentValue("hex").toByteArray());
if (secret.size() == crypt.sizeKey()) {
PIFile inf;
PIString in_file;
PIByteArray bin;
crypt.setKey(secret);
if (cli.hasArgument("crypt")) in_file = cli.argumentValue("crypt");
if (cli.hasArgument("uncrypt")) in_file = cli.argumentValue("uncrypt");
if (inf.open(in_file, PIIODevice::ReadOnly)) {
bin = inf.readAll();
if (cli.hasArgument("crypt")) bout = crypt.crypt(bin);
if (cli.hasArgument("uncrypt")) bout = crypt.decrypt(bin);
} else piCout << "error: while open input file";
} else piCout << "error: invalid secret";
}
if (!bout.isEmpty()) {
if (cli.hasArgument("text")) bout.convertToBase64();
else if (cli.hasArgument("hex") && cli.argumentValue("hex").isEmpty()) bout = bout.toHex().toByteArray();
if (outf.isOpened()) {
outf.write(bout);
outf.close();
}
else fwrite(bout.data(), 1, bout.size(), stdout);
}
return 0;
}

View File

@@ -178,14 +178,14 @@ void Daemon::TileFileProgress::tileEvent(PIScreenTile * t, TileEvent e) {
Daemon::Daemon(): PIPeer(pisd_prefix + PISystemInfo::instance()->hostname + "_" + PIString(rand() % 100)), fm(this) {
setName("Daemon");
timer.setName("__S__Daemon_timer");
dtimer.setName("__S__Daemon_timer");
mode = offset = cur = height = 0;
CONNECTU(&screen, keyPressed, this, keyEvent)
CONNECTU(&timer, tickEvent, this, timerEvent)
CONNECTU(&dtimer, tickEvent, this, timerEvent)
CONNECTU(&fm, tileKey, this, fmKeyEvent)
CONNECTU(&fm, actionRequest, this, fmActionRequest)
timer.addDelimiter(5);
timer.start(200);
dtimer.addDelimiter(5);
dtimer.start(200);
//CONNECTU(&console, keyPressed, this, keyEvent)
//dir.setDir("/home/peri4/Documents");

View File

@@ -54,6 +54,9 @@ public:
bool lockedEth() const {return eth_mutex.isLocked();}
bool lockedPeers() const {return peers_mutex.isLocked();}
bool lockedMBcasts() const {return mc_mutex.isLocked();}
bool lockedSends() const {return send_mutex.isLocked();}
bool lockedMCSends() const {return send_mc_mutex.isLocked();}
private:
enum PacketType {
@@ -138,7 +141,7 @@ private:
void sendDirToRemote(Remote * r);
mutable PIStringList available_daemons;
PITimer timer;
PITimer dtimer;
PIString conn_name;
PIMutex remote_mutex;
PIMap<int, PIString> dnames;

View File

@@ -1,6 +1,6 @@
/*
PIP - Platform Independent Primitives
Remote console viewer
PIp System Daemon
Copyright (C) 2016 Ivan Pelipenko peri4ko@gmail.com
This program is free software: you can redistribute it and/or modify
@@ -73,7 +73,11 @@ public:
mt = tpeer = peerTile();
mt->hide(); mt->name() = "peer info";
center->addTile(mt); mtiles << mt;
mt = tpeerdiag = peerDiagTile();
mt->hide(); mt->name() = "peer diag";
center->addTile(mt); mtiles << mt;
TilePICout * outt = new TilePICout();
outt->size_policy = PIScreenTypes::Expanding;
screen.rootTile()->addTile(outt);
@@ -91,6 +95,7 @@ public:
ret->content << TileList::Row("Connect to another daemon", CellFormat());
ret->content << TileList::Row("Peer info", CellFormat());
ret->content << TileList::Row("Peer reinit", CellFormat());
ret->content << TileList::Row("Peer diagnostics", CellFormat());
ret->content << TileList::Row("Exit", CellFormat());
ret->selection_mode = TileList::NoSelection;
return ret;
@@ -107,6 +112,23 @@ public:
ret->content << TileSimple::Row(" CPU count: " + PIString::fromNumber(PISystemInfo::instance()->processorsCount), CellFormat());
return ret;
}
PIScreenTile * peerDiagTile() {
PIScreenTile * ret = new PIScreenTile();
TileSimple * htl = new TileSimple();
htl->size_policy = PIScreenTypes::Fixed;
ret->direction = PIScreenTypes::Vertical;
htl->content << TileSimple::Row("Peer: " + daemon_.name() + " | " + daemon_.selfInfo().name, CellFormat(PIScreenTypes::Default, PIScreenTypes::Default, PIScreenTypes::Bold));
PIScreenTile * diag = new PIScreenTile();
diag->direction = PIScreenTypes::Horizontal;
peerdiagdata_tl = new TileSimple();
peerdiagservice_tl = new TileSimple();
ret->addTile(htl);
ret->addTile(diag);
diag->addTile(peerdiagdata_tl);
diag->addTile(peerdiagservice_tl);
// updatePeerInfo();
return ret;
}
PIScreenTile * peerTile() {
PIScreenTile* ret = new PIScreenTile();
TileSimple * htl = new TileSimple();
@@ -123,9 +145,21 @@ public:
ret->addTile(peerinfo_tl);
ret->addTile(addrs_tl);
ret->addTile(peermap_tl);
updatePeerInfo();
// updatePeerInfo();
return ret;
}
void updatePeerDiag(TileSimple * tl, const PIDiagnostics & diag) {
tl->content.clear();
tl->content << TileSimple::Row(diag.name() + " diagnostics", CellFormat(PIScreenTypes::Default, PIScreenTypes::Default, PIScreenTypes::Bold));
tl->content << TileSimple::Row("Received count: " + PIString::fromNumber(diag.receiveCount()), CellFormat());
tl->content << TileSimple::Row("Invalid count: " + PIString::fromNumber(diag.wrongCount()), CellFormat());
tl->content << TileSimple::Row("Sended count: " + PIString::fromNumber(diag.sendCount()), CellFormat());
tl->content << TileSimple::Row("Immediate Frequency, Hz: " + PIString::fromNumber(diag.immediateFrequency()), CellFormat());
tl->content << TileSimple::Row("Integral Frequency, Hz: " + PIString::fromNumber(diag.integralFrequency()), CellFormat());
tl->content << TileSimple::Row("Receive speed: " + diag.receiveSpeed(), CellFormat());
tl->content << TileSimple::Row("Send speed: " + diag.sendSpeed(), CellFormat());
tl->content << TileSimple::Row("Quality: " + PIString::fromNumber((int)diag.quality()), CellFormat());
}
void updatePeerInfo() {
bool pm = daemon_.lockedPeers();
screen.lock();
@@ -136,6 +170,9 @@ public:
peermap_tl->content.clear();
peers_tl->content << TileList::Row("this | 0 | 0 | " + PIString::fromNumber(daemon_.allPeers().size_s()) +
" [em = " + PIString::fromBool(daemon_.lockedEth()) + ", "
"mm = " + PIString::fromBool(daemon_.lockedMBcasts()) + ", "
"sm = " + PIString::fromBool(daemon_.lockedSends()) + ", "
"ms = " + PIString::fromBool(daemon_.lockedMCSends()) + ", "
"pm = " + PIString::fromBool(pm) + "]", CellFormat());
piForeachC(PIPeer::PeerInfo &p , daemon_.allPeers())
peers_tl->content << TileList::Row(p.name + " | d = " + PIString::fromNumber(p.dist) +
@@ -160,6 +197,8 @@ public:
}
piForeachC(PIString &s , peermap)
peermap_tl->content << TileList::Row(s, CellFormat());
updatePeerDiag(peerdiagdata_tl, daemon_.diagnosticData());
updatePeerDiag(peerdiagservice_tl, daemon_.diagnosticService());
daemon_.unlock();
screen.unlock();
}
@@ -176,14 +215,15 @@ public:
if (t == tmenu) {
if (e.type == TileList::RowPressed) {
piForeach (PIScreenTile * t, mtiles)
t->hide();
t->hide();
switch (e.data.toInt()) {
case 0: tinfo->show(); break;
case 1: tfm->show(); break;
case 2: daemon_.showMainList(); tdaemon->show(); break;
case 3: tpeer->show(); peers_tl->setFocus(); break;
case 4: daemon_.reinit(); tmenu->show(); break;
case 5: PIKbdListener::exiting = true; break;
case 0: tinfo->show(); break;
case 1: tfm->show(); break;
case 2: daemon_.showMainList(); tdaemon->show(); break;
case 3: tpeer->show(); peers_tl->setFocus(); break;
case 4: daemon_.reinit(); tmenu->show(); break;
case 5: tpeerdiag->show(); break;
case 6: PIKbdListener::exiting = true; break;
}
}
return;
@@ -198,13 +238,14 @@ public:
}
EVENT_HANDLER1(void, keyEvent, PIKbdListener::KeyEvent, e) {
if (screen.dialogTile()) return;
if (tpeer->visible || tinfo->visible)
if (tpeer->visible || tinfo->visible || tpeerdiag->visible)
if (e.key == PIKbdListener::Esc) menuRequest();
//piCout << "key" << e.key;
}
PIScreenTile * tmenu, * tinfo, * tfm, * tdaemon, * tpeer;
PIScreenTile * tmenu, * tinfo, * tfm, * tdaemon, * tpeer, * tpeerdiag;
TileList * peers_tl, * addrs_tl, * peermap_tl;
TileSimple * peerinfo_tl;
TileSimple * peerdiagdata_tl, * peerdiagservice_tl;
PIVector<PIScreenTile * > mtiles;
int cur_peer;
};