简体   繁体   English

尝试钛网络代理解决方案

[英]Trying titanium web proxy solution

We installed nuget titanium web proxy, created a window service and initiated titanium web proxy.我们安装了 nuget 钛网络代理,创建了一个窗口服务并启动了钛网络代理。 The windows service works, runs, and start and stop times are written to a log file. Windows 服务工作、运行,并且开始和停止时间被写入日志文件。 But the web proxy is supposed to catch internet request and afford them, though no such events happens and nothing is logged, when i open some page with different browsers.但是,当我使用不同的浏览器打开某个页面时,Web 代理应该捕获 Internet 请求并提供它们,尽管没有发生此类事件并且没有记录任何内容。 Here is our code:这是我们的代码:

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using Titanium.Web.Proxy;
using Titanium.Web.Proxy.EventArguments;

namespace WebProxy1 {
    public partial class MyNewService : ServiceBase {
        public ProxyServer proxyServer;

        public MyNewService() {
            InitializeComponent();
        }

        protected override void OnStart(string[] args) {
            proxyServer = new ProxyServer(true, true, true);

            proxyServer.BeforeRequest += OnRequest;

            proxyServer.Start();

            WriteToFile("Service is started at " + DateTime.Now);

        }

        protected override void OnStop() {
            proxyServer.Stop();
            WriteToFile("Service is stopped at " + DateTime.Now);
        }
        public void WriteToFile(string Message) {
            string path = "E:\\Downloads\\Logs";
            if (!Directory.Exists(path)) {
                Directory.CreateDirectory(path);
            }
            string filepath = "E:\\Downloads\\Logs\\ServiceLog_" + DateTime.Now.Date.ToShortDateString().Replace('/', '_') + ".txt";
            if (!File.Exists(filepath)) {
                // Create a file to write to.   
                using (StreamWriter sw = File.CreateText(filepath)) {
                    sw.WriteLine(Message);
                }
            } else {
                using (StreamWriter sw = File.AppendText(filepath)) {
                    sw.WriteLine(Message);
                }
            }
        }

        public async Task OnRequest(object sender, SessionEventArgs e) {
            WriteToFile(e.HttpClient.Request.Url);

            // To cancel a request with a custom HTML content
            // Filter URL
            if (e.HttpClient.Request.Method.ToUpper() == "GET" && e.HttpClient.Request.RequestUri.AbsoluteUri.Contains("google.com")) {
                e.Ok("<!DOCTYPE html>" +
                    "<html><body><h1>" +
                    "Website Blocked" +
                    "</h1>" +
                    "<p>Blocked by titanium web proxy.</p>" +
                    "</body>" +
                    "</html>");
            }

            // Redirect example
            if (e.HttpClient.Request.RequestUri.AbsoluteUri.Contains("wikipedia.org")) {
                e.Redirect("https://www.paypal.com");
            }
        }
    }
}

I think you did not set the titanium proxy properly.我认为您没有正确设置钛代理。

Before starting the proxy, you have to set endpoint.在启动代理之前,您必须设置端点。

There is titanium proxy using example here .有使用例如钛代理这里

This is my sample source.这是我的示例源。

var explicitEndPoint = new ExplicitProxyEndPoint(IPAddress.Any, 8000, true)
{
    // Use self-issued generic certificate on all https requests
    // Optimizes performance by not creating a certificate for each https-enabled domain
    // Useful when certificate trust is not required by proxy clients
   //GenericCertificate = new X509Certificate2(Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "genericcert.pfx"), "password")
};

// Fired when a CONNECT request is received
explicitEndPoint.BeforeTunnelConnect += OnBeforeTunnelConnect;

// An explicit endpoint is where the client knows about the existence of a proxy
// So client sends request in a proxy friendly manner
proxyServer.AddEndPoint(explicitEndPoint);
proxyServer.Start();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM