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)
|
|
|
|
|
{
|
|
|
|
|
if(length < 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(length), "length cannot be less than zero.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if((characters?.Length ?? 0) == 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(characters), "characters invalid.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const int byteSize = 0x100;
|
|
|
|
|
if(byteSize < characters.Length)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException(
|
|
|
|
|
string.Format("{0} may contain no more than {1} characters.", nameof(characters), byteSize),
|
|
|
|
|
nameof(characters));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var outOfRangeStart = byteSize - (byteSize % characters.Length);
|
|
|
|
|
using(var rng = RandomNumberGenerator.Create())
|
|
|
|
|
{
|
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
|
var buffer = new byte[128];
|
|
|
|
|
while(sb.Length < length)
|
|
|
|
|
{
|
|
|
|
|
rng.GetBytes(buffer);
|
|
|
|
|
for(var i = 0; i < buffer.Length && sb.Length < length; ++i)
|
|
|
|
|
{
|
|
|
|
|
// 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.
|
|
|
|
|
if(outOfRangeStart <= buffer[i])
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
if(alpha)
|
|
|
|
|
{
|
|
|
|
|
if(upper)
|
|
|
|
|
{
|
|
|
|
|
characters += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(lower)
|
|
|
|
|
{
|
|
|
|
|
characters += "abcdefghijklmnopqrstuvwxyz";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(numeric)
|
|
|
|
|
{
|
|
|
|
|
characters += "0123456789";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(special)
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
{
|
2017-08-24 17:16:01 +02: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");
|
2017-08-19 00:22:25 +02:00
|
|
|
|
foreach(var line in lines)
|
|
|
|
|
{
|
2017-08-24 17:16:01 +02: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
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if(!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
if(prompt.EndsWith("?"))
|
|
|
|
|
{
|
|
|
|
|
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";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void ShowBanner(string title, string message, ConsoleColor? color = null)
|
|
|
|
|
{
|
|
|
|
|
if(color != null)
|
|
|
|
|
{
|
|
|
|
|
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";
|
|
|
|
|
if(!assembly.GetManifestResourceNames().Any(f => f == fullTemplateName))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
using(var s = assembly.GetManifestResourceStream(fullTemplateName))
|
|
|
|
|
using(var sr = new StreamReader(s))
|
|
|
|
|
{
|
|
|
|
|
var templateText = sr.ReadToEnd();
|
|
|
|
|
return HandlebarsDotNet.Handlebars.Compile(templateText);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-08-07 22:31:00 +02:00
|
|
|
|
}
|
|
|
|
|
}
|