allow running lokinet as root on windows (#1347)

* - allow running lokinet as root on windows
  ($PROGRAMDATA/.lokinet/lokinet.ini)
- oops forgot to eat my own command line args

* cursed windows

* now that we eat our own startup options the entry in cxxopts is fully
ded

* aha our command line options get smashed up

* revert plus DRY fix
pull/1355/head
Rick V 4 years ago committed by GitHub
parent 21930cf667
commit 7136f77c2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -106,6 +106,8 @@ install_win32_daemon()
llarp::LogError("Cannot install service ", GetLastError()); llarp::LogError("Cannot install service ", GetLastError());
return; return;
} }
// just put the flag here. we eat it later on and specify the
// config path in the daemon entry point
StringCchCat(szPath.data(), 1024, " --win32-daemon"); StringCchCat(szPath.data(), 1024, " --win32-daemon");
// Get a handle to the SCM database. // Get a handle to the SCM database.
@ -297,8 +299,8 @@ main(int argc, char* argv[])
{NULL, NULL}}; {NULL, NULL}};
if (lstrcmpi(argv[1], "--win32-daemon") == 0) if (lstrcmpi(argv[1], "--win32-daemon") == 0)
{ {
StartServiceCtrlDispatcher(DispatchTable);
start_as_daemon = true; start_as_daemon = true;
StartServiceCtrlDispatcher(DispatchTable);
} }
else else
return lokinet_main(argc, argv); return lokinet_main(argc, argv);
@ -318,7 +320,9 @@ lokinet_main(int argc, char* argv[])
#ifdef _WIN32 #ifdef _WIN32
if (startWinsock()) if (startWinsock())
return -1; return -1;
ReportSvcStatus(SERVICE_RUNNING, NO_ERROR, 0);
SetConsoleCtrlHandler(handle_signal_win32, TRUE); SetConsoleCtrlHandler(handle_signal_win32, TRUE);
// SetUnhandledExceptionFilter(win32_signal_handler); // SetUnhandledExceptionFilter(win32_signal_handler);
#endif #endif
cxxopts::Options options( cxxopts::Options options(
@ -329,8 +333,7 @@ lokinet_main(int argc, char* argv[])
options.add_options()("v,verbose", "Verbose", cxxopts::value<bool>()) options.add_options()("v,verbose", "Verbose", cxxopts::value<bool>())
#ifdef _WIN32 #ifdef _WIN32
("install", "install win32 daemon to SCM", cxxopts::value<bool>())( ("install", "install win32 daemon to SCM", cxxopts::value<bool>())(
"remove", "remove win32 daemon from SCM", cxxopts::value<bool>())( "remove", "remove win32 daemon from SCM", cxxopts::value<bool>())
"win32-daemon", "do not use interactively", cxxopts::value<bool>())
#endif #endif
("h,help", "help", cxxopts::value<bool>())("version", "version", cxxopts::value<bool>())( ("h,help", "help", cxxopts::value<bool>())("version", "version", cxxopts::value<bool>())(
"g,generate", "generate client config", cxxopts::value<bool>())( "g,generate", "generate client config", cxxopts::value<bool>())(
@ -514,6 +517,7 @@ lokinet_main(int argc, char* argv[])
llarp::LogContext::Instance().ImmediateFlush(); llarp::LogContext::Instance().ImmediateFlush();
#ifdef _WIN32 #ifdef _WIN32
::WSACleanup(); ::WSACleanup();
ReportSvcStatus(SERVICE_STOPPED, NO_ERROR, code);
#endif #endif
if (ctx) if (ctx)
{ {
@ -572,7 +576,7 @@ SvcCtrlHandler(DWORD dwCtrl)
// to the original lokinet entry // to the original lokinet entry
// and only gets called if we get --win32-daemon in the command line // and only gets called if we get --win32-daemon in the command line
VOID FAR PASCAL VOID FAR PASCAL
win32_daemon_entry(DWORD largc, LPTSTR* largv) win32_daemon_entry(DWORD argc, LPTSTR* argv)
{ {
// Register the handler function for the service // Register the handler function for the service
SvcStatusHandle = RegisterServiceCtrlHandler("lokinet", SvcCtrlHandler); SvcStatusHandle = RegisterServiceCtrlHandler("lokinet", SvcCtrlHandler);
@ -588,7 +592,11 @@ win32_daemon_entry(DWORD largc, LPTSTR* largv)
SvcStatus.dwServiceSpecificExitCode = 0; SvcStatus.dwServiceSpecificExitCode = 0;
// Report initial status to the SCM // Report initial status to the SCM
ReportSvcStatus(SERVICE_RUNNING, NO_ERROR, 0); ReportSvcStatus(SERVICE_START_PENDING, NO_ERROR, 3000);
lokinet_main(largc, largv); // SCM clobbers startup args, regenerate them here
argc = 2;
argv[1] = "c:/programdata/.lokinet/lokinet.ini";
argv[2] = nullptr;
lokinet_main(argc, argv);
} }
#endif #endif

@ -376,18 +376,12 @@ tuntap_sys_set_ipv4(struct device *dev, t_tun_in_addr *s, uint32_t mask)
IPADDR ep[4]; IPADDR ep[4];
#pragma pack(push) #pragma pack(push)
#pragma pack(1) #pragma pack(1)
struct struct opt
{
uint8_t dhcp_opt;
uint8_t length;
uint32_t value[2];
} dns;
struct
{ {
uint8_t dhcp_opt; uint8_t dhcp_opt;
uint8_t length; uint8_t length;
uint32_t value; uint32_t value;
} gateway; } dns, gateway;
#pragma pack(pop) #pragma pack(pop)
sock[0] = s->S_un.S_addr; sock[0] = s->S_un.S_addr;
@ -424,14 +418,10 @@ tuntap_sys_set_ipv4(struct device *dev, t_tun_in_addr *s, uint32_t mask)
dns.dhcp_opt = 6; dns.dhcp_opt = 6;
dns.length = 4; dns.length = 4;
if (dev->bindaddr) if (dev->bindaddr)
dns.value[0] = dev->bindaddr; /* apparently this doesn't show in network properties, dns.value = dev->bindaddr; /* apparently this doesn't show in network properties,
but it works */ but it works */
else else
dns.value[0] = htonl(0x7f000001); dns.value = htonl(0x7f000001);
dns.value[1] = 0;
ret = DeviceIoControl(dev->tun_fd, TAP_IOCTL_CONFIG_DHCP_SET_OPT, &dns,
sizeof(dns), &dns, sizeof(dns), &len, NULL);
/* set router address to interface address */ /* set router address to interface address */
gateway.dhcp_opt = 3; gateway.dhcp_opt = 3;
@ -440,7 +430,9 @@ tuntap_sys_set_ipv4(struct device *dev, t_tun_in_addr *s, uint32_t mask)
ret = DeviceIoControl(dev->tun_fd, TAP_IOCTL_CONFIG_DHCP_SET_OPT, &gateway, ret = DeviceIoControl(dev->tun_fd, TAP_IOCTL_CONFIG_DHCP_SET_OPT, &gateway,
sizeof(gateway), &gateway, sizeof(gateway), &len, NULL); sizeof(gateway), &gateway, sizeof(gateway), &len, NULL);
ret = DeviceIoControl(dev->tun_fd, TAP_IOCTL_CONFIG_DHCP_SET_OPT, &dns,
sizeof(dns), &dns, sizeof(dns), &len, NULL);
if(!ret) if(!ret)
{ {
int errcode = GetLastError(); int errcode = GetLastError();

Loading…
Cancel
Save