Fle server & Query server improvements

This commit is contained in:
WolverinDEV
2019-07-21 10:43:26 +02:00
parent e507d1f75d
commit d4d6978d5f
18 changed files with 1138 additions and 313 deletions
@@ -3471,9 +3471,23 @@ CommandResult ConnectedClient::handleCommandFTInitUpload(Command &cmd) {
Command result(this->getExternalType() == CLIENT_TEAMSPEAK ? "notifystartupload" : "");
result["clientftfid"] = cmd["clientftfid"].as<uint64_t>();
result["ftkey"] = key->key;
result["port"] = ntohs(serverInstance->getFileServer()->boundedAddress()->sin_port);
if(serverInstance->getFileServer()->boundedAddress()->sin_addr.s_addr != 0)
result["ip"] = inet_ntoa(serverInstance->getFileServer()->boundedAddress()->sin_addr) + string(",");
auto bindings = serverInstance->getFileServer()->list_bindings();
if(!bindings.empty()) {
result["port"] = net::port(bindings[0]->address);
string ip = "";
for(auto& entry : bindings) {
if(net::is_anybind(entry->address)) {
ip = "";
break;
}
ip += net::to_string(entry->address, false) + ",";
}
if(!ip.empty())
result["ip"] = ip;
} else {
return {findError("server_is_not_running"), "file server is not bound to any address"};
}
result["seekpos"] = 0;
result["proto"] = 1;
result["serverftfid"] = key->key_id; //TODO generate!
@@ -3549,9 +3563,24 @@ CommandResult ConnectedClient::handleCommandFTInitDownload(Command &cmd) {
result["proto"] = 1;
result["serverftfid"] = key->key_id;
result["ftkey"] = key->key;
result["port"] = ntohs(serverInstance->getFileServer()->boundedAddress()->sin_port);
if(serverInstance->getFileServer()->boundedAddress()->sin_addr.s_addr != 0)
result["ip"] = inet_ntoa(serverInstance->getFileServer()->boundedAddress()->sin_addr) + string(",");
auto bindings = serverInstance->getFileServer()->list_bindings();
if(!bindings.empty()) {
result["port"] = net::port(bindings[0]->address);
string ip = "";
for(auto& entry : bindings) {
if(net::is_anybind(entry->address)) {
ip = "";
break;
}
ip += net::to_string(entry->address, false) + ",";
}
if(!ip.empty())
result["ip"] = ip;
} else {
return {findError("server_is_not_running"), "file server is not bound to any address"};
}
result["size"] = key->size;
this->sendCommand(result);
+6 -4
View File
@@ -101,7 +101,6 @@ size_t FileClient::used_bandwidth() {
}
std::string FileClient::client_prefix() {
auto ip_address = net::to_string(this->remoteAddress.sin_addr);
bool hide_ip = config::server::disable_ip_saving;
if(!hide_ip) {
auto client = this->client;
@@ -112,10 +111,13 @@ std::string FileClient::client_prefix() {
}
}
}
std::string address = "";
if(hide_ip)
ip_address = "X.X.X.X";
if(this->client) return "[" + to_string(this->client->getServerId()) + "|" + ip_address + ":" + to_string(htons(this->remoteAddress.sin_port)) + "| " + this->client->getDisplayName() + "]";
return "[0|" + ip_address + ":" + to_string(htons(this->remoteAddress.sin_port)) + "|unconnected]";
address = "X.X.X.X:" + to_string(net::port(this->remote_address));
else
address = net::to_string(this->remote_address);
if(this->client) return "[" + to_string(this->client->getServerId()) + "|" + address + "| " + this->client->getDisplayName() + "]";
return "[0|" + address + "|unconnected]";
}
size_t FileClient::transferred_bytes() {
+1 -1
View File
@@ -89,7 +89,7 @@ namespace ts {
std::recursive_mutex bandwidth_lock;
std::deque<std::unique_ptr<BandwidthEntry>> bandwidth;
sockaddr_in remoteAddress;
sockaddr_storage remote_address;
int clientFd;
bool event_read_hold = false;
+2 -2
View File
@@ -166,9 +166,9 @@ void FileClient::handleMessageRead(int fd, short, void *) {
}
if(this->state_connection == C_CONNECTED) {
if(this->state_transfer == T_TRANSFER)
logError(LOG_FT, "{} Transfer hang up. Remote peer closed the connection.", this->client_prefix());
logWarning(LOG_FT, "{} Transfer hang up. Remote peer closed the connection.", this->client_prefix());
else
logTrace(LOG_FT, "{} Received notification that the remote peer has closed the connection", this->client_prefix());
logMessage(LOG_FT, "{} Remote peer has closed the connection before initializing a transfer.", this->client_prefix());
self->disconnect(seconds(3));
}
return;
+42
View File
@@ -262,6 +262,18 @@ void WebClient::tick(const std::chrono::system_clock::time_point& point) {
this->ws_handler.send({pipes::PING, {buffer, 2}});
}
}
if(this->js_ping.last_request + seconds(1) < point) {
if(this->js_ping.last_response > this->js_ping.last_request || this->js_ping.last_request + this->js_ping.timeout < point) {
this->js_ping.current_id++;
this->js_ping.last_request = point;
Json::Value jsonCandidate;
jsonCandidate["type"] = "ping";
jsonCandidate["payload"] = to_string(this->js_ping.current_id);
this->sendJson(jsonCandidate);
}
}
}
void WebClient::onWSConnected() {
@@ -554,6 +566,36 @@ void WebClient::handleMessage(const std::string &message) {
}
this->voice_bridge->remote_ice_finished();
}
} else if(val["type"].asString() == "ping") {
Json::Value response;
response["type"] = "pong";
response["payload"] = val["payload"];
response["ping_native"] = to_string(duration_cast<microseconds>(this->ping.value).count());
this->sendJson(response);
return;
} else if(val["type"].asString() == "pong") {
auto payload = val["payload"].isString() ? val["payload"].asString() : "";
uint8_t response_id = 0;
try {
response_id = (uint8_t) stoul(payload);
} catch(std::exception& ex) {
debugMessage(this->getServerId(), "[{}] Failed to parse pong payload.");
return;
}
if(response_id != this->js_ping.current_id) {
debugMessage(
this->getServerId(),
"{} Received pong on web socket from javascript which is older than the last request. Delay may over {}ms? (Index: {}, Current index: {})",
CLIENT_STR_LOG_PREFIX,
duration_cast<milliseconds>(this->js_ping.timeout).count(),
response_id,
this->js_ping.current_id
);
return;
}
this->js_ping.last_response = system_clock::now();
this->js_ping.value = duration_cast<nanoseconds>(this->js_ping.last_response - this->js_ping.last_request);
}
} catch (const std::exception& ex) {
logError(this->server->getServerId(), "Could not handle json packet! Message {}", ex.what());
+12 -1
View File
@@ -32,6 +32,9 @@ namespace ts {
bool shouldReceiveVoice(const std::shared_ptr<ConnectedClient> &sender) override;
inline std::chrono::nanoseconds client_ping() { return this->client_ping_layer_7(); }
inline std::chrono::nanoseconds client_ping_layer_5() { return this->ping.value; }
inline std::chrono::nanoseconds client_ping_layer_7() { return this->js_ping.value; }
protected:
void handlePacketVoiceWhisper(const pipes::buffer_view &string, bool) override;
@@ -60,6 +63,15 @@ namespace ts {
std::chrono::nanoseconds timeout{2000};
} ping;
struct {
uint8_t current_id = 0;
std::chrono::system_clock::time_point last_request;
std::chrono::system_clock::time_point last_response;
std::chrono::nanoseconds value;
std::chrono::nanoseconds timeout{2000};
} js_ping;
std::mutex queue_lock;
std::deque<pipes::buffer> queue_read;
std::deque<pipes::buffer> queue_write;
@@ -93,7 +105,6 @@ namespace ts {
public:
void send_voice_packet(const pipes::buffer_view &view, const VoicePacketFlags &flags) override;
void send_voice_whisper_packet(const pipes::buffer_view &view, const VoicePacketFlags &flags) override;
protected: