2018-08-15 01:47:42 +02: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.
|
|
|
|
|
|
|
|
using namespace ircd;
|
|
|
|
|
2018-08-16 08:41:12 +02:00
|
|
|
extern "C" std::list<net::listener> listeners;
|
|
|
|
|
2018-08-31 04:16:03 +02:00
|
|
|
extern "C" bool loaded_listener(const string_view &name);
|
|
|
|
static bool load_listener(const string_view &, const json::object &);
|
|
|
|
static bool load_listener(const m::event &);
|
|
|
|
extern "C" bool unload_listener(const string_view &name);
|
|
|
|
extern "C" bool load_listener(const string_view &name);
|
2023-02-07 21:14:44 +01:00
|
|
|
static void init_conf_listeners();
|
|
|
|
static void init_room_listeners();
|
2020-06-13 02:23:30 +02:00
|
|
|
static void on_quit() noexcept;
|
2019-01-18 18:47:32 +01:00
|
|
|
static void on_run();
|
2018-09-02 07:23:08 +02:00
|
|
|
static void on_unload();
|
2018-08-15 05:09:20 +02:00
|
|
|
static void on_load();
|
|
|
|
|
2018-08-15 01:47:42 +02:00
|
|
|
mapi::header
|
|
|
|
IRCD_MODULE
|
|
|
|
{
|
2018-09-02 07:23:08 +02:00
|
|
|
"Server listeners", on_load, on_unload
|
2018-08-15 01:47:42 +02:00
|
|
|
};
|
|
|
|
|
2019-01-18 18:47:32 +01:00
|
|
|
const ircd::run::changed
|
2019-03-16 23:35:04 +01:00
|
|
|
_on_change{[](const auto &level)
|
2019-01-18 18:47:32 +01:00
|
|
|
{
|
|
|
|
if(level == run::level::RUN)
|
|
|
|
on_run();
|
2019-03-16 23:35:04 +01:00
|
|
|
else if(level == run::level::QUIT)
|
|
|
|
on_quit();
|
2019-01-18 18:47:32 +01:00
|
|
|
}};
|
|
|
|
|
2018-08-16 08:41:12 +02:00
|
|
|
/// Active listener state
|
|
|
|
decltype(listeners)
|
2018-08-15 05:09:20 +02:00
|
|
|
listeners;
|
2018-08-15 01:47:42 +02:00
|
|
|
|
2018-08-15 05:09:20 +02:00
|
|
|
//
|
2018-08-17 23:28:53 +02:00
|
|
|
// On module load any existing listener descriptions are sought out
|
|
|
|
// of room state and instantiated (i.e on startup).
|
2018-08-15 05:09:20 +02:00
|
|
|
//
|
2018-08-15 01:47:42 +02:00
|
|
|
|
|
|
|
void
|
2018-08-15 05:09:20 +02:00
|
|
|
on_load()
|
2018-08-15 01:47:42 +02:00
|
|
|
{
|
2018-12-09 00:17:13 +01:00
|
|
|
if(!bool(ircd::net::listen))
|
2018-08-15 01:47:42 +02:00
|
|
|
{
|
|
|
|
log::warning
|
|
|
|
{
|
|
|
|
"Not listening on any addresses because nolisten flag is set."
|
|
|
|
};
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-02-07 21:14:44 +01:00
|
|
|
init_conf_listeners();
|
|
|
|
init_room_listeners();
|
2018-08-15 05:09:20 +02:00
|
|
|
}
|
2018-08-15 01:47:42 +02:00
|
|
|
|
2018-09-02 07:23:08 +02:00
|
|
|
void
|
|
|
|
on_unload()
|
|
|
|
{
|
2019-01-18 18:47:32 +01:00
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
"Clearing %zu listeners...",
|
|
|
|
listeners.size()
|
|
|
|
};
|
|
|
|
|
2018-09-02 07:23:08 +02:00
|
|
|
listeners.clear();
|
|
|
|
}
|
|
|
|
|
2019-01-18 18:47:32 +01:00
|
|
|
void
|
|
|
|
on_run()
|
|
|
|
{
|
|
|
|
for(auto &listener : listeners)
|
2019-03-16 23:13:44 +01:00
|
|
|
start(listener);
|
2020-06-13 02:23:30 +02:00
|
|
|
|
|
|
|
if(!listeners.empty())
|
|
|
|
log::notice
|
|
|
|
{
|
|
|
|
"Accepting connections on %zu listeners...",
|
|
|
|
listeners.size()
|
|
|
|
};
|
2019-01-18 18:47:32 +01:00
|
|
|
}
|
|
|
|
|
2019-03-16 23:35:04 +01:00
|
|
|
void
|
|
|
|
on_quit()
|
2020-06-13 02:23:30 +02:00
|
|
|
noexcept
|
2019-03-16 23:35:04 +01:00
|
|
|
{
|
|
|
|
for(auto &listener : listeners)
|
|
|
|
stop(listener);
|
2020-06-13 02:23:30 +02:00
|
|
|
|
|
|
|
if(!listeners.empty())
|
|
|
|
log::notice
|
|
|
|
{
|
|
|
|
"Stopped accepting new connections on %zu listeners",
|
|
|
|
listeners.size()
|
|
|
|
};
|
2019-03-16 23:35:04 +01:00
|
|
|
}
|
|
|
|
|
2018-08-15 05:09:20 +02:00
|
|
|
void
|
2023-02-07 21:14:44 +01:00
|
|
|
init_conf_listeners()
|
|
|
|
{
|
|
|
|
static const string_view prefix
|
|
|
|
{
|
|
|
|
"ircd_listen"
|
|
|
|
};
|
|
|
|
|
|
|
|
std::map<string_view, json::strung> map;
|
|
|
|
for_each_env(prefix, [&map]
|
|
|
|
(const string_view &full_key, const string_view &val)
|
|
|
|
{
|
|
|
|
const auto key
|
|
|
|
{
|
|
|
|
lstrip(full_key, prefix)
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto name
|
|
|
|
{
|
|
|
|
token(key, '_', 0)
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto opt
|
|
|
|
{
|
|
|
|
tokens_after(key, '_', 0)
|
|
|
|
};
|
|
|
|
|
|
|
|
map[name] = json::replace(map[name],
|
|
|
|
{
|
|
|
|
opt, val
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
for(const std::pair<string_view, json::object> p : map)
|
|
|
|
if(load_listener(p.first, p.second))
|
|
|
|
log::notice
|
|
|
|
{
|
|
|
|
"Listener '%s' configured for %s:%s by environment",
|
|
|
|
p.first,
|
|
|
|
p.second["host"],
|
|
|
|
p.second["port"],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
init_room_listeners()
|
2018-08-15 05:09:20 +02:00
|
|
|
{
|
2019-10-01 05:50:58 +02:00
|
|
|
const m::room::id::buf my_room
|
|
|
|
{
|
|
|
|
"ircd", m::origin(m::my())
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::room::state state
|
|
|
|
{
|
|
|
|
my_room
|
|
|
|
};
|
|
|
|
|
|
|
|
state.for_each("ircd.listen", [](const m::event &event)
|
2018-08-15 01:47:42 +02:00
|
|
|
{
|
2018-08-31 04:16:03 +02:00
|
|
|
load_listener(event);
|
2018-08-15 05:09:20 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if(listeners.empty())
|
|
|
|
log::warning
|
2018-08-15 01:47:42 +02:00
|
|
|
{
|
2018-08-15 05:09:20 +02:00
|
|
|
"No listening sockets configured; can't hear anyone."
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-08-17 23:28:53 +02:00
|
|
|
//
|
|
|
|
// Upon processing of a new event which saved a listener description
|
|
|
|
// to room state in its content, we instantiate the listener here.
|
|
|
|
//
|
|
|
|
|
|
|
|
static void
|
2018-10-07 07:17:46 +02:00
|
|
|
create_listener(const m::event &event,
|
|
|
|
m::vm::eval &)
|
2018-08-17 23:28:53 +02:00
|
|
|
{
|
2018-08-31 04:16:03 +02:00
|
|
|
load_listener(event);
|
2018-08-17 23:28:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Hook for a new listener description being sent.
|
2019-08-10 06:27:12 +02:00
|
|
|
m::hookfn<m::vm::eval &>
|
2018-08-17 23:28:53 +02:00
|
|
|
create_listener_hook
|
|
|
|
{
|
|
|
|
create_listener,
|
|
|
|
{
|
2018-10-07 07:17:46 +02:00
|
|
|
{ "_site", "vm.effect" },
|
2018-08-17 23:28:53 +02:00
|
|
|
{ "room_id", "!ircd" },
|
|
|
|
{ "type", "ircd.listen" },
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// Common
|
|
|
|
//
|
|
|
|
|
2018-08-31 04:16:03 +02:00
|
|
|
bool
|
|
|
|
load_listener(const string_view &name)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
bool ret{false};
|
2019-10-01 05:50:58 +02:00
|
|
|
const m::room::id::buf my_room
|
|
|
|
{
|
|
|
|
"ircd", m::origin(m::my())
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::room::state state
|
|
|
|
{
|
|
|
|
my_room
|
|
|
|
};
|
|
|
|
|
2018-08-31 04:16:03 +02:00
|
|
|
state.get("ircd.listen", name, [&ret]
|
|
|
|
(const m::event &event)
|
|
|
|
{
|
|
|
|
ret = load_listener(event);
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
catch(const m::NOT_FOUND &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
"Failed to find any listener configuration for '%s'",
|
|
|
|
name
|
|
|
|
};
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
unload_listener(const string_view &name)
|
|
|
|
{
|
|
|
|
if(!loaded_listener(name))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
listeners.remove_if([&name]
|
|
|
|
(const auto &listener)
|
|
|
|
{
|
|
|
|
return listener.name() == name;
|
|
|
|
});
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
load_listener(const m::event &event)
|
2018-08-16 08:41:12 +02:00
|
|
|
{
|
|
|
|
const string_view &name
|
|
|
|
{
|
|
|
|
at<"state_key"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::object &opts
|
|
|
|
{
|
|
|
|
json::get<"content"_>(event)
|
|
|
|
};
|
|
|
|
|
2021-02-04 01:53:27 +01:00
|
|
|
if(!load_listener(name, opts))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
log::notice
|
|
|
|
{
|
|
|
|
"Listener '%s' configured for %s:%s by %s",
|
|
|
|
name,
|
|
|
|
opts["host"],
|
|
|
|
opts["port"],
|
|
|
|
string_view{event.event_id},
|
|
|
|
};
|
|
|
|
|
|
|
|
return true;
|
2018-08-16 08:41:12 +02:00
|
|
|
}
|
|
|
|
|
2019-04-16 07:03:26 +02:00
|
|
|
ctx::context
|
|
|
|
_listener_allow
|
|
|
|
{
|
2020-02-28 01:25:29 +01:00
|
|
|
"listener allow", 64_KiB, context::POST, []
|
2019-04-16 07:03:26 +02:00
|
|
|
{
|
|
|
|
while(1)
|
|
|
|
{
|
2022-06-24 04:18:05 +02:00
|
|
|
client::dock.wait([]() noexcept
|
2019-04-16 07:03:26 +02:00
|
|
|
{
|
|
|
|
return !client::pool.avail();
|
|
|
|
});
|
|
|
|
|
2022-06-24 04:18:05 +02:00
|
|
|
client::dock.wait([]() noexcept
|
2019-04-16 07:03:26 +02:00
|
|
|
{
|
|
|
|
if(!client::pool.avail())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(client::map.size() >= size_t(client::settings::max_client))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
for(auto &listener : listeners)
|
|
|
|
allow(listener);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-07-06 00:31:17 +02:00
|
|
|
conf::item<std::string>
|
|
|
|
listener_whitelist
|
|
|
|
{
|
|
|
|
{ "name", "ircd.net.listen.whitelist" },
|
|
|
|
{ "default", string_view{} },
|
|
|
|
};
|
|
|
|
|
2018-09-02 07:31:58 +02:00
|
|
|
static bool
|
2023-03-08 17:36:04 +01:00
|
|
|
_listener_proffer(net::acceptor &listener,
|
2019-01-18 18:42:44 +01:00
|
|
|
const net::ipport &ipport)
|
2018-09-02 07:03:25 +02:00
|
|
|
{
|
2019-04-16 07:02:30 +02:00
|
|
|
thread_local char strbuf[256];
|
2019-01-18 17:55:06 +01:00
|
|
|
if(unlikely(ircd::run::level != ircd::run::level::RUN))
|
2018-09-02 07:03:25 +02:00
|
|
|
{
|
|
|
|
log::dwarning
|
|
|
|
{
|
2022-07-06 00:31:17 +02:00
|
|
|
"Refusing to add new client from %s :runlevel %s",
|
2019-04-16 07:02:30 +02:00
|
|
|
string(strbuf, ipport),
|
2019-01-18 17:55:06 +01:00
|
|
|
reflect(ircd::run::level)
|
2018-09-02 07:03:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-03-03 23:12:28 +01:00
|
|
|
// Sets the asynchronous handler for the next accept. We can play with
|
|
|
|
// delaying this call under certain conditions to provide flow control.
|
|
|
|
allow(listener);
|
|
|
|
|
2018-09-02 07:03:25 +02:00
|
|
|
if(unlikely(client::map.size() >= size_t(client::settings::max_client)))
|
|
|
|
{
|
|
|
|
log::warning
|
|
|
|
{
|
2022-07-06 00:31:17 +02:00
|
|
|
"Refusing to add new client from %s :maximum of %zu reached",
|
2019-04-16 07:02:30 +02:00
|
|
|
string(strbuf, ipport),
|
2018-09-02 07:03:25 +02:00
|
|
|
size_t(client::settings::max_client)
|
|
|
|
};
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-04-16 07:03:26 +02:00
|
|
|
if(unlikely(!client::pool.avail()))
|
|
|
|
{
|
|
|
|
log::dwarning
|
|
|
|
{
|
2022-07-06 00:31:17 +02:00
|
|
|
"Refusing to add new client from %s :request pool exhausted.",
|
2019-04-16 07:03:26 +02:00
|
|
|
string(strbuf, ipport),
|
|
|
|
};
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-02-24 04:42:24 +01:00
|
|
|
// Trapdoor for reverse-proxies
|
|
|
|
const bool local
|
|
|
|
{
|
|
|
|
//TODO: lan cidr
|
|
|
|
net::is_loop(ipport)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!local && client::count(ipport) >= size_t(client::settings::max_client_per_peer))
|
2018-09-02 07:03:25 +02:00
|
|
|
{
|
|
|
|
log::dwarning
|
|
|
|
{
|
2022-07-06 00:31:17 +02:00
|
|
|
"Refusing to add new client from %s :maximum of %zu connections for peer.",
|
|
|
|
string(strbuf, ipport),
|
2018-09-02 07:03:25 +02:00
|
|
|
size_t(client::settings::max_client_per_peer)
|
|
|
|
};
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-07-06 00:31:17 +02:00
|
|
|
const string_view ipaddr_str
|
|
|
|
{
|
|
|
|
listener_whitelist?
|
|
|
|
net::string(strbuf, net::ipaddr(ipport)):
|
|
|
|
string_view{}
|
|
|
|
};
|
|
|
|
|
|
|
|
const bool listed
|
|
|
|
{
|
|
|
|
!ircd::tokens(listener_whitelist, ' ', [&ipaddr_str]
|
|
|
|
(const string_view &item)
|
|
|
|
{
|
|
|
|
return item == ipaddr_str? false: true;
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
if(listener_whitelist && !listed)
|
|
|
|
{
|
|
|
|
log::dwarning
|
|
|
|
{
|
|
|
|
"Refusing to add new client from %s :not whitelisted.",
|
|
|
|
ipaddr_str,
|
|
|
|
};
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-09-02 07:03:25 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-08-31 04:16:03 +02:00
|
|
|
bool
|
|
|
|
load_listener(const string_view &name,
|
2018-08-15 05:09:20 +02:00
|
|
|
const json::object &opts)
|
2018-08-30 01:20:21 +02:00
|
|
|
try
|
2018-08-15 05:09:20 +02:00
|
|
|
{
|
2018-08-31 04:16:03 +02:00
|
|
|
if(loaded_listener(name))
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
"A listener with the name '%s' is already loaded", name
|
|
|
|
};
|
|
|
|
|
2018-09-02 07:31:58 +02:00
|
|
|
listeners.emplace_back(name, opts, client::create, _listener_proffer);
|
2019-03-17 23:29:15 +01:00
|
|
|
|
|
|
|
if(ircd::run::level == ircd::run::level::RUN)
|
|
|
|
start(listeners.back());
|
|
|
|
|
2018-08-31 04:16:03 +02:00
|
|
|
return true;
|
2018-08-15 05:09:20 +02:00
|
|
|
}
|
2018-08-30 01:20:21 +02:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
"Failed to init listener '%s' :%s",
|
|
|
|
name,
|
|
|
|
e.what()
|
|
|
|
};
|
2018-08-31 04:16:03 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
loaded_listener(const string_view &name)
|
|
|
|
{
|
|
|
|
return end(listeners) != std::find_if(begin(listeners), end(listeners), [&name]
|
|
|
|
(const auto &listener)
|
|
|
|
{
|
|
|
|
return listener.name() == name;
|
|
|
|
});
|
2018-08-30 01:20:21 +02:00
|
|
|
}
|