1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-13 11:51:57 +00:00

Log: Use 8 bytes random string as context id

This commit is contained in:
winlin 2020-07-09 16:51:20 +08:00
parent 5b199249d0
commit 742826a655
6 changed files with 33 additions and 35 deletions

View file

@ -57,20 +57,6 @@ using namespace std;
#include <srs_app_rtc_server.hpp>
#include <srs_app_rtc_source.hpp>
// TODO: FIXME: Move to utility.
string gen_random_str(int len)
{
static string random_table = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
string ret;
ret.reserve(len);
for (int i = 0; i < len; ++i) {
ret.append(1, random_table[random() % random_table.size()]);
}
return ret;
}
uint64_t SrsNtp::kMagicNtpFractionalUnit = 1ULL << 32;
SrsNtp::SrsNtp()

View file

@ -75,9 +75,6 @@ const uint8_t kSLI = 2;
const uint8_t kRPSI = 3;
const uint8_t kAFB = 15;
// TODO: FIXME: Move to utility.
extern std::string gen_random_str(int len);
class SrsNtp
{
public:

View file

@ -37,6 +37,7 @@
#include <srs_app_http_api.hpp>
#include <srs_app_rtc_dtls.hpp>
#include <srs_service_utility.hpp>
#include <srs_protocol_utility.hpp>
#include <srs_app_rtc_source.hpp>
#include <srs_app_rtc_api.hpp>
@ -310,12 +311,12 @@ srs_error_t SrsRtcServer::create_session(
return srs_error_new(ERROR_RTC_SOURCE_BUSY, "stream %s busy", req->get_stream_url().c_str());
}
std::string local_pwd = gen_random_str(32);
std::string local_pwd = srs_random_str(32);
std::string local_ufrag = "";
// TODO: FIXME: Rename for a better name, it's not an username.
std::string username = "";
while (true) {
local_ufrag = gen_random_str(8);
local_ufrag = srs_random_str(8);
username = local_ufrag + ":" + remote_sdp.get_ice_ufrag();
if (!map_username_session.count(username)) {
@ -361,9 +362,9 @@ srs_error_t SrsRtcServer::create_session2(SrsSdp& local_sdp, SrsRtcConnection**
{
srs_error_t err = srs_success;
std::string local_pwd = gen_random_str(32);
std::string local_pwd = srs_random_str(32);
// TODO: FIXME: Collision detect.
std::string local_ufrag = gen_random_str(8);
std::string local_ufrag = srs_random_str(8);
SrsRtcConnection* session = new SrsRtcConnection(this);
*psession = session;

View file

@ -148,20 +148,39 @@ void srs_parse_query_string(string q, map<string,string>& query)
}
}
static bool _random_initialized = false;
void srs_random_generate(char* bytes, int size)
{
static bool _random_initialized = false;
if (!_random_initialized) {
srand(0);
_random_initialized = true;
::srandom((unsigned long)(srs_update_system_time() | (::getpid()<<13)));
}
for (int i = 0; i < size; i++) {
// the common value in [0x0f, 0xf0]
bytes[i] = 0x0f + (rand() % (256 - 0x0f - 0x0f));
bytes[i] = 0x0f + (random() % (256 - 0x0f - 0x0f));
}
}
std::string srs_random_str(int len)
{
if (!_random_initialized) {
_random_initialized = true;
::srandom((unsigned long)(srs_update_system_time() | (::getpid()<<13)));
}
static string random_table = "01234567890123456789012345678901234567890123456789abcdefghijklmnopqrstuvwxyz";
string ret;
ret.reserve(len);
for (int i = 0; i < len; ++i) {
ret.append(1, random_table[random() % random_table.size()]);
}
return ret;
}
string srs_generate_tc_url(string host, string vhost, string app, int port)
{
string tcUrl = "rtmp://";

View file

@ -65,11 +65,12 @@ extern void srs_discovery_tc_url(std::string tcUrl, std::string& schema, std::st
// must format as key=value&...&keyN=valueN
extern void srs_parse_query_string(std::string q, std::map<std::string, std::string>& query);
/**
* generate ramdom data for handshake.
*/
// Generate ramdom data for handshake.
extern void srs_random_generate(char* bytes, int size);
// Generate random string [0-9a-z] in size of len bytes.
extern std::string srs_random_str(int len);
/**
* generate the tcUrl without param.
* @remark Use host as tcUrl.vhost if vhost is default vhost.

View file

@ -31,6 +31,7 @@ using namespace std;
#include <srs_kernel_error.hpp>
#include <srs_kernel_utility.hpp>
#include <srs_protocol_utility.hpp>
#define SRS_BASIC_LOG_SIZE 1024
@ -44,14 +45,7 @@ SrsThreadContext::~SrsThreadContext()
SrsContextId SrsThreadContext::generate_id()
{
static int id = 0;
if (id == 0) {
id = (100 + ((uint32_t)(int64_t)this)%1000);
}
int gid = id++;
SrsContextId cid = SrsContextId(srs_int2str(gid));
SrsContextId cid = SrsContextId(srs_random_str(8));
cache[srs_thread_self()] = cid;
return cid;
}