master
1#!/usr/bin/env python3
2#
3# Project ___| | | | _ \| |
4# / __| | | | |_) | |
5# | (__| |_| | _ <| |___
6# \___|\___/|_| \_\_____|
7#
8# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9#
10# This software is licensed as described in the file COPYING, which
11# you should have received as part of this distribution. The terms
12# are also available at https://curl.se/docs/copyright.html.
13#
14# You may opt to use, copy, modify, merge, publish, distribute and/or sell
15# copies of the Software, and permit persons to whom the Software is
16# furnished to do so, under the terms of the COPYING file.
17#
18# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19# KIND, either express or implied.
20#
21# SPDX-License-Identifier: curl
22#
23"""A telnet server which negotiates."""
25import argparse
26import logging
27import os
28import socket
29import socketserver
30import sys
32from util import ClosingFileHandler
34log = logging.getLogger(__name__)
35HOST = "localhost"
36IDENT = "NTEL"
38# The strings that indicate the test framework is checking our aliveness
39VERIFIED_REQ = "verifiedserver"
40VERIFIED_RSP = "WE ROOLZ: {pid}"
43def telnetserver(options):
44 """Start up a TCP server with a telnet handler and serve DICT requests forever."""
45 if options.pidfile:
46 pid = os.getpid()
47 # see tests/server/util.c function write_pidfile
48 if os.name == "nt":
49 pid += 4194304
50 with open(options.pidfile, "w") as f:
51 f.write(str(pid))
53 local_bind = (HOST, options.port)
54 log.info("Listening on %s", local_bind)
56 # Need to set the allow_reuse on the class, not on the instance.
57 socketserver.TCPServer.allow_reuse_address = True
58 with socketserver.TCPServer(local_bind, NegotiatingTelnetHandler) as server:
59 server.serve_forever()
60 # leaving `with` calls server.close() automatically
61 return ScriptRC.SUCCESS
64class NegotiatingTelnetHandler(socketserver.BaseRequestHandler):
65 """Handler class for Telnet connections."""
67 def handle(self):
68 """Negotiates options before reading data."""
69 neg = Negotiator(self.request)
71 try:
72 # Send some initial negotiations.
73 neg.send_do("NEW_ENVIRON")
74 neg.send_will("NEW_ENVIRON")
75 neg.send_dont("NAWS")
76 neg.send_wont("NAWS")
78 # Get the data passed through the negotiator
79 data = neg.recv(4 * 1024)
80 log.debug("Incoming data: %r", data)
82 if VERIFIED_REQ.encode('utf-8') in data:
83 log.debug("Received verification request from test framework")
84 pid = os.getpid()
85 # see tests/server/util.c function write_pidfile
86 if os.name == "nt":
87 pid += 4194304
88 response = VERIFIED_RSP.format(pid=pid)
89 response_data = response.encode('utf-8')
90 else:
91 log.debug("Received normal request - echoing back")
92 response_data = data.decode('utf-8').strip().encode('utf-8')
94 if response_data:
95 log.debug("Sending %r", response_data)
96 self.request.sendall(response_data)
98 # put some effort into making a clean socket shutdown
99 # that does not give the client ECONNRESET
100 self.request.settimeout(0.1)
101 self.request.recv(4 * 1024)
102 self.request.shutdown(socket.SHUT_RDWR)
104 except OSError:
105 log.exception("OSError hit during request")
108class Negotiator:
109 NO_NEG = 0
110 START_NEG = 1
111 WILL = 2
112 WONT = 3
113 DO = 4
114 DONT = 5
116 def __init__(self, tcp):
117 self.tcp = tcp
118 self.state = self.NO_NEG
120 def recv(self, nbytes):
121 """
122 Read bytes from TCP, handling negotiation sequences.
124 :param nbytes: Number of bytes to read
125 :return: a buffer of bytes
126 """
127 buffer = bytearray()
129 # If we keep receiving negotiation sequences, we do not fill the buffer.
130 # Keep looping while we can, and until we have something to give back
131 # to the caller.
132 while len(buffer) == 0:
133 data = self.tcp.recv(nbytes)
134 if not data:
135 # TCP failed to give us any data. Break out.
136 break
138 for byte_int in bytearray(data):
139 if self.state == self.NO_NEG:
140 self.no_neg(byte_int, buffer)
141 elif self.state == self.START_NEG:
142 self.start_neg(byte_int)
143 elif self.state in [self.WILL, self.WONT, self.DO, self.DONT]:
144 self.handle_option(byte_int)
145 else:
146 # Received an unexpected byte. Stop negotiations
147 log.error("Unexpected byte %s in state %s",
148 byte_int,
149 self.state)
150 self.state = self.NO_NEG
152 return buffer
154 def no_neg(self, byte_int, buffer):
155 # Not negotiating anything thus far. Check to see if we
156 # should.
157 if byte_int == NegTokens.IAC:
158 # Start negotiation
159 log.debug("Starting negotiation (IAC)")
160 self.state = self.START_NEG
161 else:
162 # Append the incoming byte to the buffer
163 buffer.append(byte_int)
165 def start_neg(self, byte_int):
166 # In a negotiation.
167 log.debug("In negotiation (%s)",
168 NegTokens.from_val(byte_int))
170 if byte_int == NegTokens.WILL:
171 # Client is confirming they are willing to do an option
172 log.debug("Client is willing")
173 self.state = self.WILL
174 elif byte_int == NegTokens.WONT:
175 # Client is confirming they are unwilling to do an
176 # option
177 log.debug("Client is unwilling")
178 self.state = self.WONT
179 elif byte_int == NegTokens.DO:
180 # Client is indicating they can do an option
181 log.debug("Client can do")
182 self.state = self.DO
183 elif byte_int == NegTokens.DONT:
184 # Client is indicating they cannot do an option
185 log.debug("Client cannot do")
186 self.state = self.DONT
187 else:
188 # Received an unexpected byte. Stop negotiations
189 log.error("Unexpected byte %s in state %s",
190 byte_int,
191 self.state)
192 self.state = self.NO_NEG
194 def handle_option(self, byte_int):
195 if byte_int in [NegOptions.BINARY,
196 NegOptions.CHARSET,
197 NegOptions.SUPPRESS_GO_AHEAD,
198 NegOptions.NAWS,
199 NegOptions.NEW_ENVIRON]:
200 log.debug("Option: %s", NegOptions.from_val(byte_int))
202 # No further negotiation of this option needed. Reset the state.
203 self.state = self.NO_NEG
205 else:
206 # Received an unexpected byte. Stop negotiations
207 log.error("Unexpected byte %s in state %s",
208 byte_int,
209 self.state)
210 self.state = self.NO_NEG
212 def send_message(self, message_ints):
213 self.tcp.sendall(bytearray(message_ints))
215 def send_iac(self, arr):
216 message = [NegTokens.IAC]
217 message.extend(arr)
218 self.send_message(message)
220 def send_do(self, option_str):
221 log.debug("Sending DO %s", option_str)
222 self.send_iac([NegTokens.DO, NegOptions.to_val(option_str)])
224 def send_dont(self, option_str):
225 log.debug("Sending DONT %s", option_str)
226 self.send_iac([NegTokens.DONT, NegOptions.to_val(option_str)])
228 def send_will(self, option_str):
229 log.debug("Sending WILL %s", option_str)
230 self.send_iac([NegTokens.WILL, NegOptions.to_val(option_str)])
232 def send_wont(self, option_str):
233 log.debug("Sending WONT %s", option_str)
234 self.send_iac([NegTokens.WONT, NegOptions.to_val(option_str)])
237class NegBase:
238 @classmethod
239 def to_val(cls, name):
240 return getattr(cls, name)
242 @classmethod
243 def from_val(cls, val):
244 for k in cls.__dict__:
245 if getattr(cls, k) == val:
246 return k
248 return "<unknown>"
251class NegTokens(NegBase):
252 # The start of a negotiation sequence
253 IAC = 255
254 # Confirm willingness to negotiate
255 WILL = 251
256 # Confirm unwillingness to negotiate
257 WONT = 252
258 # Indicate willingness to negotiate
259 DO = 253
260 # Indicate unwillingness to negotiate
261 DONT = 254
263 # The start of sub-negotiation options.
264 SB = 250
265 # The end of sub-negotiation options.
266 SE = 240
269class NegOptions(NegBase):
270 # Binary Transmission
271 BINARY = 0
272 # Suppress Go Ahead
273 SUPPRESS_GO_AHEAD = 3
274 # NAWS - width and height of client
275 NAWS = 31
276 # NEW-ENVIRON - environment variables on client
277 NEW_ENVIRON = 39
278 # Charset option
279 CHARSET = 42
282def get_options():
283 parser = argparse.ArgumentParser()
285 parser.add_argument("--port", action="store", default=9019,
286 type=int, help="port to listen on")
287 parser.add_argument("--verbose", action="store", type=int, default=0,
288 help="verbose output")
289 parser.add_argument("--pidfile", action="store",
290 help="filename for the PID")
291 parser.add_argument("--logfile", action="store",
292 help="filename for the log")
293 parser.add_argument("--srcdir", action="store", help="test directory")
294 parser.add_argument("--id", action="store", help="server ID")
295 parser.add_argument("--ipv4", action="store_true", default=0,
296 help="IPv4 flag")
298 return parser.parse_args()
301def setup_logging(options):
302 """Set up logging from the command line options."""
303 root_logger = logging.getLogger()
304 add_stdout = False
306 formatter = logging.Formatter(f"%(asctime)s %(levelname)-5.5s [{IDENT}] %(message)s")
308 # Write out to a logfile
309 if options.logfile:
310 handler = ClosingFileHandler(options.logfile)
311 handler.setFormatter(formatter)
312 handler.setLevel(logging.DEBUG)
313 root_logger.addHandler(handler)
314 else:
315 # The logfile was not specified. Add a stdout logger.
316 add_stdout = True
318 if options.verbose:
319 # Add a stdout logger as well in verbose mode
320 root_logger.setLevel(logging.DEBUG)
321 add_stdout = True
322 else:
323 root_logger.setLevel(logging.INFO)
325 if add_stdout:
326 stdout_handler = logging.StreamHandler(sys.stdout)
327 stdout_handler.setFormatter(formatter)
328 stdout_handler.setLevel(logging.DEBUG)
329 root_logger.addHandler(stdout_handler)
332class ScriptRC:
333 """Enum for script return codes."""
335 SUCCESS = 0
336 FAILURE = 1
337 EXCEPTION = 2
340if __name__ == '__main__':
341 # Get the options from the user.
342 options = get_options()
344 # Setup logging using the user options
345 setup_logging(options)
347 # Run main script.
348 try:
349 rc = telnetserver(options)
350 except Exception:
351 log.exception('Error in telnet server')
352 rc = ScriptRC.EXCEPTION
354 if options.pidfile and os.path.isfile(options.pidfile):
355 os.unlink(options.pidfile)
357 log.info("Returning %d", rc)
358 sys.exit(rc)
Diagnostics4 git calls · 30 ms
CallStatusSizeTime030 ms
discover200152 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200149 B · hit2 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=149 git.response.cache=HIT git.response.duration_ms=2 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=149 git.pkt.payload.max=78 git.ls_refs.count=2 git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=293470 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=10 git.pkt.data=8 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=293470 git.pkt.payload.max=65516 git.sideband.band1.bytes=293354 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=87 git.pack.bytes=293354 git.pack.ofs_delta=0 git.pack.ref_delta=1 git.pack.chain.max=1 git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0 git.pack.full=86Open pack map
fetch2003.8 KiB · hit25 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=25 git.response.bytes=3867 git.response.cache=HIT git.response.duration_ms=25 git.fetch.wants=4d7b49604abaa3d9e1b3f1548cfce4c661fef980 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=3867 git.pkt.payload.max=3846 git.sideband.band1.bytes=3845 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
- Ref
- refs/heads/master
- Commit
- 31c911efe5652725e930f4d2e60c9060e5f6646d
- cf-ray
- a5e2c4569a40aedd
- Request
- 161229b5…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format