Skip to content

Commit

Permalink
deps: make socks and serial optional
Browse files Browse the repository at this point in the history
  • Loading branch information
Arusekk committed Aug 20, 2023
1 parent 40c1ef5 commit 24b912f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 13 deletions.
7 changes: 6 additions & 1 deletion pwn/toplevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import os
import platform
import re
import socks
import signal
import string
import struct
Expand Down Expand Up @@ -84,12 +83,18 @@
debug = log.debug
success = log.success

# optional deps
try:
import colored_traceback
except ImportError:
pass
else:
colored_traceback.add_hook()

try:
import socks
except ImportError:
pass

# Equivalence with the default behavior of "from import *"
# __all__ = [x for x in tuple(globals()) if not x.startswith('_')]
4 changes: 2 additions & 2 deletions pwnlib/context/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
import threading
import time

import socks

from pwnlib.config import register_config
from pwnlib.device import Device
from pwnlib.timeout import Timeout
Expand Down Expand Up @@ -1191,6 +1189,8 @@ def proxy(self, proxy):
socket.socket = _original_socket
return None

import socks # keep dependency optional

if isinstance(proxy, str):
proxy = (socks.SOCKS5, proxy)

Expand Down
1 change: 0 additions & 1 deletion pwnlib/protocols/adb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
from pwnlib.util.lists import group
from pwnlib.util.misc import size
from pwnlib.util.packing import p32
from pwnlib.util.proc import pidof
from pwnlib.util.sh_string import sh_string

log = getLogger(__name__)
Expand Down
15 changes: 8 additions & 7 deletions pwnlib/tubes/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from __future__ import division

import socket
import socks

from pwnlib.log import getLogger
from pwnlib.timeout import Timeout
Expand Down Expand Up @@ -97,11 +96,12 @@ def __init__(self, host, port,
self.sock = ssl_context.wrap_socket(self.sock,**ssl_args)

def _connect(self, fam, typ):
sock = None
err = None
sock = None
timeout = self.timeout

with self.waitfor('Opening connection to %s on port %s' % (self.rhost, self.rport)) as h:
for res in socket.getaddrinfo(self.rhost, self.rport, fam, typ, 0, socket.AI_PASSIVE):
for res in socket.getaddrinfo(self.rhost, self.rport, fam, typ):
self.family, self.type, self.proto, _canonname, sockaddr = res

if self.type not in [socket.SOCK_STREAM, socket.SOCK_DGRAM]:
Expand All @@ -119,11 +119,12 @@ def _connect(self, fam, typ):

try:
sock.connect(sockaddr)
err = None # break ref cycle
return sock
except socks.ProxyError:
raise
except socket.error:
pass
except IOError as e:
err = e
if err is not None:
raise err
self.error("Could not connect to %s on port %s", self.rhost, self.rport)

@classmethod
Expand Down
5 changes: 3 additions & 2 deletions pwnlib/tubes/serialtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import sys
import time

import serial

from pwnlib.log import getLogger
from pwnlib.tubes import tube

Expand All @@ -30,6 +28,9 @@ def __init__(
self.convert_newlines = convert_newlines
# serial.Serial might throw an exception, which must be handled
# and propagated accordingly using self.exception

import serial

try:
self.conn = serial.Serial(
port = port,
Expand Down

0 comments on commit 24b912f

Please sign in to comment.