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

Discover api server and ip as candidates. v4.0.218

This commit is contained in:
winlin 2022-01-04 22:18:38 +08:00
parent 013fa8ac6c
commit fbb6d9464c
9 changed files with 97 additions and 22 deletions

View file

@ -6,6 +6,7 @@
#include <srs_app_rtc_server.hpp>
#include <set>
using namespace std;
#include <srs_app_config.hpp>
@ -149,18 +150,66 @@ bool srs_is_rtcp(const uint8_t* data, size_t len)
return (len >= 12) && (data[0] & 0x80) && (data[1] >= 192 && data[1] <= 223);
}
static std::vector<std::string> get_candidate_ips()
srs_error_t api_server_as_candidates(string api, set<string>& candidate_ips)
{
std::vector<std::string> candidate_ips;
srs_error_t err = srs_success;
if (api.empty() || !_srs_config->get_api_as_candidates()) {
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();
if (hostname.empty() || hostname == SRS_CONSTS_LOCALHOST_NAME) {
return err;
}
if (hostname == SRS_CONSTS_LOCALHOST || hostname == SRS_CONSTS_LOOPBACK || hostname == SRS_CONSTS_LOOPBACK6) {
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;
}
// Try to add the original hostname and ip as candidates.
candidate_ips.insert(hostname);
candidate_ips.insert(ip);
return err;
}
static set<string> discover_candidates(SrsRtcUserConfig* ruc)
{
srs_error_t err = srs_success;
// Try to discover the eip as candidate, specified by user.
set<string> candidate_ips;
if (!ruc->eip_.empty()) {
candidate_ips.insert(ruc->eip_);
}
// 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());
srs_freep(err);
}
// If not * or 0.0.0.0, use the candidate as exposed IP.
string candidate = _srs_config->get_rtc_server_candidates();
if (candidate != "*" && candidate != "0.0.0.0") {
candidate_ips.push_back(candidate);
candidate_ips.insert(candidate);
return candidate_ips;
}
// For * or 0.0.0.0, auto discovery expose ip addresses.
std::vector<SrsIPAddress*>& ips = srs_get_local_ips();
// Discover from local network interface addresses.
vector<SrsIPAddress*>& ips = srs_get_local_ips();
if (ips.empty()) {
return candidate_ips;
}
@ -180,7 +229,7 @@ static std::vector<std::string> get_candidate_ips()
continue;
}
candidate_ips.push_back(ip->ip);
candidate_ips.insert(ip->ip);
srs_trace("Best matched ip=%s, ifname=%s", ip->ip.c_str(), ip->ifname.c_str());
}
@ -195,7 +244,7 @@ static std::vector<std::string> get_candidate_ips()
continue;
}
candidate_ips.push_back(ip->ip);
candidate_ips.insert(ip->ip);
srs_trace("No best matched, use first ip=%s, ifname=%s", ip->ip.c_str(), ip->ifname.c_str());
return candidate_ips;
}
@ -203,7 +252,7 @@ static std::vector<std::string> get_candidate_ips()
// We use the first one.
if (candidate_ips.empty()) {
SrsIPAddress* ip = ips[0];
candidate_ips.push_back(ip->ip);
candidate_ips.insert(ip->ip);
srs_warn("No best matched, use first ip=%s, ifname=%s", ip->ip.c_str(), ip->ifname.c_str());
return candidate_ips;
}
@ -524,19 +573,17 @@ srs_error_t SrsRtcServer::do_create_session(SrsRtcUserConfig* ruc, SrsSdp& local
local_sdp.set_fingerprint(_srs_rtc_dtls_certificate->get_fingerprint());
// We allows to mock the eip of server.
if (!ruc->eip_.empty()) {
string host;
int port = _srs_config->get_rtc_server_listen();
srs_parse_hostport(ruc->eip_, host, port);
local_sdp.add_candidate(host, port, "host");
srs_trace("RTC: Use candidate mock_eip %s as %s:%d", ruc->eip_.c_str(), host.c_str(), port);
} else {
std::vector<string> candidate_ips = get_candidate_ips();
for (int i = 0; i < (int)candidate_ips.size(); ++i) {
local_sdp.add_candidate(candidate_ips[i], _srs_config->get_rtc_server_listen(), "host");
if (true) {
int listen_port = _srs_config->get_rtc_server_listen();
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);
local_sdp.add_candidate(hostname, port, "host");
}
srs_trace("RTC: Use candidates %s", srs_join_vector_string(candidate_ips, ", ").c_str());
vector<string> v = vector<string>(candidates.begin(), candidates.end());
srs_trace("RTC: Use candidates %s", srs_join_vector_string(v, ", ").c_str());
}
// Setup the negotiate DTLS by config.