首页 > 其他分享 >Frpc配置文件生成

Frpc配置文件生成

时间:2024-08-01 17:41:34浏览次数:13  
标签:return string 配置文件 filePath 生成 Frpc new public userIniPath

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 下级文件夹、文件都不继承权限。
    }
}

 

标签:return,string,配置文件,filePath,生成,Frpc,new,public,userIniPath
From: https://www.cnblogs.com/terryK/p/18337158

相关文章

  • Frpc 内网穿透客户端配置教程
    github:https://github.com/fatedier/frp/releases1下载操作系统对应版本的Frpc.exe客户端程序2配置对应的frpc.ini文件3切换到Frpc.exe目录,cmd执行:Frpc.exe-cfrpc.ini 例如:frpc.ini如下例如 frps服务地址:10.10.10.10 端口10000本地服务端口5000,  frps域......
  • OpenSSH秘钥指纹图像生成算法
    OpenSSH秘钥指纹图像生成算法使用SSH秘钥生成时产生疑惑,它的randomartimage是如何生成的?下面进行了探索和研究引入生成位数为4096位的rsa公私钥ssh-keygen-trsa-b4096Generatingpublic/privatersakeypair.Enterfileinwhichtosavethekey(/root/.s......
  • prim算法求最小生成树
    prim算法求最小生成树#include<bits/stdc++.h>usingnamespacestd;constintN=600;intg[N][N];//n的平方约等于m,所以用邻接矩阵,存放权值。g[i][j]表示边ij的长度为g[i][j]constintinf=0x3f3f3f3f;//无穷大0x3f3f3f3fintdist[N];//该点到集合里点的最小值boolst[N]......
  • 今日头条测试开发的编程面试题---生成随机数
    题目:程序中使用的数字只能是0和1,生成0-1000的随机数算法描述:如果生成的十进制数大于1000,则算法重新开始生成新的随机数,直到得到一个在0到1000范围内的数为止。生成二进制数:算法开始时,会创建一个空的字符串 binary_string 用于存储二进制数。在一个循环中,算法会随机生......
  • Midjourney保姆级教程!7大方向教你快速生成你想要的人物形象!
    一、前言由于在工作中,经常会涉及到使用人物素材完成Banner设计的工作,于是最近开始探索关于Midjourney进行AI人物生成的相关测试,同时将这一段时间的经验分享出来。刚开始接触和使用Midjourney的时候,最大的问题在于生成的人物图片无法保证人物位置的大小和视角,想要......
  • ComfyUI PVC 手办工作流!一键生成你的专属卡通形象!
    前言ComfyUIPVC手办工作流!一键生成你的专属卡通形象!......
  • SemanticKernel/C#:检索增强生成(RAG)简易实践
    检索增强生成(RAG)是什么?RAG是“Reference-basedGenerativemodelwithAttention”的缩写,也可以被称为“Retrieval-AugmentedGeneration”,是一种结合了检索技术和生成模型的方法,主要用于自然语言处理任务,如文本生成、对话系统、机器翻译等。RAG模型通过从外部知识库中检索相关......
  • 如何计算任意可迭代对象(例如生成器)中的项目数量?
    假设我有一个任意的可迭代对象-例如,一个迭代文件行和yield匹配正则表达式的生成器。我如何计算该可迭代对象中的项目数,假设我不这样做不关心元素本身?可以使用sum()函数来计算可迭代对象中的元素数量,而无需存储它们。以下是如何操作:defcount_item......
  • Java sshtools 生成的 EDDSA 签名与 Python 的 pycryptome 生成的签名不匹配
    我有一个python库,它使用pycryptodomelibrary使用openssh格式的ED25519私钥使用Ed25519算法对数据进行签名。然后需要使用sshtools库和相应的公钥在Java应用程序中验证签名。但是签名验证失败。约束:从文件中读取私钥/公钥很重要。我无法......
  • 14. 迭代器、生成器、模块与包、json模块
    1.迭代器1.1迭代器介绍迭代器是用来迭代取值的工具每一次迭代得到的结果会作为下一次迭代的初始值,单纯的重复并不是迭代#while循环实现迭代取值a=[1,2,3,4,5,6]index=0whileindex<len(a):print(a[index])index+=1 1.2可迭代对象内置有_......