Skip to content

Commit d2159c3

Browse files
committed
4.13.5 fix crash issue.
1 parent 0161116 commit d2159c3

File tree

7 files changed

+251
-71
lines changed

7 files changed

+251
-71
lines changed

code/default/launcher/download_modules.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ def download_worker():
128128
if not os.path.isdir(switchyomega_path):
129129
return
130130

131+
time.sleep(150)
131132
sha256_fn = os.path.join(switchyomega_path, "Sha256.txt")
132133
download_file("https://raw.githubusercontent.com/XX-net/XX-Net/master/SwitchyOmega/Sha256.txt", sha256_fn)
133134
sha256_dict = get_sha256(sha256_fn)
@@ -138,7 +139,6 @@ def download_worker():
138139

139140

140141
def start_download():
141-
time.sleep(150)
142142
th = threading.Thread(target=download_worker)
143143
th.start()
144144
return True

code/default/launcher/start.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def exit_handler():
9393

9494
atexit.register(exit_handler)
9595

96-
has_desktop = True
96+
has_desktop = sys_platform.has_desktop
9797

9898

9999
def main():

code/default/lib/noarch/front_base/boringssl_wrap.py

+94-64
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55

66
import socket
7+
import threading
78

89
import utils
910

@@ -14,6 +15,7 @@ class SSLConnection(object):
1415
BIO_CLOSE = 1
1516

1617
def __init__(self, context, sock, ip_str=None, sni=None, on_close=None):
18+
self._lock = threading.Lock()
1719
self._context = context
1820
self._sock = sock
1921
self.ip_str = utils.to_bytes(ip_str)
@@ -66,6 +68,9 @@ def wrap(self):
6668
raise socket.error("SSL_connect fail: %s" % error)
6769

6870
def do_handshake(self):
71+
if not self._connection:
72+
raise socket.error("do_handshake fail: not connected")
73+
6974
ret = bssl.SSL_do_handshake(self._connection)
7075
if ret == 1:
7176
return
@@ -74,6 +79,9 @@ def do_handshake(self):
7479
raise socket.error("do_handshake fail: %s" % error)
7580

7681
def is_support_h2(self):
82+
if not self._connection:
83+
return False
84+
7785
out_data_pp = ffi.new("uint8_t**", ffi.NULL)
7886
out_len_p = ffi.new("unsigned*")
7987
bssl.SSL_get0_alpn_selected(self._connection, out_data_pp, out_len_p)
@@ -90,21 +98,13 @@ def setblocking(self, block):
9098
self._sock.setblocking(block)
9199

