mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2024-11-25 12:11:36 +08:00
实现whip/whep delete相关功能
通过whip/whep 回复http头中的Location url进行删除资源 新增delete token随机数实现删除鉴权
This commit is contained in:
parent
d2349f01bd
commit
8c049d4e28
@ -1628,18 +1628,21 @@ void installWebApi() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
static constexpr char delete_webrtc_url [] = "/index/api/delete_webrtc";
|
||||||
static auto whip_whep_func = [](const char *type, API_ARGS_STRING_ASYNC) {
|
static auto whip_whep_func = [](const char *type, API_ARGS_STRING_ASYNC) {
|
||||||
auto offer = allArgs.getArgs();
|
auto offer = allArgs.getArgs();
|
||||||
CHECK(!offer.empty(), "http body(webrtc offer sdp) is empty");
|
CHECK(!offer.empty(), "http body(webrtc offer sdp) is empty");
|
||||||
|
|
||||||
WebRtcPluginManager::Instance().getAnswerSdp(static_cast<Session&>(sender), type,
|
auto &session = static_cast<Session&>(sender);
|
||||||
WebRtcArgsImp(allArgs, sender.getIdentifier()),
|
auto location = std::string("http") + (session.overSsl() ? "s" : "") + "://" + allArgs["host"] + delete_webrtc_url;
|
||||||
[invoker, offer, headerOut](const WebRtcInterface &exchanger) mutable {
|
WebRtcPluginManager::Instance().getAnswerSdp(session, type, WebRtcArgsImp(allArgs, sender.getIdentifier()),
|
||||||
|
[invoker, offer, headerOut, location](const WebRtcInterface &exchanger) mutable {
|
||||||
// 设置跨域
|
// 设置跨域
|
||||||
headerOut["Access-Control-Allow-Origin"] = "*";
|
headerOut["Access-Control-Allow-Origin"] = "*";
|
||||||
try {
|
try {
|
||||||
// 设置返回类型
|
// 设置返回类型
|
||||||
headerOut["Content-Type"] = "application/sdp";
|
headerOut["Content-Type"] = "application/sdp";
|
||||||
|
headerOut["Location"] = location + "?id=" + exchanger.getIdentifier() + "&token=" + exchanger.deleteRandStr();
|
||||||
invoker(201, headerOut, exchangeSdp(exchanger, offer));
|
invoker(201, headerOut, exchangeSdp(exchanger, offer));
|
||||||
} catch (std::exception &ex) {
|
} catch (std::exception &ex) {
|
||||||
headerOut["Content-Type"] = "text/plain";
|
headerOut["Content-Type"] = "text/plain";
|
||||||
@ -1650,6 +1653,22 @@ void installWebApi() {
|
|||||||
|
|
||||||
api_regist("/index/api/whip", [](API_ARGS_STRING_ASYNC) { whip_whep_func("push", API_ARGS_VALUE, invoker); });
|
api_regist("/index/api/whip", [](API_ARGS_STRING_ASYNC) { whip_whep_func("push", API_ARGS_VALUE, invoker); });
|
||||||
api_regist("/index/api/whep", [](API_ARGS_STRING_ASYNC) { whip_whep_func("play", API_ARGS_VALUE, invoker); });
|
api_regist("/index/api/whep", [](API_ARGS_STRING_ASYNC) { whip_whep_func("play", API_ARGS_VALUE, invoker); });
|
||||||
|
|
||||||
|
api_regist(delete_webrtc_url, [](API_ARGS_MAP_ASYNC) {
|
||||||
|
CHECK_ARGS("id", "token");
|
||||||
|
CHECK(allArgs.getParser().Method() == "DELETE", "http method is not DELETE: " + allArgs.getParser().Method());
|
||||||
|
auto obj = WebRtcTransportManager::Instance().getItem(allArgs["id"]);
|
||||||
|
if (!obj) {
|
||||||
|
invoker(404, headerOut, "id not found");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (obj->deleteRandStr() != allArgs["token"]) {
|
||||||
|
invoker(401, headerOut, "token incorrect");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
obj->safeShutdown(SockException(Err_shutdown, "deleted by http api"));
|
||||||
|
invoker(200, headerOut, "");
|
||||||
|
});
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(ENABLE_VERSION)
|
#if defined(ENABLE_VERSION)
|
||||||
|
@ -52,6 +52,7 @@ ssize_t HttpSession::onRecvHeader(const char *header,size_t len) {
|
|||||||
static unordered_map<string, HttpCMDHandle> s_func_map;
|
static unordered_map<string, HttpCMDHandle> s_func_map;
|
||||||
static onceToken token([]() {
|
static onceToken token([]() {
|
||||||
s_func_map.emplace("GET",&HttpSession::Handle_Req_GET);
|
s_func_map.emplace("GET",&HttpSession::Handle_Req_GET);
|
||||||
|
s_func_map.emplace("DELETE",&HttpSession::Handle_Req_GET);
|
||||||
s_func_map.emplace("POST",&HttpSession::Handle_Req_POST);
|
s_func_map.emplace("POST",&HttpSession::Handle_Req_POST);
|
||||||
s_func_map.emplace("HEAD",&HttpSession::Handle_Req_HEAD);
|
s_func_map.emplace("HEAD",&HttpSession::Handle_Req_HEAD);
|
||||||
s_func_map.emplace("OPTIONS",&HttpSession::Handle_Req_OPTIONS);
|
s_func_map.emplace("OPTIONS",&HttpSession::Handle_Req_OPTIONS);
|
||||||
|
@ -113,6 +113,13 @@ const string &WebRtcTransport::getIdentifier() const {
|
|||||||
return _identifier;
|
return _identifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const std::string& WebRtcTransport::deleteRandStr() const {
|
||||||
|
if (_delete_rand_str.empty()) {
|
||||||
|
_delete_rand_str = makeRandStr(32);
|
||||||
|
}
|
||||||
|
return _delete_rand_str;
|
||||||
|
}
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
void WebRtcTransport::OnIceServerSendStunPacket(
|
void WebRtcTransport::OnIceServerSendStunPacket(
|
||||||
@ -1053,6 +1060,15 @@ void WebRtcTransportImp::onBeforeEncryptRtp(const char *buf, int &len, void *ctx
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void WebRtcTransportImp::safeShutdown(const SockException &ex) {
|
||||||
|
std::weak_ptr<WebRtcTransportImp> weak_self = static_pointer_cast<WebRtcTransportImp>(shared_from_this());
|
||||||
|
getPoller()->async([ex, weak_self]() {
|
||||||
|
if (auto strong_self = weak_self.lock()) {
|
||||||
|
strong_self->onShutdown(ex);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
void WebRtcTransportImp::onShutdown(const SockException &ex) {
|
void WebRtcTransportImp::onShutdown(const SockException &ex) {
|
||||||
WarnL << ex;
|
WarnL << ex;
|
||||||
unrefSelf();
|
unrefSelf();
|
||||||
|
@ -41,6 +41,7 @@ public:
|
|||||||
virtual ~WebRtcInterface() = default;
|
virtual ~WebRtcInterface() = default;
|
||||||
virtual std::string getAnswerSdp(const std::string &offer) = 0;
|
virtual std::string getAnswerSdp(const std::string &offer) = 0;
|
||||||
virtual const std::string& getIdentifier() const = 0;
|
virtual const std::string& getIdentifier() const = 0;
|
||||||
|
virtual const std::string& deleteRandStr() const { static std::string s_null; return s_null; }
|
||||||
};
|
};
|
||||||
|
|
||||||
std::string exchangeSdp(const WebRtcInterface &exchanger, const std::string& offer);
|
std::string exchangeSdp(const WebRtcInterface &exchanger, const std::string& offer);
|
||||||
@ -92,6 +93,7 @@ public:
|
|||||||
* 获取对象唯一id
|
* 获取对象唯一id
|
||||||
*/
|
*/
|
||||||
const std::string& getIdentifier() const override;
|
const std::string& getIdentifier() const override;
|
||||||
|
const std::string& deleteRandStr() const override;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* socket收到udp数据
|
* socket收到udp数据
|
||||||
@ -174,6 +176,7 @@ protected:
|
|||||||
std::shared_ptr<RTC::IceServer> _ice_server;
|
std::shared_ptr<RTC::IceServer> _ice_server;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
mutable std::string _delete_rand_str;
|
||||||
std::string _identifier;
|
std::string _identifier;
|
||||||
EventPoller::Ptr _poller;
|
EventPoller::Ptr _poller;
|
||||||
std::shared_ptr<RTC::DtlsTransport> _dtls_transport;
|
std::shared_ptr<RTC::DtlsTransport> _dtls_transport;
|
||||||
@ -248,6 +251,7 @@ public:
|
|||||||
|
|
||||||
void createRtpChannel(const std::string &rid, uint32_t ssrc, MediaTrack &track);
|
void createRtpChannel(const std::string &rid, uint32_t ssrc, MediaTrack &track);
|
||||||
void removeTuple(RTC::TransportTuple* tuple);
|
void removeTuple(RTC::TransportTuple* tuple);
|
||||||
|
void safeShutdown(const SockException &ex);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void OnIceServerSelectedTuple(const RTC::IceServer *iceServer, RTC::TransportTuple *tuple) override;
|
void OnIceServerSelectedTuple(const RTC::IceServer *iceServer, RTC::TransportTuple *tuple) override;
|
||||||
|
Loading…
Reference in New Issue
Block a user