-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathJSONInterface.cpp
94 lines (74 loc) · 2.79 KB
/
JSONInterface.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
// Copyright 2020 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// source tree.
#include "JSONInterface.h"
#include <mumble/json_bridge/Bridge.h>
#include <mumble/json_bridge/Util.h>
#include <filesystem>
#include <iostream>
namespace Mumble {
namespace JsonBridge {
namespace CLI {
JSONInterface::JSONInterface(uint32_t readTimeout, uint32_t writeTimeout)
: m_readTimeout(readTimeout), m_writeTimeout(writeTimeout) {
std::filesystem::path pipePath;
#ifdef PLATFORM_WINDOWS
pipePath = "\\\\.\\pipe\\";
#else
pipePath = "/tmp/";
#endif // PLATFORM_WINDOWS
pipePath = pipePath / ".mumble-json-bridge-cli";
m_pipe = NamedPipe::create(pipePath);
m_secret = Util::generateRandomString(12);
// clang-format off
nlohmann::json registration = {
{"message_type", "registration"},
{"message",
{
{"pipe_path", pipePath.string()},
{"secret", m_secret}
}
}
};
// clang-format off
NamedPipe::write(Bridge::s_pipePath, registration.dump(), m_writeTimeout);
nlohmann::json response = nlohmann::json::parse(m_pipe.read_blocking(m_readTimeout));
m_bridgeSecret = response["secret"].get<std::string>();
m_id = response["response"]["client_id"].get<client_id_t>();
}
JSONInterface::~JSONInterface() {
// clang-format off
nlohmann::json message = {
{ "message_type", "disconnect" },
{ "client_id", m_id },
{ "secret", m_secret }
};
// clang-format on
try {
NamedPipe::write(Bridge::s_pipePath, message.dump(), m_writeTimeout);
// We patiently wait for the Bridge's reply, even though we don't care about it. This is in
// order for the Bridge's operation to not error due to timeout.
std::string answer = m_pipe.read_blocking(m_readTimeout);
} catch (...) {
// Ignore any exceptions that this might cause. If it does throw then this client might not
// be disconnected from the Bridge, which isn't that bad. Besides: We probably can't do anything
// about it anyways, but we certainly don't want our program to terminate because of it.
}
}
nlohmann::json JSONInterface::process(nlohmann::json msg) const {
msg["secret"] = m_secret;
msg["client_id"] = m_id;
NamedPipe::write(Bridge::s_pipePath, msg.dump(), m_writeTimeout);
nlohmann::json response = nlohmann::json::parse(m_pipe.read_blocking(m_readTimeout));
if (response["secret"].get< std::string >() != m_bridgeSecret) {
std::cerr << "[ERROR]: Bridge secret doesn't match" << std::endl;
return {};
}
// Remove the secret field as it has already been validated here
response.erase("secret");
return response;
}
}; // namespace CLI
}; // namespace JsonBridge
}; // namespace Mumble