static void Main(string[] args) { if (File.Exists(GetInfoFilePath()) == false || File.Exists(GetHostsFilePath()) == false) { if (File.Exists(GetInfoFilePath()) == false) { File.WriteAllText( GetInfoFilePath(), CreateText( "#开头的是注释", "#按位置决定参数的意义", "#Day", "#KeySize", "#CaName", "#SubCertName"), Encoding.UTF8); } if (File.Exists(GetHostsFilePath()) == false) { File.WriteAllText(GetHostsFilePath(), "", Encoding.UTF8); } Console.WriteLine("已创建配置文件请编辑后继续运行"); Console.ReadLine(); return; } var certInfo = CreateCertInfo(); var hosts = CreateHosts(); var caCert = TLSBouncyCastleHelper.GenerateCA( certInfo.CaName, certInfo.KeySize, certInfo.Day); var tlsCert = TLSBouncyCastleHelper.GenerateTls( CaPack.Create(caCert), certInfo.SubCertName, certInfo.KeySize, certInfo.Day, hosts); SaveCa(caCert); SaveTls(tlsCert); SaveTlsPem(tlsCert); }
static void CreateCert() { var ca = TLSBouncyCastleHelper.GenerateCA(CANAME, 2048, 3000); var cert = TLSBouncyCastleHelper.GenerateTls( CaPack.Create(ca), "Leikaifng iwara.tv", 2048, 3000, GetSubName()); var pemCert = TLSBouncyCastleHelper.CreatePem.AsPem(cert); var pemKey = TLSBouncyCastleHelper.CreatePem.AsKey(cert); using (X509Store store = new X509Store(StoreName.Root, StoreLocation.CurrentUser)) { store.Open(OpenFlags.ReadWrite); store.Add(new X509Certificate2(ca.Export(X509ContentType.Cert))); } var basePath = GetNginxConfigFolderPath(); Directory.CreateDirectory(basePath); var pemPath = Path.Combine(basePath, "cert.pem"); var cerPath = Path.Combine(basePath, "cert.cer"); var keyPath = Path.Combine(basePath, "cert.key"); var caPath = Path.Combine(basePath, "ca.cer"); File.WriteAllBytes(cerPath, cert.Export(X509ContentType.Cert)); File.WriteAllBytes(pemPath, pemCert); File.WriteAllBytes(keyPath, pemKey); File.WriteAllBytes(caPath, ca.Export(X509ContentType.Cert)); }
static void Main(string[] args) { Task.Run(async() => { while (true) { await Task.Delay(new TimeSpan(0, 0, 10)).ConfigureAwait(false); GC.Collect(); } }); //AppDomain.CurrentDomain.FirstChanceException += CurrentDomain_FirstChanceException; //TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException; const string PORNHUB_HOST = "www.livehub.com"; const string IWARA_HOST = "iwara.tv"; var ip = Dns.GetHostAddresses(Dns.GetHostName()).Where((item) => item.AddressFamily == AddressFamily.InterNetwork).FirstOrDefault() ?? IPAddress.Loopback; var pornhubListensEndPoint = new IPEndPoint(ip, 1080); var pacListensEndPoint = new IPEndPoint(ip, 8080); var adErrorEndpoint = new IPEndPoint(IPAddress.Loopback, 80); var iwaraLsitensPoint = new IPEndPoint(ip, 6456); var adVido = File.ReadAllBytes("ad.mp4"); PacServer pacServer = PacServer.Start(pacListensEndPoint, PacHelper.Create((host) => host == "www.pornhub.com", ProxyMode.CreateHTTP(adErrorEndpoint)), PacHelper.Create((host) => host == "hubt.pornhub.com", ProxyMode.CreateHTTP(adErrorEndpoint)), PacHelper.Create((host) => host == "ajax.googleapis.com", ProxyMode.CreateHTTP(adErrorEndpoint)), PacHelper.Create((host) => PacMethod.dnsDomainIs(host, "pornhub.com"), ProxyMode.CreateHTTP(pornhubListensEndPoint)), PacHelper.Create((host) => PacMethod.dnsDomainIs(host, "adtng.com"), ProxyMode.CreateHTTP(pornhubListensEndPoint)), PacHelper.Create((host) => PacMethod.dnsDomainIs(host, IWARA_HOST), ProxyMode.CreateHTTP(iwaraLsitensPoint))); SetProxy.Set(PacServer.CreatePacUri(pacListensEndPoint)); var ca = File.ReadAllBytes("myCA.pfx"); X509Certificate2 mainCert = TLSBouncyCastleHelper.GenerateTls(ca, "pornhub.com", 2048, 2, new string[] { "pornhub.com", "*.pornhub.com" }); X509Certificate2 adCert = TLSBouncyCastleHelper.GenerateTls(ca, "adtng.com", 2048, 2, new string[] { "adtng.com", "*.adtng.com" }); X509Certificate2 iwaraCert = TLSBouncyCastleHelper.GenerateTls(ca, "iwara.tv", 2048, 2, new string[] { "*.iwara.tv" }); PornhubProxyInfo info = new PornhubProxyInfo { MainPageStreamCreate = Connect.CreateLocalStream(new X509Certificate2(mainCert), SslProtocols.Tls12), ADPageStreamCreate = Connect.CreateLocalStream(new X509Certificate2(adCert), SslProtocols.Tls12), RemoteStreamCreate = Connect.CreateRemoteStream(PORNHUB_HOST, 443, PORNHUB_HOST, (a, b) => new MHttpStream(a, b), SslProtocols.Tls12), MaxContentSize = 1024 * 1024 * 5, ADVideoBytes = adVido, CheckingVideoHtml = Connect.CheckingVideoHtml, MaxRefreshRequestCount = 30, ReplaceResponseHtml = Connect.ReplaceResponseHtml, }; PornhubProxyServer server = new PornhubProxyServer(info); Task t1 = server.Start(pornhubListensEndPoint); SniProxyInfo iwaraSniInfo = new SniProxyInfo( iwaraLsitensPoint, Connect.CreateDnsLocalStream(), Connect.CreateDnsRemoteStream("104.20.27.25", 443)); SniProxy iwaraSniProxy = new SniProxy(iwaraSniInfo); Task t2 = iwaraSniProxy.Start(); Task.WaitAll(t1, t2); }