Frpc配置文件生成

Frpc代理依赖Frpc.ini, 本文介绍其配置操作

1 ini操作

 /// <summary>
 /// INI文件读写辅助类
 /// </summary>
 internal static class IniHelper
 {
     #region Public Methods

     /// <summary> 
     /// 写入INI文件 
     /// </summary>
     /// <param name="filePath"></param>
     /// <param name="section">项目名称(如 [TypeName] )</param> 
     /// <param name="key"></param> 
     /// <param name="value"></param> 
     public static void Write(string filePath, string section, string key, string value)
     {
         CreateFileIfNotExist(filePath);
         WritePrivateProfileString(section, key, value, filePath);
     }

     /// <summary> 
     /// 读出INI文件 
     /// </summary>
     /// <param name="userIniPath">路径</param> 
     /// <param name="section">项目名称(如 [TypeName] )</param> 
     /// <param name="key"></param>
     /// <param name="stringLength">对应的值的字符串长度</param> 
     public static string Read(string userIniPath, string section, string key, int stringLength = 2000)
     {
         StringBuilder stringBuilder = new StringBuilder(stringLength);
         if (!File.Exists(userIniPath))
         {
             return "";
         }

         GetPrivateProfileString(section, key, "", stringBuilder, stringLength, userIniPath);
         return stringBuilder.ToString();
     }

     /// <summary>
     /// 删段
     /// </summary>
     /// <param name="userIniPath"></param>
     /// <param name="section"></param>
     /// <returns></returns>
     public static bool DeleteSection(string userIniPath, string section)
     {
         var value = WritePrivateProfileString(section, null, null, userIniPath);
         if (value != 0)
             return true;
         else
             return false;
     }

     /// <summary>
     /// 读段
     /// </summary>
     /// <param name="userIniPath"></param>
     /// <returns></returns>
     public static List<string> ReadSections(string userIniPath)
     {
         List<string> sections = new List<string>();
         byte[] buf = new byte[65535];

         var charLength = GetPrivateProfileStringA(null, null, "", buf, 65536, userIniPath);
         int j = 0;
         for (int i = 0; i < charLength; i++)
         {
             if (buf[i] == 0)
             {
                 sections.Add(Encoding.Default.GetString(buf, j, i - j));
                 j = i + 1;
             }
         }

         return sections;
     }
     #endregion

     #region Private Methods
     [DllImport("kernel32")]
     private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

     [DllImport("kernel32")]
     private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

     [DllImport("kernel32")]
     private static extern int GetPrivateProfileStringA(string section, string key, string def, byte[] retVal, int size, string filePath);
     /// <summary> 
     /// 验证文件是否存在 
     /// </summary> 
     /// <returns>布尔值</returns> 
     private static void CreateFileIfNotExist(string userIniPath)
     {
         if (string.IsNullOrWhiteSpace(userIniPath))
         {
             throw new ArgumentNullException($"{nameof(userIniPath)}");
         }

         string directFold = Path.GetDirectoryName(userIniPath);
         if (!Directory.Exists(directFold))
         {
             Directory.CreateDirectory(directFold);
         }

         if (!File.Exists(userIniPath))
         {
             using (File.Create(userIniPath)) { }
         }
     }

     #endregion
 }

2 资源释放

/// <summary>
/// 释放资源
/// </summary>
internal class ReleaseResources
{
    public ReleaseResources()
    {
        _securityOperation = new SecurityOperation();
    }

    public string ResourcesPath { get; set; }

    /// <summary>
    /// 应用程序的启动路径
    /// </summary>
    public string ApplicationPath
    {
        get
        {
            if (string.IsNullOrEmpty(ResourcesPath))
                ResourcesPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            return ResourcesPath;
        }
        set => ResourcesPath = value;
    }

