mirror of
https://github.com/2dust/v2rayN.git
synced 2025-08-24 03:46:55 +00:00
Compare commits
6 commits
ca38239bce
...
89ce7c23c9
Author | SHA1 | Date | |
---|---|---|---|
![]() |
89ce7c23c9 | ||
![]() |
a5d99b1eb5 | ||
![]() |
800d193acb | ||
![]() |
7a1d12be76 | ||
![]() |
1b9c95e801 | ||
![]() |
9f44815470 |
6 changed files with 132 additions and 17 deletions
115
v2rayN/ServiceLib/Common/AesUtils.cs
Normal file
115
v2rayN/ServiceLib/Common/AesUtils.cs
Normal file
|
@ -0,0 +1,115 @@
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Security.Cryptography;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace ServiceLib.Common
|
||||||
|
{
|
||||||
|
public class AesUtils
|
||||||
|
{
|
||||||
|
private const int KeySize = 256; // AES-256
|
||||||
|
private const int IvSize = 16; // AES block size
|
||||||
|
private const int Iterations = 10000;
|
||||||
|
|
||||||
|
private static readonly byte[] Salt = Encoding.ASCII.GetBytes("saltysalt".PadRight(16, ' '));//google浏览器默认盐值
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Encrypt
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="text">Plain text</param>
|
||||||
|
/// <param name="password">Password for key derivation</param>
|
||||||
|
/// <returns>Base64 encoded cipher text with IV</returns>
|
||||||
|
public static string Encrypt(string text, string password)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(text))
|
||||||
|
return string.Empty;
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(password))
|
||||||
|
throw new ArgumentNullException("Password cannot be null.");
|
||||||
|
|
||||||
|
byte[] plaintext = Encoding.UTF8.GetBytes(text);
|
||||||
|
byte[] key = GetDefaultKey(password);
|
||||||
|
byte[] iv = GenerateIv();
|
||||||
|
|
||||||
|
using (Aes aes = Aes.Create())
|
||||||
|
{
|
||||||
|
aes.Key = key;
|
||||||
|
aes.IV = iv;
|
||||||
|
|
||||||
|
using (MemoryStream ms = new MemoryStream())
|
||||||
|
{
|
||||||
|
ms.Write(iv, 0, iv.Length);
|
||||||
|
|
||||||
|
using (CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write))
|
||||||
|
{
|
||||||
|
cs.Write(plaintext, 0, plaintext.Length);
|
||||||
|
cs.FlushFinalBlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
byte[] cipherTextWithIv = ms.ToArray();
|
||||||
|
return Convert.ToBase64String(cipherTextWithIv);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Decrypt
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="cipherTextWithIv">Base64 encoded cipher text with IV</param>
|
||||||
|
/// <param name="password">Password for key derivation</param>
|
||||||
|
/// <returns>Plain text</returns>
|
||||||
|
public static string Decrypt(string cipherTextWithIv, string password)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(cipherTextWithIv))
|
||||||
|
return string.Empty;
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(password))
|
||||||
|
throw new ArgumentNullException("Password cannot be null.");
|
||||||
|
|
||||||
|
byte[] cipherTextWithIvBytes = Convert.FromBase64String(cipherTextWithIv);
|
||||||
|
byte[] key = GetDefaultKey(password);
|
||||||
|
|
||||||
|
byte[] iv = new byte[IvSize];
|
||||||
|
Buffer.BlockCopy(cipherTextWithIvBytes, 0, iv, 0, IvSize);
|
||||||
|
|
||||||
|
byte[] cipherText = new byte[cipherTextWithIvBytes.Length - IvSize];
|
||||||
|
Buffer.BlockCopy(cipherTextWithIvBytes, IvSize, cipherText, 0, cipherText.Length - IvSize);
|
||||||
|
|
||||||
|
using (Aes aes = Aes.Create())
|
||||||
|
{
|
||||||
|
aes.Key = key;
|
||||||
|
aes.IV = iv;
|
||||||
|
|
||||||
|
using (MemoryStream ms = new MemoryStream())
|
||||||
|
{
|
||||||
|
using (CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write))
|
||||||
|
{
|
||||||
|
cs.Write(cipherText, 0, cipherText.Length);
|
||||||
|
cs.FlushFinalBlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
byte[] plainText = ms.ToArray();
|
||||||
|
return Encoding.UTF8.GetString(plainText);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static byte[] GetDefaultKey(string password)
|
||||||
|
{
|
||||||
|
using (Rfc2898DeriveBytes pbkdf2 = new Rfc2898DeriveBytes(password, Salt, Iterations, HashAlgorithmName.SHA256))
|
||||||
|
{
|
||||||
|
return pbkdf2.GetBytes(KeySize / 8);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static byte[] GenerateIv()
|
||||||
|
{
|
||||||
|
using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
|
||||||
|
{
|
||||||
|
byte[] iv = new byte[IvSize];
|
||||||
|
rng.GetBytes(iv);
|
||||||
|
return iv;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -361,31 +361,28 @@ namespace ServiceLib.Handler
|
||||||
{
|
{
|
||||||
var cmdLine = $"{fileName.AppendQuotes()} {string.Format(coreInfo.Arguments, Utils.GetConfigPath(configPath).AppendQuotes())}";
|
var cmdLine = $"{fileName.AppendQuotes()} {string.Format(coreInfo.Arguments, Utils.GetConfigPath(configPath).AppendQuotes())}";
|
||||||
|
|
||||||
//Prefer shell scripts
|
//Shell scripts
|
||||||
var shFilePath = Utils.GetBinPath("run_as_root.sh");
|
var shFilePath = Utils.GetBinPath("run_as_root.sh");
|
||||||
File.Delete(shFilePath);
|
File.Delete(shFilePath);
|
||||||
var sb = new StringBuilder();
|
var sb = new StringBuilder();
|
||||||
sb.AppendLine("#!/bin/sh");
|
sb.AppendLine("#!/bin/sh");
|
||||||
sb.AppendLine(cmdLine);
|
if (_config.TunModeItem.LinuxSudoPwd.IsNullOrEmpty())
|
||||||
await File.WriteAllTextAsync(shFilePath, sb.ToString());
|
|
||||||
await Utils.SetLinuxChmod(shFilePath);
|
|
||||||
|
|
||||||
//Replace command
|
|
||||||
var args = File.Exists(shFilePath) ? shFilePath : cmdLine;
|
|
||||||
if (_config.TunModeItem.LinuxSudoPwd.IsNotEmpty())
|
|
||||||
{
|
{
|
||||||
proc.StartInfo.FileName = $"/bin/sudo";
|
sb.AppendLine($"pkexec {cmdLine}");
|
||||||
proc.StartInfo.Arguments = $"-S {args}";
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
proc.StartInfo.FileName = $"/bin/pkexec";
|
sb.AppendLine($"sudo -S {cmdLine}");
|
||||||
proc.StartInfo.Arguments = $"{args}";
|
|
||||||
}
|
|
||||||
proc.StartInfo.WorkingDirectory = null;
|
|
||||||
proc.StartInfo.StandardInputEncoding = Encoding.UTF8;
|
proc.StartInfo.StandardInputEncoding = Encoding.UTF8;
|
||||||
proc.StartInfo.RedirectStandardInput = true;
|
proc.StartInfo.RedirectStandardInput = true;
|
||||||
Logging.SaveLog(proc.StartInfo.Arguments);
|
}
|
||||||
|
await File.WriteAllTextAsync(shFilePath, sb.ToString());
|
||||||
|
await Utils.SetLinuxChmod(shFilePath);
|
||||||
|
|
||||||
|
proc.StartInfo.FileName = shFilePath;
|
||||||
|
proc.StartInfo.Arguments = "";
|
||||||
|
proc.StartInfo.WorkingDirectory = "";
|
||||||
|
Logging.SaveLog(shFilePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task KillProcess(Process? proc)
|
private async Task KillProcess(Process? proc)
|
||||||
|
|
|
@ -548,6 +548,7 @@ namespace ServiceLib.Services.CoreConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
var tunInbound = JsonUtils.Deserialize<Inbound4Sbox>(Utils.GetEmbedText(Global.TunSingboxInboundFileName)) ?? new Inbound4Sbox { };
|
var tunInbound = JsonUtils.Deserialize<Inbound4Sbox>(Utils.GetEmbedText(Global.TunSingboxInboundFileName)) ?? new Inbound4Sbox { };
|
||||||
|
tunInbound.interface_name = Utils.IsOSX()? $"utun{new Random().Next(99)}": "singbox_tun";
|
||||||
tunInbound.mtu = _config.TunModeItem.Mtu;
|
tunInbound.mtu = _config.TunModeItem.Mtu;
|
||||||
tunInbound.strict_route = _config.TunModeItem.StrictRoute;
|
tunInbound.strict_route = _config.TunModeItem.StrictRoute;
|
||||||
tunInbound.stack = _config.TunModeItem.Stack;
|
tunInbound.stack = _config.TunModeItem.Stack;
|
||||||
|
|
|
@ -379,6 +379,7 @@
|
||||||
</Grid.ColumnDefinitions>
|
</Grid.ColumnDefinitions>
|
||||||
|
|
||||||
<TextBlock
|
<TextBlock
|
||||||
|
x:Name="tbAutoRun"
|
||||||
Grid.Row="1"
|
Grid.Row="1"
|
||||||
Grid.Column="0"
|
Grid.Column="0"
|
||||||
VerticalAlignment="Center"
|
VerticalAlignment="Center"
|
||||||
|
|
|
@ -172,6 +172,7 @@ namespace v2rayN.Desktop.Views
|
||||||
|
|
||||||
if (Utils.IsOSX())
|
if (Utils.IsOSX())
|
||||||
{
|
{
|
||||||
|
tbAutoRun.IsVisible = false;
|
||||||
togAutoRun.IsVisible = false;
|
togAutoRun.IsVisible = false;
|
||||||
//TODO
|
//TODO
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue