Make more use of theApp.m_pMainWnd
[TortoiseGit.git] / src / TortoiseProc / Commands / DaemonCommand.cpp
blobe61ef08cb0bc23c7def99a625739010cbeef7b5f
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2013-2016, 2018 - TortoiseGit
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "stdafx.h"
20 #include "DaemonCommand.h"
21 #include "ProgressDlg.h"
22 #include "UnicodeUtils.h"
23 #include "MessageBox.h"
26 bool DaemonCommand::Execute()
28 if (CMessageBox::ShowCheck(GetExplorerHWND(), IDS_DAEMON_SECURITY_WARN, IDS_APPNAME, 2, IDI_EXCLAMATION, IDS_PROCEEDBUTTON, IDS_ABORTBUTTON, NULL, L"DaemonNoSecurityWarning", IDS_MSGBOX_DONOTSHOWAGAIN) == 2)
30 CMessageBox::RemoveRegistryKey(L"DaemonNoSecurityWarning"); // only store answer if it is "Proceed"
31 return false;
34 WSADATA wsaData;
35 if (WSAStartup(MAKEWORD(2, 2), &wsaData) != NO_ERROR)
37 MessageBox(GetExplorerHWND(), L"WSAStartup failed!", L"TortoiseGit", MB_OK | MB_ICONERROR);
38 return false;
40 SCOPE_EXIT { WSACleanup(); };
42 char hostName[128] = { 0 };
43 if (gethostname(hostName, sizeof(hostName)) == SOCKET_ERROR)
45 MessageBox(GetExplorerHWND(), L"gethostname failed!", L"TortoiseGit", MB_OK | MB_ICONERROR);
46 return false;
49 STRING_VECTOR ips;
50 ADDRINFOA addrinfo = { 0 };
51 addrinfo.ai_family = AF_UNSPEC;
52 PADDRINFOA result = nullptr;
53 GetAddrInfoA(hostName, nullptr, &addrinfo, &result);
54 for (auto ptr = result; ptr != nullptr; ptr = ptr->ai_next)
56 if (ptr->ai_family != AF_INET && ptr->ai_family != AF_INET6)
57 continue;
59 DWORD ipbufferlength = 46;
60 CString ip;
61 if (WSAAddressToString(ptr->ai_addr, (DWORD)ptr->ai_addrlen, nullptr, CStrBuf(ip, ipbufferlength), &ipbufferlength))
62 continue;
64 if (ptr->ai_family == AF_INET6)
66 if (CStringUtils::StartsWith(ip, L"fe80:")) // strip % interface number at the end
67 ip = ip.Left(ip.Find(L'%'));
68 ip = L'[' + ip; // IPv6 addresses needs to be enclosed within braces
69 ip += L']';
71 ips.push_back(ip);
73 if (result)
74 FreeAddrInfoA(result);
76 CString basePath(g_Git.m_CurrentDir);
77 basePath.TrimRight(L'\\');
78 if (basePath.GetLength() == 2)
79 basePath += L"\\.";
81 CString cmd;
82 cmd.Format(L"git.exe daemon --verbose --export-all --base-path=\"%s\"", (LPCTSTR)basePath);
83 CProgressDlg progDlg;
84 theApp.m_pMainWnd = &progDlg;
85 progDlg.m_GitCmd = cmd;
86 if (ips.empty())
87 progDlg.m_PreText = L"git://localhost/";
88 else
90 for (const auto& ip : ips)
92 progDlg.m_PreText += L"git://";
93 progDlg.m_PreText += ip;
94 progDlg.m_PreText += L"/\n";
97 progDlg.DoModal();
98 return true;