2019-08-08 19:01:45 +08:00
|
|
|
|
/*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
|
2019-06-11 09:25:54 +08:00
|
|
|
|
*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
|
2019-06-11 09:25:54 +08:00
|
|
|
|
*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* Use of this source code is governed by MIT-like license that can be found in the
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* LICENSE file in the root of the source tree. All contributing project authors
|
|
|
|
|
* may be found in the AUTHORS file in the root of the source tree.
|
2019-06-11 09:25:54 +08:00
|
|
|
|
*/
|
2019-06-06 18:28:33 +08:00
|
|
|
|
|
|
|
|
|
#ifndef ZLMEDIAKIT_WEBAPI_H
|
|
|
|
|
#define ZLMEDIAKIT_WEBAPI_H
|
|
|
|
|
|
2019-06-19 10:32:54 +08:00
|
|
|
|
#include <string>
|
2020-12-27 22:14:59 +08:00
|
|
|
|
#include <functional>
|
2022-10-16 21:10:18 +08:00
|
|
|
|
#include "json/json.h"
|
2020-12-27 22:14:59 +08:00
|
|
|
|
#include "Common/Parser.h"
|
|
|
|
|
#include "Network/Socket.h"
|
|
|
|
|
#include "Http/HttpSession.h"
|
2022-03-12 13:19:21 +08:00
|
|
|
|
#include "Common/MultiMediaSourceMuxer.h"
|
2020-12-27 22:14:59 +08:00
|
|
|
|
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 配置文件路径 [AUTO-TRANSLATED:8a373c2f]
|
|
|
|
|
// Configuration file path
|
2022-02-02 20:34:50 +08:00
|
|
|
|
extern std::string g_ini_file;
|
2019-06-19 10:32:54 +08:00
|
|
|
|
|
2020-12-27 22:14:59 +08:00
|
|
|
|
namespace mediakit {
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// //////////RTSP服务器配置/////////// [AUTO-TRANSLATED:950e1981]
|
|
|
|
|
// //////////RTSP server configuration///////////
|
2019-06-06 18:28:33 +08:00
|
|
|
|
namespace Rtsp {
|
2022-02-02 20:34:50 +08:00
|
|
|
|
extern const std::string kPort;
|
2019-06-06 18:28:33 +08:00
|
|
|
|
} //namespace Rtsp
|
|
|
|
|
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// //////////RTMP服务器配置/////////// [AUTO-TRANSLATED:8de6f41f]
|
|
|
|
|
// //////////RTMP server configuration///////////
|
2019-06-06 18:28:33 +08:00
|
|
|
|
namespace Rtmp {
|
2022-02-02 20:34:50 +08:00
|
|
|
|
extern const std::string kPort;
|
2019-06-06 18:28:33 +08:00
|
|
|
|
} //namespace RTMP
|
|
|
|
|
} // namespace mediakit
|
|
|
|
|
|
2020-12-27 22:14:59 +08:00
|
|
|
|
namespace API {
|
|
|
|
|
typedef enum {
|
|
|
|
|
NotFound = -500,//未找到
|
|
|
|
|
Exception = -400,//代码抛异常
|
|
|
|
|
InvalidArgs = -300,//参数不合法
|
|
|
|
|
SqlFailed = -200,//sql执行失败
|
|
|
|
|
AuthFailed = -100,//鉴权失败
|
|
|
|
|
OtherFailed = -1,//业务代码执行失败,
|
|
|
|
|
Success = 0//执行成功
|
|
|
|
|
} ApiErr;
|
2023-07-22 17:30:20 +08:00
|
|
|
|
|
|
|
|
|
extern const std::string kSecret;
|
2020-12-27 22:14:59 +08:00
|
|
|
|
}//namespace API
|
|
|
|
|
|
|
|
|
|
class ApiRetException: public std::runtime_error {
|
|
|
|
|
public:
|
|
|
|
|
ApiRetException(const char *str = "success" ,int code = API::Success):runtime_error(str){
|
|
|
|
|
_code = code;
|
|
|
|
|
}
|
|
|
|
|
int code(){ return _code; }
|
|
|
|
|
private:
|
|
|
|
|
int _code;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class AuthException : public ApiRetException {
|
|
|
|
|
public:
|
|
|
|
|
AuthException(const char *str):ApiRetException(str,API::AuthFailed){}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class InvalidArgsException: public ApiRetException {
|
|
|
|
|
public:
|
|
|
|
|
InvalidArgsException(const char *str):ApiRetException(str,API::InvalidArgs){}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class SuccessException: public ApiRetException {
|
|
|
|
|
public:
|
|
|
|
|
SuccessException():ApiRetException("success",API::Success){}
|
|
|
|
|
};
|
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
using ApiArgsType = std::map<std::string, std::string, mediakit::StrCaseCompare>;
|
2020-12-27 22:14:59 +08:00
|
|
|
|
|
2021-08-12 16:07:31 +08:00
|
|
|
|
template<typename Args, typename First>
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::string getValue(Args &args, const First &first) {
|
2021-08-12 16:07:31 +08:00
|
|
|
|
return args[first];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename First>
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::string getValue(Json::Value &args, const First &first) {
|
2021-08-12 16:07:31 +08:00
|
|
|
|
return args[first].asString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename First>
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::string getValue(std::string &args, const First &first) {
|
2021-08-12 16:07:31 +08:00
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename First>
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::string getValue(const mediakit::Parser &parser, const First &first) {
|
2021-08-12 16:07:31 +08:00
|
|
|
|
auto ret = parser.getUrlArgs()[first];
|
|
|
|
|
if (!ret.empty()) {
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
return parser.getHeader()[first];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename First>
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::string getValue(mediakit::Parser &parser, const First &first) {
|
|
|
|
|
return getValue((const mediakit::Parser &) parser, first);
|
2021-08-12 16:07:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename Args, typename First>
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::string getValue(const mediakit::Parser &parser, Args &args, const First &first) {
|
2021-08-12 16:07:31 +08:00
|
|
|
|
auto ret = getValue(args, first);
|
|
|
|
|
if (!ret.empty()) {
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
return getValue(parser, first);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename Args>
|
|
|
|
|
class HttpAllArgs {
|
2024-03-23 22:46:30 +08:00
|
|
|
|
mediakit::Parser* _parser = nullptr;
|
|
|
|
|
Args* _args = nullptr;
|
2021-08-12 16:07:31 +08:00
|
|
|
|
public:
|
2024-03-23 22:46:30 +08:00
|
|
|
|
const mediakit::Parser& parser;
|
|
|
|
|
Args& args;
|
|
|
|
|
|
|
|
|
|
HttpAllArgs(const mediakit::Parser &p, Args &a): parser(p), args(a) {}
|
2021-08-12 16:07:31 +08:00
|
|
|
|
|
2024-03-23 22:46:30 +08:00
|
|
|
|
HttpAllArgs(const HttpAllArgs &that): _parser(new mediakit::Parser(that.parser)),
|
|
|
|
|
_args(new Args(that.args)),
|
|
|
|
|
parser(*_parser), args(*_args) {}
|
|
|
|
|
~HttpAllArgs() {
|
|
|
|
|
if (_parser) {
|
|
|
|
|
delete _parser;
|
|
|
|
|
}
|
|
|
|
|
if (_args) {
|
|
|
|
|
delete _args;
|
2021-08-12 16:07:31 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename Key>
|
2022-02-02 20:34:50 +08:00
|
|
|
|
toolkit::variant operator[](const Key &key) const {
|
2024-03-23 22:46:30 +08:00
|
|
|
|
return (toolkit::variant)getValue(parser, args, key);
|
2021-08-12 16:07:31 +08:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-03-23 22:46:30 +08:00
|
|
|
|
using ArgsMap = HttpAllArgs<ApiArgsType>;
|
|
|
|
|
using ArgsJson = HttpAllArgs<Json::Value>;
|
|
|
|
|
using ArgsString = HttpAllArgs<std::string>;
|
|
|
|
|
|
|
|
|
|
#define API_ARGS_MAP toolkit::SockInfo &sender, mediakit::HttpSession::KeyValue &headerOut, const ArgsMap &allArgs, Json::Value &val
|
2022-02-02 20:34:50 +08:00
|
|
|
|
#define API_ARGS_MAP_ASYNC API_ARGS_MAP, const mediakit::HttpSession::HttpResponseInvoker &invoker
|
2024-03-23 22:46:30 +08:00
|
|
|
|
#define API_ARGS_JSON toolkit::SockInfo &sender, mediakit::HttpSession::KeyValue &headerOut, const ArgsJson &allArgs, Json::Value &val
|
2022-02-02 20:34:50 +08:00
|
|
|
|
#define API_ARGS_JSON_ASYNC API_ARGS_JSON, const mediakit::HttpSession::HttpResponseInvoker &invoker
|
2024-03-23 22:46:30 +08:00
|
|
|
|
#define API_ARGS_STRING toolkit::SockInfo &sender, mediakit::HttpSession::KeyValue &headerOut, const ArgsString &allArgs, Json::Value &val
|
2022-02-02 20:34:50 +08:00
|
|
|
|
#define API_ARGS_STRING_ASYNC API_ARGS_STRING, const mediakit::HttpSession::HttpResponseInvoker &invoker
|
2021-08-12 16:07:31 +08:00
|
|
|
|
#define API_ARGS_VALUE sender, headerOut, allArgs, val
|
2020-12-27 22:14:59 +08:00
|
|
|
|
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 注册http请求参数是map<string, variant, StrCaseCompare>类型的http api [AUTO-TRANSLATED:8a273897]
|
|
|
|
|
// Register http request parameters as map<string, variant, StrCaseCompare> type http api
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void api_regist(const std::string &api_path, const std::function<void(API_ARGS_MAP)> &func);
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 注册http请求参数是map<string, variant, StrCaseCompare>类型,但是可以异步回复的的http api [AUTO-TRANSLATED:9da5d5f5]
|
|
|
|
|
// Register http request parameters as map<string, variant, StrCaseCompare> type, but can be replied asynchronously http api
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void api_regist(const std::string &api_path, const std::function<void(API_ARGS_MAP_ASYNC)> &func);
|
2020-12-27 22:14:59 +08:00
|
|
|
|
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 注册http请求参数是Json::Value类型的http api(可以支持多级嵌套的json参数对象) [AUTO-TRANSLATED:c4794456]
|
|
|
|
|
// Register http request parameters as Json::Value type http api (can support multi-level nested json parameter objects)
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void api_regist(const std::string &api_path, const std::function<void(API_ARGS_JSON)> &func);
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 注册http请求参数是Json::Value类型,但是可以异步回复的的http api [AUTO-TRANSLATED:742e57fd]
|
|
|
|
|
// Register http request parameters as Json::Value type, but can be replied asynchronously http api
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void api_regist(const std::string &api_path, const std::function<void(API_ARGS_JSON_ASYNC)> &func);
|
2020-12-27 22:14:59 +08:00
|
|
|
|
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 注册http请求参数是http原始请求信息的http api [AUTO-TRANSLATED:72d3fe93]
|
|
|
|
|
// Register http request parameters as http original request information http api
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void api_regist(const std::string &api_path, const std::function<void(API_ARGS_STRING)> &func);
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 注册http请求参数是http原始请求信息的异步回复的http api [AUTO-TRANSLATED:49feefa8]
|
|
|
|
|
// Register http request parameters as http original request information asynchronous reply http api
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void api_regist(const std::string &api_path, const std::function<void(API_ARGS_STRING_ASYNC)> &func);
|
2021-03-31 17:15:26 +08:00
|
|
|
|
|
2020-12-27 22:14:59 +08:00
|
|
|
|
template<typename Args, typename First>
|
2021-08-12 16:07:31 +08:00
|
|
|
|
bool checkArgs(Args &args, const First &first) {
|
2020-12-27 22:14:59 +08:00
|
|
|
|
return !args[first].empty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename Args, typename First, typename ...KeyTypes>
|
2021-08-12 16:07:31 +08:00
|
|
|
|
bool checkArgs(Args &args, const First &first, const KeyTypes &...keys) {
|
|
|
|
|
return checkArgs(args, first) && checkArgs(args, keys...);
|
2021-03-31 17:15:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 检查http url中或body中或http header参数是否为空的宏 [AUTO-TRANSLATED:9de001a4]
|
|
|
|
|
// Check whether the http url, body or http header parameters are empty
|
2020-12-27 22:14:59 +08:00
|
|
|
|
#define CHECK_ARGS(...) \
|
|
|
|
|
if(!checkArgs(allArgs,##__VA_ARGS__)){ \
|
2023-12-02 15:02:00 +08:00
|
|
|
|
throw InvalidArgsException("Required parameter missed: " #__VA_ARGS__); \
|
2020-12-27 22:14:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 检查http参数中是否附带secret密钥的宏,127.0.0.1的ip不检查密钥 [AUTO-TRANSLATED:7546956c]
|
|
|
|
|
// Check whether the http parameters contain the secret key, the ip of 127.0.0.1 does not check the key
|
|
|
|
|
// 同时检测是否在ip白名单内 [AUTO-TRANSLATED:d12f963d]
|
|
|
|
|
// Check whether it is in the ip whitelist at the same time
|
2020-12-27 22:14:59 +08:00
|
|
|
|
#define CHECK_SECRET() \
|
2023-07-26 17:18:33 +08:00
|
|
|
|
do { \
|
|
|
|
|
auto ip = sender.get_peer_ip(); \
|
|
|
|
|
if (!HttpFileManager::isIPAllowed(ip)) { \
|
|
|
|
|
throw AuthException("Your ip is not allowed to access the service."); \
|
2020-12-27 22:14:59 +08:00
|
|
|
|
} \
|
2023-07-27 15:42:53 +08:00
|
|
|
|
CHECK_ARGS("secret"); \
|
|
|
|
|
if (api_secret != allArgs["secret"]) { \
|
2023-12-02 15:02:00 +08:00
|
|
|
|
throw AuthException("Incorrect secret"); \
|
2023-07-26 17:18:33 +08:00
|
|
|
|
} \
|
|
|
|
|
} while(false);
|
2019-06-06 18:28:33 +08:00
|
|
|
|
|
2019-06-24 14:58:56 +08:00
|
|
|
|
void installWebApi();
|
|
|
|
|
void unInstallWebApi();
|
2022-06-22 10:31:53 +08:00
|
|
|
|
|
2023-05-08 15:27:43 +08:00
|
|
|
|
#if defined(ENABLE_RTPPROXY)
|
2024-07-09 10:42:10 +08:00
|
|
|
|
uint16_t openRtpServer(uint16_t local_port, const mediakit::MediaTuple &tuple, int tcp_mode, const std::string &local_ip, bool re_use_port, uint32_t ssrc, int only_track, bool multiplex=false);
|
2023-05-08 15:27:43 +08:00
|
|
|
|
#endif
|
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
Json::Value makeMediaSourceJson(mediakit::MediaSource &media);
|
|
|
|
|
void getStatisticJson(const std::function<void(Json::Value &val)> &cb);
|
2024-07-14 09:32:41 +08:00
|
|
|
|
void addStreamProxy(const mediakit::MediaTuple &tuple, const std::string &url, int retry_count,
|
2023-12-01 14:33:07 +08:00
|
|
|
|
const mediakit::ProtocolOption &option, int rtp_type, float timeout_sec, const toolkit::mINI &args,
|
2022-02-02 20:34:50 +08:00
|
|
|
|
const std::function<void(const toolkit::SockException &ex, const std::string &key)> &cb);
|
2019-06-06 18:28:33 +08:00
|
|
|
|
#endif //ZLMEDIAKIT_WEBAPI_H
|