Debian Codegraph @

src/http_relay/__main__.py

raw symbols SCIP
1 # SPDX-License-Identifier: BSD-3-Clause
2 # SPDX-FileCopyrightText: Czech Technical University in Prague
3
4 from __future__ import print_function
5
6 import argparse
7 import logging
8 import sys
9 import threading
10
11 from http_relay import HttpRelay
12
13
14 def get_parser():
15 parser = argparse.ArgumentParser(
16 description="Relay HTTP requests from localhost to a remote host (act as reverse HTTP proxy).")
17
18 parser.add_argument("host", type=str, help='The remote host to connect to (e.g. "cvut.cz")')
19 parser.add_argument("port", type=int, default=80, nargs='?',
20 help='The remote host\'s port to connect to (e.g. 80).')
21 parser.add_argument("local_port", type=int, default=0, nargs='?',
22 help='The local port to be used for the relay. If left out, will be the same as remote port.')
23 parser.add_argument("local_addr", type=str, default='0.0.0.0', nargs='?',
24 help='Local interface (IP or hostname) to run the relay on. By default, it runs on all IPv4 '
25 'interfaces (0.0.0.0).')
26 parser.add_argument("-n", "--num-threads", type=int, default=8,
27 help='Number of threads servicing the incoming requests.')
28 parser.add_argument("-b", "--buffer-size", type=int, default=1,
29 help='Size of the buffer used for reading responses. Generally, a larger buffer should be more '
30 'efficient, but if it is too large, the local clients may time out before they '
31 'receive any data.')
32 parser.add_argument("-t", "--sigkill-timeout", type=int, required=False,
33 help='If specified, the relay will be sigkilled in this number of seconds.')
34 parser.add_argument("-s", "--sigkill-on-stream-stop", action="store_true",
35 help='If True, --sigkill-timeout will not be counted when no requests are active, and '
36 'during requests, each successful data transmission will reset the timeout. This can be '
37 'used to detect stale streams if you expect an application to be constantly receiving '
38 'data.')
39 return parser
40
41
42 def main(cli_args=None):
43 parser = get_parser()
44 args = parser.parse_args(cli_args)
45
46 host = args.host.lstrip("[").rstrip("]") # strip [] from IPv6 addresses
47 port = args.port
48 local_port = args.local_port if args.local_port != 0 else port
49 local_addr = args.local_addr.lstrip("[").rstrip("]") # strip [] from IPv6 addresses
50 num_threads = args.num_threads
51 buffer_size = args.buffer_size
52 sigkill_timeout = args.sigkill_timeout
53 sigkill_on_stream_stop = args.sigkill_on_stream_stop
54
55 logging.info("Relaying HTTP requests from %s:%i to %s:%i using %i threads" % (
56 local_addr if ":" not in local_addr else ("[" + local_addr + "]"), local_port, # wrap IPv6 in []
57 host if ":" not in host else ("[" + host + "]"), port, # wrap IPv6 in []
58 num_threads))
59
60 relay = HttpRelay(local_addr, local_port, host, port, buffer_size)
61
62 if sigkill_timeout is not None:
63 logging.info("HTTP relay has sigkill timeout set to %i seconds. After that time%s, the node will be killed." % (
64 sigkill_timeout, " with a stale stream" if sigkill_on_stream_stop else ""))
65 t = threading.Thread(target=relay.sigkill_after, args=(sigkill_timeout, sigkill_on_stream_stop))
66 t.daemon = True
67 t.start()
68
69 try:
70 relay.run(num_threads)
71 except Exception as e:
72 print(str(e), file=sys.stderr)
73 sys.exit(1)
74
75
76 if __name__ == '__main__':
77 logging.basicConfig(level=logging.INFO)
78 main()