Reece Griffin Reece Griffin - 6 months ago 13
Vb.net Question

I found a proxy example online but it's throwing an exception what could be wrong?

I found a proxy server example online while searching and well i was really happy it was broken but i managed to fix the really simple problems and then i hit the biggest problem, Because i didn't make this program i don't really know how it all works so when i got this error i was pretty much baffled.

Proxy Server Code

Alright the problem is from the line that says

sURL = part1.Substring(index1 + 4, part1.Length - index5 - 8)


That gives me this error when someone connects

System.ArgumentOutOfRangeException {"Length cannot be less than zero Parameter name: length"}


Does anyone know what the problem could be, And if so could you please explain the cause behind this?

Thanks.

here is clientmessage

"CONNECT googleads.g.doubleclick.net:443 HTTP/1.1" & vbCrLf & "Host: googleads.g.doubleclick.net:443" & vbCrLf & "Proxy-Connection: keep-alive" & vbCrLf & "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36" & vbCrLf & "" & vbCrLf & ""

Answer

So - this bit:

Dim index1 As Integer = clientmessage.IndexOf(" "c)
Dim index2 As Integer = clientmessage.IndexOf(" "c, index1 + 1)
If index1 = -1 Or index2 = -1 Then
    Stop
End If
Console.WriteLine("Connecting to Site: {0}", clientmessage.Substring(index1 + 1, index2 - index1))
Console.WriteLine("Connection from {0}", clientSocket.RemoteEndPoint)
Dim part1 As String = clientmessage.Substring(index1 + 1, index2 - index1)

Just isolates the googleads.g.doubleclick.net:443 part of the string.

The next section is problematic for two reasons. Firstly, it uses a 'magic number' i.e. 8 which maybe worked in the original code but is now is a bit opaque. Secondly, the line for index3 tries to find a / which may or may not exist in the string.

Perhaps the original programmer was always dealing with some complex URL but that's not a problem for you and therefore the code is giving an exception because the combination of indexes used trying to assign sURL is basically invalid:

Dim index3 As Integer = part1.IndexOf("/"c, index1 + 8)
Dim index4 As Integer = part1.IndexOf(" "c, index1 + 8)
Dim index5 As Integer = index4 - index3
sURL = part1.Substring(index1 + 4, part1.Length - index5 - 8)

Since you only need to strip off the port to get the host name, then you could try commenting out those four lines above and replacing with:

sURL = part1.Substring(0, part1.LastIndexOf(":"))

That's more guesswork than answer but I couldn't write it all in a comment!

Comments