diff --git a/neo-cli/CLI/MainService.Plugins.cs b/neo-cli/CLI/MainService.Plugins.cs index 230f4d625..0d1271190 100644 --- a/neo-cli/CLI/MainService.Plugins.cs +++ b/neo-cli/CLI/MainService.Plugins.cs @@ -1,4 +1,5 @@ using Neo.ConsoleService; +using Neo.IO.Json; using Neo.Plugins; using System; using System.IO; @@ -17,51 +18,49 @@ partial class MainService [ConsoleCommand("install", Category = "Plugin Commands")] private void OnInstallCommand(string pluginName) { - bool isTemp; - string fileName; - - if (!File.Exists(pluginName)) - { - if (string.IsNullOrEmpty(Settings.Default.PluginURL)) - { - Console.WriteLine("You must define `PluginURL` in your `config.json`"); - return; - } - - var address = string.Format(Settings.Default.PluginURL, pluginName, typeof(Plugin).Assembly.GetVersion()); - fileName = Path.Combine(Path.GetTempPath(), $"{pluginName}.zip"); - isTemp = true; - - Console.WriteLine($"Downloading from {address}"); - using (WebClient wc = new WebClient()) - { - wc.DownloadFile(address, fileName); - } - } - else - { - fileName = pluginName; - isTemp = false; - } - + HttpWebRequest request = WebRequest.CreateHttp($"https://github.com/neo-project/neo-modules/releases/download/v{typeof(Plugin).Assembly.GetVersion()}/{pluginName}.zip"); + HttpWebResponse response; try { - ZipFile.ExtractToDirectory(fileName, "."); + response = (HttpWebResponse)request.GetResponse(); } - catch (IOException) + catch (WebException ex) when (((HttpWebResponse)ex.Response).StatusCode == HttpStatusCode.NotFound) { - Console.WriteLine($"Plugin already exist."); - return; + Version version_core = typeof(Plugin).Assembly.GetName().Version; + request = WebRequest.CreateHttp($"https://api.github.com/repos/neo-project/neo-modules/releases"); + request.UserAgent = $"{GetType().Assembly.GetName().Name}/{GetType().Assembly.GetVersion()}"; + using HttpWebResponse response_api = (HttpWebResponse)request.GetResponse(); + using Stream stream = response_api.GetResponseStream(); + using StreamReader reader = new(stream); + JObject releases = JObject.Parse(reader.ReadToEnd()); + JObject asset = releases.GetArray() + .Where(p => !p["tag_name"].GetString().Contains('-')) + .Select(p => new + { + Version = Version.Parse(p["tag_name"].GetString().TrimStart('v')), + Assets = p["assets"].GetArray() + }) + .OrderByDescending(p => p.Version) + .First(p => p.Version <= version_core).Assets + .FirstOrDefault(p => p["name"].GetString() == $"{pluginName}.zip"); + if (asset is null) throw new Exception("Plugin doesn't exist."); + request = WebRequest.CreateHttp(asset["browser_download_url"].GetString()); + response = (HttpWebResponse)request.GetResponse(); } - finally + using (response) { - if (isTemp) + using Stream stream = response.GetResponseStream(); + using ZipArchive zip = new(stream, ZipArchiveMode.Read); + try + { + zip.ExtractToDirectory("."); + Console.WriteLine($"Install successful, please restart neo-cli."); + } + catch (IOException) { - File.Delete(fileName); + Console.WriteLine($"Plugin already exist."); } } - - Console.WriteLine($"Install successful, please restart neo-cli."); } /// diff --git a/neo-cli/Settings.cs b/neo-cli/Settings.cs index ec0d3766c..fa1e501fe 100644 --- a/neo-cli/Settings.cs +++ b/neo-cli/Settings.cs @@ -10,7 +10,6 @@ public class Settings public StorageSettings Storage { get; } public P2PSettings P2P { get; } public UnlockWalletSettings UnlockWallet { get; } - public string PluginURL { get; } static Settings _default; @@ -45,7 +44,6 @@ public Settings(IConfigurationSection section) this.Storage = new StorageSettings(section.GetSection("Storage")); this.P2P = new P2PSettings(section.GetSection("P2P")); this.UnlockWallet = new UnlockWalletSettings(section.GetSection("UnlockWallet")); - this.PluginURL = section.GetValue("PluginURL", "https://github.com/neo-project/neo-modules/releases/download/v{1}/{0}.zip"); } } diff --git a/neo-cli/config.json b/neo-cli/config.json index 8ba47dee2..8ec8bc254 100644 --- a/neo-cli/config.json +++ b/neo-cli/config.json @@ -17,8 +17,7 @@ "Path": "", "Password": "", "IsActive": false - }, - "PluginURL": "https://github.com/neo-project/neo-modules/releases/download/v{1}/{0}.zip" + } }, "ProtocolConfiguration": { "Network": 860833102, diff --git a/neo-cli/config.mainnet.json b/neo-cli/config.mainnet.json index 8ba47dee2..8ec8bc254 100644 --- a/neo-cli/config.mainnet.json +++ b/neo-cli/config.mainnet.json @@ -17,8 +17,7 @@ "Path": "", "Password": "", "IsActive": false - }, - "PluginURL": "https://github.com/neo-project/neo-modules/releases/download/v{1}/{0}.zip" + } }, "ProtocolConfiguration": { "Network": 860833102, diff --git a/neo-cli/config.testnet.json b/neo-cli/config.testnet.json index a078aa78f..75baada05 100644 --- a/neo-cli/config.testnet.json +++ b/neo-cli/config.testnet.json @@ -17,8 +17,7 @@ "Path": "", "Password": "", "IsActive": false - }, - "PluginURL": "https://github.com/neo-project/neo-modules/releases/download/v{1}/{0}.zip" + } }, "ProtocolConfiguration": { "Network": 877933390,