throughput_client.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/python3
  2. #
  3. import throughput_protocols
  4. import basic_protocols
  5. import useful
  6. import os
  7. import argparse
  8. import logging
  9. import socket
  10. #
  11. if __name__ == '__main__':
  12. logging.basicConfig(level=logging.DEBUG)
  13. #
  14. parser = argparse.ArgumentParser(description='Test the network throughput (optionally through a proxy).')
  15. parser.add_argument('ip', type=str, help='destination ip address')
  16. parser.add_argument('port', type=int, help='destination port')
  17. parser.add_argument('num_bytes', type=useful.parse_bytes,
  18. help='number of bytes to send (can also end with \'B\', \'KiB\', \'MiB\', or \'GiB\')', metavar='num-bytes')
  19. parser.add_argument('--proxy', type=str, help='proxy ip address and port', metavar=('ip','port'), nargs=2)
  20. parser.add_argument('--fake-proxy', action='store_true', help='connecting to a fake-tor proxy')
  21. parser.add_argument('--wait', type=int,
  22. help='wait until the given time before pushing data (time in seconds since epoch)', metavar='time')
  23. parser.add_argument('--buffer-len', type=useful.parse_bytes,
  24. help='size of the send and receive buffers (can also end with \'B\', \'KiB\', \'MiB\', or \'GiB\')', metavar='bytes')
  25. parser.add_argument('--no-accel', action='store_true', help='don\'t use C acceleration (use pure Python)')
  26. args = parser.parse_args()
  27. #
  28. '''
  29. endpoint = ('127.0.0.1', 4741)
  30. #endpoint = ('127.0.0.1', 8627)
  31. #proxy = ('127.0.0.1', 9003+int(sys.argv[3])-1)
  32. proxy = ('127.0.0.1', int(sys.argv[3]))
  33. #proxy = ('127.0.0.1', 9003)
  34. #proxy = ('127.0.0.1', 12849)
  35. #proxy = None
  36. '''
  37. #
  38. endpoint = (args.ip, args.port)
  39. client_socket = socket.socket()
  40. protocols = []
  41. #
  42. if args.proxy is None:
  43. logging.debug('Socket %d connecting to endpoint %r...', client_socket.fileno(), endpoint)
  44. client_socket.connect(endpoint)
  45. elif not args.fake_proxy:
  46. proxy_username = bytes([x for x in os.urandom(12) if x != 0])
  47. proxy_endpoint = (args.proxy[0], int(args.proxy[1]))
  48. #
  49. logging.debug('Socket %d connecting to proxy %r...', client_socket.fileno(), proxy_endpoint)
  50. client_socket.connect(proxy_endpoint)
  51. #
  52. proxy_protocol = basic_protocols.Socks4Protocol(client_socket, endpoint, username=proxy_username)
  53. protocols.append(proxy_protocol)
  54. elif args.fake_proxy:
  55. proxy_endpoint = (args.proxy[0], int(args.proxy[1]))
  56. #
  57. logging.debug('Socket %d connecting to fake proxy %r...', client_socket.fileno(), proxy_endpoint)
  58. client_socket.connect(proxy_endpoint)
  59. #
  60. proxy_protocol = basic_protocols.FakeProxyProtocol(client_socket, endpoint)
  61. protocols.append(proxy_protocol)
  62. #
  63. group_id = int(args.wait*1000) if args.wait is not None else None
  64. throughput_protocol = throughput_protocols.ClientProtocol(client_socket, args.num_bytes,
  65. wait_until=args.wait,
  66. group_id=group_id,
  67. send_buffer_len=args.buffer_len,
  68. use_acceleration=(not args.no_accel))
  69. protocols.append(throughput_protocol)
  70. #
  71. combined_protocol = basic_protocols.ChainedProtocol(protocols)
  72. combined_protocol.run()
  73. #