Fixed timings for license system

This commit is contained in:
Thulinma 2017-03-30 12:17:19 +02:00
parent ea4b0f8f45
commit aa0e09e45b
2 changed files with 19 additions and 12 deletions

View file

@ -14,6 +14,7 @@
namespace Controller{ namespace Controller{
static JSON::Value currentLicense; static JSON::Value currentLicense;
static uint64_t lastCheck = 0;
const JSON::Value & getLicense(){ const JSON::Value & getLicense(){
return currentLicense; return currentLicense;
@ -27,10 +28,14 @@ namespace Controller{
INFO_MSG("Reading license from storage") INFO_MSG("Reading license from storage")
readLicense(Storage["license_id"].asInt(), Storage["license"].asStringRef()); readLicense(Storage["license_id"].asInt(), Storage["license"].asStringRef());
if (!isLicensed()){ if (!isLicensed()){
updateLicense(); updateLicense("&boot=1");
checkLicense();
}else{
lastCheck = std::min(Util::epoch(), currentLicense["valid_from"].asInt());
} }
}else{ }else{
updateLicense(); updateLicense("&boot=1");
checkLicense();
} }
} }
@ -55,6 +60,7 @@ namespace Controller{
} }
bool checkLicense(){ bool checkLicense(){
if (!conf.is_active){return true;}
if (!currentLicense.isMember("interval")){ if (!currentLicense.isMember("interval")){
currentLicense["interval"] = 3600ll; currentLicense["interval"] = 3600ll;
} }
@ -62,8 +68,10 @@ namespace Controller{
if(!isLicensed()){ if(!isLicensed()){
FAIL_MSG("Not licensed, shutting down"); FAIL_MSG("Not licensed, shutting down");
kill(getpid(), SIGINT); kill(getpid(), SIGINT);
conf.is_active = false;
return false; return false;
} }
lastCheck = Util::epoch();
return true; return true;
} }
@ -75,8 +83,8 @@ namespace Controller{
} }
} }
void updateLicense(){ void updateLicense(const std::string & extra){
INFO_MSG("Running license updater"); INFO_MSG("Running license updater %s", extra.c_str());
JSON::Value response; JSON::Value response;
HTTP::Parser http; HTTP::Parser http;
@ -89,7 +97,7 @@ namespace Controller{
//Sending request to server. //Sending request to server.
//http.url = "/licensing.php" //http.url = "/licensing.php"
//also see statics at start function. //also see statics at start function.
http.url = "/license.php?release="+Encodings::URL::encode(RELEASE)+"&version="+Encodings::URL::encode(PACKAGE_VERSION)+"&iid="+Encodings::URL::encode(instanceId)+"&lid="+currentLicense["lic_id"].asString(); http.url = "/license.php?release="+Encodings::URL::encode(RELEASE)+"&version="+Encodings::URL::encode(PACKAGE_VERSION)+"&iid="+Encodings::URL::encode(instanceId)+"&lid="+currentLicense["lic_id"].asString() + extra;
long long currID = currentLicense["lic_id"].asInt(); long long currID = currentLicense["lic_id"].asInt();
http.method = "GET"; http.method = "GET";
http.SetHeader("Host", "releases.mistserver.org"); http.SetHeader("Host", "releases.mistserver.org");
@ -156,7 +164,7 @@ namespace Controller{
//verify checksum //verify checksum
if (deCrypted.size() < 33 || Secure::md5(deCrypted.substr(32)) != deCrypted.substr(0,32)){ if (deCrypted.size() < 33 || Secure::md5(deCrypted.substr(32)) != deCrypted.substr(0,32)){
FAIL_MSG("Could not decode license"); WARN_MSG("Could not decode license");
Storage.removeMember("license"); Storage.removeMember("license");
return; return;
} }
@ -177,15 +185,14 @@ namespace Controller{
} }
void licenseLoop(void * np){ void licenseLoop(void * np){
unsigned long now = std::min(Util::epoch(), currentLicense["valid_from"].asInt());
while (conf.is_active){ while (conf.is_active){
if (Util::epoch() - now > currentLicense["interval"].asInt()){ if (Util::epoch() - lastCheck > currentLicense["interval"].asInt()){
updateLicense(); updateLicense();
if (checkLicense()){ checkLicense();
now = Util::epoch();
}
} }
Util::sleep(1000);//sleep a bit Util::sleep(1000);//sleep a bit
} }
updateLicense("&shutdown=1");
} }
} }

View file

@ -7,7 +7,7 @@ namespace Controller{
void initLicense(); void initLicense();
bool isLicensed(); //checks/verifies license time bool isLicensed(); //checks/verifies license time
bool checkLicense(); //Call from Mainloop. bool checkLicense(); //Call from Mainloop.
void updateLicense(); //retrieves update from license server void updateLicense(const std::string & extra = ""); //retrieves update from license server
void licenseLoop(void * np); void licenseLoop(void * np);
void readLicense(uint64_t licId, const std::string & input); //checks/interprets license void readLicense(uint64_t licId, const std::string & input); //checks/interprets license