-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNativeMessagingProxy.cpp
executable file
·157 lines (134 loc) · 4.82 KB
/
NativeMessagingProxy.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* Copyright (C) 2022 KeePassXC Team <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "NativeMessagingProxy.h"
#include <QCoreApplication>
#include <QtConcurrent/QtConcurrent>
#include <iostream>
#ifdef Q_OS_WIN
#include <fcntl.h>
#include <winsock2.h>
#include <windows.h>
#else
#include <sys/socket.h>
#include <sys/types.h>
#endif
namespace BrowserShared
{
constexpr int NATIVEMSG_MAX_LENGTH = 1024 * 1024;
QString localServerPath()
{
const auto serverName = QStringLiteral("/org.keepassxc.KeePassXC.BrowserServer");
#if defined(KEEPASSXC_DIST_SNAP)
return QProcessEnvironment::systemEnvironment().value("SNAP_USER_COMMON") + serverName;
#elif defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)
// This returns XDG_RUNTIME_DIR or else a temporary subdirectory.
QString path = QStandardPaths::writableLocation(QStandardPaths::RuntimeLocation);
// Put the socket in a dedicated directory.
// This directory will be easily mountable by sandbox containers.
QString subPath = path + "/app/org.keepassxc.KeePassXC/";
QDir().mkpath(subPath);
QString socketPath = subPath + serverName;
// Create a symlink at the legacy location for backwards compatibility.
QFile::link(socketPath, path + serverName);
return socketPath;
#elif defined(Q_OS_WIN)
// Windows uses named pipes
return serverName + "_" + qgetenv("USERNAME");
#else // Q_OS_MACOS and others
return QStandardPaths::writableLocation(QStandardPaths::TempLocation) + serverName;
#endif
}
} // namespace BrowserShared
NativeMessagingProxy::NativeMessagingProxy()
: QObject()
{
connect(this,
&NativeMessagingProxy::stdinMessage,
this,
&NativeMessagingProxy::transferStdinMessage,
Qt::QueuedConnection);
setupStandardInput();
setupLocalSocket();
}
void NativeMessagingProxy::setupStandardInput()
{
#ifdef Q_OS_WIN
#ifdef Q_CC_MSVC
_setmode(_fileno(stdin), _O_BINARY);
_setmode(_fileno(stdout), _O_BINARY);
#else
setmode(fileno(stdin), _O_BINARY);
setmode(fileno(stdout), _O_BINARY);
#endif
#endif
QtConcurrent::run([this] {
while (std::cin.good()) {
if (std::cin.peek() != EOF) {
uint length = 0;
for (uint i = 0; i < sizeof(uint); ++i) {
length |= getchar() << (i * 8);
}
QString msg;
msg.reserve(length);
for (uint i = 0; i < length; ++i) {
msg.append(getchar());
}
if (msg.length() > 0) {
emit stdinMessage(msg);
}
}
QThread::msleep(100);
}
QCoreApplication::quit();
});
}
void NativeMessagingProxy::transferStdinMessage(const QString& msg)
{
if (m_localSocket && m_localSocket->state() == QLocalSocket::ConnectedState) {
m_localSocket->write(msg.toUtf8(), msg.length());
m_localSocket->flush();
}
}
void NativeMessagingProxy::setupLocalSocket()
{
m_localSocket.reset(new QLocalSocket());
m_localSocket->connectToServer(BrowserShared::localServerPath());
m_localSocket->setReadBufferSize(BrowserShared::NATIVEMSG_MAX_LENGTH);
int socketDesc = m_localSocket->socketDescriptor();
if (socketDesc) {
int max = BrowserShared::NATIVEMSG_MAX_LENGTH;
setsockopt(socketDesc, SOL_SOCKET, SO_SNDBUF, reinterpret_cast<char*>(&max), sizeof(max));
}
connect(m_localSocket.data(), SIGNAL(readyRead()), this, SLOT(transferSocketMessage()));
connect(m_localSocket.data(), SIGNAL(disconnected()), this, SLOT(socketDisconnected()));
}
void NativeMessagingProxy::transferSocketMessage()
{
auto msg = m_localSocket->readAll();
if (!msg.isEmpty()) {
// Explicitly write the message length as 1 byte chunks
uint len = msg.size();
std::cout.write(reinterpret_cast<char*>(&len), sizeof(len));
// Write the message and flush the stream
std::cout << msg.toStdString() << std::flush;
}
}
void NativeMessagingProxy::socketDisconnected()
{
// Shutdown the proxy when disconnected from the application
QCoreApplication::quit();
}