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

Following this nice tutorial (quite old but good anyways ) : http://www.geeknewz.fr/tuto-proxy-socks-via-ssh-sur-synology-15567

Here is the same thing, but aotumatised in C#: don’t forget to replace YOUR_SYNOLOGY_DNS_OR_IP_HERE  with your synology address or DNS name

and also your syno admin password instead of  YOUR_SYNOLOGY_PASSWORD_HERE

you can’t slightly change code to not have to type your password every new connection (Console.ReadLine()…and remove:

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");

        }
    }
}

Tagged , , , , , , . Bookmark the permalink.

About Xavier

7 years+ .net consulting

Leave a Reply

Your email address will not be published. Required fields are marked *

Captcha * Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.