Project

Profile

Help

Revision 1f0e117b

ID1f0e117ba9e9842ffb71857c674d14c4478577bf
Parent feed0779
Child 5e892396

Added by Vincent Le Goff about 2 months ago

Implement #130: Recognize the Telnet Go-Ahead command

View differences:

src/client.py
1 1
# Copyright (c) 2016-2020, LE GOFF Vincent
2
# Copyright (c) 2016-2020, LE GOFF Vincent
3
# Copyright (c) 2016-2020, LE GOFF Vincent
4 2
# All rights reserved.
5 3

  
6 4
# Redistribution and use in source and binary forms, with or without
......
39 37
import os
40 38
import re
41 39
import socket
42
from telnetlib import Telnet, WONT, WILL, ECHO, NOP, AYT, IAC
40
from telnetlib import Telnet, WONT, WILL, ECHO, NOP, AYT, IAC, GA
43 41
import threading
44 42
import time
45 43

  
44
from twisted.internet import reactor
46 45
from twisted.internet.error import ConnectionDone
47 46
from twisted.internet.protocol import ReconnectingClientFactory
48 47
from twisted.conch.telnet import Telnet
......
69 68

  
70 69
    def connectionMade(self):
71 70
        """Established connection, send the differed commands."""
71
        self.has_GA = False
72
        self.queue = b""
73
        self.defer = None
72 74
        host = self.transport.getPeer().host
73 75
        port = self.transport.getPeer().port
74 76
        log = logger("client")
75 77
        log.info("Connected to {host}:{port}".format(
76 78
                host=host, port=port))
79
        # Record commands
80
        self.commandMap[GA] = self.handle_GA
81

  
77 82
        self.factory.resetDelay()
78 83
        for command in self.factory.commands:
79 84
            self.transport.write(command.encode() + b"\r\n")
......
92 97

  
93 98
    def applicationDataReceived(self, data):
94 99
        """Receive something."""
95
        encoding = self.factory.engine.settings["options.general.encoding"]
96
        msg = data.decode(encoding, errors="replace")
97
        with self.factory.world.lock:
98
            self.handle_lines(msg)
100
        if self.has_GA:
101
            self.queue += data
102
            if self.defer:
103
                self.defer.cancel()
104
            self.defer = reactor.callLater(0.2, self.send_queue)
105
        else:
106
            if self.queue:
107
                data = self.queue + b"\r\n" + data
108
                self.queue = b""
109

  
110
            # Cancel the deferred, if exists
111
            if self.defer:
112
                self.defer.cancel()
113
                self.defer = None
114

  
115
            encoding = self.factory.engine.settings["options.general.encoding"]
116
            msg = data.decode(encoding, errors="replace")
117
            with self.factory.world.lock:
118
                self.handle_lines(msg)
119

  
120
    def send_queue(self):
121
        old_GA = self.has_GA
122
        self.has_GA = False
123
        self.defer = None
124
        if self.queue:
125
            queue = self.queue
126
            self.queue = b""
127
            self.applicationDataReceived(queue)
128
        self.has_GA = old_GA
129

  
130
    def handle_GA(self, *args, **kwargs):
131
        """Handle the Telnet Go-Ahead."""
132
        self.has_GA = False
133
        if self.queue:
134
            queue = self.queue
135
            self.queue = b""
136
            self.applicationDataReceived(queue)
137
        self.has_GA = True
99 138

  
100 139
    def run(self):
101 140
        """Run the thread."""

Also available in: Unified diff