Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 24b912f

Browse files
committedAug 20, 2023
deps: make socks and serial optional
1 parent 40c1ef5 commit 24b912f

File tree

5 files changed

+19
-13
lines changed

5 files changed

+19
-13
lines changed
 

‎pwn/toplevel.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import os
77
import platform
88
import re
9-
import socks
109
import signal
1110
import string
1211
import struct
@@ -84,12 +83,18 @@
8483
debug = log.debug
8584
success = log.success
8685

86+
# optional deps
8787
try:
8888
import colored_traceback
8989
except ImportError:
9090
pass
9191
else:
9292
colored_traceback.add_hook()
9393

94+
try:
95+
import socks
96+
except ImportError:
97+
pass
98+
9499
# Equivalence with the default behavior of "from import *"
95100
# __all__ = [x for x in tuple(globals()) if not x.startswith('_')]

‎pwnlib/context/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
import threading
2424
import time
2525

26-
import socks
27-
2826
from pwnlib.config import register_config
2927
from pwnlib.device import Device
3028
from pwnlib.timeout import Timeout
@@ -1191,6 +1189,8 @@ def proxy(self, proxy):
11911189
socket.socket = _original_socket
11921190
return None
11931191

1192+
import socks # keep dependency optional
1193+
11941194
if isinstance(proxy, str):
11951195
proxy = (socks.SOCKS5, proxy)
11961196

‎pwnlib/protocols/adb/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
from pwnlib.util.lists import group
2424
from pwnlib.util.misc import size
2525
from pwnlib.util.packing import p32
26-
from pwnlib.util.proc import pidof
2726
from pwnlib.util.sh_string import sh_string
2827

2928
log = getLogger(__name__)

‎pwnlib/tubes/remote.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from __future__ import division
33

44
import socket
5-
import socks
65

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

9998
def _connect(self, fam, typ):
100-
sock = None
99+
err = None
100+
sock = None
101101
timeout = self.timeout
102102

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

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

120120
try:
121121
sock.connect(sockaddr)
122+
err = None # break ref cycle
122123
return sock
123-
except socks.ProxyError:
124-
raise
125-
except socket.error:
126-
pass
124+
except IOError as e:
125+
err = e
126+
if err is not None:
127+
raise err
127128
self.error("Could not connect to %s on port %s", self.rhost, self.rport)
128129

129130
@classmethod

‎pwnlib/tubes/serialtube.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import sys
77
import time
88

9-
import serial
10-
119
from pwnlib.log import getLogger
1210
from pwnlib.tubes import tube
1311

@@ -30,6 +28,9 @@ def __init__(
3028
self.convert_newlines = convert_newlines
3129
# serial.Serial might throw an exception, which must be handled
3230
# and propagated accordingly using self.exception
31+
32+
import serial
33+
3334
try:
3435
self.conn = serial.Serial(
3536
port = port,

0 commit comments

Comments
 (0)
Please sign in to comment.