Menu
Home
Forums
New posts
Search forums
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
C# Community
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Home
Forums
VB.NET
VB.NET General Discussion
How to implement SOCKS 4/5 support in my application?
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="ilovemayo" data-source="post: 179237" data-attributes="member: 54609"><p><span style="color: #333333">Greetings,</span></p><p></p><p></p><p><span style="color: #333333">I'm working on my application that sends emails anonymously. I encountered a problem... I can't seem to implement support for socks proxies.</span></p><p></p><p><span style="color: #333333">After hours of googling I found out that I should work with the WinInet API (wininet.dll)... I found some code that changes the following fields:</span></p><p><span style="color: #333333"></span></p><p><span style="color: #333333"></span>[ATTACH]4380[/ATTACH]</p><p></p><p></p><p><span style="color: #333333">Good. I need to modify only the socks field, but after a lot of hours trying I couldn't find a way to modify the code to change only the socks field...</span></p><p></p><p><span style="color: #333333">Here is the source code: </span></p><p><span style="color: #333333"></span></p><p><span style="color: #333333"></span>[CODE][COLOR=#333333]Imports System[/COLOR]</p><p>Imports System.Runtime.InteropServices</p><p></p><p></p><p>Public Class IEProxy</p><p></p><p> Public Enum Options</p><p></p><p> INTERNET_PER_CONN_FLAGS = 1</p><p></p><p> INTERNET_PER_CONN_PROXY_SERVER = 2</p><p></p><p> INTERNET_PER_CONN_PROXY_BYPASS = 3</p><p></p><p> INTERNET_PER_CONN_AUTOCONFIG_URL = 4</p><p></p><p> INTERNET_PER_CONN_AUTODISCOVERY_FLAGS = 5</p><p></p><p> INTERNET_OPTION_REFRESH = 37</p><p></p><p> INTERNET_OPTION_PER_CONNECTION_OPTION = 75</p><p></p><p> INTERNET_OPTION_SETTINGS_CHANGED = 39</p><p></p><p> PROXY_TYPE_PROXY = &H2</p><p></p><p> PROXY_TYPE_DIRECT = &H1</p><p></p><p> End Enum</p><p>End Class</p><p></p><p> <StructLayout(LayoutKind.Sequential)> _</p><p> Private Class FILETIME</p><p></p><p> Public dwLowDateTime As Integer</p><p></p><p> Public dwHighDateTime As Integer</p><p></p><p> End Class</p><p></p><p></p><p> <StructLayout(LayoutKind.Explicit, Size:=12)> _</p><p> Private Structure INTERNET_PER_CONN_OPTION</p><p></p><p> <FieldOffset(0)> Dim dwOption As Integer</p><p></p><p> <FieldOffset(4)> Dim dwValue As Integer</p><p></p><p> <FieldOffset(4)> Dim pszValue As IntPtr</p><p></p><p> <FieldOffset(4)> Dim ftValue As IntPtr</p><p></p><p></p><p> Public Function GetBytes() As Byte()</p><p></p><p> Dim b(12) As Byte</p><p></p><p> BitConverter.GetBytes(dwOption).CopyTo(b, 0)</p><p></p><p> Select Case dwOption</p><p></p><p> Case (Options.INTERNET_PER_CONN_FLAGS)</p><p></p><p> BitConverter.GetBytes(dwValue).CopyTo(b, 4)</p><p></p><p> Case (Options.INTERNET_PER_CONN_PROXY_BYPASS)</p><p></p><p> BitConverter.GetBytes(pszValue.ToInt32()).CopyTo(b, 4)</p><p></p><p> Case (Options.INTERNET_PER_CONN_PROXY_SERVER)</p><p></p><p> BitConverter.GetBytes(pszValue.ToInt32()).CopyTo(b, 4)</p><p></p><p> End Select</p><p></p><p> Return (b)</p><p></p><p> End Function</p><p></p><p> End Structure</p><p></p><p></p><p> <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _</p><p> Private Class INTERNET_PER_CONN_OPTION_LIST</p><p></p><p> Public dwSize As Integer</p><p></p><p> Public pszConnection As String</p><p></p><p> Public dwOptionCount As Integer</p><p></p><p> Public dwOptionError As Integer</p><p></p><p> Public pOptions As IntPtr</p><p></p><p> End Class</p><p></p><p> <StructLayout(LayoutKind.Sequential)> _</p><p> Private Class INTERNET_PROXY_INFO</p><p></p><p> Public dwAccessType As Integer</p><p></p><p> Public lpszProxy As IntPtr</p><p></p><p> Public lpszProxyBypass As IntPtr</p><p></p><p> End Class</p><p></p><p></p><p> Private Const ERROR_INSUFFICIENT_BUFFER = 122</p><p></p><p> Private Const INTERNET_OPTION_PROXY = 38</p><p></p><p> Private Const INTERNET_OPEN_TYPE_DIRECT = 1</p><p></p><p></p><p> <DllImport("wininet.dll")> _</p><p> Private Shared Function InternetSetOption(ByVal hInternet As IntPtr, _</p><p> ByVal dwOption As Integer, _</p><p> ByVal lpBuffer As INTERNET_PER_CONN_OPTION_LIST, _</p><p> ByVal dwBufferLength As Integer) As Boolean</p><p></p><p> End Function</p><p></p><p></p><p> <DllImport("kernel32.dll")> _</p><p> Private Shared Function GetLastError() As Integer</p><p></p><p> End Function</p><p></p><p></p><p> Public Function SetProxy(ByVal proxy_full_addr As String) As Boolean</p><p></p><p> Dim bReturn As Boolean</p><p></p><p> Dim list As New INTERNET_PER_CONN_OPTION_LIST</p><p></p><p> Dim dwBufSize As Integer = Marshal.SizeOf(list)</p><p></p><p> Dim opts(3) As INTERNET_PER_CONN_OPTION</p><p></p><p> Dim opt_size As Integer = Marshal.SizeOf(opts(0))</p><p></p><p></p><p> list.dwSize = dwBufSize</p><p></p><p> list.pszConnection = ControlChars.NullChar</p><p></p><p> list.dwOptionCount = 3</p><p></p><p></p><p> 'set flags</p><p></p><p> opts(0).dwOption = Options.INTERNET_PER_CONN_FLAGS</p><p></p><p> opts(0).dwValue = Options.PROXY_TYPE_DIRECT Or Options.PROXY_TYPE_PROXY</p><p></p><p></p><p> 'set proxyname</p><p></p><p> opts(1).dwOption = Options.INTERNET_PER_CONN_PROXY_SERVER</p><p></p><p> opts(1).pszValue = Marshal.StringToHGlobalAnsi(proxy_full_addr)</p><p></p><p></p><p> 'set override</p><p></p><p> opts(2).dwOption = Options.INTERNET_PER_CONN_PROXY_BYPASS</p><p></p><p> opts(2).pszValue = Marshal.StringToHGlobalAnsi("local")</p><p></p><p></p><p> Dim b(3 * opt_size) As Byte</p><p></p><p> opts(0).GetBytes().CopyTo(b, 0)</p><p></p><p> opts(1).GetBytes().CopyTo(b, opt_size)</p><p></p><p> opts(2).GetBytes().CopyTo(b, 2 * opt_size)</p><p></p><p></p><p> Dim ptr As IntPtr = Marshal.AllocCoTaskMem(3 * opt_size)</p><p></p><p> Marshal.Copy(b, 0, ptr, 3 * opt_size)</p><p></p><p></p><p> list.pOptions = ptr</p><p></p><p> 'Set the options on the connection</p><p></p><p> bReturn = InternetSetOption(IntPtr.Zero, Options.INTERNET_OPTION_PER_CONNECTION_OPTION, list, dwBufSize)</p><p></p><p> If Not bReturn Then</p><p></p><p> Debug.WriteLine(GetLastError)</p><p></p><p> End If</p><p></p><p></p><p> 'Notify existing Internet Explorer instances that the settings have changed</p><p></p><p> bReturn = InternetSetOption(IntPtr.Zero, Options.INTERNET_OPTION_SETTINGS_CHANGED, Nothing, 0)</p><p></p><p> If Not bReturn Then</p><p></p><p> Debug.WriteLine(GetLastError)</p><p></p><p> End If</p><p></p><p></p><p> 'Flush the current IE proxy setting</p><p></p><p> bReturn = InternetSetOption(IntPtr.Zero, Options.INTERNET_OPTION_REFRESH, Nothing, 0)</p><p></p><p> If Not bReturn Then</p><p></p><p> Debug.WriteLine(GetLastError)</p><p></p><p> End If</p><p></p><p></p><p> Marshal.FreeHGlobal(opts(1).pszValue)</p><p></p><p> Marshal.FreeHGlobal(opts(2).pszValue)</p><p></p><p> Marshal.FreeCoTaskMem(ptr)</p><p></p><p> Return (bReturn)</p><p></p><p> End Function</p><p></p><p></p><p> Public Function DisableProxy() As Boolean</p><p></p><p> Dim bReturn As Boolean</p><p></p><p> Dim list As New INTERNET_PER_CONN_OPTION_LIST</p><p></p><p> Dim dwBufSize As Integer = Marshal.SizeOf(list)</p><p></p><p> Dim opts(0) As INTERNET_PER_CONN_OPTION</p><p></p><p> Dim opt_size As Integer = Marshal.SizeOf(opts(0))</p><p></p><p></p><p> list.dwSize = dwBufSize</p><p></p><p> list.pszConnection = ControlChars.NullChar</p><p></p><p> list.dwOptionCount = 1</p><p></p><p></p><p> opts(0).dwOption = Options.INTERNET_PER_CONN_FLAGS</p><p></p><p> opts(0).dwValue = Options.PROXY_TYPE_DIRECT</p><p></p><p></p><p> Dim b(opt_size) As Byte</p><p></p><p> opts(0).GetBytes().CopyTo(b, 0)</p><p></p><p></p><p> Dim ptr As IntPtr = Marshal.AllocCoTaskMem(opt_size)</p><p></p><p> Marshal.Copy(b, 0, ptr, opt_size)</p><p></p><p></p><p> list.pOptions = ptr</p><p></p><p> 'Set the options on the connection</p><p></p><p> bReturn = InternetSetOption(IntPtr.Zero, Options.INTERNET_OPTION_PER_CONNECTION_OPTION, list, dwBufSize)</p><p></p><p> If Not bReturn Then</p><p></p><p> Debug.WriteLine(GetLastError)</p><p></p><p> End If</p><p></p><p></p><p> 'Notify existing Internet Explorer instances that the settings have changed</p><p></p><p> bReturn = InternetSetOption(IntPtr.Zero, Options.INTERNET_OPTION_SETTINGS_CHANGED, Nothing, 0)</p><p></p><p> If Not bReturn Then</p><p></p><p> Debug.WriteLine(GetLastError)</p><p></p><p> End If</p><p></p><p></p><p> 'Flush the current IE proxy setting</p><p></p><p> bReturn = InternetSetOption(IntPtr.Zero, Options.INTERNET_OPTION_REFRESH, Nothing, 0)</p><p></p><p> If Not bReturn Then</p><p></p><p> Debug.WriteLine(GetLastError)</p><p></p><p> End If</p><p></p><p></p><p> Marshal.FreeCoTaskMem(ptr)</p><p></p><p> Return (bReturn)</p><p></p><p> End Function [/CODE]<span style="color: #333333"></span></p><p><span style="color: #333333"></span>[CODE][COLOR=#333333]sProxy.SetProxy("proxy:port") 'to start the thing[/COLOR][/CODE]<span style="color: #333333"></span></p><p><span style="color: #333333"></span>[CODE][COLOR=#333333] sProxy.DisableProxy() 'to stop the thing[/COLOR][/CODE]<span style="color: #333333"></span></p><p><span style="color: #333333"></span></p><p><span style="color: #333333"></span></p><p><span style="color: #333333">Trust me, I searched and tried a lot of things, but I couldn't get it, that's why I'm asking you guys.</span></p><p></p><p></p><p><span style="color: #333333">Thanks!</span></p></blockquote><p></p>
[QUOTE="ilovemayo, post: 179237, member: 54609"] [COLOR=#333333]Greetings,[/COLOR] [COLOR=#333333]I'm working on my application that sends emails anonymously. I encountered a problem... I can't seem to implement support for socks proxies.[/COLOR] [COLOR=#333333]After hours of googling I found out that I should work with the WinInet API (wininet.dll)... I found some code that changes the following fields: [/COLOR][ATTACH]4380.vB[/ATTACH] [COLOR=#333333]Good. I need to modify only the socks field, but after a lot of hours trying I couldn't find a way to modify the code to change only the socks field...[/COLOR] [COLOR=#333333]Here is the source code: [/COLOR][CODE][COLOR=#333333]Imports System[/COLOR] Imports System.Runtime.InteropServices Public Class IEProxy Public Enum Options INTERNET_PER_CONN_FLAGS = 1 INTERNET_PER_CONN_PROXY_SERVER = 2 INTERNET_PER_CONN_PROXY_BYPASS = 3 INTERNET_PER_CONN_AUTOCONFIG_URL = 4 INTERNET_PER_CONN_AUTODISCOVERY_FLAGS = 5 INTERNET_OPTION_REFRESH = 37 INTERNET_OPTION_PER_CONNECTION_OPTION = 75 INTERNET_OPTION_SETTINGS_CHANGED = 39 PROXY_TYPE_PROXY = &H2 PROXY_TYPE_DIRECT = &H1 End Enum End Class <StructLayout(LayoutKind.Sequential)> _ Private Class FILETIME Public dwLowDateTime As Integer Public dwHighDateTime As Integer End Class <StructLayout(LayoutKind.Explicit, Size:=12)> _ Private Structure INTERNET_PER_CONN_OPTION <FieldOffset(0)> Dim dwOption As Integer <FieldOffset(4)> Dim dwValue As Integer <FieldOffset(4)> Dim pszValue As IntPtr <FieldOffset(4)> Dim ftValue As IntPtr Public Function GetBytes() As Byte() Dim b(12) As Byte BitConverter.GetBytes(dwOption).CopyTo(b, 0) Select Case dwOption Case (Options.INTERNET_PER_CONN_FLAGS) BitConverter.GetBytes(dwValue).CopyTo(b, 4) Case (Options.INTERNET_PER_CONN_PROXY_BYPASS) BitConverter.GetBytes(pszValue.ToInt32()).CopyTo(b, 4) Case (Options.INTERNET_PER_CONN_PROXY_SERVER) BitConverter.GetBytes(pszValue.ToInt32()).CopyTo(b, 4) End Select Return (b) End Function End Structure <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _ Private Class INTERNET_PER_CONN_OPTION_LIST Public dwSize As Integer Public pszConnection As String Public dwOptionCount As Integer Public dwOptionError As Integer Public pOptions As IntPtr End Class <StructLayout(LayoutKind.Sequential)> _ Private Class INTERNET_PROXY_INFO Public dwAccessType As Integer Public lpszProxy As IntPtr Public lpszProxyBypass As IntPtr End Class Private Const ERROR_INSUFFICIENT_BUFFER = 122 Private Const INTERNET_OPTION_PROXY = 38 Private Const INTERNET_OPEN_TYPE_DIRECT = 1 <DllImport("wininet.dll")> _ Private Shared Function InternetSetOption(ByVal hInternet As IntPtr, _ ByVal dwOption As Integer, _ ByVal lpBuffer As INTERNET_PER_CONN_OPTION_LIST, _ ByVal dwBufferLength As Integer) As Boolean End Function <DllImport("kernel32.dll")> _ Private Shared Function GetLastError() As Integer End Function Public Function SetProxy(ByVal proxy_full_addr As String) As Boolean Dim bReturn As Boolean Dim list As New INTERNET_PER_CONN_OPTION_LIST Dim dwBufSize As Integer = Marshal.SizeOf(list) Dim opts(3) As INTERNET_PER_CONN_OPTION Dim opt_size As Integer = Marshal.SizeOf(opts(0)) list.dwSize = dwBufSize list.pszConnection = ControlChars.NullChar list.dwOptionCount = 3 'set flags opts(0).dwOption = Options.INTERNET_PER_CONN_FLAGS opts(0).dwValue = Options.PROXY_TYPE_DIRECT Or Options.PROXY_TYPE_PROXY 'set proxyname opts(1).dwOption = Options.INTERNET_PER_CONN_PROXY_SERVER opts(1).pszValue = Marshal.StringToHGlobalAnsi(proxy_full_addr) 'set override opts(2).dwOption = Options.INTERNET_PER_CONN_PROXY_BYPASS opts(2).pszValue = Marshal.StringToHGlobalAnsi("local") Dim b(3 * opt_size) As Byte opts(0).GetBytes().CopyTo(b, 0) opts(1).GetBytes().CopyTo(b, opt_size) opts(2).GetBytes().CopyTo(b, 2 * opt_size) Dim ptr As IntPtr = Marshal.AllocCoTaskMem(3 * opt_size) Marshal.Copy(b, 0, ptr, 3 * opt_size) list.pOptions = ptr 'Set the options on the connection bReturn = InternetSetOption(IntPtr.Zero, Options.INTERNET_OPTION_PER_CONNECTION_OPTION, list, dwBufSize) If Not bReturn Then Debug.WriteLine(GetLastError) End If 'Notify existing Internet Explorer instances that the settings have changed bReturn = InternetSetOption(IntPtr.Zero, Options.INTERNET_OPTION_SETTINGS_CHANGED, Nothing, 0) If Not bReturn Then Debug.WriteLine(GetLastError) End If 'Flush the current IE proxy setting bReturn = InternetSetOption(IntPtr.Zero, Options.INTERNET_OPTION_REFRESH, Nothing, 0) If Not bReturn Then Debug.WriteLine(GetLastError) End If Marshal.FreeHGlobal(opts(1).pszValue) Marshal.FreeHGlobal(opts(2).pszValue) Marshal.FreeCoTaskMem(ptr) Return (bReturn) End Function Public Function DisableProxy() As Boolean Dim bReturn As Boolean Dim list As New INTERNET_PER_CONN_OPTION_LIST Dim dwBufSize As Integer = Marshal.SizeOf(list) Dim opts(0) As INTERNET_PER_CONN_OPTION Dim opt_size As Integer = Marshal.SizeOf(opts(0)) list.dwSize = dwBufSize list.pszConnection = ControlChars.NullChar list.dwOptionCount = 1 opts(0).dwOption = Options.INTERNET_PER_CONN_FLAGS opts(0).dwValue = Options.PROXY_TYPE_DIRECT Dim b(opt_size) As Byte opts(0).GetBytes().CopyTo(b, 0) Dim ptr As IntPtr = Marshal.AllocCoTaskMem(opt_size) Marshal.Copy(b, 0, ptr, opt_size) list.pOptions = ptr 'Set the options on the connection bReturn = InternetSetOption(IntPtr.Zero, Options.INTERNET_OPTION_PER_CONNECTION_OPTION, list, dwBufSize) If Not bReturn Then Debug.WriteLine(GetLastError) End If 'Notify existing Internet Explorer instances that the settings have changed bReturn = InternetSetOption(IntPtr.Zero, Options.INTERNET_OPTION_SETTINGS_CHANGED, Nothing, 0) If Not bReturn Then Debug.WriteLine(GetLastError) End If 'Flush the current IE proxy setting bReturn = InternetSetOption(IntPtr.Zero, Options.INTERNET_OPTION_REFRESH, Nothing, 0) If Not bReturn Then Debug.WriteLine(GetLastError) End If Marshal.FreeCoTaskMem(ptr) Return (bReturn) End Function [/CODE][COLOR=#333333] [/COLOR][CODE][COLOR=#333333]sProxy.SetProxy("proxy:port") 'to start the thing[/COLOR][/CODE][COLOR=#333333] [/COLOR][CODE][COLOR=#333333] sProxy.DisableProxy() 'to stop the thing[/COLOR][/CODE][COLOR=#333333] [/COLOR] [COLOR=#333333]Trust me, I searched and tried a lot of things, but I couldn't get it, that's why I'm asking you guys.[/COLOR] [COLOR=#333333]Thanks![/COLOR] [/QUOTE]
Insert quotes…
Verification
Post reply
Home
Forums
VB.NET
VB.NET General Discussion
How to implement SOCKS 4/5 support in my application?
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.
Accept
Learn more…
Top
Bottom