#!/usr/bin/env python3
# -*- coding : utf-8 -*-

# http://speedtest.tele2.net/10GB.zip
# https://docs.python.org/3/library/http.server.html
# http://blog.csdn.net/cteng/article/details/51584766

"""
Anti-HTTP-Scanner : Redirect all requests to 10GB speedtest file
Patrick Young 2017/10/8
usage: "antiscanhttp.py" [-h] [--port] [--ip] redirect_url
positional arguments:
  redirect_url (such as http://speedtest.tele2.net/10GB.zip)
optional arguments:
  -h,--help    Show this help message and exit
  --port,-p    Port to listen on , Default 80
  --ip,-i      Host interface to listen on
  redirect_url Recommend to use 'http://speedtest.tele2.net/10GB.zip'
"""

import socketserver
import http.server
import argparse

def redirect_handler(url):
    class RedirectHandler(http.server.BaseHTTPRequestHandler):
        def do_GET(self):
            self.send_response(301)
            self.send_header('Location', url)
            self.end_headers()

        def do_POST(self):
            self.send_response(301)
            self.send_header('Location', url)
            self.end_headers()

        def do_HEAD(self):
            self.send_response(301)
            self.send_header('Location', url)
            self.end_headers()

        def do_PUT(self):
            self.send_response(301)
            self.send_header('Location', url)
            self.end_headers()

    return RedirectHandler


def main():
    parser = argparse.ArgumentParser(description='Anti HTTP Scanner redirector')
    parser.add_argument('--port', '-p', action='store', type=int, default=80, help='Server listens on this port')
    parser.add_argument('--ip', '-i', action='store', default='', help='Host Interface to listen on')
    parser.add_argument('redirect_url', action='store',help='(such as http://speedtest.tele2.net/10GB.zip)')

    userinput = parser.parse_args()
    redirect_url = userinput.redirect_url
    port = userinput.port
    host = userinput.ip

    redirect_handle = redirect_handler(redirect_url)
    handler = socketserver.TCPServer((host, port), redirect_handle)
    print('Server now is running on the port %s' % port)
    handler.serve_forever()


if __name__ == "__main__":
    main()
