-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
outline_controller_server.cpp
178 lines (161 loc) · 6.9 KB
/
outline_controller_server.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright 2018 The Outline Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <cstdio>
#include <iostream>
#include <map>
#include <memory>
#include <stdexcept>
#include <string>
#include <grp.h>
#include <unistd.h>
#include <pwd.h>
#include "outline_error.h"
#include "outline_controller_server.h"
using namespace outline;
using boost::asio::local::stream_protocol;
static const char * const OUTLINE_GRP_NAME = "outlinevpn";
void session::start() {
auto self(shared_from_this());
boost::asio::spawn(executor_, [this, self](boost::asio::yield_context yield) {
try {
std::ostringstream response;
std::string clientCommand, buffer;
std::cout << "Client Connected" << std::endl;
for (;;) {
for (;;) {
boost::asio::async_read_until(socket_, boost::asio::dynamic_buffer(buffer, 1024), "}",
yield);
std::cout << buffer << std::endl;
clientCommand.append(buffer);
buffer.clear();
if (isValidJson(clientCommand)) {
std::cout << "Valid JSON" << std::endl;
break;
}
}
auto [err, result, action] = runClientCommand(clientCommand);
// TODO: replace the following code with a json library to handle special characters
response << "{\"statusCode\": " << err
<< ",\"returnValue\": \"" << result << "\""
<< ",\"action\": \"" << action << "\"}" << std::endl;
boost::asio::async_write(
socket_, boost::asio::buffer(response.str(), response.str().length()), yield);
std::cout << "Wrote back (" << response.str() << ") to unix socket" << std::endl;
clientCommand.clear();
response.str(std::string());
}
} catch (std::exception& e) {
socket_.close();
}
});
}
bool session::isValidJson(std::string str) {
std::stringstream ss;
boost::property_tree::ptree pt;
if (str.length() >= JSON_INPUT_MIN_LENGTH) {
ss << str;
try {
boost::property_tree::read_json(ss, pt);
return true;
} catch (std::exception const& e) {
}
}
return false;
}
std::tuple<int, std::string, std::string> session::runClientCommand(std::string clientCommand) {
std::stringstream ss;
std::string action, outline_server_ip;
boost::property_tree::ptree pt;
// std::cout << clientCommand << std::endl;
ss << clientCommand;
try {
boost::property_tree::read_json(ss, pt);
} catch (std::exception const& e) {
std::cerr << e.what() << std::endl;
return {static_cast<int>(ErrorCode::kUnexpected), "Invalid JSON", {}};
}
boost::property_tree::ptree::assoc_iterator _action_iter = pt.find("action");
if (_action_iter == pt.not_found()) {
std::cerr << "Invalid input JSON - action doesn't exist" << std::endl;
return {static_cast<int>(ErrorCode::kUnexpected), "Invalid JSON", {}};
}
action = boost::lexical_cast<std::string>(pt.to_iterator(_action_iter)->second.data());
// std::cout << action << std::endl;
try {
if (action == CONFIGURE_ROUTING) {
boost::property_tree::ptree::assoc_iterator _parameters_iter = pt.find("parameters");
if (_parameters_iter == pt.not_found()) {
std::cerr << "Invalid input JSON - parameters doesn't exist" << std::endl;
return {static_cast<int>(ErrorCode::kUnexpected), "Invalid JSON", action};
}
boost::property_tree::ptree parameters = pt.to_iterator(_parameters_iter)->second;
boost::property_tree::ptree::assoc_iterator _proxyIp_iter = parameters.find("proxyIp");
if (_proxyIp_iter == parameters.not_found()) {
std::cerr << "Invalid input JSON - parameters doesn't exist" << std::endl;
return {static_cast<int>(ErrorCode::kUnexpected), "Invalid JSON", action};
}
outline_server_ip =
boost::lexical_cast<std::string>(pt.to_iterator(_proxyIp_iter)->second.data());
outlineProxyController_->routeThroughOutline(outline_server_ip);
std::cout << "Configure Routing to " << outline_server_ip << " is done." << std::endl;
return {static_cast<int>(ErrorCode::kOk), {}, action};
} else if (action == RESET_ROUTING) {
outlineProxyController_->routeDirectly();
std::cout << "Reset Routing done" << std::endl;
return {static_cast<int>(ErrorCode::kOk), {}, action};
} else if (action == GET_DEVICE_NAME) {
std::cout << "Get device name done" << std::endl;
return {static_cast<int>(ErrorCode::kOk), outlineProxyController_->getTunDeviceName(), action};
} else {
std::cerr << "Invalid action specified in JSON (" << action << ")" << std::endl;
return {static_cast<int>(ErrorCode::kUnexpected), "Undefined Action", {}};
}
} catch (const std::system_error& err) {
std::cerr << "[" << err.code() << "] " << err.what() << std::endl;
if (err.code().category() == OutlineErrorCategory()) {
// TODO: add err.what() to give more details to the client
return {err.code().value(), {}, action};
}
return {static_cast<int>(ErrorCode::kUnexpected), {}, action};
}
}
OutlineControllerServer::OutlineControllerServer(boost::asio::io_context& io_context,
const std::string& file,
uid_t owning_user)
: outlineProxyController_(std::make_shared<OutlineProxyController>()),
unix_socket_name(file)
{
::unlink(unix_socket_name.c_str());
boost::asio::spawn(io_context, [&](boost::asio::yield_context yield) {
stream_protocol::acceptor acceptor(io_context, stream_protocol::endpoint(unix_socket_name));
auto outlineGrp = getgrnam(OUTLINE_GRP_NAME);
if (outlineGrp != nullptr) {
auto ownerUid = getpwuid(owning_user) != nullptr ? owning_user : -1;
if (chown(unix_socket_name.c_str(), ownerUid, outlineGrp->gr_gid) == 0) {
std::cout << "updated unix socket owner to " << ownerUid << "," << outlineGrp->gr_gid << std::endl;
} else {
std::cerr << "failed to update unix socket owner" << std::endl;
}
} else {
std::cerr << "failed to get the id of " << OUTLINE_GRP_NAME << " group" << std::endl;
}
chmod(unix_socket_name.c_str(), S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
for (;;) {
boost::system::error_code ec;
stream_protocol::socket socket(io_context);
acceptor.async_accept(socket, yield[ec]);
if (!ec) std::make_shared<session>(std::move(socket), outlineProxyController_)->start();
}
});
}