Raised This Month: $32 Target: $400
 8% 

Plugin auto update


Post New Thread Reply   
 
Thread Tools Display Modes
ghostdlr
Senior Member
Join Date: Aug 2010
Old 01-19-2023 , 14:59   Re: Plugin auto update
Reply With Quote #11

Quote:
Originally Posted by lexzor View Post
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

Last edited by ghostdlr; 01-20-2023 at 11:50.
ghostdlr is offline
ghostdlr
Senior Member
Join Date: Aug 2010
Old 01-19-2023 , 15:14   Re: Plugin auto update
Reply With Quote #12

I use md5 hash check to see if the downloaded file was intact so I don't end up replacing the plugin with a corrupted file.

Last edited by ghostdlr; 01-19-2023 at 15:15.
ghostdlr is offline
lexzor
Veteran Member
Join Date: Nov 2020
Old 01-19-2023 , 20:04   Re: Plugin auto update
Reply With Quote #13

i almost forgot about md5_file from amxx.

looks nice. what i would do it's saving the current version in a folder so the users won't face the situation they didn t want to update or having bugs due to incompatibility with another plugins. like a back up
lexzor is offline
bigdaddy424
Senior Member
Join Date: Oct 2021
Location: Jupiter
Old 01-21-2023 , 15:49   Re: Plugin auto update
Reply With Quote #14

wouldnt md5 produce a new string since the plugin update has changed its file size?
__________________
bigdaddy424 is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 01-21-2023 , 16:08   Re: Plugin auto update
Reply With Quote #15

Quote:
Originally Posted by bigdaddy424 View Post
wouldnt md5 produce a new string since the plugin update has changed its file size?
You don't compare the MD5 to the "old" plugin, you use the MD5 of the plugin file on the web server to the MD5 of the same file after it was downloaded.
__________________
fysiks is offline
bigdaddy424
Senior Member
Join Date: Oct 2021
Location: Jupiter
Old 01-21-2023 , 18:02   Re: Plugin auto update
Reply With Quote #16

Quote:
Originally Posted by fysiks View Post
You don't compare the MD5 to the "old" plugin, you use the MD5 of the plugin file on the web server to the MD5 of the same file after it was downloaded.
makes sense, thanks
__________________
bigdaddy424 is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 00:19.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode