Issue
I want to create a proxy in kotlin with sockets. I created a socket server which connect the client (google.com) to a socket. The http request from Google is:
CONNECT www.google.com:443 HTTP/1.1
Host: www.google.com:443
Proxy-Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36
So I send to the socket:
HTTP/1.1 200 Connection Established
Connection: close
Then Google sends:
; 7����r���9 ����l&�����cO��9���� �.rW�Vs9�T���}�$_aX�ys��&�X "���+�/�,�0̨̩�� � � / 5
��� www.google.com �
zz # hhttp/1.1
3 + )zz
(��>�%܇� �fT5
��5��ue�u��` - +
** ZZ ) � � +c���>L����#9�����'���Ʌͩ�5��h�Bn��O�G)�� ����f�^���S�d|bC�7VT5�:���^�QSRך`MM�g#�[�|\�T�9h@�K:7?��t��49 �;`�ʛ�"��ٹ��9����H�f�@��=�����2���(��T)B�`�#�nhS���!뷊��E�ao����/z)e��}���,>�����G��B�� ! ��OF�p�-�>q
I send these bytes with a new ssl socket to the domain google.com
and the port 443
, but this is not working. The response is:
HTTP/1.0 400 Bad Request
Content-Length: 54
Content-Type: text/html; charset=UTF-8
Date: Sat, 31 Aug 2019 14:16:46 GMT
<html><title>Error 400 (Bad Request)!!1</title></html>
My code:
fun createServerSocket() {
println("Starting Server")
val server_socket = ServerSocket(555)
Thread{
val connection_socket = server_socket.accept()
//connection_socket.soTimeout = 2000
_handleConnectionSocket(connection_socket)
}.start()
}
private fun _handleConnectionSocket(client_socket: Socket) {
Thread{
val client_inputStream = client_socket.getInputStream()
val client_outputStream = client_socket.getOutputStream()
val client_bufferedWriter = BufferedWriter(OutputStreamWriter(client_outputStream))
val ssf = SSLSocketFactory.getDefault() as SSLSocketFactory
val server_socket = ssf.createSocket("google.com", 443)
val server_inputStream = server_socket.getInputStream()
val server_outputStream = server_socket.getOutputStream()
var should_save = false
Thread{
try {
val buffer = ByteArray(4096)
do {
val read = client_inputStream.read(buffer)
if (should_save) {
if (read > 0) {
server_outputStream.write(buffer, 0, read)
if (client_inputStream.available() < 1)
server_outputStream.flush()
}
}
} while (read >= 0)
} catch (e: java.net.SocketTimeoutException) {}
}.start()
Thread.sleep(1000)
should_save = true
val client_success_response =
"""
HTTP/1.1 200 Connection Established
Connection: close
""".trimIndent()
client_bufferedWriter.appendln(client_success_response)
client_bufferedWriter.flush()
Thread.sleep(800)
try {
val buffer = ByteArray(4096)
do {
val read = server_inputStream.read(buffer)
if (read > 0) {
client_outputStream.write(buffer, 0, read)
if (server_inputStream.available() < 1)
client_outputStream.flush()
}
} while (read >= 0)
} catch (e: java.net.SocketTimeoutException) {}
println("Fertig")
}.start()
}
What am I doing wrong?
Solution
Based on the code it looks like you got the idea how CONNECT works wrong. It should work as follows:
- The client (browser) sends a CONNECT request to the proxy which includes the hostname and port it likes to connect to.
- The proxy creates a TCP (not SSL as in your case) connection to the requested server.
- If this connection is successfully established the proxy sends the
HTTP/1.1 200 Connection Established...
response to the browser. - From then on the proxy will simply forward all data between client and server in both directions.
Answered By - Steffen Ullrich Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.