Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
VB
Imports System.Text
Imports System.Net.Sockets
Imports System.Net
Imports System.Runtime.InteropServices
Imports System.IO


Public Class Form2
    Dim cln As TcpClient
    <StructLayout(LayoutKind.Sequential)> _
    Public Structure MARGINS
        Public cxLeftWidth As Integer
        Public cxRightWidth As Integer
        Public cyTopHeight As Integer
        Public cyButtomheight As Integer
    End Structure
    <DllImport("dwmapi.dll")> _
    Private Shared Function DwmExtendFrameIntoClientArea(ByVal hwnd As IntPtr, ByRef margin As MARGINS) As Integer
    End Function

    Dim WithEvents Svr As TcpListener
    Dim threadListen As Threading.Thread
    Dim threadCount As Integer
    Dim server_injek_ip As String = "10.199.212.2"
    Dim server_injek_port = 8080
    Dim tid As Integer = 0
    'Dim endHeader() As Byte = {13, 13}
    'Const cr = 13, lf = 10, bufZ = 1024, maxloop = 20
    Dim thrC As New List(Of Threading.Thread), thrS As New List(Of Threading.Thread)

    Dim injectXLStr As String = "Get http://www.4shared.com/ HTTP/1.1" & vbCr & _
     "Host:meonline.com" & vbCr & vbCr
    Dim InjectXLByte As Byte()

    Const about = "wolfsoftonline.com"


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Button1.Text = "start" Then
            Button1.Text = "stop"
            Timer1.Enabled = True
            Dim ipc As Byte() = New Byte() {127, 0, 0, 1}

            Dim ip = New System.Net.IPEndPoint(New System.Net.IPAddress(ipc), TextBox1.Text)
            Svr = New TcpListener(ip)
            Try
                Svr.Start()
                threadListen = New Threading.Thread(AddressOf StartListen)
                threadListen.Start()
            Catch ex As Exception
                MsgBox("Error: " & ex.Message)
            End Try

        Else
            Button1.Text = "start"
            Timer1.Enabled = False
            Try
                Svr.Stop()
            Catch ex As Exception
            End Try

            For Each t As Threading.Thread In thrC
                Try
                    If t.IsAlive Then t.Abort()
                Catch ex As Exception

                End Try
            Next
            For Each t As Threading.Thread In thrS
                Try
                    If t.IsAlive Then t.Abort()
                Catch ex As Exception
                End Try
            Next
        End If
    End Sub

    Private Sub StartListen()
        Debug.Print("[Thread Listen] START - " & threadCount)
        While True
            Try
                Dim tc As TcpClient = Svr.AcceptTcpClient
                Dim thr As New Threading.Thread(AddressOf requestHandler)
                Dim write As New StreamReader(tc.GetStream)

                thr.Start(tc)
                Debug.Print("[Thread Listen] Accepted : " & tc.ToString)
            Catch ex As Exception
                Debug.Print("[Thread Listen] Exception: " & ex.Message)
                Exit While
            End Try
        End While
        Debug.Print("[Thread Listen] END")
    End Sub

    Private Sub requestHandler()

        Dim sck As Socket = konekProxyServer()
        If sck Is Nothing Then
            cln.Close()
            Console.WriteLine("  [req] END THREAD " & threadCount & " Ngga konek ke ProxXL")
        ElseIf sck.Connected Then
            Dim t As New Threading.Thread(AddressOf transferClientToServer)

            t.Start(New Object() {cln, sck, thrC.Count})
            thrC.Add(t)
            t = New Threading.Thread(AddressOf transferServerToClient)
            t.Start(New Object() {cln, sck, thrS.Count})
            thrS.Add(t)
        Else
            cln.Close()
        End If
    End Sub

    Private Sub transferClientToServer(ByVal param As Object())
        Dim cln As TcpClient = param(0)
        Dim sck As Socket = param(1)
        Dim id As Integer = param(2)
        'transfer dari client, ke proxy server
        Debug.Print("C >--> S {" & id & "}")
        Dim recv As Integer = 0
        Dim terus = True
        Dim pos = 0, pos1 = 0, btSent = 0
        Dim res As String = ""
        Do
            Try
                Do
                    Dim b(cln.SendBufferSize) As Byte
                    recv = cln.GetStream.Read(b, 0, b.Length)
                    sck.Send(b, recv, SocketFlags.None)
                    'Debug.Print("C >--> S {" & id & "}: sent " & recv & " bytes")
                    pos += 1
                    btSent += recv
                    If recv < 2 Then
                        'Debug.Print("C -<- S {" & id & "}: Small Packed, exit!")
                        If Not cln Is Nothing Then
                            If cln.Connected Then cln.Close()
                        End If
                        Exit Do
                    End If
                Loop While cln.GetStream.DataAvailable

            Catch ex As Exception
                'Debug.Print("C >--> S {" & id & "}: END ERROR " & ex.Message)
                If cln.Connected Then cln.Close()
                Exit Do
            End Try

        Loop While terus And cln.Connected And sck.Connected
        Try
            If Not thrS(id) Is Nothing Then
                If thrS(id).IsAlive Then thrS(id).Abort()
            End If
            If cln.Connected Then cln.Close()
        Catch ex As Exception

        End Try
        Try
            If Not sck Is Nothing Then
                If sck.Connected Then sck.Close()
            End If
        Catch ex As Exception

        End Try
        'Debug.Print("C >--> S {" & id & "}: END, Total " & btSent & " bytes, loop " & pos)
    End Sub

    Private Sub transferServerToClient(ByVal param As Object())
        Dim cln As TcpClient = param(0)
        Dim sck As Socket = param(1)
        Dim id As Integer = param(2)
        Debug.Print("C -<- S {" & id & "}")
        Dim pos = 0, pos1 = 0, btRecv = 0
        Do
            Try
                Dim res As String = ""
                Do
                    pos1 += 1
                    Dim b(sck.ReceiveBufferSize) As Byte
                    Dim recv = sck.Receive(b)
                    cln.GetStream.Write(b, 0, recv)

                    btRecv += recv
                    'Debug.Print("C -<- S {" & id & "}: Received " & recv & " bytes")
                    If recv < 2 Then
                        Debug.Print("C -<- S {" & id & "}: Small Packed, exit!")
                        If Not sck Is Nothing Then
                            If sck.Connected Then sck.Close()
                        End If
                        Exit Do
                    End If
                Loop While sck.Available > 0

            Catch ex As Exception
                Debug.Print("C -<- S {" & id & "}: END ERROR " & ex.Message)
                If sck.Connected Then sck.Close()
                Exit Do
            End Try

        Loop While cln.Connected And sck.Connected

        Try
            If Not thrC(id) Is Nothing Then
                If thrC(id).IsAlive Then thrC(id).Abort()
            End If
            If Not cln Is Nothing Then
                If cln.Connected Then cln.Close()
            End If
            If sck.Connected Then sck.Close()
        Catch ex As Exception

        End Try
        'Debug.Print("C -<- S {" & id & "}: END ,total received " & btRecv & " bytes")


    End Sub

    Private Function konekProxyServer() As Socket

        'Konek ke server injek & kirim header injeknya,, returnnya adalah koneksi socket yg siap transfer data
        Dim sck As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        Dim bytesRec = 0
        Try

            sck.Connect(server_injek_ip, server_injek_port)
           
            ''   sck.Send(out)

            Dim bytes(sck.ReceiveBufferSize) As Byte
            bytesRec = sck.Receive(bytes)
            Debug.Print("[RECV XL-PROXY] " & bytesRec & " + " & sck.Available & " ***************")
            If bytesRec <= 2 Then
                sck.Close()
                sck = Nothing
            End If
        Catch ex As Exception
            Debug.WriteLine(" Gagal Konek server, coba lagi. : " & ex.Message)
            If sck.Connected Then sck.Close()
            sck = Nothing
        End Try
        Return sck

    End Function


    Private Sub Form2_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
        End
    End Sub

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.BackColor = Color.Black
        Dim mg As MARGINS = New MARGINS
        mg.cxLeftWidth = -1
        mg.cxRightWidth = -1
        mg.cyTopHeight = -1
        mg.cyButtomheight = -1
        Timer1.Enabled = False
        'set all value -1 to apply glass effect to the all of visible window
        Try
            DwmExtendFrameIntoClientArea(Me.Handle, mg)
        Catch ex As Exception
        End Try
    End Sub
    Dim massage As String

    Dim out As Byte = massage

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        If Svr.Pending = True Then
            massage = " "
            cln = Svr.AcceptTcpClient
            Dim write As New StreamReader(cln.GetStream)
            While write.Peek > -1
                massage = massage + Convert.ToChar(write.Read()).ToString
            End While
            TextBox2.AppendText(massage)
        End If


    End Sub

End Class
Posted
Updated 17-Feb-15 4:37am
v2
Comments
Richard MacCutchan 17-Feb-15 11:50am    
What does this mean?
Wolfsoftonline 18-Feb-15 5:53am    
i want my proxy to use a Virtual network streaming all my connection to the internet through it
Richard MacCutchan 18-Feb-15 6:24am    
Saying "I want ...", is not giving us any clue as to what your problem is. You really need to read http://www.codeproject.com/KB/FAQs/QuickAnswersFAQ.aspx, and learn how to ask a question properly.
Wolfsoftonline 18-Feb-15 6:48am    
my problem is that i dont have a clue on how to route all my connections my proxy to virtual network Adapter
Richard MacCutchan 18-Feb-15 11:14am    
That is a networking issue. I suggest you do some research into it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900