Hey San

Hey San

  • 2k
  • 2
  • 270

Trying titanium web proxy solution

Oct 5 2020 1:43 PM
We installed nuget titanium web proxy, created a window service and initiated titanium web proxy. The windows service works, runs, and start and stop times are written to a log file. But the web proxy is supposed to catch internet request and afford them, though no such events happen; nothing is logged, when i open some page with different browsers. Here is our code:
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.ComponentModel;    
  4. using System.Data;    
  5. using System.Diagnostics;    
  6. using System.IO;    
  7. using System.Linq;    
  8. using System.ServiceProcess;    
  9. using System.Text;    
  10. using System.Threading.Tasks;    
  11. using Titanium.Web.Proxy;    
  12. using Titanium.Web.Proxy.EventArguments;    
  13.     
  14. namespace WebProxyOne {    
  15.     public partial class MyNewService : ServiceBase {    
  16.         public ProxyServer proxyServer;    
  17.     
  18.         public MyNewService() {    
  19.             InitializeComponent();    
  20.         }    
  21.     
  22.         protected override void OnStart(string[] args) {    
  23.             proxyServer = new ProxyServer(truetruetrue);    
  24.     
  25.             proxyServer.BeforeRequest += OnRequest;    
  26.     
  27.             proxyServer.Start();    
  28.     
  29.             WriteToFile("Service is started at " + DateTime.Now);    
  30.     
  31.         }    
  32.     
  33.         protected override void OnStop() {    
  34.             proxyServer.Stop();    
  35.             WriteToFile("Service is stopped at " + DateTime.Now);    
  36.         }    
  37.         public void WriteToFile(string Message) {    
  38.             string path = "E:\\Downloads\\Logs";    
  39.             if (!Directory.Exists(path)) {    
  40.                 Directory.CreateDirectory(path);    
  41.             }    
  42.             string filepath = "E:\\Downloads\\Logs\\ServiceLog_" + DateTime.Now.Date.ToShortDateString().Replace('/''_') + ".txt";    
  43.             if (!File.Exists(filepath)) {    
  44.                 // Create a file to write to.       
  45.                 using (StreamWriter sw = File.CreateText(filepath)) {    
  46.                     sw.WriteLine(Message);    
  47.                 }    
  48.             } else {    
  49.                 using (StreamWriter sw = File.AppendText(filepath)) {    
  50.                     sw.WriteLine(Message);    
  51.                 }    
  52.             }    
  53.         }    
  54.     
  55.         public async Task OnRequest(object sender, SessionEventArgs e) {    
  56.             WriteToFile(e.HttpClient.Request.Url);    
  57.     
  58.             // To cancel a request with a custom HTML content    
  59.             // Filter URL    
  60.             if (e.HttpClient.Request.Method.ToUpper() == "GET" && e.HttpClient.Request.RequestUri.AbsoluteUri.Contains("google.com")) {    
  61.                 e.Ok("<!DOCTYPE html>" +    
  62.                     "<html><body><h1>" +    
  63.                     "Website Blocked" +    
  64.                     "</h1>" +    
  65.                     "<p>Blocked by titanium web proxy.</p>" +    
  66.                     "</body>" +    
  67.                     "</html>");    
  68.             }    
  69.     
  70.             // Redirect example    
  71.             if (e.HttpClient.Request.RequestUri.AbsoluteUri.Contains("wikipedia.org")) {    
  72.                 e.Redirect("https://www.paypal.com");    
  73.             }    
  74.         }    
  75.     }    
  76. }