# Path: sshuttle/helpers.py
# def b(s):
#     return s.encode("ASCII")
# 
# def log(s):
#     global logprefix
#     try:
#         sys.stdout.flush()
#         # Put newline at end of string if line doesn't have one.
#         if not s.endswith("\n"):
#             s = s+"\n"
# 
#         prefix = logprefix
#         s = s.rstrip("\n")
#         for line in s.split("\n"):
#             # We output with \r\n instead of \n because when we use
#             # sudo with the use_pty option, the firewall process, the
#             # other processes printing to the terminal will have the
#             # \n move to the next line, but they will fail to reset
#             # cursor to the beginning of the line. Printing output
#             # with \r\n endings fixes that problem and does not appear
#             # to cause problems elsewhere.
#             sys.stderr.write(prefix + line + "\r\n")
#             prefix = "    "
#         sys.stderr.flush()
#     except IOError:
#         # this could happen if stderr gets forcibly disconnected, eg. because
#         # our tty closes.  That sucks, but it's no reason to abort the program.
#         pass
# 
# def debug1(s):
#     if verbose >= 1:
#         log(s)
# 
# def debug2(s):
#     if verbose >= 2:
#         log(s)
# 
# def debug3(s):
#     if verbose >= 3:
#         log(s)
# 
# class Fatal(Exception):
#     pass


import sys
import struct
import socket
import errno
import select
import os
import fcntl
from sshuttle.helpers import b, log, debug1, debug2, debug3, Fatal
        try:
            os.set_blocking(self.wfile.fileno(), False)
        except AttributeError:
            # python < 3.5
            flags = fcntl.fcntl(self.wfile.fileno(), fcntl.F_GETFL)
            flags |= os.O_NONBLOCK
            flags = fcntl.fcntl(self.wfile.fileno(), fcntl.F_SETFL, flags)
        if self.outbuf and self.outbuf[0]:
            wrote = _nb_clean(os.write, self.wfile.fileno(), self.outbuf[0])
            debug2('mux wrote: %r/%d' % (wrote, len(self.outbuf[0])))
            if wrote:
                self.outbuf[0] = self.outbuf[0][wrote:]
        while self.outbuf and not self.outbuf[0]:
            self.outbuf[0:1] = []

    def fill(self):
        try:
            os.set_blocking(self.rfile.fileno(), False)
        except AttributeError:
            # python < 3.5
            flags = fcntl.fcntl(self.rfile.fileno(), fcntl.F_GETFL)
            flags |= os.O_NONBLOCK
            flags = fcntl.fcntl(self.rfile.fileno(), fcntl.F_SETFL, flags)
        try:
            # If LATENCY_BUFFER_SIZE is inappropriately large, we will
            # get a MemoryError here. Read no more than 1MiB.
            read = _nb_clean(os.read, self.rfile.fileno(),
                             min(1048576, LATENCY_BUFFER_SIZE))
        except OSError:
            _, e = sys.exc_info()[:2]

# Path: sshuttle/helpers.py
# def b(s):
#     return s.encode("ASCII")
# 
# def log(s):
#     global logprefix
#     try:
#         sys.stdout.flush()
#         # Put newline at end of string if line doesn't have one.
#         if not s.endswith("\n"):
#             s = s+"\n"
# 
#         prefix = logprefix
#         s = s.rstrip("\n")
#         for line in s.split("\n"):
#             # We output with \r\n instead of \n because when we use
#             # sudo with the use_pty option, the firewall process, the
#             # other processes printing to the terminal will have the
#             # \n move to the next line, but they will fail to reset
#             # cursor to the beginning of the line. Printing output
#             # with \r\n endings fixes that problem and does not appear
#             # to cause problems elsewhere.
#             sys.stderr.write(prefix + line + "\r\n")
#             prefix = "    "
#         sys.stderr.flush()
#     except IOError:
#         # this could happen if stderr gets forcibly disconnected, eg. because
#         # our tty closes.  That sucks, but it's no reason to abort the program.
#         pass
# 
# def debug1(s):
#     if verbose >= 1:
#         log(s)
# 
# def debug2(s):
#     if verbose >= 2:
#         log(s)
# 
# def debug3(s):
#     if verbose >= 3:
#         log(s)
# 
# class Fatal(Exception):
#     pass


# Path: sshuttle/ssnet.py
import sys
import struct
import socket
import errno
import select
import os
import fcntl
from sshuttle.helpers import b, log, debug1, debug2, debug3, Fatal

        try:
            os.set_blocking(self.wfile.fileno(), False)
        except AttributeError:
            # python < 3.5
            flags = fcntl.fcntl(self.wfile.fileno(), fcntl.F_GETFL)
            flags |= os.O_NONBLOCK
            flags = fcntl.fcntl(self.wfile.fileno(), fcntl.F_SETFL, flags)
        if self.outbuf and self.outbuf[0]:
            wrote = _nb_clean(os.write, self.wfile.fileno(), self.outbuf[0])
            debug2('mux wrote: %r/%d' % (wrote, len(self.outbuf[0])))
            if wrote:
                self.outbuf[0] = self.outbuf[0][wrote:]
        while self.outbuf and not self.outbuf[0]:
            self.outbuf[0:1] = []

    def fill(self):
        try:
            os.set_blocking(self.rfile.fileno(), False)
        except AttributeError:
            # python < 3.5
            flags = fcntl.fcntl(self.rfile.fileno(), fcntl.F_GETFL)
            flags |= os.O_NONBLOCK
            flags = fcntl.fcntl(self.rfile.fileno(), fcntl.F_SETFL, flags)
        try:
            # If LATENCY_BUFFER_SIZE is inappropriately large, we will
            # get a MemoryError here. Read no more than 1MiB.
            read = _nb_clean(os.read, self.rfile.fileno(),
                             min(1048576, LATENCY_BUFFER_SIZE))
        except OSError:
            _, e = sys.exc_info()[:2]

            raise Fatal('other end: %r' % e)