2017-08-07 22:31:00 +02:00
|
|
|
|
using System;
|
2017-12-21 04:31:30 +01:00
|
|
|
|
using System.Diagnostics;
|
2017-08-19 00:22:25 +02:00
|
|
|
|
using System.IO;
|
2018-08-30 17:35:44 +02:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
2017-12-21 04:31:30 +01:00
|
|
|
|
using System.Runtime.InteropServices;
|
2017-08-07 22:31:00 +02:00
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
2017-09-08 17:45:20 +02:00
|
|
|
|
namespace Bit.Setup
|
2017-08-07 22:31:00 +02:00
|
|
|
|
{
|
|
|
|
|
public static class Helpers
|
|
|
|
|
{
|
|
|
|
|
public static string SecureRandomString(int length, bool alpha = true, bool upper = true, bool lower = true,
|
|
|
|
|
bool numeric = true, bool special = false)
|
|
|
|
|
{
|
|
|
|
|
return SecureRandomString(length, RandomStringCharacters(alpha, upper, lower, numeric, special));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ref https://stackoverflow.com/a/8996788/1090359 with modifications
|
|
|
|
|
public static string SecureRandomString(int length, string characters)
|
|
|
|
|
{
|
2020-03-27 19:36:37 +01:00
|
|
|
|
if (length < 0)
|
2017-08-07 22:31:00 +02:00
|
|
|
|
{
|
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(length), "length cannot be less than zero.");
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-27 19:36:37 +01:00
|
|
|
|
if ((characters?.Length ?? 0) == 0)
|
2017-08-07 22:31:00 +02:00
|
|
|
|
{
|
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(characters), "characters invalid.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const int byteSize = 0x100;
|
2020-03-27 19:36:37 +01:00
|
|
|
|
if (byteSize < characters.Length)
|
2017-08-07 22:31:00 +02:00
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException(
|
|
|
|
|
string.Format("{0} may contain no more than {1} characters.", nameof(characters), byteSize),
|
|
|
|
|
nameof(characters));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var outOfRangeStart = byteSize - (byteSize % characters.Length);
|
2020-03-27 19:36:37 +01:00
|
|
|
|
using (var rng = RandomNumberGenerator.Create())
|
2017-08-07 22:31:00 +02:00
|
|
|
|
{
|
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
|
var buffer = new byte[128];
|
2020-03-27 19:36:37 +01:00
|
|
|
|
while (sb.Length < length)
|
2017-08-07 22:31:00 +02:00
|
|
|
|
{
|
|
|
|
|
rng.GetBytes(buffer);
|
2020-03-27 19:36:37 +01:00
|
|
|
|
for (var i = 0; i < buffer.Length && sb.Length < length; ++i)
|
2017-08-07 22:31:00 +02:00
|
|
|
|
{
|
|
|
|
|
// Divide the byte into charSet-sized groups. If the random value falls into the last group and the
|
|
|
|
|
// last group is too small to choose from the entire allowedCharSet, ignore the value in order to
|
|
|
|
|
// avoid biasing the result.
|
2020-03-27 19:36:37 +01:00
|
|
|
|
if (outOfRangeStart <= buffer[i])
|
2017-08-07 22:31:00 +02:00
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sb.Append(characters[buffer[i] % characters.Length]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string RandomStringCharacters(bool alpha, bool upper, bool lower, bool numeric, bool special)
|
|
|
|
|
{
|
|
|
|
|
var characters = string.Empty;
|
2020-03-27 19:36:37 +01:00
|
|
|
|
if (alpha)
|
2017-08-07 22:31:00 +02:00
|
|
|
|
{
|
2020-03-27 19:36:37 +01:00
|
|
|
|
if (upper)
|
2017-08-07 22:31:00 +02:00
|
|
|
|
{
|
|
|
|
|
characters += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-27 19:36:37 +01:00
|
|
|
|
if (lower)
|
2017-08-07 22:31:00 +02:00
|
|
|
|
{
|
|
|
|
|
characters += "abcdefghijklmnopqrstuvwxyz";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-27 19:36:37 +01:00
|
|
|
|
if (numeric)
|
2017-08-07 22:31:00 +02:00
|
|
|
|
{
|
|
|
|
|
characters += "0123456789";
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-27 19:36:37 +01:00
|
|
|
|
if (special)
|
2017-08-07 22:31:00 +02:00
|
|
|
|
{
|
|
|
|
|
characters += "!@#$%^*&";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return characters;
|
|
|
|
|
}
|
2017-08-19 00:22:25 +02:00
|
|
|
|
|
2018-10-18 17:41:49 +02:00
|
|
|
|
public static string GetValueFromEnvFile(string envFile, string key)
|
2017-08-19 00:22:25 +02:00
|
|
|
|
{
|
2020-03-27 19:36:37 +01:00
|
|
|
|
if (!File.Exists($"/bitwarden/env/{envFile}.override.env"))
|
2017-08-19 00:22:25 +02:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-24 17:16:01 +02:00
|
|
|
|
var lines = File.ReadAllLines($"/bitwarden/env/{envFile}.override.env");
|
2020-03-27 19:36:37 +01:00
|
|
|
|
foreach (var line in lines)
|
2017-08-19 00:22:25 +02:00
|
|
|
|
{
|
2020-03-27 19:36:37 +01:00
|
|
|
|
if (line.StartsWith($"{key}="))
|
2017-08-19 00:22:25 +02:00
|
|
|
|
{
|
2018-11-06 22:31:11 +01:00
|
|
|
|
return line.Split(new char[] { '=' }, 2)[1].Trim('"');
|
2017-08-19 00:22:25 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2017-12-21 04:31:30 +01:00
|
|
|
|
|
|
|
|
|
public static string Exec(string cmd, bool returnStdout = false)
|
|
|
|
|
{
|
|
|
|
|
var process = new Process
|
|
|
|
|
{
|
|
|
|
|
StartInfo = new ProcessStartInfo
|
|
|
|
|
{
|
|
|
|
|
RedirectStandardOutput = true,
|
|
|
|
|
UseShellExecute = false,
|
|
|
|
|
CreateNoWindow = true,
|
|
|
|
|
WindowStyle = ProcessWindowStyle.Hidden
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2020-03-27 19:36:37 +01:00
|
|
|
|
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
2017-12-21 04:31:30 +01:00
|
|
|
|
{
|
|
|
|
|
var escapedArgs = cmd.Replace("\"", "\\\"");
|
|
|
|
|
process.StartInfo.FileName = "/bin/bash";
|
|
|
|
|
process.StartInfo.Arguments = $"-c \"{escapedArgs}\"";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
process.StartInfo.FileName = "powershell";
|
|
|
|
|
process.StartInfo.Arguments = cmd;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
process.Start();
|
|
|
|
|
var result = returnStdout ? process.StandardOutput.ReadToEnd() : null;
|
|
|
|
|
process.WaitForExit();
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2018-03-30 15:40:14 +02:00
|
|
|
|
|
|
|
|
|
public static string ReadInput(string prompt)
|
|
|
|
|
{
|
|
|
|
|
Console.ForegroundColor = ConsoleColor.Cyan;
|
|
|
|
|
Console.Write("(!) ");
|
|
|
|
|
Console.ResetColor();
|
|
|
|
|
Console.Write(prompt);
|
2020-03-27 19:36:37 +01:00
|
|
|
|
if (prompt.EndsWith("?"))
|
2018-03-30 15:40:14 +02:00
|
|
|
|
{
|
|
|
|
|
Console.Write(" (y/n)");
|
|
|
|
|
}
|
|
|
|
|
Console.Write(": ");
|
|
|
|
|
var input = Console.ReadLine();
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
return input;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool ReadQuestion(string prompt)
|
|
|
|
|
{
|
|
|
|
|
var input = ReadInput(prompt).ToLowerInvariant().Trim();
|
|
|
|
|
return input == "y" || input == "yes";
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-12 15:26:14 +01:00
|
|
|
|
public static void ShowBanner(Context context, string title, string message, ConsoleColor? color = null)
|
2018-03-30 15:40:14 +02:00
|
|
|
|
{
|
2020-03-27 19:36:37 +01:00
|
|
|
|
if (!context.PrintToScreen())
|
2019-03-12 15:26:14 +01:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-03-27 19:36:37 +01:00
|
|
|
|
if (color != null)
|
2018-03-30 15:40:14 +02:00
|
|
|
|
{
|
|
|
|
|
Console.ForegroundColor = color.Value;
|
|
|
|
|
}
|
|
|
|
|
Console.WriteLine($"!!!!!!!!!! {title} !!!!!!!!!!");
|
|
|
|
|
Console.WriteLine(message);
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
Console.ResetColor();
|
|
|
|
|
}
|
2018-08-30 17:35:44 +02:00
|
|
|
|
|
|
|
|
|
public static Func<object, string> ReadTemplate(string templateName)
|
|
|
|
|
{
|
|
|
|
|
var assembly = typeof(Helpers).GetTypeInfo().Assembly;
|
|
|
|
|
var fullTemplateName = $"Bit.Setup.Templates.{templateName}.hbs";
|
2020-03-27 19:36:37 +01:00
|
|
|
|
if (!assembly.GetManifestResourceNames().Any(f => f == fullTemplateName))
|
2018-08-30 17:35:44 +02:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2020-03-27 19:36:37 +01:00
|
|
|
|
using (var s = assembly.GetManifestResourceStream(fullTemplateName))
|
|
|
|
|
using (var sr = new StreamReader(s))
|
2018-08-30 17:35:44 +02:00
|
|
|
|
{
|
|
|
|
|
var templateText = sr.ReadToEnd();
|
|
|
|
|
return HandlebarsDotNet.Handlebars.Compile(templateText);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-03-12 15:26:14 +01:00
|
|
|
|
|
|
|
|
|
public static void WriteLine(Context context, string format = null, object arg0 = null, object arg1 = null,
|
|
|
|
|
object arg2 = null)
|
|
|
|
|
{
|
2020-03-27 19:36:37 +01:00
|
|
|
|
if (!context.PrintToScreen())
|
2019-03-12 15:26:14 +01:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-03-27 19:36:37 +01:00
|
|
|
|
if (format != null && arg0 != null && arg1 != null && arg2 != null)
|
2019-03-12 15:26:14 +01:00
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(format, arg0, arg1, arg2);
|
|
|
|
|
}
|
2020-03-27 19:36:37 +01:00
|
|
|
|
else if (format != null && arg0 != null && arg1 != null)
|
2019-03-12 15:26:14 +01:00
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(format, arg0, arg1);
|
|
|
|
|
}
|
2020-03-27 19:36:37 +01:00
|
|
|
|
else if (format != null && arg0 != null)
|
2019-03-12 15:26:14 +01:00
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(format, arg0);
|
|
|
|
|
}
|
2020-03-27 19:36:37 +01:00
|
|
|
|
else if (format != null)
|
2019-03-12 15:26:14 +01:00
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(format);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-08-07 22:31:00 +02:00
|
|
|
|
}
|
|
|
|
|
}
|