1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

Merge branch '4.0release' into develop

This commit is contained in:
winlin 2022-09-02 10:57:56 +08:00
commit 84c96076a9
6 changed files with 116 additions and 44 deletions

View file

@ -2291,7 +2291,8 @@ srs_error_t SrsConfig::check_normal_config()
string n = conf->at(i)->name;
if (n != "enabled" && n != "listen" && n != "dir" && n != "candidate" && n != "ecdsa"
&& n != "encrypt" && n != "reuseport" && n != "merge_nalus" && n != "black_hole"
&& n != "ip_family" && n != "api_as_candidates") {
&& n != "ip_family" && n != "api_as_candidates" && n != "resolve_api_domain"
&& n != "keep_api_domain" && n != "use_auto_detect_network_ip") {
return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal rtc_server.%s", n.c_str());
}
}
@ -3649,6 +3650,57 @@ bool SrsConfig::get_api_as_candidates()
return SRS_CONF_PERFER_TRUE(conf->arg0());
}
bool SrsConfig::get_resolve_api_domain()
{
static bool DEFAULT = true;
SrsConfDirective* conf = root->get("rtc_server");
if (!conf) {
return DEFAULT;
}
conf = conf->get("resolve_api_domain");
if (!conf || conf->arg0().empty()) {
return DEFAULT;
}
return SRS_CONF_PERFER_TRUE(conf->arg0());
}
bool SrsConfig::get_keep_api_domain()
{
static bool DEFAULT = false;
SrsConfDirective* conf = root->get("rtc_server");
if (!conf) {
return DEFAULT;
}
conf = conf->get("keep_api_domain");
if (!conf || conf->arg0().empty()) {
return DEFAULT;
}
return SRS_CONF_PERFER_FALSE(conf->arg0());
}
bool SrsConfig::get_use_auto_detect_network_ip()
{
static bool DEFAULT = true;
SrsConfDirective* conf = root->get("rtc_server");
if (!conf) {
return DEFAULT;
}
conf = conf->get("use_auto_detect_network_ip");
if (!conf || conf->arg0().empty()) {
return DEFAULT;
}
return SRS_CONF_PERFER_TRUE(conf->arg0());
}
std::string SrsConfig::get_rtc_server_ip_family()
{
static string DEFAULT = "ipv4";

View file

@ -495,6 +495,9 @@ public:
virtual int get_rtc_server_listen();
virtual std::string get_rtc_server_candidates();
virtual bool get_api_as_candidates();
virtual bool get_resolve_api_domain();
virtual bool get_keep_api_domain();
virtual bool get_use_auto_detect_network_ip();
virtual std::string get_rtc_server_ip_family();
virtual bool get_rtc_server_ecdsa();
virtual bool get_rtc_server_encrypt();

View file

@ -158,12 +158,7 @@ srs_error_t api_server_as_candidates(string api, set<string>& candidate_ips)
return err;
}
SrsHttpUri uri;
if ((err = uri.initialize(api)) != srs_success) {
return srs_error_wrap(err, "parse %s", api.c_str());
}
string hostname = uri.get_host();
string hostname = api;
if (hostname.empty() || hostname == SRS_CONSTS_LOCALHOST_NAME) {
return err;
}
@ -171,15 +166,27 @@ srs_error_t api_server_as_candidates(string api, set<string>& candidate_ips)
return err;
}
// Try to parse the domain name if not IP.
int family = 0;
string ip = srs_dns_resolve(hostname, family);
if (ip.empty() || ip == SRS_CONSTS_LOCALHOST || ip == SRS_CONSTS_LOOPBACK || ip == SRS_CONSTS_LOOPBACK6) {
return err;
// Whether add domain name.
if (!srs_is_ipv4(hostname) && _srs_config->get_keep_api_domain()) {
candidate_ips.insert(hostname);
}
// Try to add the API server ip as candidates.
candidate_ips.insert(ip);
// Try to parse the domain name if not IP.
if (!srs_is_ipv4(hostname) && _srs_config->get_resolve_api_domain()) {
int family = 0;
string ip = srs_dns_resolve(hostname, family);
if (ip.empty() || ip == SRS_CONSTS_LOCALHOST || ip == SRS_CONSTS_LOOPBACK || ip == SRS_CONSTS_LOOPBACK6) {
return err;
}
// Try to add the API server ip as candidates.
candidate_ips.insert(ip);
}
// If hostname is IP, use it.
if (srs_is_ipv4(hostname)) {
candidate_ips.insert(hostname);
}
return err;
}
@ -195,8 +202,8 @@ static set<string> discover_candidates(SrsRtcUserConfig* ruc)
}
// Try to discover from api of request, if api_as_candidates enabled.
if ((err = api_server_as_candidates(ruc->api_, candidate_ips)) != srs_success) {
srs_warn("ignore discovering ip from api %s, err %s", ruc->api_.c_str(), srs_error_summary(err).c_str());
if ((err = api_server_as_candidates(ruc->req_->host, candidate_ips)) != srs_success) {
srs_warn("ignore discovering ip from api %s, err %s", ruc->req_->host.c_str(), srs_error_summary(err).c_str());
srs_freep(err);
}
@ -207,29 +214,32 @@ static set<string> discover_candidates(SrsRtcUserConfig* ruc)
return candidate_ips;
}
// Discover from local network interface addresses.
// All automatically detected IP list.
vector<SrsIPAddress*>& ips = srs_get_local_ips();
if (ips.empty()) {
return candidate_ips;
}
// We try to find the best match candidates, no loopback.
string family = _srs_config->get_rtc_server_ip_family();
for (int i = 0; i < (int)ips.size(); ++i) {
SrsIPAddress* ip = ips[i];
if (ip->is_loopback) {
continue;
}
// Discover from local network interface addresses.
if (_srs_config->get_use_auto_detect_network_ip()) {
// We try to find the best match candidates, no loopback.
string family = _srs_config->get_rtc_server_ip_family();
for (int i = 0; i < (int) ips.size(); ++i) {
SrsIPAddress* ip = ips[i];
if (ip->is_loopback) {
continue;
}
if (family == "ipv4" && !ip->is_ipv4) {
continue;
}
if (family == "ipv6" && ip->is_ipv4) {
continue;
}
if (family == "ipv4" && !ip->is_ipv4) {
continue;
}
if (family == "ipv6" && ip->is_ipv4) {
continue;
}
candidate_ips.insert(ip->ip);
srs_trace("Best matched ip=%s, ifname=%s", ip->ip.c_str(), ip->ifname.c_str());
candidate_ips.insert(ip->ip);
srs_trace("Best matched ip=%s, ifname=%s", ip->ip.c_str(), ip->ifname.c_str());
}
}
if (!candidate_ips.empty()) {
@ -248,7 +258,7 @@ static set<string> discover_candidates(SrsRtcUserConfig* ruc)
return candidate_ips;
}
// We use the first one.
// We use the first one, to make sure there will be at least one CANDIDATE.
if (candidate_ips.empty()) {
SrsIPAddress* ip = ips[0];
candidate_ips.insert(ip->ip);
@ -577,7 +587,7 @@ srs_error_t SrsRtcServer::do_create_session(SrsRtcUserConfig* ruc, SrsSdp& local
set<string> candidates = discover_candidates(ruc);
for (set<string>::iterator it = candidates.begin(); it != candidates.end(); ++it) {
string hostname; int port = listen_port;
srs_parse_hostport(*it, hostname,port);
srs_parse_hostport(*it, hostname, port);
local_sdp.add_candidate(hostname, port, "host");
}

View file

@ -9,6 +9,6 @@
#define VERSION_MAJOR 4
#define VERSION_MINOR 0
#define VERSION_REVISION 258
#define VERSION_REVISION 259
#endif