2009-02-03 14:42:07 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* "$Id: retroshare.cc,v 1.4 2007-04-21 19:08:51 rmf24 Exp $"
|
|
|
|
*
|
|
|
|
* RetroShare C++ Interface.
|
|
|
|
*
|
|
|
|
* Copyright 2004-2006 by Robert Fernie.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License Version 2 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
* USA.
|
|
|
|
*
|
|
|
|
* Please report all bugs and problems to "retroshare@lunamutt.com".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2010-08-06 05:40:23 -04:00
|
|
|
#include <retroshare/rsiface.h> /* definition of iface */
|
|
|
|
#include <retroshare/rsinit.h> /* definition of iface */
|
2009-02-03 14:42:07 -05:00
|
|
|
|
|
|
|
#include "notifytxt.h"
|
|
|
|
|
2012-04-12 19:29:39 -04:00
|
|
|
#include <unistd.h>
|
2013-08-06 13:01:38 -04:00
|
|
|
#include <util/argstream.h>
|
2009-02-03 14:42:07 -05:00
|
|
|
#include <iostream>
|
|
|
|
#ifdef WINDOWS_SYS
|
|
|
|
#include <winsock2.h>
|
|
|
|
#endif
|
|
|
|
|
2012-02-07 12:01:07 -05:00
|
|
|
#ifdef RS_INTRO_SERVER
|
|
|
|
#include "introserver.h"
|
|
|
|
#endif
|
2009-02-03 14:42:07 -05:00
|
|
|
|
2015-03-18 14:48:43 -04:00
|
|
|
#ifdef ENABLE_WEBUI
|
2015-03-18 16:20:40 -04:00
|
|
|
#include <stdarg.h>
|
2015-03-18 14:48:43 -04:00
|
|
|
#include "api/ApiServerMHD.h"
|
|
|
|
#include "api/RsControlModule.h"
|
2015-03-31 14:00:40 -04:00
|
|
|
#include "TerminalApiClient.h"
|
2013-08-06 13:01:38 -04:00
|
|
|
#endif
|
|
|
|
|
2009-02-03 14:42:07 -05:00
|
|
|
/* Basic instructions for running libretroshare as background thread.
|
|
|
|
* ******************************************************************* *
|
|
|
|
* This allows your program to communicate with authenticated peers.
|
|
|
|
*
|
|
|
|
* libretroshare's interfaces are defined in libretroshare/src/rsiface.
|
|
|
|
* This should be the only headers that you need to include.
|
|
|
|
*
|
|
|
|
* The startup routine's are defined in rsiface.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2015-03-18 14:48:43 -04:00
|
|
|
#ifdef ENABLE_WEBUI
|
|
|
|
|
2015-05-03 10:46:10 -04:00
|
|
|
std::string docroot = resource_api::getDefaultDocroot();
|
2015-03-22 08:29:30 -04:00
|
|
|
uint16_t httpPort = 0;
|
2015-03-26 03:20:28 -04:00
|
|
|
std::string listenAddress;
|
|
|
|
bool allowAllIps = false;
|
2015-03-22 08:29:30 -04:00
|
|
|
|
|
|
|
argstream args(argc, argv);
|
|
|
|
args >> parameter("webinterface", httpPort, "port", "Enable webinterface on the specified port", false);
|
|
|
|
args >> parameter("docroot", docroot, "path", "Serve static files from this path.", false);
|
2015-03-26 03:20:28 -04:00
|
|
|
// unfinished
|
|
|
|
//args >> parameter("http-listen", listenAddress, "ipv6 address", "Listen only on the specified address.", false);
|
2016-04-04 06:07:09 -04:00
|
|
|
args >> option("http-allow-all", allowAllIps, "allow connections from all IP adresses (default= localhost only)");
|
2015-11-20 23:40:00 -05:00
|
|
|
args >> help('h',"help","Display this Help");
|
2016-04-04 06:07:09 -04:00
|
|
|
|
2015-03-22 08:29:30 -04:00
|
|
|
if (args.helpRequested())
|
|
|
|
{
|
|
|
|
std::cerr << args.usage() << std::endl;
|
2015-05-03 10:46:10 -04:00
|
|
|
// print libretroshare command line args and exit
|
|
|
|
RsInit::InitRsConfig();
|
|
|
|
RsInit::InitRetroShare(argc, argv, true);
|
|
|
|
return 0;
|
2015-03-18 14:48:43 -04:00
|
|
|
}
|
|
|
|
|
2015-03-31 14:00:40 -04:00
|
|
|
resource_api::ApiServer api;
|
|
|
|
resource_api::RsControlModule ctrl_mod(argc, argv, api.getStateTokenServer(), &api, true);
|
2015-11-20 23:40:00 -05:00
|
|
|
api.addResourceHandler("control", dynamic_cast<resource_api::ResourceRouter*>(&ctrl_mod), &resource_api::RsControlModule::handleRequest);
|
2015-03-31 14:00:40 -04:00
|
|
|
|
|
|
|
resource_api::ApiServerMHD* httpd = 0;
|
2015-03-22 08:29:30 -04:00
|
|
|
if(httpPort != 0)
|
|
|
|
{
|
2015-03-31 14:00:40 -04:00
|
|
|
httpd = new resource_api::ApiServerMHD(&api);
|
|
|
|
if(!httpd->configure(docroot, httpPort, listenAddress, allowAllIps))
|
2015-03-26 03:20:28 -04:00
|
|
|
{
|
|
|
|
std::cerr << "Failed to configure the http server. Check your parameters." << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
2015-03-31 14:00:40 -04:00
|
|
|
httpd->start();
|
|
|
|
}
|
2015-03-18 14:48:43 -04:00
|
|
|
|
2015-03-31 14:00:40 -04:00
|
|
|
resource_api::TerminalApiClient tac(&api);
|
2017-07-08 11:38:09 -04:00
|
|
|
tac.start();
|
2017-07-09 15:37:39 -04:00
|
|
|
bool already = false ;
|
2017-07-08 11:38:09 -04:00
|
|
|
|
2015-03-31 14:00:40 -04:00
|
|
|
while(ctrl_mod.processShouldExit() == false)
|
|
|
|
{
|
2017-07-09 15:37:39 -04:00
|
|
|
usleep(1000*1000);
|
2017-07-08 11:38:09 -04:00
|
|
|
|
2017-07-09 15:37:39 -04:00
|
|
|
if(!tac.isRunning() && !already)
|
2017-07-08 11:38:09 -04:00
|
|
|
{
|
2017-07-09 15:37:39 -04:00
|
|
|
std::cerr << "Terminal API client terminated." << std::endl;
|
|
|
|
already = true ;
|
2017-07-08 11:38:09 -04:00
|
|
|
}
|
2015-03-31 14:00:40 -04:00
|
|
|
}
|
2015-03-18 14:48:43 -04:00
|
|
|
|
2015-03-31 14:00:40 -04:00
|
|
|
if(httpd)
|
|
|
|
{
|
|
|
|
httpd->stop();
|
|
|
|
delete httpd;
|
2015-03-22 08:29:30 -04:00
|
|
|
}
|
2015-03-31 14:00:40 -04:00
|
|
|
|
|
|
|
return 0;
|
2015-03-18 14:48:43 -04:00
|
|
|
#endif
|
|
|
|
|
2009-02-03 14:42:07 -05:00
|
|
|
/* Retroshare startup is configured using an RsInit object.
|
|
|
|
* This is an opaque class, which the user cannot directly tweak
|
|
|
|
* If you want to peek at whats happening underneath look in
|
|
|
|
* libretroshare/src/rsserver/p3face-startup.cc
|
|
|
|
*
|
|
|
|
* You create it with InitRsConfig(), and delete with CleanupRsConfig()
|
|
|
|
* InitRetroshare(argv, argc, config) parses the command line options,
|
|
|
|
* and initialises the config paths.
|
|
|
|
*
|
|
|
|
* *** There are several functions that I should add to modify
|
|
|
|
* **** the config the moment these can only be set via the commandline
|
|
|
|
* - RsConfigDirectory(...) is probably the most useful.
|
|
|
|
* - RsConfigNetAddr(...) for setting port, etc.
|
|
|
|
* - RsConfigOutput(...) for logging and debugging.
|
|
|
|
*
|
|
|
|
* Next you need to worry about loading your certificate, or making
|
|
|
|
* a new one:
|
|
|
|
*
|
|
|
|
* RsGenerateCertificate(...) To create a new key, certificate
|
|
|
|
* LoadPassword(...) set password for existing certificate.
|
|
|
|
**/
|
|
|
|
|
2012-08-09 09:45:05 -04:00
|
|
|
bool strictCheck = true;
|
2009-02-08 09:30:28 -05:00
|
|
|
RsInit::InitRsConfig();
|
2012-08-09 09:45:05 -04:00
|
|
|
int initResult = RsInit::InitRetroShare(argc, argv, strictCheck);
|
2010-11-03 18:54:34 -04:00
|
|
|
|
|
|
|
if (initResult < 0) {
|
|
|
|
/* Error occured */
|
|
|
|
switch (initResult) {
|
|
|
|
case RS_INIT_AUTH_FAILED:
|
|
|
|
std::cerr << "RsInit::InitRetroShare AuthGPG::InitAuth failed" << std::endl;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* Unexpected return code */
|
|
|
|
std::cerr << "RsInit::InitRetroShare unexpected return code " << initResult << std::endl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2009-02-03 14:42:07 -05:00
|
|
|
|
|
|
|
/* load password should be called at this point: LoadPassword()
|
|
|
|
* otherwise loaded from commandline.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/* Now setup the libretroshare interface objs
|
|
|
|
* You will need to create you own NotifyXXX class
|
|
|
|
* if you want to receive notifications of events */
|
|
|
|
|
2014-01-07 17:51:22 -05:00
|
|
|
// This is needed to allocate rsNotify, so that it can be used to ask for PGP passphrase
|
|
|
|
//
|
|
|
|
RsControl::earlyInitNotificationSystem() ;
|
|
|
|
|
|
|
|
NotifyTxt *notify = new NotifyTxt() ;
|
|
|
|
rsNotify->registerNotifyClient(notify);
|
2009-02-03 14:42:07 -05:00
|
|
|
|
2014-01-31 22:26:26 -05:00
|
|
|
/* PreferredId => Key + Certificate are loaded into libretroshare */
|
2009-02-03 14:42:07 -05:00
|
|
|
|
2011-04-11 09:15:48 -04:00
|
|
|
std::string error_string ;
|
|
|
|
int retVal = RsInit::LockAndLoadCertificates(false,error_string);
|
2010-07-01 16:30:36 -04:00
|
|
|
switch(retVal)
|
|
|
|
{
|
|
|
|
case 0: break;
|
|
|
|
case 1: std::cerr << "Error: another instance of retroshare is already using this profile" << std::endl;
|
|
|
|
return 1;
|
|
|
|
case 2: std::cerr << "An unexpected error occurred while locking the profile" << std::endl;
|
|
|
|
return 1;
|
|
|
|
case 3: std::cerr << "An error occurred while login with the profile" << std::endl;
|
|
|
|
return 1;
|
|
|
|
default: std::cerr << "Main: Unexpected switch value " << retVal << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
2009-02-26 09:10:26 -05:00
|
|
|
|
2010-06-27 12:28:44 -04:00
|
|
|
/* Start-up libretroshare server threads */
|
2014-01-07 17:51:22 -05:00
|
|
|
RsControl::instance() -> StartupRetroShare();
|
2012-02-07 12:01:07 -05:00
|
|
|
|
|
|
|
#ifdef RS_INTRO_SERVER
|
|
|
|
RsIntroServer rsIS;
|
|
|
|
#endif
|
2009-02-03 14:42:07 -05:00
|
|
|
|
|
|
|
/* pass control to the GUI */
|
|
|
|
while(1)
|
|
|
|
{
|
2012-08-07 19:55:43 -04:00
|
|
|
//std::cerr << "GUI Tick()" << std::endl;
|
2012-02-07 12:01:07 -05:00
|
|
|
|
|
|
|
#ifdef RS_INTRO_SERVER
|
|
|
|
rsIS.tick();
|
|
|
|
#endif
|
2012-08-07 19:55:43 -04:00
|
|
|
|
2012-08-09 09:45:05 -04:00
|
|
|
int rt = 0;
|
|
|
|
// If we have a MenuTerminal ...
|
|
|
|
// only want to sleep if there is no input. (rt == 0).
|
|
|
|
if (rt == 0)
|
|
|
|
{
|
|
|
|
#ifndef WINDOWS_SYS
|
|
|
|
sleep(1);
|
|
|
|
#else
|
|
|
|
Sleep(1000);
|
2012-08-07 19:55:43 -04:00
|
|
|
#endif
|
2012-08-09 09:45:05 -04:00
|
|
|
}
|
2012-08-07 19:55:43 -04:00
|
|
|
|
2012-08-20 10:59:41 -04:00
|
|
|
usleep(1000);
|
|
|
|
|
2009-02-03 14:42:07 -05:00
|
|
|
}
|
|
|
|
return 1;
|
2016-10-08 06:59:27 -04:00
|
|
|
}
|