Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Daemon, simple file logging, etc #33

Merged
merged 4 commits into from
Feb 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ namespace util
public:

MsgQueue (): m_Thread (std::bind (&MsgQueue<Msg>::Run, this)) {};
void Stop()
{
m_Thread.detach();
}

private:
void Run ()
Expand Down
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,21 @@ $ ./i2p --host=YOUR_PUBLIC_IP

The client should now reseed by itself.

Other options:
* --port= - The port to listen on
* --httpport= - The http port to listen on


To visit an I2P page, you need to find the b32 address of your destination.
After that, go to the webconsole and add it behind the url. (Remove http:// and b32.i2p from the address)

This should resulting in for example:
http://localhost:7070/4oes3rlgrpbkmzv4lqcfili23h3cvpwslqcfjlk6vvguxyggspwa


Options
-------

* --host= - The external IP
* --port= - The port to listen on
* --httpport= - The http port to listen on
* --log= - Enable or disable logging to file. 1 for yes, 0 for no.
* --daemon= - Eanble or disable daemon mode. 1 for yes, 0 for no.



107 changes: 101 additions & 6 deletions i2p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
#include <thread>
#include <cryptopp/integer.h>
#include <boost/filesystem.hpp>

#ifndef _WIN32
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#endif

#include "Log.h"
#include "base64.h"
#include "Transports.h"
Expand All @@ -13,6 +20,32 @@
#include "HTTPServer.h"
#include "util.h"


// Global
int running = 1;

#ifndef _WIN32
void handle_sighup(int n)
{
if (i2p::util::config::GetArg("daemon", 0) == 1)
{
static bool first=true;
if (first)
{
first=false;
return;
}
}
LogPrint("Reloading config.");
i2p::util::filesystem::ReadConfigFile(i2p::util::config::mapArgs, i2p::util::config::mapMultiArgs);
}
void handle_shutdown(int sig)
{
running = 0; // Exit loop
}
#endif


int main( int argc, char* argv[] )
{
i2p::util::config::OptionParser(argc,argv);
Expand All @@ -23,27 +56,89 @@ int main( int argc, char* argv[] )
setlocale(LC_ALL, "Russian");
#endif


LogPrint("\n\n\n\ni2pd starting\n");
LogPrint("data directory: ", i2p::util::filesystem::GetDataDir().string());
i2p::util::filesystem::ReadConfigFile(i2p::util::config::mapArgs, i2p::util::config::mapMultiArgs);

#ifndef _WIN32
struct sigaction sa;
sa.sa_handler = handle_sighup;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
if (sigaction(SIGHUP,&sa,0) == -1)
{
LogPrint("Failed to install SIGHUP handler.");
}

if (i2p::util::config::GetArg("-daemon", 0) == 1)
{
pid_t pid;
pid = fork();
if (pid > 0)
{
g_Log.Stop();
return 0;
}
if (pid < 0)
{
return -1;
}

umask(0);
int sid = setsid();
if (sid < 0)
{
LogPrint("Error, could not create process group.");
return -1;
}
}

// Handle shutdown
signal(SIGABRT, &handle_shutdown);
signal(SIGTERM, &handle_shutdown);
signal(SIGINT, &handle_shutdown);
#endif

if (i2p::util::config::GetArg("-log", 0) == 1)
{
std::string logfile = i2p::util::filesystem::GetDataDir().string();
#ifndef _WIN32
logfile.append("/debug.log");
#else
logfile.append("\\debug.log");
#endif
LogPrint("Logging to file enabled.");
freopen(logfile.c_str(),"a",stdout);
}

//TODO: This is an ugly workaround. fix it.
//TODO: Autodetect public IP.
i2p::context.OverrideNTCPAddress(i2p::util::config::GetCharArg("-host", "127.0.0.1"),
i2p::util::config::GetArg("-port", 17070));
int httpport = i2p::util::config::GetArg("-httpport", 7070);

i2p::util::HTTPServer httpServer (httpport);
i2p::util::HTTPServer httpServer (i2p::util::config::GetArg("-httpport", 7070));

httpServer.Start ();
i2p::data::netdb.Start ();
i2p::transports.Start ();
i2p::tunnel::tunnels.Start ();

std::this_thread::sleep_for (std::chrono::seconds(10000));
i2p::tunnel::tunnels.Start ();

while (running)
{
//TODO Meeh: Find something better to do here.
std::this_thread::sleep_for (std::chrono::seconds(1));
}
LogPrint("Shutdown started.");

i2p::tunnel::tunnels.Stop ();
i2p::transports.Stop ();
i2p::data::netdb.Stop ();
httpServer.Stop ();
httpServer.Stop ();

if (i2p::util::config::GetArg("-log", 0) == 1)
{
fclose (stdout);
}
return 0;
}