Commit 635276e8 authored by Matt Traudt's avatar Matt Traudt Committed by Matt Traudt
Browse files

Move some functions so other code can use them

parent 82fb154f
......@@ -9,15 +9,15 @@ from ..lib.relaylist import RelayList
from ..lib.relayprioritizer import RelayPrioritizer
from ..lib.helperrelay import HelperRelayList
from ..util.simpleauth import authenticate_to_server
from ..util.sockio import (make_socket, close_socket, socket_connect)
from sbws.globals import (fail_hard, is_initted, time_now)
from sbws.globals import (MIN_REQ_BYTES, MAX_REQ_BYTES, SOCKET_TIMEOUT)
from sbws.globals import (MIN_REQ_BYTES, MAX_REQ_BYTES)
import sbws.util.stem as stem_utils
from stem.control import EventType
from argparse import ArgumentDefaultsHelpFormatter
from multiprocessing.dummy import Pool
from threading import Event
from threading import RLock
import socks
import socket
import time
import os
......@@ -28,37 +28,6 @@ stream_building_lock = RLock()
log = logging.getLogger(__name__)
def make_socket(socks_host, socks_port):
''' Make a socket that uses the provided socks5 proxy. Note at this point
the socket hasn't connect()ed anywhere '''
s = socks.socksocket()
s.set_proxy(socks.PROXY_TYPE_SOCKS5, socks_host, socks_port)
s.settimeout(SOCKET_TIMEOUT)
return s
def close_socket(s):
''' Close the socket, and ignore errors '''
try:
s.shutdown(socket.SHUT_RDWR)
s.close()
except Exception:
pass
def socket_connect(s, addr, port):
''' connect() to addr:port on the given socket. Unknown compatibility with
IPv6. Works with IPv4 and hostnames '''
try:
s.connect((addr, port))
log.debug('Connected to %s:%d via %d', addr, port, s.fileno())
except (socket.timeout, socks.GeneralProxyError,
socks.ProxyConnectionError) as e:
log.warning(e)
return False
return True
def tell_server_amount(sock, expected_amount):
''' Returns True on success; else False '''
assert expected_amount >= MIN_REQ_BYTES
......
from sbws.globals import SOCKET_TIMEOUT
import socket
import socks
import logging
log = logging.getLogger(__name__)
......@@ -45,3 +47,47 @@ def read_line(s, max_len=None):
if max_len is not None and len(chars) >= max_len:
return chars[0:max_len]
return chars
def make_socket(socks_host, socks_port):
'''
Make a socket that uses the provided socks5 proxy. Note at this point
the socket hasn't ``connect()``ed anywhere.
:param str socks_host: IP address or hostname of the socks5 proxy to use
:param int socks_port: Port of the socks5 proxy to use
:returns: A socket ready to ``connect()`` somewhere
'''
s = socks.socksocket()
s.set_proxy(socks.PROXY_TYPE_SOCKS5, socks_host, socks_port)
s.settimeout(SOCKET_TIMEOUT)
return s
def close_socket(s):
''' Close the socket, and ignore any errors. '''
try:
s.shutdown(socket.SHUT_RDWR)
s.close()
except Exception:
pass
def socket_connect(s, addr, port):
'''
``connect()`` to addr:port on the given socket. **addr** can be a hostname,
IPv4, or IPv6 address.
:param socket s: Socket, possibly through a socks5 proxy
:param str addr: host to connect to
:param int port: port to connect to
:returns: True if connect was successful, otherwise False.
'''
try:
s.connect((addr, port))
log.debug('Connected to %s:%d via %d', addr, port, s.fileno())
except (socket.timeout, socks.GeneralProxyError,
socks.ProxyConnectionError) as e:
log.warning(e)
return False
return True
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment