2016-12-01 16:48:04 +01:00
|
|
|
import socket
|
|
|
|
|
2024-05-24 05:42:29 +02:00
|
|
|
from .advanceddispatcher import AdvancedDispatcher
|
|
|
|
from network import asyncore_pollchoose as asyncore
|
|
|
|
from .proxy import ProxyError
|
|
|
|
from .socks5 import Socks5Connection, Socks5Resolver
|
|
|
|
from .socks4a import Socks4aConnection, Socks4aResolver
|
2016-12-01 16:48:04 +01:00
|
|
|
|
2019-09-03 16:26:59 +02:00
|
|
|
|
|
|
|
class HttpError(ProxyError):
|
|
|
|
pass
|
2016-12-01 16:48:04 +01:00
|
|
|
|
|
|
|
|
2017-03-10 23:11:57 +01:00
|
|
|
class HttpConnection(AdvancedDispatcher):
|
2019-09-04 09:34:53 +02:00
|
|
|
def __init__(self, host, path="/"): # pylint: disable=redefined-outer-name
|
2017-03-10 23:11:57 +01:00
|
|
|
AdvancedDispatcher.__init__(self)
|
|
|
|
self.path = path
|
|
|
|
self.destination = (host, 80)
|
|
|
|
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
self.connect(self.destination)
|
2021-02-15 15:23:26 +01:00
|
|
|
print("connecting in background to %s:%i" % self.destination)
|
2016-12-01 16:48:04 +01:00
|
|
|
|
2017-03-10 23:11:57 +01:00
|
|
|
def state_init(self):
|
2019-09-03 16:26:59 +02:00
|
|
|
self.append_write_buf(
|
|
|
|
"GET %s HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n" % (
|
|
|
|
self.path, self.destination[0]))
|
2021-02-15 15:23:26 +01:00
|
|
|
print("Sending %ib" % len(self.write_buf))
|
2017-03-10 23:11:57 +01:00
|
|
|
self.set_state("http_request_sent", 0)
|
|
|
|
return False
|
2016-12-01 16:48:04 +01:00
|
|
|
|
2017-03-10 23:11:57 +01:00
|
|
|
def state_http_request_sent(self):
|
2019-09-04 09:34:53 +02:00
|
|
|
if self.read_buf:
|
2021-02-15 15:23:26 +01:00
|
|
|
print("Received %ib" % len(self.read_buf))
|
2017-03-10 23:11:57 +01:00
|
|
|
self.read_buf = b""
|
|
|
|
if not self.connected:
|
|
|
|
self.set_state("close", 0)
|
|
|
|
return False
|
2016-12-01 16:48:04 +01:00
|
|
|
|
|
|
|
|
2017-03-10 23:11:57 +01:00
|
|
|
class Socks5HttpConnection(Socks5Connection, HttpConnection):
|
2019-09-04 09:34:53 +02:00
|
|
|
def __init__(self, host, path="/"): # pylint: disable=super-init-not-called, redefined-outer-name
|
2017-03-10 23:11:57 +01:00
|
|
|
self.path = path
|
|
|
|
Socks5Connection.__init__(self, address=(host, 80))
|
|
|
|
|
|
|
|
def state_socks_handshake_done(self):
|
|
|
|
HttpConnection.state_init(self)
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
class Socks4aHttpConnection(Socks4aConnection, HttpConnection):
|
2019-09-04 09:34:53 +02:00
|
|
|
def __init__(self, host, path="/"): # pylint: disable=super-init-not-called, redefined-outer-name
|
2017-03-10 23:11:57 +01:00
|
|
|
Socks4aConnection.__init__(self, address=(host, 80))
|
|
|
|
self.path = path
|
|
|
|
|
|
|
|
def state_socks_handshake_done(self):
|
|
|
|
HttpConnection.state_init(self)
|
|
|
|
return False
|
|
|
|
|
2016-12-01 16:48:04 +01:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
# initial fill
|
2017-03-10 23:11:57 +01:00
|
|
|
for host in ("bootstrap8080.bitmessage.org", "bootstrap8444.bitmessage.org"):
|
|
|
|
proxy = Socks5Resolver(host=host)
|
2019-09-04 09:34:53 +02:00
|
|
|
while asyncore.socket_map:
|
2021-02-15 15:23:26 +01:00
|
|
|
print("loop %s, len %i" % (proxy.state, len(asyncore.socket_map)))
|
2017-03-10 23:11:57 +01:00
|
|
|
asyncore.loop(timeout=1, count=1)
|
|
|
|
proxy.resolved()
|
|
|
|
|
|
|
|
proxy = Socks4aResolver(host=host)
|
2019-09-04 09:34:53 +02:00
|
|
|
while asyncore.socket_map:
|
2021-02-15 15:23:26 +01:00
|
|
|
print("loop %s, len %i" % (proxy.state, len(asyncore.socket_map)))
|
2017-03-10 23:11:57 +01:00
|
|
|
asyncore.loop(timeout=1, count=1)
|
|
|
|
proxy.resolved()
|
|
|
|
|
|
|
|
for host in ("bitmessage.org",):
|
|
|
|
direct = HttpConnection(host)
|
2019-09-04 09:34:53 +02:00
|
|
|
while asyncore.socket_map:
|
2019-09-03 16:26:59 +02:00
|
|
|
# print "loop, state = %s" % (direct.state)
|
2017-03-10 23:11:57 +01:00
|
|
|
asyncore.loop(timeout=1, count=1)
|
|
|
|
|
|
|
|
proxy = Socks5HttpConnection(host)
|
2019-09-04 09:34:53 +02:00
|
|
|
while asyncore.socket_map:
|
2019-09-03 16:26:59 +02:00
|
|
|
# print "loop, state = %s" % (proxy.state)
|
2017-03-10 23:11:57 +01:00
|
|
|
asyncore.loop(timeout=1, count=1)
|
|
|
|
|
|
|
|
proxy = Socks4aHttpConnection(host)
|
2019-09-04 09:34:53 +02:00
|
|
|
while asyncore.socket_map:
|
2019-09-03 16:26:59 +02:00
|
|
|
# print "loop, state = %s" % (proxy.state)
|
2017-03-10 23:11:57 +01:00
|
|
|
asyncore.loop(timeout=1, count=1)
|