use new netlib exceptions

This commit is contained in:
Maximilian Hils 2015-09-17 02:13:42 +02:00
parent a7291a7e78
commit f61109db84
7 changed files with 27 additions and 24 deletions

View File

@ -1,4 +1,5 @@
import time import time
from netlib.exceptions import TcpDisconnect
import netlib.tcp import netlib.tcp
BLOCKSIZE = 1024 BLOCKSIZE = 1024
@ -62,5 +63,5 @@ def write_values(fp, vals, actions, sofar=0, blocksize=BLOCKSIZE):
return True return True
elif a[1] == "inject": elif a[1] == "inject":
send_chunk(fp, a[2], blocksize, 0, len(a[2])) send_chunk(fp, a[2], blocksize, 0, len(a[2]))
except netlib.tcp.NetLibDisconnect: # pragma: no cover except TcpDisconnect: # pragma: no cover
return True return True

View File

@ -13,7 +13,8 @@ import OpenSSL.crypto
import six import six
from netlib import tcp, http, certutils, websockets, socks from netlib import tcp, http, certutils, websockets, socks
from netlib.exceptions import HttpException from netlib.exceptions import HttpException, TcpDisconnect, TcpTimeout, TlsException, TcpException, \
NetlibException
from netlib.http import http1, http2 from netlib.http import http1, http2
import language.http import language.http
@ -123,7 +124,7 @@ class WebsocketFrameReader(threading.Thread):
with self.logger.ctx() as log: with self.logger.ctx() as log:
try: try:
frm = websockets.Frame.from_file(self.rfile) frm = websockets.Frame.from_file(self.rfile)
except tcp.NetLibDisconnect: except TcpDisconnect:
return return
self.frames_queue.put(frm) self.frames_queue.put(frm)
log("<< %s" % frm.header.human_readable()) log("<< %s" % frm.header.human_readable())
@ -270,7 +271,7 @@ class Pathoc(tcp.TCPClient):
connect_reply.msg, connect_reply.msg,
"SOCKS server error" "SOCKS server error"
) )
except (socks.SocksError, tcp.NetLibDisconnect) as e: except (socks.SocksError, TcpDisconnect) as e:
raise PathocError(str(e)) raise PathocError(str(e))
def connect(self, connect_to=None, showssl=False, fp=sys.stdout): def connect(self, connect_to=None, showssl=False, fp=sys.stdout):
@ -301,7 +302,7 @@ class Pathoc(tcp.TCPClient):
cipher_list=self.ciphers, cipher_list=self.ciphers,
alpn_protos=alpn_protos alpn_protos=alpn_protos
) )
except tcp.NetLibError as v: except TlsException as v:
raise PathocError(str(v)) raise PathocError(str(v))
self.sslinfo = SSLInfo( self.sslinfo = SSLInfo(
@ -394,7 +395,7 @@ class Pathoc(tcp.TCPClient):
Returns Response if we have a non-ignored response. Returns Response if we have a non-ignored response.
May raise http.HTTPError, tcp.NetLibError May raise a NetlibException
""" """
logger = log.ConnectionLogger( logger = log.ConnectionLogger(
self.fp, self.fp,
@ -414,7 +415,7 @@ class Pathoc(tcp.TCPClient):
except HttpException as v: except HttpException as v:
lg("Invalid server response: %s" % v) lg("Invalid server response: %s" % v)
raise raise
except tcp.NetLibTimeout: except TcpTimeout:
if self.ignoretimeout: if self.ignoretimeout:
lg("Timeout (ignored)") lg("Timeout (ignored)")
return None return None
@ -438,7 +439,7 @@ class Pathoc(tcp.TCPClient):
Returns Response if we have a non-ignored response. Returns Response if we have a non-ignored response.
May raise http.HTTPError, tcp.NetLibError May raise a NetlibException
""" """
if isinstance(r, basestring): if isinstance(r, basestring):
r = language.parse_pathoc(r, self.use_http2).next() r = language.parse_pathoc(r, self.use_http2).next()
@ -495,7 +496,7 @@ def main(args): # pragma: nocover
trycount = 0 trycount = 0
try: try:
p.connect(args.connect_to, args.showssl) p.connect(args.connect_to, args.showssl)
except tcp.NetLibError as v: except TcpException as v:
print >> sys.stderr, str(v) print >> sys.stderr, str(v)
continue continue
except PathocError as v: except PathocError as v:
@ -523,7 +524,7 @@ def main(args): # pragma: nocover
# We consume the queue when we can, so it doesn't build up. # We consume the queue when we can, so it doesn't build up.
for i_ in p.wait(timeout=0, finish=False): for i_ in p.wait(timeout=0, finish=False):
pass pass
except (http.HttpError, tcp.NetLibError) as v: except NetlibException:
break break
for i_ in p.wait(timeout=0.01, finish=True): for i_ in p.wait(timeout=0.01, finish=True):
pass pass

View File

@ -6,8 +6,8 @@ import threading
import urllib import urllib
from netlib import tcp, http, certutils, websockets from netlib import tcp, http, certutils, websockets
from netlib.exceptions import HttpException, HttpReadDisconnect from netlib.exceptions import HttpException, HttpReadDisconnect, TcpTimeout, TcpDisconnect, \
from netlib.http import ALPN_PROTO_HTTP1, ALPN_PROTO_H2 TlsException
from . import version, app, language, utils, log, protocols from . import version, app, language, utils, log, protocols
import language.http import language.http
@ -41,7 +41,7 @@ class SSLOptions(object):
ssl_options=tcp.SSL_DEFAULT_OPTIONS, ssl_options=tcp.SSL_DEFAULT_OPTIONS,
ciphers=None, ciphers=None,
certs=None, certs=None,
alpn_select=ALPN_PROTO_H2, alpn_select=http.ALPN_PROTO_H2,
): ):
self.confdir = confdir self.confdir = confdir
self.cn = cn self.cn = cn
@ -247,7 +247,7 @@ class PathodHandler(tcp.BaseHandler):
options=self.server.ssloptions.ssl_options, options=self.server.ssloptions.ssl_options,
alpn_select=self.server.ssloptions.alpn_select, alpn_select=self.server.ssloptions.alpn_select,
) )
except tcp.NetLibError as v: except TlsException as v:
s = str(v) s = str(v)
self.server.add_log( self.server.add_log(
dict( dict(
@ -259,7 +259,7 @@ class PathodHandler(tcp.BaseHandler):
return return
alp = self.get_alpn_proto_negotiated() alp = self.get_alpn_proto_negotiated()
if alp == ALPN_PROTO_H2: if alp == http.ALPN_PROTO_H2:
self.protocol = protocols.http2.HTTP2Protocol(self) self.protocol = protocols.http2.HTTP2Protocol(self)
self.use_http2 = True self.use_http2 = True
@ -387,7 +387,7 @@ class Pathod(tcp.TCPServer):
try: try:
h.handle() h.handle()
h.finish() h.finish()
except tcp.NetLibDisconnect: # pragma: no cover except TcpDisconnect: # pragma: no cover
log.write_raw(self.logfp, "Disconnect") log.write_raw(self.logfp, "Disconnect")
self.add_log( self.add_log(
dict( dict(
@ -396,7 +396,7 @@ class Pathod(tcp.TCPServer):
) )
) )
return return
except tcp.NetLibTimeout: except TcpTimeout:
log.write_raw(self.logfp, "Timeout") log.write_raw(self.logfp, "Timeout")
self.add_log( self.add_log(
dict( dict(

View File

@ -1,5 +1,5 @@
from netlib import tcp, wsgi from netlib import tcp, wsgi
from netlib.exceptions import HttpReadDisconnect from netlib.exceptions import HttpReadDisconnect, TlsException
from netlib.http import http1, Request from netlib.http import http1, Request
from .. import version, language from .. import version, language
@ -61,7 +61,7 @@ class HTTPProtocol(object):
options=self.pathod_handler.server.ssloptions.ssl_options, options=self.pathod_handler.server.ssloptions.ssl_options,
alpn_select=self.pathod_handler.server.ssloptions.alpn_select, alpn_select=self.pathod_handler.server.ssloptions.alpn_select,
) )
except tcp.NetLibError as v: except TlsException as v:
s = str(v) s = str(v)
lg(s) lg(s)
return None, dict(type="error", msg=s) return None, dict(type="error", msg=s)

View File

@ -1,5 +1,6 @@
import StringIO import StringIO
from libpathod import log from libpathod import log
from netlib.exceptions import TcpDisconnect
import netlib.tcp import netlib.tcp
@ -19,6 +20,6 @@ def test_disconnect():
try: try:
with l.ctx() as lg: with l.ctx() as lg:
lg("Test") lg("Test")
except netlib.tcp.NetLibDisconnect: except TcpDisconnect:
pass pass
assert "Test" in outf.getvalue() assert "Test" in outf.getvalue()

View File

@ -5,7 +5,7 @@ import OpenSSL
from mock import Mock from mock import Mock
from netlib import tcp, http, socks from netlib import tcp, http, socks
from netlib.exceptions import HttpException from netlib.exceptions import HttpException, TcpException, NetlibException
from netlib.http import http1, http2 from netlib.http import http1, http2
from libpathod import pathoc, test, version, pathod, language from libpathod import pathoc, test, version, pathod, language
@ -84,7 +84,7 @@ class _TestDaemon:
r = r.freeze(language.Settings()) r = r.freeze(language.Settings())
try: try:
c.request(r) c.request(r)
except (HttpException, tcp.NetLibError): except NetlibException:
pass pass
return s.getvalue() return s.getvalue()

View File

@ -4,7 +4,7 @@ import OpenSSL
from libpathod import pathod, version from libpathod import pathod, version
from netlib import tcp, http from netlib import tcp, http
from netlib.exceptions import HttpException from netlib.exceptions import HttpException, TlsException
import tutils import tutils
@ -264,7 +264,7 @@ class TestDaemonSSL(CommonTests):
c.wbufsize = 0 c.wbufsize = 0
c.connect() c.connect()
c.wfile.write("\0\0\0\0") c.wfile.write("\0\0\0\0")
tutils.raises(tcp.NetLibError, c.convert_to_ssl) tutils.raises(TlsException, c.convert_to_ssl)
l = self.d.last_log() l = self.d.last_log()
assert l["type"] == "error" assert l["type"] == "error"
assert "SSL" in l["msg"] assert "SSL" in l["msg"]