    /// <summary>
    /// 释放内网穿透代理
    /// </summary>
    public bool ReleaseFrpcAgent(string agentPath)
    {
        ReleaseFileResources($"{FrpcIniConst.FrpcName}.exe", Resources.frpc, agentPath);
        var agentConfigurationArray = Encoding.UTF8.GetBytes(Resources.frpc1);
        return ReleaseFileResources($"{FrpcIniConst.FrpcName}.ini", agentConfigurationArray, agentPath);
    }

    /// <summary>
    /// 释放库资源
    /// </summary>
    /// <param name="resourcesFile"></param>
    /// <param name="datas"></param>
    /// <param name="savePath"></param>
    public bool ReleaseFileResources(string resourcesFile, byte[] datas, string savePath)
    {
        SetupSecureDirectory(savePath);
        string filePath = Path.Combine(savePath, resourcesFile);
        bool fileExists = File.Exists(filePath);
        if (fileExists) return true;
        try
        {
            using FileStream fs = new FileStream(filePath, FileMode.Create);
            fs.Write(datas, 0, datas.Length);
            _securityOperation.AddFileSecurity(filePath, UserAccount, FileSystemRights.Read | FileSystemRights.Write, AccessControlType.Allow);
            return true;
        }
        catch (Exception e)
        {
            throw new IOException($"ReleaseFileResources {filePath} 失败:{e})");
        }
    }

    /// <summary>
    /// 初始目录
    /// </summary>
    /// <param name="directoryName"></param>
    private void SetupSecureDirectory(string directoryName)
    {
        if (string.IsNullOrWhiteSpace(directoryName)) throw new ArgumentNullException();
        // 确保路径以目录分隔符结束
        if (!directoryName.EndsWith(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal))
            directoryName += Path.DirectorySeparatorChar;
        var directFold = Path.GetDirectoryName(directoryName);
        if (string.IsNullOrWhiteSpace(directFold)) throw new ArgumentNullException();
        if (Directory.Exists(directFold)) return;
        Directory.CreateDirectory(directFold);
        _securityOperation.AddDirectorySecurity(directFold, UserAccount, FileSystemRights.Modify, AccessControlType.Allow);
    }

    //用户
    private const string UserAccount = "everyone";

    //权限操作
    private readonly SecurityOperation _securityOperation;
}

3 权限操作

/// <summary>
/// 权限操作
/// </summary>
internal class SecurityOperation
{
    /// <summary>
    /// 在ACL中增加文件的访问权限
    /// </summary>
    /// <param name="fileName">文件名</param>
    /// <param name="userAccount">用户</param>
    /// <param name="accessRights">权限</param>
    /// <param name="accessRightControlType">允许或禁止</param>
    public void AddFileSecurity(string fileName, string userAccount, FileSystemRights accessRights, AccessControlType accessRightControlType)
    {
        if (!File.Exists(fileName)) return;
        FileInfo fInfo = new FileInfo(fileName);
        FileSecurity fSecurity = fInfo.GetAccessControl();
        fSecurity.AddAccessRule(new FileSystemAccessRule(userAccount, accessRights, accessRightControlType));
        fInfo.SetAccessControl(fSecurity);
    }

    /// <summary>
    /// 在ACL中增加目录的访问权限
    /// </summary>
    /// <param name="pathName">文件名</param>
    /// <param name="userAccount">用户</param>
    /// <param name="accessRights">权限</param>
    /// <param name="accessRightControlType">允许或禁止</param>
    public void AddDirectorySecurity(string pathName, string userAccount, FileSystemRights accessRights,
        AccessControlType accessRightControlType)
    {
        DirectoryInfo di = new DirectoryInfo(pathName);
        DirectorySecurity ds = di.GetAccessControl();
        ds.AddAccessRule(new FileSystemAccessRule(userAccount, FileSystemRights.FullControl,
            InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, accessRightControlType));
        //InheritanceFlags.ContainerInherit 下级文件夹要继承权限。
        //InheritanceFlags.None 下级文件夹、文件都不继承权限。
    }
}

 

posted on 2024-08-01 17:35  TanZhiWei  阅读(111)  评论(0编辑  收藏  举报