Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Install.cs
1using System;
2using System.IO;
3using System.Text.RegularExpressions;
4using System.Threading.Tasks;
7
9{
13 public class Install : CommandRegEx
14 {
18 public Install()
19 : base(@"^(upgrade|update|install)(\s+nobackup)?(\s+[^\s]+){0,2}$")
20 {
21 }
22
26 public override string Name => "Install";
27
31 public override string[] Aliases => new string[] { "Update", "Upgrade" };
32
41 public override async Task Execute(ChatState State, string[] Arguments, string OrgMessage, Match Details,
42 ResponseCallbackHandler ResponseCallback)
43 {
44 int i = 0;
45 int c = Arguments.Length;
46 bool Backup = true;
47
48 if (i < c && string.Compare(Arguments[i], "nobackup", true) == 0)
49 {
50 i++;
51 Backup = false;
52 }
53
54 if (i >= c)
55 await XmppServerModule.Instance.UpdateSoftware(null, State, Backup, ResponseCallback);
56 else
57 {
58 string PackageFileName = Arguments[i++];
59 Package Package = await Provisioning.ProvisioningComponent.GetPackage(PackageFileName);
60 if (Package is null)
61 {
62 await ResponseCallback("Package not found.", string.Empty);
63 return;
64 }
65
66 string FullFileName = Path.Combine(XmppServerModule.PackagesFolder, PackageFileName);
67 if (!File.Exists(FullFileName))
68 {
69 await ResponseCallback("Package file not found.", string.Empty);
70 return;
71 }
72
74 {
75 await ResponseCallback("Reserved package.", string.Empty);
76 return;
77 }
78
79 byte[] PublicKey;
80 byte[] AesKey;
81
82 if (i < c)
83 {
84 string Key = Arguments[i++];
85
86 if (Key.Length != 76 && Key.Length != 76 + 32)
87 {
88 await ResponseCallback("Invalid key.", string.Empty);
89 return;
90 }
91
92 PublicKey = Convert.FromBase64String(Key.Substring(0, 76));
93 AesKey = Key.Length == 76 ? null : Hashes.StringToBinary(Key.Substring(76));
94 }
95 else
96 {
97 PublicKey = Package.PublicKey;
98 AesKey = Package.AesKey;
99 }
100
101 using (FileStream fs = File.OpenRead(FullFileName))
102 {
103 if (!XmppServerModule.ValidatePackage(fs, PublicKey, Package.Signature))
104 {
105 await ResponseCallback("Invalid public key.", string.Empty);
106 return;
107 }
108 }
109
110 Package.Installed = Package.Published;
111 Package.PublicKey = PublicKey;
112 Package.AesKey = AesKey;
113 Package.ContentOnly = XmppServerModule.IsContentPackage(Package);
114
115 await Persistence.Database.Update(Package);
116
117 await XmppServerModule.Instance.UpdateSoftware(Package, State, Backup, ResponseCallback);
118 }
119 }
120
125 public override HelpItem[] GetHelp()
126 {
127 return new HelpItem[]
128 {
129 new HelpItem("upgrade", "Upgrades the software of the service, including installed packages. Performas a backup first."),
130 new HelpItem("upgrade PACKAGE", "Upgrades a specific software package that is already installed. Performas a backup first."),
131 new HelpItem("install PACKAGE KEY", "install a specific software package that is downloaded but not installed. Performas a backup first."),
132 new HelpItem("upgrade nobackup", "Upgrades the software of the service, including installed packages. Does not perform a backup first."),
133 new HelpItem("upgrade nobackup PACKAGE", "Upgrades a specific software package that is already installed. Does not perform a backup first."),
134 new HelpItem("install nobackup PACKAGE KEY", "install a specific software package that is downloaded but not installed. Does not perform a backup first."),
135 };
136 }
137 }
138}
Contains methods for simple hash calculations.
Definition: Hashes.cs:59
static byte[] StringToBinary(string s)
Parses a hex string.
Definition: Hashes.cs:102
An administrative command whose syntax is validated with a regular expression.
Definition: CommandRegEx.cs:12
Contains an item of information about a command.
Definition: HelpItem.cs:9
Upgrades the service to the latest version.
Definition: Install.cs:14
override HelpItem[] GetHelp()
Gets help about the command.
Definition: Install.cs:125
override string[] Aliases
Optional set of aliases. Can be null.
Definition: Install.cs:31
override async Task Execute(ChatState State, string[] Arguments, string OrgMessage, Match Details, ResponseCallbackHandler ResponseCallback)
Executes the command.
Definition: Install.cs:41
Install()
Upgrades the service to the latest version.
Definition: Install.cs:18
override string Name
Command name
Definition: Install.cs:26
Identity of the IoT Broker package.
Definition: BrokerPackage.cs:7
const string FileName
IoTBroker.package
Contains information about a software package.
Definition: Package.cs:18
byte[] PublicKey
Public key of issuer, used to create signature.
Definition: Package.cs:52
byte[] Signature
Cryptographic signature of package, as calculated by the issuer of the package.
Definition: Package.cs:46
CaseInsensitiveString FileName
Filename of package.
Definition: Package.cs:40
DateTime Published
When package was published.
Definition: Package.cs:76
byte[] AesKey
Symmetric cipher used to encrypt package file.
Definition: Package.cs:58
Service Module hosting the XMPP broker and its components.
delegate Task< string > ResponseCallbackHandler(string Markdown, string MessageId)
Delegate for response callback handler methods.