示例#1
0
文件: rrs.py 项目: chopin1998/pyrrs
def listener(port):
    print '\tworking in *** listener *** mode\n'

    """ declare global var(s) """
    global mmrrs_special_command
    global mmrrs_cmd_change_wsize
    global g_listener_old_ttyattr
    global g_listener_win_changed
    g_listener_old_ttyattr = tty.tcgetattr(sys.stdin)
    g_listener_win_changed = False
    """.end declare global var(s)"""

    sd = socket(AF_INET, SOCK_STREAM)
    sd.setsockopt(SOL_SOCKET, SO_REUSEADDR, True)
    sd.setsockopt(SOL_SOCKET, SO_KEEPALIVE, True)
    sd.setsockopt(SOL_TCP, TCP_KEEPCNT, 3)
    sd.setsockopt(SOL_TCP, TCP_KEEPINTVL, 2)
    sd.setsockopt(SOL_TCP, TCP_KEEPIDLE, 9)
    sd.bind(('0', port))
    sd.listen(1)
    cli_sd, cli_addr = sd.accept()
    print '[^_^] got connection from', cli_addr

    asd = AES_SOCKET(cli_sd)
    listener_auth(asd)

    """ setup signal handler """
    custom_signal_set = [signal.SIGINT, signal.SIGTERM, signal.SIGWINCH]
    for sig in custom_signal_set:
        signal.signal(sig, listener_sig_handler)
    """ .end setup signal handler """

    """ init tty winsize """
    print '[II] send local termios attributes..'
    winsize = struct.pack('hhhh', 0, 0, 0, 0) # for struct winsize in include/asm/termios.h
    struct_winsize = fcntl.ioctl(sys.stdin, tty.TIOCGWINSZ, winsize)
    data = mmrrs_special_command + mmrrs_cmd_change_wsize + struct_winsize
    asd.send(data)
    """ .end init tty winsize"""

    #tty.setcbreak(sys.stdin, tty.TCSANOW)
    tty.setraw(sys.stdin, tty.TCSAFLUSH)

    while True:

        try:
            stdin, stdout, stderr = select.select((asd, sys.stdin), (), ())
        except:
            pass
            #print '[DD] SELECT ERROR'
            #return False

        if g_listener_win_changed:
            struct_winsize = fcntl.ioctl(sys.stdin, tty.TIOCGWINSZ, winsize) # should be 2bytes * 4
            data = mmrrs_special_command + mmrrs_cmd_change_wsize + struct_winsize
            asd.send(data)
            g_listener_win_changed = False
            continue

        if asd in stdin:
            data = asd.recv()
            if not data:
                return False
            else:
                try:
                    os.write(sys.stdout.fileno(), data)
                except:
                    continue

        if sys.stdin in stdin:
            data = os.read(sys.stdin.fileno(), 1)
            if not data:
                return False
            else:
                asd.send(data)
示例#2
0
文件: rrs.py 项目: chopin1998/pyrrs
def connector(hostenv):
    print '\tworking in *** connector *** mode\n'

    """ declare global var(s) """
    global mmrrs_special_command
    global mmrrs_cmd_change_wsize
    """.end declare global var(s)"""

    custom_signal_set = [signal.SIGINT, signal.SIGTERM]
    for sig in custom_signal_set:
        signal.signal(sig, connector_sig_handler)

    addr, port = hostenv
    sd = socket(AF_INET, SOCK_STREAM)
    sd.setsockopt(SOL_SOCKET, SO_KEEPALIVE, True)
    sd.setsockopt(SOL_TCP, TCP_KEEPCNT, 3)
    sd.setsockopt(SOL_TCP, TCP_KEEPINTVL, 2)
    sd.setsockopt(SOL_TCP, TCP_KEEPIDLE, 9)
    try:
        sd.connect((addr, port))
        print '[^_^] got connection from', sd.getpeername()
    except:
        print '[EE] cannot connect to', hostenv
        print '[SS] listener running or network robust?'
        return False
    asd = AES_SOCKET(sd)

    if connector_auth(asd):
        pass
    else:
        print '[EE] auth failed'
        return False

    master, slave = pty.openpty()
    pid = os.fork()
    if pid == 0: # child
        sd.close()
        os.close(master)
        os.setsid()
        os.dup2(slave, 0)
        os.dup2(slave, 1)
        os.dup2(slave, 2)
        os.close(slave)
        os.execl('/bin/sh')

    else: # parent
        os.close(slave)
        while True:
            try:
                stdin, stdout, stderr = select.select((asd, master), (), ())
            except:
                print '[DD] SELECT ERROR'
                #return False

            if asd in stdin:
                try:
                    data = asd.recv()
                except:
                    return False
                if not data:
                    return False
                else:
                    if data[0] == mmrrs_special_command:
                        if data[1] == mmrrs_cmd_change_wsize:
                            try:
                                row, col = struct.unpack('hhhh', data[2:])[0:2]
                                print '[DD] set terminal wsize to %d x %d' %(row, col)
                                fcntl.ioctl(master, tty.TIOCSWINSZ, data[2:])
                            except:
                                print '[DD] it should not be happend, but terminal size error'
                        elif data[1] == AES_SOCKET.mmrrs_cmd_continue:
                            continue
                        else:
                            print '[DD] command not implement yet'
                    else: # normal data
                        try:
                            os.write(master, data)
                        except:
                            return False

            if master in stdin:
                try:
                    data = os.read(master, 8192)
                except:
                    return False
                if not data:
                    return False
                else:
                    try:
                        asd.send(data)
                    except:
                        return False