92100
def __getattr__(self, attr):
93-
if attr == "socket_closed":
94-
# work around in case close before finished init.
95-
return True
96-
97-
elif attr in ('is_support_h2', "_on_close", '_context', '_sock', '_connection', '_makefile_refs',
101+
if attr in ('is_support_h2', "_on_close", '_context', '_sock', '_connection', '_makefile_refs',
98102
'sni', 'wrap', 'socket_closed'):
99103
return getattr(self, attr)
100104

101105
elif hasattr(self._connection, attr):
102106
return getattr(self._connection, attr)
103107

104-
def __del__(self):
105-
if not self.socket_closed and self._connection:
106-
self.close()
107-
108108
def get_cert(self):
109109
if self.peer_cert:
110110
return self.peer_cert
@@ -113,25 +113,27 @@ def x509_name_to_string(xname):
113113
line = bssl.X509_NAME_oneline(xname, ffi.NULL, 0)
114114
return ffi.string(line)
115115

116-
try:
117-
cert = bssl.SSL_get_peer_certificate(self._connection)
118-
if cert == ffi.NULL:
119-
raise Exception("get cert failed")
116+
with self._lock:
117+
if self._connection:
118+
try:
119+
cert = bssl.SSL_get_peer_certificate(self._connection)
120+
if cert == ffi.NULL:
121+
raise Exception("get cert failed")
120122

121-
alt_names_p = bssl.get_alt_names(cert)
122-
if alt_names_p == ffi.NULL:
123-
raise Exception("get alt_names failed")
123+
alt_names_p = bssl.get_alt_names(cert)
124+
if alt_names_p == ffi.NULL:
125+
raise Exception("get alt_names failed")
124126

125-
alt_names = utils.to_str(ffi.string(alt_names_p))
126-
bssl.free(alt_names_p)
127+
alt_names = utils.to_str(ffi.string(alt_names_p))
128+
bssl.free(alt_names_p)
127129

128-
subject = x509_name_to_string(bssl.X509_get_subject_name(cert))
129-
issuer = x509_name_to_string(bssl.X509_get_issuer_name(cert))
130-
altName = alt_names.split(";")
131-
except Exception as e:
132-
subject = ""
133-
issuer = ""
134-
altName = []
130+
subject = x509_name_to_string(bssl.X509_get_subject_name(cert))
131+
issuer = x509_name_to_string(bssl.X509_get_issuer_name(cert))
132+
altName = alt_names.split(";")
133+
except Exception as e:
134+
subject = ""
135+
issuer = ""
136+
altName = []
135137

136138
self.peer_cert = {
137139
"cert": subject,
@@ -143,40 +145,66 @@ def x509_name_to_string(xname):
143145
return self.peer_cert
144146

145147
def send(self, data, flags=0):
146-
try:
147-
ret = bssl.SSL_write(self._connection, data, len(data))
148-
return ret
149-
except Exception as e:
150-
self._context.logger.exception("ssl send:%r", e)
151-
raise e
148+
with self._lock:
149+
if not self._connection:
150+
e = socket.error(5)
151+
e.errno = 5
152+
raise e
153+
154+
try:
155+
ret = bssl.SSL_write(self._connection, data, len(data))
156+
if ret <= 0:
157+
errno = bssl.SSL_get_error(self._connection, ret)
158+
self._context.logger.warn("send n:%d errno: %d ip:%s", ret, errno, self.ip_str)
159+
e = socket.error(2)
160+
e.errno = errno
161+
raise e
162+
163+
return ret
164+
except Exception as e:
165+
self._context.logger.exception("ssl send:%r", e)
166+
raise e
152167

153168
def recv(self, bufsiz, flags=0):
154-
buf = bytes(bufsiz)
155-
n = bssl.SSL_read(self._connection, buf, bufsiz)
156-
if n <= 0:
157-
errno = bssl.SSL_get_error(self._connection, n)
158-
self._context.logger.warn("recv errno: %d ip:%s", errno, self.ip_str)
159-
e = socket.error(2)
160-
e.errno = errno
161-
raise e
162-
163-
dat = buf[:n]
164-
return dat
169+
with self._lock:
170+
if not self._connection:
171+
e = socket.error(2)
172+
e.errno = 5
173+
raise e
174+
175+
buf = bytes(bufsiz)
176+
n = bssl.SSL_read(self._connection, buf, bufsiz)
177+
if n <= 0:
178+
errno = bssl.SSL_get_error(self._connection, n)
179+
self._context.logger.warn("recv n:%d errno: %d ip:%s", n, errno, self.ip_str)
180+
e = socket.error(2)
181+
e.errno = errno
182+
raise e
183+
184+
dat = buf[:n]
185+
self._context.logger.debug("recv %d", n)
186+
return dat
165187

166188
def recv_into(self, buf, nbytes=None):
167-
if not nbytes:
168-
nbytes = len(buf)
169-
170-
b = ffi.from_buffer(buf)
171-
n = bssl.SSL_read(self._connection, b, nbytes)
172-
if n <= 0:
173-
errno = bssl.SSL_get_error(self._connection, n)
174-
self._context.logger.warn("recv_into errno: %d ip:%s", errno, self.ip_str)
175-
e = socket.error(2)
176-
e.errno = errno
177-
raise e
178-
179-
return n
189+
with self._lock:
190+
if not self._connection:
191+
e = socket.error(2)
192+
e.errno = 5
193+
raise e
194+
195+
if not nbytes:
196+
nbytes = len(buf)
197+
buf_new = bytes(nbytes)
198+
199+
n = bssl.SSL_read(self._connection, buf_new, nbytes)
200+
if n <= 0:
201+
errno = bssl.SSL_get_error(self._connection, n)
202+
e = socket.error(2)
203+
e.errno = errno
204+
raise e
205+
206+
buf[:n] = buf_new[:n]
207+
return n
180208

181209
def read(self, bufsiz, flags=0):
182210
return self.recv(bufsiz, flags)
@@ -185,27 +213,29 @@ def write(self, buf, flags=0):
185213
return self.send(buf, flags)
186214

187215
def close(self):
188-
if self._makefile_refs < 1:
216+
with self._lock:
189217
self.running = False
190218
if not self.socket_closed:
219+
if self._connection:
220+
bssl.SSL_shutdown(self._connection)
191221

192-
bssl.SSL_shutdown(self._connection)
193-
bssl.SSL_free(self._connection)
194-
self._connection = None
195-
196-
self._sock = None
197222
self.socket_closed = True
198223
if self._on_close:
199224
self._on_close(self.ip_str)
200-
else:
201-
self._makefile_refs -= 1
225+
226+
def __del__(self):
227+
self.close()
228+
if self._connection:
229+
bssl.SSL_free(self._connection)
230+
self._connection = None
231+
self._sock = None
202232

203233
def settimeout(self, t):
204234
if not self.running:
205235
return
206236

207237
if self.timeout != t:
208-
# self._sock.settimeout(t)
238+
self._sock.settimeout(t)
209239
self.timeout = t
210240

211241
def makefile(self, mode='r', bufsize=-1):

code/default/lib/tests/stress_boringssl.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
logger = xlog.getLogger("stress")
3232

3333
from front_base.openssl_wrap import SSLContext
34-
from front_base.host_manager import HostManagerBase
3534
from front_base.connect_creator import ConnectCreator
3635
from front_base.check_ip import CheckIp
3736

@@ -61,4 +60,11 @@ def round():
6160
front.stop()
6261

6362

64-
round()
63+
def loop():
64+
while True:
65+
round()
66+
# time.sleep(1)
67+
68+
69+
loop()
70+

0 commit comments

Comments
 (0)