Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello friends,

I am in the middle of a project and i am stock somewhere where i need help. I was given a program (proxy tunnelling program) written in java and i was told to re-write it using vb.net and python.

What the program does is that it the work of a proxy server. The program gets request from browser (e.g Mozilla Firefox), foward the request header and request body to a specified IP address, read the response and send the response back to the browser. I am through with the http part of the code i.e any HTTP request are handled successfully without any error. But when it comes to HTTPS request that issues the CONNECT method in the http header, things get more difficult in this area.

This is the problem I am facing:
After i receive the CONNECT header from the browser, i send back HTTP/1.1 200 OK response back to the browser and read the next statement from the browser. Now this statement is what is giving me problem. The statement is full of wierd characters, characters that does not even exist on the keyboard. A little explanation of my code are as follows:

'Import necessary namespaces
Imports System.Net.Security
Imports System.Net.Sockets
Imports System.Text
Imports System.Net

Module Module1

    Sub Main()
   'start a listener
        Dim ss As TcpListener = New TcpListener(IPAddress.Any, 6053)
        ss.Start()
        Dim cs As TcpClient
   
   'accept pending client
        cs = ss.AcceptTcpClient()
        Dim nw As NetworkStream
        nw = cs.GetStream()
        Dim data(1024) As Byte
        Dim msg As String = ""
        Dim count As Integer
        While True
       'Read data from browser
       'Here, i got the CONNECT header
            count = nw.Read(data, 0, data.Length)
            msg = msg & Encoding.ASCII.GetString(data, 0, count)
            If msg.IndexOf(vbNewLine & vbNewLine) <> -1 Then
                Exit While
            End If
        End While

        Console.WriteLine(msg)

   'Prepare response string
        Dim res As String
        res = "HTTP/1.1 200 OK" & vbNewLine & _
        "Proxy-Agent: Mozilla" & vbNewLine & _
        "Connection: Keep-Alive" & vbNewLine & vbNewLine

        Dim bRes As Byte() = Encoding.ASCII.GetBytes(res)
   'Send HTTP/1.1 200 OK back to browser
        nw.Write(bRes, 0, bRes.Length)
        nw.Flush()

        msg = ""
        While True
       'Reading the next request from the browser
       'Here, i got the wierd character from the browser
            count = nw.Read(data, 0, data.Length)
            msg = msg & Encoding.ASCII.GetString(data, 0, count)
            If count < 1024 Then
                Exit While
            End If
        End While
    End Sub

End Module


I went ahead and i did some research and i discover that i need a certificate. In my research, i discovered that the requsest i received from the browser is encrypted that is why it is displaying wierd characters. When translating the java sll part of the code to vb.net i noticed something which i did not take into consideration. I notice that there is a line of code that talks about certificate issues which i ignored. The lines are:

String KEYSTORE_PATH = new File("").getAbsolutePath()+ File.separator + "ProxyCertificate.ser";
String KEYSTORE_PASSWORD = "password";

// Set SSL Certificate .
System.setProperty("javax.net.ssl.keyStore",KEYSTORE_PATH);
System.setProperty("javax.net.ssl.keyStorePassword",KEYSTORE_PASSWORD);

//The proxyCertificate.ser file exists in the application startup path


I guess these lines are causing the problem which means, i need to add the path to the proxyCertificate.ser file to my code and its password. Now this is where the question arises.

The system.setProperty() method does not exist in vb.net and this is the method that is used in the java code to add the certificate.ser file to their code. Now what method am i going to use to add the proxyCertificate.ser file and its password to my code?
The certificate code is placed at the top of the code before starting server socket port in the java code.
What method or what way am i going to use to add these lines of certificate code to my vb.net code. Hope you understand my question!

Thanks in advance.
I went through some other research and i got the info that i need to put my certificate in something called X509CertificateCollection and send it along with the HTTP 200 OK response header which i sent back to browser. These things are getting more complicated. Pls help me, i dont know what to do again.

@shakryukuv thanks for the info. My bad....
Posted
Updated 24-Nov-11 21:10pm
v2
Comments
Sergey Alexandrovich Kryukov 24-Nov-11 16:09pm    
OP added comment:

help me! I went through some other research and i got the info that i need to send the certificate to the browser along with the HTTP 200 OK i sent. How can i do this? Pls help
Sergey Alexandrovich Kryukov 24-Nov-11 16:10pm    
Please don't post texts like that as a solution. Use "Improve question", "Have a Question or Comment", reply to existing comment. Those non-solutions will be removed, no one gets e-mail notification (except yourself).
--SA

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