removed Qt from unix version of retroshare service. Enabled loging at start with option -u

This commit is contained in:
csoler 2019-08-27 21:54:17 +02:00
parent a84a96e0b7
commit 90d0686e88
No known key found for this signature in database
GPG Key ID: 7BCA522266C0804C
4 changed files with 196 additions and 57 deletions

View File

@ -52,6 +52,8 @@ extern JsonApiServer* jsonApiServer;
*/
struct JsonApiServer : RsSingleJobThread, p3Config
{
static const uint16_t DEFAULT_PORT = 9092 ;
/**
* @brief construct a JsonApiServer instance with given parameters
* @param[in] port listening port fpt the JSON API socket
@ -62,7 +64,7 @@ struct JsonApiServer : RsSingleJobThread, p3Config
* false otherwise, this usually requires user interacion to confirm access
*/
JsonApiServer(
uint16_t port = 9092,
uint16_t port = DEFAULT_PORT,
const std::string& bindAddress = "127.0.0.1",
const std::function<bool(const std::string&)> newAccessRequestCallback = [](const std::string&){return false;} );

View File

@ -22,6 +22,8 @@
/// RetroShare initialization and login API implementation
#include <unistd.h>
#include <math.h>
#include <termios.h>
#ifndef WINDOWS_SYS
// for locking instances
@ -42,6 +44,8 @@
#include "util/folderiterator.h"
#include "util/rsstring.h"
#include "retroshare/rsinit.h"
#include "retroshare/rsnotify.h"
#include "retroshare/rsiface.h"
#include "plugins/pluginmanager.h"
#include "rsserver/rsloginhandler.h"
@ -114,7 +118,7 @@ RsAccounts* rsAccounts = nullptr;
struct RsInitConfig
{
RsInitConfig() : jsonApiPort(0), jsonApiBindAddress("127.0.0.1") {}
RsInitConfig() : jsonApiPort(JsonApiServer::DEFAULT_PORT), jsonApiBindAddress("127.0.0.1") {}
RsFileHash main_executable_hash;
@ -243,14 +247,6 @@ void RsInit::InitRsConfig()
setOutputLevel(RsLog::Warning);
}
/********
* LOCALNET_TESTING - allows port restrictions
*
* #define LOCALNET_TESTING 1
*
********/
#ifdef LOCALNET_TESTING
std::string portRestrictions;
@ -264,6 +260,12 @@ bool doPortRestrictions = false;
#endif
#endif
/********
* LOCALNET_TESTING - allows port restrictions
*
* #define LOCALNET_TESTING 1
*
********/
int RsInit::InitRetroShare(int argc, char **argv, bool /* strictCheck */)
{
#ifdef DEBUG_RSINIT
@ -290,25 +292,20 @@ int RsInit::InitRetroShare(int argc, char **argv, bool /* strictCheck */)
argstream as(argc,argv);
as >> option('m',"minimized" ,rsInitConfig->startMinimised ,"Start minimized." )
>> option('s',"stderr" ,rsInitConfig->outStderr ,"output to stderr instead of log file." )
as >> option('s',"stderr" ,rsInitConfig->outStderr ,"output to stderr instead of log file." )
>> option('u',"udp" ,rsInitConfig->udpListenerOnly,"Only listen to UDP." )
>> option('e',"external-port" ,rsInitConfig->forceExtPort ,"Use a forwarded external port." )
>> parameter('c',"base-dir" ,opt_base_dir ,"directory", "Set base directory." ,false)
>> parameter('l',"log-file" ,rsInitConfig->logfname ,"logfile" ,"Set Log filename." ,false)
>> parameter('d',"debug-level" ,rsInitConfig->debugLevel ,"level" ,"Set debug level." ,false)
>> parameter('i',"ip-address" ,rsInitConfig->inet ,"nnn.nnn.nnn.nnn", "Force IP address to use (if cannot be detected)." ,false)
>> parameter('o',"opmode" ,rsInitConfig->opModeStr ,"opmode" ,"Set Operating mode (Full, NoTurtle, Gaming, Minimal)." ,false)
>> parameter('p',"port" ,rsInitConfig->port ,"port", "Set listenning port to use." ,false)
>> parameter('c',"base-dir" ,opt_base_dir ,"directory", "Set base directory." ,false)
>> parameter('U',"user-id" ,prefUserString ,"ID", "[ocation Id] Sets Account to Use, Useful when Autologin is enabled.",false);
>> parameter('U',"user-id" ,prefUserString ,"ID", "[ocation Id] Selected account to use and asks for passphrase. Use \"-u list\" in order to list available accounts.",false);
#ifdef RS_JSONAPI
as >> parameter(
"jsonApiPort", rsInitConfig->jsonApiPort, "jsonApiPort",
"Enable JSON API on the specified port", false )
>> parameter(
"jsonApiBindAddress", rsInitConfig->jsonApiBindAddress,
"jsonApiBindAddress", "JSON API Bind Address.", false);
as >> parameter('J', "jsonApiPort", rsInitConfig->jsonApiPort, "jsonApiPort", "Enable JSON API on the specified port", false )
>> parameter('P', "jsonApiBindAddress", rsInitConfig->jsonApiBindAddress, "jsonApiBindAddress", "JSON API Bind Address.", false);
#endif // ifdef RS_JSONAPI
#ifdef LOCALNET_TESTING
@ -322,6 +319,7 @@ int RsInit::InitRetroShare(int argc, char **argv, bool /* strictCheck */)
as >> help('h',"help","Display this Help");
as.defaultErrorHandling(true,true);
if(rsInitConfig->autoLogin) rsInitConfig->startMinimised = true ;
if(rsInitConfig->outStderr) rsInitConfig->haveLogFile = false ;
if(!rsInitConfig->logfname.empty()) rsInitConfig->haveLogFile = true;
@ -408,20 +406,88 @@ int RsInit::InitRetroShare(int argc, char **argv, bool /* strictCheck */)
// choose alternative account.
if(prefUserString != "")
{
if(prefUserString == "list")
{
std::cerr << "Available accounts:" << std::endl;
std::list<RsPeerId> account_ids;
RsAccounts::GetAccountIds(account_ids);
int account_number_size = (int)ceil(log(account_ids.size())/log(10.0f)) ;
int i=1;
for(auto it(account_ids.begin());it!=account_ids.end();++it,++i)
{
RsPgpId pgp_id;
std::string pgp_name;
std::string loc_name;
std::string pgp_email;
RsAccounts::GetAccountDetails(*it,pgp_id,pgp_name,pgp_email,loc_name);
std::cout << "[" << std::setw(account_number_size) << std::setfill('0')
<< i << "] " << *it << " (" << pgp_id << "): " << pgp_name << " \t (" << loc_name << ")" << std::endl;
}
int nacc=0;
while(nacc < 1 || nacc >= account_ids.size())
{
std::cout << "Please enter account number: ";
std::cout.flush();
std::string str;
std::getline(std::cin, str);
nacc = atoi(str.c_str());
i=1;
for(auto it(account_ids.begin());it!=account_ids.end();++it,++i)
if(i==nacc)
{
prefUserString = (*it).toStdString();
break;
}
nacc=0; // allow to continue if something goes wrong.
RsPeerId ssl_id(prefUserString);
if(ssl_id.isNull())
{
std::cerr << "Invalid User location id: not found in list";
std::cerr << std::endl;
return RS_INIT_AUTH_FAILED ;
continue;
}
if(RsAccounts::SelectAccount(ssl_id))
if(!RsAccounts::SelectAccount(ssl_id))
continue;
if(!RsLoginHandler::getSSLPassword(ssl_id,true,rsInitConfig->passwd))
{
std::cerr << "Auto-selectng account ID " << ssl_id << std::endl;
return RS_INIT_HAVE_ACCOUNT;
std::cerr << "No valid password supplied for this account." << std::endl;
continue;
}
std::string lockFile;
int retVal = RsInit::LockAndLoadCertificates(false, lockFile);
switch (retVal)
{
case 0: break;
case 1: std::cerr << "Another RetroShare using the same profile is already running on your system. Please close "
"that instance first\n Lock file:\n" << lockFile << std::endl;
continue;
case 2: std::cerr << "An unexpected error occurred when Retroshare tried to acquire the single instance lock\n Lock file:\n"
<< lockFile.c_str() << std::endl;
continue;
case 3:
default: std::cerr << "Rshare::loadCertificate() unexpected switch value " << retVal << std::endl;
continue;
}
RsControl::instance()->StartupRetroShare();
break;
}
}
}
#ifdef RS_AUTOLOGIN

View File

@ -20,34 +20,99 @@
CrashStackTrace gCrashStackTrace;
#include <QCoreApplication>
#include <csignal>
#include <QObject>
#include <QStringList>
#ifndef __ANDROID__
#include <termios.h>
#endif
#ifdef __ANDROID__
# include <QAndroidService>
# include <QCoreApplication>
# include <QObject>
# include <QStringList>
# include "util/androiddebug.h"
#endif // def __ANDROID__
#include "retroshare/rsinit.h"
#include "retroshare/rsiface.h"
#ifdef __ANDROID__
# include "util/androiddebug.h"
#endif
#ifndef RS_JSONAPI
# error Inconsistent build configuration retroshare_service needs rs_jsonapi
#endif
int getch() {
int ch;
struct termios t_old, t_new;
tcgetattr(STDIN_FILENO, &t_old);
t_new = t_old;
t_new.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &t_new);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &t_old);
return ch;
}
std::string readStringFromKeyboard(const char *prompt, bool show_asterisk=true)
{
const char BACKSPACE=127;
const char RETURN=10;
std::string password;
unsigned char ch=0;
std::cout <<prompt; std::cout.flush();
while((ch=getch())!=RETURN)
{
if(ch==BACKSPACE)
{
if(password.length()!=0)
{
if(show_asterisk)
std::cout <<"\b \b";
password.resize(password.length()-1);
}
}
else
{
password+=ch;
if(show_asterisk)
std::cout <<'*';
else
std::cout << ch,std::cout.flush();
}
}
std::cout <<std::endl;
return password;
}
class NotifyTxt: public NotifyClient
{
public:
NotifyTxt(){}
virtual ~NotifyTxt() {}
virtual bool askForPassword(const std::string& title, const std::string& question, bool prev_is_bad, std::string& password,bool& cancel)
{
std::string question1=title + "\nPlease enter your PGP password for key:\n " + question + " :";
password = readStringFromKeyboard(question1.c_str()) ;
cancel = false ;
return !password.empty();
}
};
int main(int argc, char* argv[])
{
#ifdef __ANDROID__
AndroidStdIOCatcher dbg; (void) dbg;
QAndroidService app(argc, argv);
#else // def __ANDROID__
QCoreApplication app(argc, argv);
#endif // def __ANDROID__
signal(SIGINT, QCoreApplication::exit);
signal(SIGTERM, QCoreApplication::exit);
@ -55,23 +120,24 @@ int main(int argc, char* argv[])
signal(SIGBREAK, QCoreApplication::exit);
#endif // ifdef SIGBREAK
#endif // def __ANDROID__
RsInit::InitRsConfig();
// clumsy way to enable JSON API by default
if(!QCoreApplication::arguments().contains("--jsonApiPort"))
{
int argc2 = argc + 2;
char* argv2[argc2]; for (int i = 0; i < argc; ++i ) argv2[i] = argv[i];
char opt[] = "--jsonApiPort";
char val[] = "9092";
argv2[argc] = opt;
argv2[argc+1] = val;
RsInit::InitRetroShare(argc2, argv2, true);
}
else RsInit::InitRetroShare(argc, argv, true);
RsControl::earlyInitNotificationSystem();
#ifndef __ANDROID__
NotifyTxt *notify = new NotifyTxt();
rsNotify->registerNotifyClient(notify);
#endif
if(RsInit::InitRetroShare(argc, argv, true))
{
std::cerr << "Could not properly init Retroshare core." << std::endl;
return 1;
}
#ifdef __ANDROID__
rsControl->setShutdownCallback(QCoreApplication::exit);
QObject::connect(
&app, &QCoreApplication::aboutToQuit,
[](){
@ -79,4 +145,9 @@ int main(int argc, char* argv[])
RsControl::instance()->rsGlobalShutDown(); } );
return app.exec();
#else
while(true)
sleep(1);
#endif
}