proxy socks via tunnel SSH avec Putty et C# .NET

Suite à l’excellent tuto (qui date un peu mais excellent quand même ! ) : http://www.geeknewz.fr/tuto-proxy-socks-via-ssh-sur-synology-15567

Voici la même chose, mais automatisée en C#: n’oubliez pas de remplacer YOUR_SYNOLOGY_DNS_OR_IP_HERE  par l’adresse ou le dns name de votre synology

et aussi le mot de passe admin de votre syno à la place de YOUR_SYNOLOGY_PASSWORD_HERE

on peut légèrement modifier le code pour ne pas mettre en dur le mot de passe et l’entrer à chaque nouvelle connexion (Console.ReadLine()… et supprimer:

cmd.StandardInput.WriteLine("./myscript.sh");
            cmd.StandardInput.WriteLine("y");
            cmd.StandardInput.WriteLine("exit");
using Microsoft.Win32;
using Microsoft.Win32;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace MyProxyTunnel
{
    class Program
    {

        private static bool keepRunning = true;
        static void Main(string[] args)
        {
            //CTRL+C to quit console app
            Console.CancelKeyPress += delegate (object sender, ConsoleCancelEventArgs e)
            {
                e.Cancel = true;
                Program.keepRunning = false;

            };

            //PART 1 :
            //checks which browsers are installed
            RegistryKey browserKeys;
            //on 64bit the browsers are in a different location
            browserKeys = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\WOW6432Node\Clients\StartMenuInternet");
            if (browserKeys == null)
                browserKeys = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Clients\StartMenuInternet");

            string[] browserNames = browserKeys.GetSubKeyNames();
            var driverpath = Path.GetDirectoryName(Assembly.GetExecutingAssembl‌​y().Location);
            IWebDriver driver = null;

            //PART II : run proxy tunnel
            Process cmd = new Process();
            cmd.StartInfo.FileName = @"C:\Program Files (x86)\PuTTY\plink.exe";
            cmd.StartInfo.UseShellExecute = false;
            cmd.StartInfo.RedirectStandardInput = true;
            cmd.StartInfo.RedirectStandardOutput = true;
            cmd.StartInfo.Arguments = " -ssh admin@YOUR_SYNOLOGY_DNS_OR_IP_HERE:443 -pw YOUR_SYNOLOGY_PASSWORD_HERE  -C -T -D 443 -N";
            cmd.Start();
            //this below is for removing the 'enter "yes"' keyboard confirmation 
            cmd.StandardInput.WriteLine("./myscript.sh");
            cmd.StandardInput.WriteLine("y");
            cmd.StandardInput.WriteLine("exit");
          //  string output = cmd.StandardOutput.ReadToEnd();

            //PART III : run browser
            if (browserNames.Contains("Google Chromes"))
            { //starts chrome with custom proxy settings ; you need chromedriver.exe //just install the WebDriver from Nuget Package manager

                ChromeOptions options = new ChromeOptions();
                options.AddArguments("--proxy-server=socks5://localhost:443");
                driver = new ChromeDriver(driverpath, options);


            }
            else
                if (browserNames.Contains("FIREFOX.EXE"))
            { //else starts firefox
              //Create a new Firefox profile
                var firefoxProfile = new FirefoxProfile();
                //Create a new proxy object
                var proxy = new Proxy()
                {
                    SocksProxy = "localhost:443"
                };
                //We then add this proxt setting to the Firefox profile we created
                firefoxProfile.SetProxyPreferences(proxy);
                //Then create a new Firefox Driver passing in the profile we created
                //WebDriver we open a Firefox using this profile now
                driver = new FirefoxDriver(firefoxProfile);

            }
            if (driver != null)
                driver.Navigate().GoToUrl("http://www.google.fr");

        }
    }
}

Taggé , , , , , , .Mettre en favori le Permaliens.

A propos Xavier

7 years+ .net consulting

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Captcha * Time limit is exhausted. Please reload CAPTCHA.