2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2017-10-25 18:42:23 +02:00
|
|
|
|
|
|
|
using namespace ircd;
|
|
|
|
|
2017-11-16 02:48:25 +01:00
|
|
|
void sender_worker();
|
|
|
|
ircd::context sender_context
|
|
|
|
{
|
|
|
|
"sender",
|
|
|
|
256_KiB,
|
|
|
|
&sender_worker,
|
|
|
|
ircd::context::POST,
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto on_unload{[]
|
|
|
|
{
|
|
|
|
sender_context.interrupt();
|
|
|
|
sender_context.join();
|
|
|
|
}};
|
|
|
|
|
2017-10-25 18:42:23 +02:00
|
|
|
mapi::header IRCD_MODULE
|
|
|
|
{
|
2017-11-16 02:48:25 +01:00
|
|
|
"federation send",
|
|
|
|
nullptr,
|
|
|
|
on_unload
|
2017-10-25 18:42:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct send
|
|
|
|
:resource
|
|
|
|
{
|
|
|
|
using resource::resource;
|
|
|
|
}
|
|
|
|
send_resource
|
|
|
|
{
|
2017-12-12 21:26:39 +01:00
|
|
|
"/_matrix/federation/v1/send/",
|
2017-10-25 18:42:23 +02:00
|
|
|
{
|
2017-12-12 21:26:39 +01:00
|
|
|
"federation send",
|
2017-10-25 18:42:23 +02:00
|
|
|
resource::DIRECTORY,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
handle_edu(client &client,
|
|
|
|
const resource::request::object<m::txn> &request,
|
|
|
|
const string_view &txn_id,
|
|
|
|
const json::object &edu)
|
|
|
|
{
|
|
|
|
//std::cout << edu << std::endl;
|
2017-11-16 02:48:25 +01:00
|
|
|
log::debug("%s :%s | %s | %s",
|
2017-10-25 18:42:23 +02:00
|
|
|
txn_id,
|
2017-11-16 02:48:25 +01:00
|
|
|
at<"origin"_>(request),
|
2017-10-25 18:42:23 +02:00
|
|
|
edu.at("edu_type"),
|
|
|
|
edu.get("sender", string_view{"*"}));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
handle_pdu(client &client,
|
|
|
|
const resource::request::object<m::txn> &request,
|
|
|
|
const string_view &txn_id,
|
|
|
|
const m::event &event)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
//std::cout << event << std::endl;
|
2017-11-16 02:48:25 +01:00
|
|
|
log::debug("%s %s",
|
2017-10-25 18:42:23 +02:00
|
|
|
txn_id,
|
2017-11-16 02:48:25 +01:00
|
|
|
pretty_oneline(event));
|
2017-10-25 18:42:23 +02:00
|
|
|
|
2017-11-16 02:48:25 +01:00
|
|
|
m::vm::eval{event};
|
2017-10-25 18:42:23 +02:00
|
|
|
}
|
|
|
|
catch(const ed25519::bad_sig &e)
|
|
|
|
{
|
|
|
|
throw m::BAD_SIGNATURE
|
|
|
|
{
|
|
|
|
":%s %s %s %s",
|
|
|
|
at<"origin"_>(request),
|
|
|
|
at<"room_id"_>(event),
|
|
|
|
at<"event_id"_>(event),
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
handle_pdu_failure(client &client,
|
|
|
|
const resource::request::object<m::txn> &request,
|
|
|
|
const string_view &txn_id,
|
|
|
|
const json::object &pdu_failure)
|
|
|
|
{
|
2017-11-16 02:48:25 +01:00
|
|
|
log::debug("%s :%s | (pdu_failure) %s",
|
2017-10-25 18:42:23 +02:00
|
|
|
txn_id,
|
2017-11-16 02:48:25 +01:00
|
|
|
at<"origin"_>(request),
|
2017-10-25 18:42:23 +02:00
|
|
|
pdu_failure.get("sender", string_view{"*"}),
|
|
|
|
string_view{pdu_failure});
|
|
|
|
}
|
|
|
|
|
|
|
|
resource::response
|
|
|
|
handle_put(client &client,
|
|
|
|
const resource::request::object<m::txn> &request)
|
|
|
|
{
|
|
|
|
const auto txn_id
|
|
|
|
{
|
|
|
|
request.parv[0]
|
|
|
|
};
|
|
|
|
|
|
|
|
const string_view &origin
|
|
|
|
{
|
|
|
|
json::at<"origin"_>(request)
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::array &edus
|
|
|
|
{
|
|
|
|
json::get<"edus"_>(request)
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::array &pdus
|
|
|
|
{
|
|
|
|
json::get<"pdus"_>(request)
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::array &pdu_failures
|
|
|
|
{
|
|
|
|
json::get<"pdu_failures"_>(request)
|
|
|
|
};
|
|
|
|
|
|
|
|
for(const auto &pdu_failure : pdu_failures)
|
|
|
|
handle_pdu_failure(client, request, txn_id, pdu_failure);
|
|
|
|
|
|
|
|
for(const auto &edu : edus)
|
|
|
|
handle_edu(client, request, txn_id, edu);
|
|
|
|
|
|
|
|
for(const auto &pdu : pdus)
|
|
|
|
handle_pdu(client, request, txn_id, m::event{pdu});
|
|
|
|
|
2017-11-16 02:48:25 +01:00
|
|
|
log::debug("%s :%s | %s --> edus:%zu pdus:%zu errors:%zu",
|
2017-10-25 18:42:23 +02:00
|
|
|
txn_id,
|
2017-11-16 02:48:25 +01:00
|
|
|
origin,
|
2017-10-25 18:42:23 +02:00
|
|
|
string(remote(client)),
|
|
|
|
edus.count(),
|
|
|
|
pdus.count(),
|
|
|
|
pdu_failures.count());
|
|
|
|
|
|
|
|
return resource::response
|
|
|
|
{
|
2017-11-16 02:48:25 +01:00
|
|
|
client, http::OK
|
2017-10-25 18:42:23 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
resource::method method_put
|
|
|
|
{
|
|
|
|
send_resource, "PUT", handle_put,
|
|
|
|
{
|
2018-02-11 06:28:49 +01:00
|
|
|
method_put.VERIFY_ORIGIN,
|
|
|
|
4_MiB // larger = HTTP 413 //TODO: conf
|
2017-10-25 18:42:23 +02:00
|
|
|
}
|
|
|
|
};
|
2017-11-16 02:48:25 +01:00
|
|
|
|
|
|
|
//
|
|
|
|
// Main worker stack
|
|
|
|
//
|
|
|
|
|
|
|
|
void sender_handle(const m::event &, const m::room::id &room_id);
|
|
|
|
void sender_handle(const m::event &);
|
|
|
|
|
|
|
|
void
|
|
|
|
sender_worker()
|
|
|
|
{
|
|
|
|
while(1) try
|
|
|
|
{
|
|
|
|
std::unique_lock<decltype(m::vm::inserted)> lock
|
|
|
|
{
|
|
|
|
m::vm::inserted
|
|
|
|
};
|
|
|
|
|
|
|
|
// reference to the event on the inserter's stack
|
|
|
|
const auto &event
|
|
|
|
{
|
|
|
|
m::vm::inserted.wait(lock)
|
|
|
|
};
|
|
|
|
|
|
|
|
sender_handle(event);
|
|
|
|
}
|
|
|
|
catch(const ircd::ctx::interrupted &e)
|
|
|
|
{
|
|
|
|
ircd::log::debug("sender worker interrupted");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
catch(const timeout &e)
|
|
|
|
{
|
|
|
|
ircd::log::debug("sender worker: %s", e.what());
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
ircd::log::error("sender worker: %s", e.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sender_handle(const m::event &event)
|
|
|
|
{
|
|
|
|
const auto &room_id
|
|
|
|
{
|
|
|
|
json::get<"room_id"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(room_id)
|
|
|
|
{
|
|
|
|
sender_handle(event, room_id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
|
2017-11-30 20:01:14 +01:00
|
|
|
ssize_t txn_ctr
|
|
|
|
{
|
|
|
|
8
|
|
|
|
};
|
|
|
|
|
2017-11-16 02:48:25 +01:00
|
|
|
void
|
|
|
|
sender_handle(const m::event &event,
|
|
|
|
const m::room::id &room_id)
|
2017-11-30 20:01:14 +01:00
|
|
|
try
|
2017-11-16 02:48:25 +01:00
|
|
|
{
|
|
|
|
const m::room room
|
|
|
|
{
|
|
|
|
room_id
|
|
|
|
};
|
|
|
|
|
2017-11-30 20:01:14 +01:00
|
|
|
const m::event::id &event_id
|
|
|
|
{
|
|
|
|
json::get<"event_id"_>(event)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
catch(const http::error &e)
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
throw;
|
2017-11-16 02:48:25 +01:00
|
|
|
}
|