Dave Davidson
Dave Davidson

Reputation: 11

Automatically Setting Internet Explorer/Windows to use a Socks5 proxy using C#

I'm making a SSH Tunnelling application, and need to be able to automatically force the system to use HTTP & Socks5 proxies, and have the changes take effect instantly. HTTP proxies are now taken care of perfectly by the PoshHTTP class , but I can't figure out how to do the same with SOCKS5.

I've already tried forcing the changes in the registry, but they don't take effect instantly and it's just not reliable. In most cases I had to open internet options > lan settings before the settings would take effect, so the user may as well have set the proxy up manually by this point.

Is there a way to do this that I'm missing ? It would be amazing if I could just modify poshHTTP to do this, but I don't have high hopes.

Upvotes: 1

Views: 1545

Answers (2)

Sam
Sam

Reputation: 2970

Anyone came here wondering about Tor, this may be useful to you

    public struct Struct_INTERNET_PROXY_INFO
    {
        public int dwAccessType;
        public IntPtr proxy;
        public IntPtr proxyBypass;
    };

    [DllImport("wininet.dll", SetLastError = true)]
    public static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength);

    public static void RefreshProxy()
    {
        try
        {
            //RESTART TOR
            Struct_INTERNET_PROXY_INFO struct_IPI;
            struct_IPI.dwAccessType = 3;
            struct_IPI.proxy = Marshal.StringToHGlobalAnsi("socks=127.0.0.1:9050");
            struct_IPI.proxyBypass = Marshal.StringToHGlobalAnsi("local");
            IntPtr intptrStruct = Marshal.AllocCoTaskMem(Marshal.SizeOf(struct_IPI));
            Marshal.StructureToPtr(struct_IPI, intptrStruct, true);
            InternetSetOption(IntPtr.Zero, 38, intptrStruct, Marshal.SizeOf(struct_IPI));
        }
        catch (Exception){ }
    }

Upvotes: 1

Dave Davidson
Dave Davidson

Reputation: 11

Found the solution after asking for help on another forum. In the end I was able to continue to use the PoshHTTP class I used elsewhere but instead of passing just ip:port use it like this

PoshHttp.Proxies.SetProxy("socks=socks://$ip:$port"); 

Upvotes: 0

Related Questions