Quote:
Originally Posted by lexzor
how? i don't know this error check can be done by a md5 hash. it is supposed to be made by comparing both md5 hash file of the original file from host and downloaded file?
|
That's exactly what I am doing.
Here is my code.
It requires amxx easy http:
https://github.com/Next21Team/AmxxEasyHttp
and string_stocks by exolent:
https://forums.alliedmods.net/showthread.php?t=163205
It compiles on amxmodx 1.8.2 .
Code:
#include <amxmodx>
#include <easy_http>
#include <string_stocks>
#define PLUGIN "myplugin"
#define VERSION "1.0.6"
#define AUTHOR "me"
public plugin_init()
{
set_task(5.0, "check_update")
}
public check_update()
{
ezhttp_get("https://mywebsite/update/getversion", "version_result")
}
new last_version[100], last_version_md5[100]
public version_result(EzHttpRequest:request_id)
{
if (ezhttp_get_error_code(request_id) != EZH_OK)
{
new error[64]
ezhttp_get_error_message(request_id, error, charsmax(error))
return
}
new data[512]
ezhttp_get_data(request_id, data, charsmax(data))
if(equal(data,"error"))
{
return
}
// the web page returns something like: "version=1.0.9,md5=8e421035705ebe79f9e881f46426a5ef"
//this code is splitting the string and reading the 2 cvars, version and md5
new lines[10][50], cvars[2][50]
str_explode(data, ',', lines, 10, 50)
new i
for(i=0;i<sizeof(lines);i++)
{
if(!is_str_empty(lines[i]))
{
str_explode(lines[i], '=', cvars, 2, 50)
if(!is_str_empty(cvars[0]) && !is_str_empty(cvars[1]))
{
if(equal(cvars[0],"version"))
{
last_version = cvars[1]
}
if(equal(cvars[0],"md5"))
{
last_version_md5 = cvars[1]
}
}
}
}
if(!equal(VERSION,last_version))
{
server_print("New Plugin version found. Updating...")
delete_file("addons/amxmodx/plugins/myplugin_update.amxx")
ezhttp_get("https://mywebsite/myplugin.amxx", "plugin_download")
}
}
public plugin_download(EzHttpRequest:request_id)
{
if (ezhttp_get_error_code(request_id) != EZH_OK)
{
new error[64]
ezhttp_get_error_message(request_id, error, charsmax(error))
return
}
ezhttp_save_data_to_file(request_id, "addons/amxmodx/plugins/myplugin_update.amxx")
if(file_exists("addons/amxmodx/plugins/myplugin_update.amxx"))
{
new hash[34];
md5_file("addons/amxmodx/plugins/myplugin_update.amxx", hash);
if(equal(last_version_md5, hash))
{
server_print("MD5 hash ok")
delete_file("addons/amxmodx/plugins/myplugin.amxx")
rename_file("addons/amxmodx/plugins/myplugin_update.amxx", "addons/amxmodx/plugins/myplugin.amxx", 1)
server_print("Update completed")
}
else
server_print("MD5 doesn't match")
}
else
server_print("Couldn't update the plugin. Check if /addons/amxmodx/plugins folder has write permissions.")
}
Website is made in asp.net core mvc and this is the api page that returns the plugin version and md5 hash.
Example: version=1.0.9,md5=8e421035705ebe79f9e881f4642 6a5ef
I think you can easily do it in PHP or any other language.
Code:
public string GetVersion()
{
try
{
string txt = System.IO.File.ReadAllText(Directory.GetCurrentDirectory() + @"\wwwroot\pluginVersion.txt");
using var md5 = MD5.Create();
using var stream = System.IO.File.OpenRead(Directory.GetCurrentDirectory() + @"\wwwroot\myplugin.amxx");
var hash = md5.ComputeHash(stream);
return "version=" + txt + ",md5=" + BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
}
catch
{
return "error";
}
}
pluginVersion.txt contains the plugin version
Example: 1.0.8