K
K
KPEBETKA2014-01-20 22:13:47
Python
KPEBETKA, 2014-01-20 22:13:47

Python.SSLSocket. How to get the hostname that the browser has accessed?

Everything is quite simple:
There are many entries in the /etc/hosts file like
0.0.0.0 mail.ru
0.0.0.0 yandex.ru
0.0.0.0 google.ru
0.0.0.0 yahoo.com
...
I.e. any browser, when accessing these addresses, will be redirected to ip 0.0.0.0
On ip 0.0.0.0 on port 443, a kind of server hangs, which, when accessed, should return a GIF 1x1 pixel in size.
Because everything happens over https, then in order to return the pixel normally, the server must generate a certificate. Actually, in order to generate a certificate, you need to know the host name to which the browser addressed

. Accordingly, this is the problem ... I can’t find out which host the browser turned to. Is it really possible to know this?

#!/usr/bin/env python

import socket, ssl

s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('0.0.0.0', 443))
s.listen(0)

while True:
  conn, addr = s.accept()

  #тут надо получить имя хоста )))

  gen_cert_key(host)
  ss = ssl.wrap_socket(conn, 
        server_side=True, 
        certfile="/tmp/cert.crt", 
        keyfile="/tmp/key.key")
  ss.send("HTTP/1.1 200 OK\r\nContent-type: image/gif\r\nAccept-ranges: none\r\nContent-length: 43\r\nConnection: close\r\n\r\nGIF89a\1\0\1\0\200\0\0\377\377\377\0\0\0\41\371\4\1\0\0\0\0\54\0\0\0\0\1\0\1\0\0\2\2\104\1\0;")
  ss.close()
  conn.close()
s.close()

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question