CefSharp访问加密的HTML / JS / CSS文件 [英] CefSharp Accessing Encrypted HTML/JS/CSS Files

查看:1652
本文介绍了CefSharp访问加密的HTML / JS / CSS文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在WPF中构建一个Windows桌面应用程序,它有一个加载本地HTML / JS / CSS文件的嵌入式浏览器(CefSharp)。这些文件不适用于应用程序。每次启动程序时都会从服务器上下载它们。这些文件保存在用户的应用程序数据文件夹中。



我担心这些文件虽然隐藏在应用程序数据文件夹中,但仍然可以找到它们,打开它们并阅读他们的内容。这些文件实际上是程序功能的最大部分,我的客户真的希望没有人能够窃取源代码,即下载的HTML / JS / CSS文件的内容。



所以我的第一个想法是只有在程序访问文件时才加密文件并解密它们。问题是,CefSharp是否支持这个? CefSharp何时或在哪一步开始访问文件?在CefSharp可以读取文件之前,我可以截取它并解密文件的内容吗?否则CefSharp将无法正确读取加密内容?请注意,即使程序正在运行,文件应该始终保持加密状态。

解决方案

经过漫长的争斗,我终于找到自己的工作解决方案。基本思路是编写我自己的资源处理程序以及资源处理程序工厂。


  1. 创建实现IResourceHandler的MyResourceHandler。

  2. 如果ResponseLength没有值并且Stream为null(可选),则在GetResponse函数中进行解密。
  3. 创建实现IResourceHandlerFactory的MyResourceHandlerFactory。
  4. 在GetResourceHandler函数中进行解密。
  5. 在初始化ChromiumWebBrowser的同时使用MyResourceHandlerFactory。

GetResponse示例函数在MyResourceHandler中:

pre $ public virtual Stream GetResponse(IResponse response,out long responseLength,out string redirectUrl)
{
redirectUrl = null;
responseLength = -1;

response.MimeType = MimeType;
response.StatusCode = StatusCode;
response.StatusText = StatusText;
response.ResponseHeaders =标题;

if(ResponseLength.HasValue)
{
responseLength = ResponseLength.Value;
}
else
{
//如果没有提供ResponseLength,则尝试推断长度
var memoryStream = Stream作为MemoryStream;
if(memoryStream!= null)
{
responseLength = memoryStream.Length;
}
else
{
var absoluteFilePath = new Uri(FilePath).AbsolutePath;
var fileBytes = File.ReadAllBytes(absoluteFilePath);
if(ShouldDecrypt)
{
memoryStream = Decrypt(fileBytes);
}
else
{
memoryStream = new MemoryStream(fileBytes);
}
responseLength = memoryStream.Length;
Stream = memoryStream作为Stream;
}
}

返回Stream;

MyResourceHandlerFactory中的示例GetResourceHandler函数:

  public virtual IResourceHandler GetResourceHandler(IWebBrowser browserControl,IBrowser browser,IFrame frame,IRequest request)
{
try
{
var uri = new Uri(request.Url);
var filePath = uri.AbsolutePath;

if(!File.Exists(filePath))返回null;

var mime = GetMimeType(filePath);
var fileBytes = File.ReadAllBytes(filePath);

if(ShouldDecrypt)
{
Stream decryptedStream = Decrypt(fileBytes);
返回MyResourceHandler.FromStream(decryptedStream,mime);
}

流fileStream = new MemoryStream(fileBytes);
返回MyResourceHandler.FromStream(fileStream,mime);
}
finally
{
request.Dispose();




$ b $ p
$ b

最后如果你想知道如何初始化使用MyResourceHandlerFactory的ChromiumWebBrowser:

  var browser = new ChromiumWebBrowser 
{
ResourceHandlerFactory = new MyResourceHandlerFactory()
};


I have been building a Windows desktop application in WPF and it has an embedded browser (CefSharp) that load local HTML/JS/CSS files. These files do not come readily with the application. They will be downloaded from a server each time the program is started. These files are kept in user's application data folder.

My concern is that these files, although hidden in the application data folder, user could still find them, open them and read their content. These files actually play the biggest part for the program's functionalities and my client really hope that no one could steal the source codes, meaning the content of the downloaded HTML/JS/CSS files.

So my first thought is to encrypt the files and decrypt them only when the program access the files. Question is, does CefSharp support this somehow? When or at which step does CefSharp begin to access the files? Could I intercept it and decrypt the files' content before CefSharp could read them? Otherwise CefSharp would not be able to read the encrypted content right? Note that the files should remain encrypted the whole time even when the program is running.

解决方案

After a long struggle, I finally found a working solution myself. The basic idea is to write my own resource handler as well as resource handler factory.

  1. Create MyResourceHandler that implements IResourceHandler.
  2. Do decryption in GetResponse function if ResponseLength has no value and Stream is null (optional).
  3. Create MyResourceHandlerFactory that implements IResourceHandlerFactory.
  4. Do decryption in GetResourceHandler function.
  5. Use MyResourceHandlerFactory while initializing ChromiumWebBrowser.

Sample GetResponse function in MyResourceHandler:

public virtual Stream GetResponse(IResponse response, out long responseLength, out string redirectUrl)
{
    redirectUrl = null;
    responseLength = -1;

    response.MimeType = MimeType;
    response.StatusCode = StatusCode;
    response.StatusText = StatusText;
    response.ResponseHeaders = Headers;

    if (ResponseLength.HasValue)
    {
        responseLength = ResponseLength.Value;
    }
    else
    {
        //If no ResponseLength provided then attempt to infer the length
        var memoryStream = Stream as MemoryStream;
        if (memoryStream != null)
        {
            responseLength = memoryStream.Length;
        }
        else
        {
            var absoluteFilePath = new Uri(FilePath).AbsolutePath;
            var fileBytes = File.ReadAllBytes(absoluteFilePath);
            if (ShouldDecrypt)
            {
                memoryStream = Decrypt(fileBytes);
            }
            else
            {
                memoryStream = new MemoryStream(fileBytes);
            }
            responseLength = memoryStream.Length;
            Stream = memoryStream as Stream;
        }
    }

    return Stream;
}

Sample GetResourceHandler function in MyResourceHandlerFactory:

public virtual IResourceHandler GetResourceHandler(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request)
{
    try
    {
        var uri = new Uri(request.Url);
        var filePath = uri.AbsolutePath;

        if (!File.Exists(filePath)) return null;

        var mime = GetMimeType(filePath);
        var fileBytes = File.ReadAllBytes(filePath);

        if (ShouldDecrypt)
        {
            Stream decryptedStream = Decrypt(fileBytes);
            return MyResourceHandler.FromStream(decryptedStream, mime);
        }

        Stream fileStream = new MemoryStream(fileBytes);
        return MyResourceHandler.FromStream(fileStream, mime);
    }
    finally
    {
        request.Dispose();
    }
}

And finally in case you wish to know how to initialize ChromiumWebBrowser that uses MyResourceHandlerFactory:

var browser = new ChromiumWebBrowser
{
    ResourceHandlerFactory = new MyResourceHandlerFactory()
};

这篇关于CefSharp访问加密的HTML / JS / CSS文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