This commit is contained in:
DDVTech 2021-09-10 23:44:31 +02:00 committed by Thulinma
parent 5b79f296d6
commit fccf66fba2
280 changed files with 56975 additions and 71885 deletions

View file

@ -46,4 +46,3 @@ int main(int argc, char **argv){
std::cout << amfData.Print() << std::endl;
return 0;
}

View file

@ -1,28 +1,27 @@
/// \file util_certbot.cpp
/// Certbot integration utility
/// Intended to be ran like so:
//certbot certonly --manual --preferred-challenges=http --manual-auth-hook MistUtilCertbot --deploy-hook MistUtilCertbot -d yourdomain.example.com
// certbot certonly --manual --preferred-challenges=http --manual-auth-hook MistUtilCertbot --deploy-hook MistUtilCertbot -d yourdomain.example.com
// When called from --deploy-hook:
// RENEWED_LINEAGE: directory with the certificate
// RENEWED_DOMAINS: space-delimited list of domains
//When called from --deploy-hook:
//RENEWED_LINEAGE: directory with the certificate
//RENEWED_DOMAINS: space-delimited list of domains
//When called from --manual-auth-hook:
//CERTBOT_DOMAIN: The domain being authenticated
//CERTBOT_VALIDATION: The validation string
//CERTBOT_TOKEN: Resource name part of the HTTP-01 challenge
// When called from --manual-auth-hook:
// CERTBOT_DOMAIN: The domain being authenticated
// CERTBOT_VALIDATION: The validation string
// CERTBOT_TOKEN: Resource name part of the HTTP-01 challenge
#include <mist/config.h>
#include <mist/defines.h>
#include <mist/stream.h>
#include <mist/config.h>
#include <string>
///Checks if port 80 is HTTP, returns the indice number (>= 0) if it is.
///Returns -1 if nothing is running on port 80.
///Returns -2 if the port is taken by another protocol (and prints a FAIL-level message).
///If found, sets currConf to the current configuration of the HTTP protocol.
int checkPort80(JSON::Value & currConf){
/// Checks if port 80 is HTTP, returns the indice number (>= 0) if it is.
/// Returns -1 if nothing is running on port 80.
/// Returns -2 if the port is taken by another protocol (and prints a FAIL-level message).
/// If found, sets currConf to the current configuration of the HTTP protocol.
int checkPort80(JSON::Value &currConf){
Util::DTSCShmReader rCapa(SHM_CAPA);
DTSC::Scan conns = rCapa.getMember("connectors");
Util::DTSCShmReader rProto(SHM_PROTO);
@ -32,17 +31,16 @@ int checkPort80(JSON::Value & currConf){
std::string ctor = prtcls.getIndice(i).getMember("connector").asString();
DTSC::Scan capa = conns.getMember(ctor);
uint16_t port = prtcls.getIndice(i).getMember("port").asInt();
//get the default port if none is set
if (!port){
port = capa.getMember("optional").getMember("port").getMember("default").asInt();
}
//Found a port 80 entry?
// get the default port if none is set
if (!port){port = capa.getMember("optional").getMember("port").getMember("default").asInt();}
// Found a port 80 entry?
if (port == 80){
if (ctor == "HTTP" || ctor == "HTTP.exe"){
currConf = prtcls.getIndice(i).asJSON();
return i;
}else{
FAIL_MSG("Found non-HTTP protocol %s on port 80; aborting! Please free up port 80 for HTTP", ctor.c_str());
FAIL_MSG("Found non-HTTP protocol %s on port 80; aborting! Please free up port 80 for HTTP",
ctor.c_str());
return -2;
}
}
@ -55,7 +53,7 @@ int main(int argc, char **argv){
Util::Config conf(argv[0]);
conf.parseArgs(argc, argv);
//Handle --deploy-hook
// Handle --deploy-hook
if (getenv("RENEWED_LINEAGE")){
INFO_MSG("Detected '--deploy-hook' calling. Installing certificate.");
std::string cbPath = getenv("RENEWED_LINEAGE");
@ -103,19 +101,20 @@ int main(int argc, char **argv){
return 0;
}
//Handle --manual-auth-hook
// Handle --manual-auth-hook
if (getenv("CERTBOT_VALIDATION") && getenv("CERTBOT_TOKEN")){
INFO_MSG("Detected '--manual-auth-hook' calling. Performing authentication.");
//Store certbot variables for later use
// Store certbot variables for later use
std::string cbValidation = getenv("CERTBOT_VALIDATION");
std::string cbToken = getenv("CERTBOT_TOKEN");
std::string cbCombo = cbToken+":"+cbValidation;
//Check Mist config, find HTTP output, check config
std::string cbCombo = cbToken + ":" + cbValidation;
// Check Mist config, find HTTP output, check config
JSON::Value currConf;
int foundHTTP80 = checkPort80(currConf);
if (foundHTTP80 == -2){return 1;}//abort if port already taken by non-HTTP process
if (foundHTTP80 == -2){return 1;}// abort if port already taken by non-HTTP process
if (foundHTTP80 == -1){
INFO_MSG("Nothing on port 80 found - adding HTTP connector on port 80 with correct config for certbot");
INFO_MSG("Nothing on port 80 found - adding HTTP connector on port 80 with correct config "
"for certbot");
JSON::Value cmd;
cmd["addprotocol"]["connector"] = "HTTP";
cmd["addprotocol"]["port"] = 80;
@ -125,7 +124,8 @@ int main(int argc, char **argv){
uSock.SendNow(cmd.toString());
Util::wait(1000);
int counter = 10;
while (--counter && ((foundHTTP80 = checkPort80(currConf)) == -1 || currConf["certbot"].asStringRef() != cbCombo)){
while (--counter && ((foundHTTP80 = checkPort80(currConf)) == -1 ||
currConf["certbot"].asStringRef() != cbCombo)){
INFO_MSG("Waiting for Controller to pick up new config...");
uSock.SendNow(cmd.toString());
Util::wait(1000);
@ -151,7 +151,8 @@ int main(int argc, char **argv){
uSock.SendNow(cmd.toString());
Util::wait(1000);
int counter = 10;
while (--counter && ((foundHTTP80 = checkPort80(currConf)) == -1 || currConf["certbot"].asStringRef() != cbCombo)){
while (--counter && ((foundHTTP80 = checkPort80(currConf)) == -1 ||
currConf["certbot"].asStringRef() != cbCombo)){
INFO_MSG("Waiting for Controller to pick up new config...");
uSock.SendNow(cmd.toString());
Util::wait(1000);
@ -166,10 +167,14 @@ int main(int argc, char **argv){
return 0;
}
//Print usage message to help point users in the right direction
// Print usage message to help point users in the right direction
FAIL_MSG("This utility is meant to be ran by certbot, not by hand.");
FAIL_MSG("Sample usage: certbot certonly --manual --preferred-challenges=http --manual-auth-hook MistUtilCertbot --deploy-hook MistUtilCertbot -d yourdomain.example.com");
WARN_MSG("Note: This utility will alter your MistServer configuration. If ran as deploy hook it will install the certificate generated by certbot to any already enabled HTTPS output or enable HTTPS on port 443 (if it was disabled). If ran as auth hook it will change the HTTP port to 80 (and enable HTTP if it wasn't enabled already) in order to perform the validation.");
FAIL_MSG("Sample usage: certbot certonly --manual --preferred-challenges=http --manual-auth-hook "
"MistUtilCertbot --deploy-hook MistUtilCertbot -d yourdomain.example.com");
WARN_MSG(
"Note: This utility will alter your MistServer configuration. If ran as deploy hook it will "
"install the certificate generated by certbot to any already enabled HTTPS output or enable "
"HTTPS on port 443 (if it was disabled). If ran as auth hook it will change the HTTP port to "
"80 (and enable HTTP if it wasn't enabled already) in order to perform the validation.");
return 1;
}

View file

@ -1,6 +1,5 @@
#include <stdint.h>
#include <cstdlib>
#include <cmath>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <mist/config.h>
@ -10,6 +9,7 @@
#include <mist/tinythread.h>
#include <mist/util.h>
#include <set>
#include <stdint.h>
#include <string>
Util::Config *cfg = 0;
@ -25,7 +25,7 @@ unsigned int weight_bonus = 50;
unsigned long hostsCounter = 0; // This is a pointer to guarantee atomic accesses.
#define HOSTLOOP \
unsigned long i = 0; \
i < hostsCounter; \
i < hostsCounter; \
++i
#define HOST(no) (hosts[no])
#define HOSTCHECK \
@ -37,7 +37,9 @@ unsigned long hostsCounter = 0; // This is a pointer to guarantee atomic accesse
#define STATE_ONLINE 3
#define STATE_GODOWN 4
#define STATE_REQCLEAN 5
const char *stateLookup[] ={"Offline", "Starting monitoring", "Monitored (error)", "Monitored (online)", "Requesting stop", "Requesting clean"};
const char *stateLookup[] ={"Offline", "Starting monitoring",
"Monitored (error)", "Monitored (online)",
"Requesting stop", "Requesting clean"};
struct streamDetails{
uint32_t total;
@ -47,19 +49,17 @@ struct streamDetails{
};
class outUrl{
public:
public:
std::string pre, post;
outUrl(){};
outUrl(const std::string &u, const std::string &host){
std::string tmp = u;
if (u.find("HOST") != std::string::npos){
tmp = u.substr(0, u.find("HOST")) + host + u.substr(u.find("HOST")+4);
tmp = u.substr(0, u.find("HOST")) + host + u.substr(u.find("HOST") + 4);
}
size_t dolsign = tmp.find('$');
pre = tmp.substr(0, dolsign);
if (dolsign != std::string::npos){
post = tmp.substr(dolsign+1);
}
if (dolsign != std::string::npos){post = tmp.substr(dolsign + 1);}
}
};
@ -69,8 +69,7 @@ inline double toRad(double degree){
double geoDist(double lat1, double long1, double lat2, double long2){
double dist;
dist = sin(toRad(lat1)) * sin(toRad(lat2)) +
cos(toRad(lat1)) * cos(toRad(lat2)) * cos(toRad(long1 - long2));
dist = sin(toRad(lat1)) * sin(toRad(lat2)) + cos(toRad(lat1)) * cos(toRad(lat2)) * cos(toRad(long1 - long2));
return .31830988618379067153 * acos(dist);
}
@ -185,11 +184,11 @@ public:
return 0;
}
if (upSpeed >= availBandwidth || (upSpeed + addBandwidth) >= availBandwidth){
INFO_MSG("Host %s over bandwidth: %llu+%llu >= %llu", host.c_str(), upSpeed, addBandwidth,
availBandwidth);
INFO_MSG("Host %s over bandwidth: %llu+%llu >= %llu", host.c_str(), upSpeed, addBandwidth, availBandwidth);
return 0;
}
if (conf_streams.size() && !conf_streams.count(s) && !conf_streams.count(s.substr(0, s.find_first_of("+ ")))){
if (conf_streams.size() && !conf_streams.count(s) &&
!conf_streams.count(s.substr(0, s.find_first_of("+ ")))){
MEDIUM_MSG("Stream %s not available from %s", s.c_str(), host.c_str());
return 0;
}
@ -219,8 +218,7 @@ public:
return 1;
}
if (upSpeed >= availBandwidth || (upSpeed + addBandwidth) >= availBandwidth){
INFO_MSG("Host %s over bandwidth: %llu+%llu >= %llu", host.c_str(), upSpeed, addBandwidth,
availBandwidth);
INFO_MSG("Host %s over bandwidth: %llu+%llu >= %llu", host.c_str(), upSpeed, addBandwidth, availBandwidth);
return 1;
}
// Calculate score
@ -233,8 +231,8 @@ public:
}
unsigned int score = cpu_score + ram_score + bw_score + geo_score + 1;
// Print info on host
MEDIUM_MSG("SOURCE %s: CPU %u, RAM %u, Stream %u, BW %u (max %llu MB/s), Geo %u -> %u", host.c_str(),
cpu_score, ram_score, streams.count(s) ? weight_bonus : 0, bw_score,
MEDIUM_MSG("SOURCE %s: CPU %u, RAM %u, Stream %u, BW %u (max %llu MB/s), Geo %u -> %u",
host.c_str(), cpu_score, ram_score, streams.count(s) ? weight_bonus : 0, bw_score,
availBandwidth / 1024 / 1024, geo_score, score);
return score;
}
@ -242,7 +240,7 @@ public:
if (!hostMutex){hostMutex = new tthread::mutex();}
tthread::lock_guard<tthread::mutex> guard(*hostMutex);
if (!outputs.count(proto)){return "";}
const outUrl& o = outputs[proto];
const outUrl &o = outputs[proto];
return o.pre + s + o.post;
}
void addViewer(std::string &s){
@ -267,9 +265,7 @@ public:
if (!hostMutex){hostMutex = new tthread::mutex();}
tthread::lock_guard<tthread::mutex> guard(*hostMutex);
cpu = d["cpu"].asInt();
if (d.isMember("bwlimit") && d["bwlimit"].asInt()){
availBandwidth = d["bwlimit"].asInt();
}
if (d.isMember("bwlimit") && d["bwlimit"].asInt()){availBandwidth = d["bwlimit"].asInt();}
long long nRamMax = d["mem_total"].asInt();
long long nRamCur = d["mem_used"].asInt();
long long nShmMax = d["shm_total"].asInt();
@ -335,15 +331,11 @@ public:
}
conf_streams.clear();
if (d.isMember("conf_streams") && d["conf_streams"].size()){
jsonForEach(d["conf_streams"], it){
conf_streams.insert(it->asStringRef());
}
jsonForEach(d["conf_streams"], it){conf_streams.insert(it->asStringRef());}
}
outputs.clear();
if (d.isMember("outputs") && d["outputs"].size()){
jsonForEach(d["outputs"], op){
outputs[op.key()] = outUrl(op->asStringRef(), host);
}
jsonForEach(d["outputs"], op){outputs[op.key()] = outUrl(op->asStringRef(), host);}
}
addBandwidth *= 0.75;
}
@ -387,7 +379,7 @@ int handleRequest(Socket::Connection &conn){
H.Clean();
H.SetHeader("Content-Type", "text/plain");
JSON::Value ret;
//Get/set weights
// Get/set weights
if (weights.size()){
JSON::Value newVals = JSON::fromString(weights);
if (newVals.isMember("cpu")){weight_cpu = newVals["cpu"].asInt();}
@ -506,7 +498,7 @@ int handleRequest(Socket::Connection &conn){
unsigned int bestScore = 0;
for (HOSTLOOP){
HOSTCHECK;
if (Socket::matchIPv6Addr(std::string(HOST(i).details->binHost ,16), conn.getBinHost(), 0)){
if (Socket::matchIPv6Addr(std::string(HOST(i).details->binHost, 16), conn.getBinHost(), 0)){
INFO_MSG("Ignoring same-host entry %s", HOST(i).details->host.data());
continue;
}
@ -567,12 +559,8 @@ int handleRequest(Socket::Connection &conn){
lon = atof(H.GetVar("lon").c_str());
H.SetVar("lon", "");
}
if (H.hasHeader("X-Latitude")){
lat = atof(H.GetHeader("X-Latitude").c_str());
}
if (H.hasHeader("X-Longitude")){
lon = atof(H.GetHeader("X-Longitude").c_str());
}
if (H.hasHeader("X-Latitude")){lat = atof(H.GetHeader("X-Latitude").c_str());}
if (H.hasHeader("X-Longitude")){lon = atof(H.GetHeader("X-Longitude").c_str());}
std::string vars = H.allVars();
if (stream == "favicon.ico"){
H.Clean();
@ -639,8 +627,9 @@ void handleServer(void *hostEntryPointer){
bool down = true;
HTTP::Downloader DL;
if (DL.get(HTTP::URL("http://api.ipstack.com/"+url.host+"?access_key=05eb21db3983dec4cd6d22131ec0a40d&format=1")) && DL.isOk()){
if (DL.get(HTTP::URL("http://api.ipstack.com/" + url.host + "?access_key=05eb21db3983dec4cd6d22131ec0a40d&format=1")) &&
DL.isOk()){
JSON::Value &gDet = entry->details->geoDetails;
INFO_MSG("Location: %s", DL.data().c_str());
gDet = JSON::fromString(DL.data());
@ -795,7 +784,7 @@ int main(int argc, char **argv){
weight_bonus = conf.getInteger("extra");
fallback = conf.getString("fallback");
localMode = conf.getBool("localmode");
INFO_MSG("Local control only mode is %s", localMode?"on":"off");
INFO_MSG("Local control only mode is %s", localMode ? "on" : "off");
JSON::Value &nodes = conf.getOption("server", true);
conf.activate();
@ -848,4 +837,3 @@ void cleanupHost(hostEntry &H){
memset(H.name, 0, sizeof(H.name));
H.state = STATE_OFF;
}

View file

@ -1,8 +1,8 @@
#include <iostream>
#include <mist/util.h>
#include <mist/shared_memory.h>
#include <mist/util.h>
int main(int argc, char ** argv){
int main(int argc, char **argv){
Util::redirectLogsIfNeeded();
if (argc < 1){
FAIL_MSG("Usage: %s MEMORY_PAGE_NAME");
@ -16,4 +16,3 @@ int main(int argc, char ** argv){
std::cout << "Memory structure " << argv[1] << " is not ready" << std::endl;
}
}

View file

@ -3,16 +3,15 @@
#include <stdlib.h>
#include <mist/config.h>
#include <mist/defines.h>
#include <mist/dtsc.h>
#include <mist/json.h>
#include <mist/shared_memory.h>
#include <mist/config.h>
#include <mist/defines.h>
#include <mist/socket.h>
#include <mist/util.h>
void printStatistics(char * data, size_t len, unsigned int id){
void printStatistics(char *data, size_t len, unsigned int id){
std::cout << "#" << id << ": (state: " << (unsigned int)(data[-1] & 0x7F) << ")" << std::endl;
IPC::statExchange tmpEx(data);
std::cout << " Now: " << tmpEx.now() << std::endl;
@ -30,20 +29,34 @@ void printStatistics(char * data, size_t len, unsigned int id){
std::cout << " PID: " << tmpEx.getPID() << std::endl;
}
/// Will emulate a given amount of clients in the statistics.
int main(int argc, char ** argv){
int main(int argc, char **argv){
Util::redirectLogsIfNeeded();
Util::Config conf = Util::Config(argv[0]);
conf.addOption("clients", JSON::fromString("{\"arg\":\"num\", \"short\":\"c\", \"long\":\"clients\", \"default\":1000, \"help\":\"Amount of clients to emulate.\"}"));
conf.addOption("stream", JSON::fromString("{\"arg\":\"string\", \"short\":\"s\", \"long\":\"stream\", \"default\":\"test\", \"help\":\"Streamname to pretend to request.\"}"));
conf.addOption("up", JSON::fromString("{\"arg\":\"string\", \"short\":\"u\", \"long\":\"up\", \"default\":131072, \"help\":\"Bytes per second upstream.\"}"));
conf.addOption("down", JSON::fromString("{\"arg\":\"string\", \"short\":\"d\", \"long\":\"down\", \"default\":13000, \"help\":\"Bytes per second downstream.\"}"));
conf.addOption("sine", JSON::fromString("{\"arg\":\"string\", \"short\":\"S\", \"long\":\"sine\", \"default\":0, \"help\":\"Bytes per second variance in a sine pattern.\"}"));
conf.addOption("userscale", JSON::fromString("{\"arg\":\"string\", \"short\":\"U\", \"long\":\"userscale\", \"default\":0, \"help\":\"If != 0, scales users from 0% to 100% bandwidth.\"}"));
conf.addOption("read", JSON::fromString("{\"arg\":\"num\", \"short\":\"r\", \"long\":\"read\", \"default\":0, \"help\":\"If != 0, does not simulate, just reads existing data.\"}"));
conf.addOption("clients",
JSON::fromString("{\"arg\":\"num\", \"short\":\"c\", \"long\":\"clients\", "
"\"default\":1000, \"help\":\"Amount of clients to emulate.\"}"));
conf.addOption("stream", JSON::fromString("{\"arg\":\"string\", \"short\":\"s\", "
"\"long\":\"stream\", \"default\":\"test\", "
"\"help\":\"Streamname to pretend to request.\"}"));
conf.addOption("up",
JSON::fromString("{\"arg\":\"string\", \"short\":\"u\", \"long\":\"up\", "
"\"default\":131072, \"help\":\"Bytes per second upstream.\"}"));
conf.addOption("down",
JSON::fromString("{\"arg\":\"string\", \"short\":\"d\", \"long\":\"down\", "
"\"default\":13000, \"help\":\"Bytes per second downstream.\"}"));
conf.addOption("sine", JSON::fromString("{\"arg\":\"string\", \"short\":\"S\", "
"\"long\":\"sine\", \"default\":0, \"help\":\"Bytes per "
"second variance in a sine pattern.\"}"));
conf.addOption("userscale",
JSON::fromString(
"{\"arg\":\"string\", \"short\":\"U\", \"long\":\"userscale\", \"default\":0, "
"\"help\":\"If != 0, scales users from 0% to 100% bandwidth.\"}"));
conf.addOption("read", JSON::fromString("{\"arg\":\"num\", \"short\":\"r\", \"long\":\"read\", "
"\"default\":0, \"help\":\"If != 0, does not simulate, "
"just reads existing data.\"}"));
conf.parseArgs(argc, argv);
std::string streamName = conf.getString("stream");
long long clientCount = conf.getInteger("clients");
long long up = conf.getInteger("up");
@ -52,7 +65,6 @@ int main(int argc, char ** argv){
long long scale = conf.getInteger("userscale");
long long currsine = sine;
long long goingUp = 0;
if (conf.getInteger("read")){
IPC::sharedServer statServer(SHM_STATISTICS, STAT_EX_SIZE, true);
@ -61,27 +73,24 @@ int main(int argc, char ** argv){
return 0;
}
IPC::sharedClient ** clients = (IPC::sharedClient **)malloc(sizeof(IPC::sharedClient *)*clientCount);
IPC::sharedClient **clients = (IPC::sharedClient **)malloc(sizeof(IPC::sharedClient *) * clientCount);
for (long long i = 0; i < clientCount; i++){
clients[i] = new IPC::sharedClient(SHM_STATISTICS, STAT_EX_SIZE, true);
}
unsigned long long int counter = 0;
conf.activate();
while (conf.is_active){
unsigned long long int now = Util::epoch();
counter++;
if (sine){
currsine += goingUp;
if (currsine < -down || currsine < -up){
currsine = std::max(-down, -up);
}
if (currsine < -down || currsine < -up){currsine = std::max(-down, -up);}
if (currsine > 0){
goingUp -= sine/100 + 1;
goingUp -= sine / 100 + 1;
}else{
goingUp += sine/100 + 1;
goingUp += sine / 100 + 1;
}
}
for (long long i = 0; i < clientCount; i++){
@ -93,11 +102,11 @@ int main(int argc, char ** argv){
tmpEx.streamName(streamName);
tmpEx.connector("TEST");
if (scale){
tmpEx.up(tmpEx.up() + (up+currsine)*i/clientCount);
tmpEx.down(tmpEx.down() + (down+currsine)*i/clientCount);
tmpEx.up(tmpEx.up() + (up + currsine) * i / clientCount);
tmpEx.down(tmpEx.down() + (down + currsine) * i / clientCount);
}else{
tmpEx.up(tmpEx.up()+up+currsine);
tmpEx.down(tmpEx.down()+down+currsine);
tmpEx.up(tmpEx.up() + up + currsine);
tmpEx.down(tmpEx.down() + down + currsine);
}
tmpEx.time(counter);
tmpEx.lastSecond(counter * 1000);
@ -106,12 +115,12 @@ int main(int argc, char ** argv){
}
Util::wait(1000);
}
for (long long i = 0; i < clientCount; i++){
clients[i]->finish();
delete clients[i];
}
free(clients);
return 0;
}