Project

General

Profile

Download (2.74 KB) Statistics
| Branch: | Tag: | Revision:

haketilo / test / haketilo_test / __main__.py @ fd9f2fc4

1
# SPDX-License-Identifier: AGPL-3.0-or-later
2

    
3
"""
4
Run a Firefox-type browser with WebDriver attached and Python console open
5
"""
6

    
7
# This file is part of Haketilo.
8
#
9
# Copyright (C) 2021 jahoti <jahoti@tilde.team>
10
# Copyright (C) 2021 Wojtek Kosior <koszko@koszko.org>
11
#
12
# This program is free software: you can redistribute it and/or modify
13
# it under the terms of the GNU Affero General Public License as
14
# published by the Free Software Foundation, either version 3 of the
15
# License, or (at your option) any later version.
16
#
17
# This program is distributed in the hope that it will be useful,
18
# but WITHOUT ANY WARRANTY; without even the implied warranty of
19
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
# GNU Affero General Public License for more details.
21
#
22
# You should have received a copy of the GNU Affero General Public License
23
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
24
#
25
#
26
# I, Wojtek Kosior, thereby promise not to sue for violation of this
27
# file's license. Although I request that you do not make use of this code
28
# in a proprietary program, I am not going to enforce this in court.
29

    
30
import sys
31
import time
32
import code
33
from rlcompleter import Completer
34
import readline
35

    
36
from .server import do_an_internet
37
from .misc_constants import *
38
from .profiles import firefox_safe_mode
39
from .extension_crafting import get_extension_base_url
40

    
41
def fail(msg, error_code):
42
    print('Error:', msg)
43
    print('Usage:', sys.argv[0], '[--load-haketilo]', '[certificates_directory] [proxy_port]')
44
    sys.exit(error_code)
45

    
46
load_haketilo = False
47
argv_idx = 1
48
if len(sys.argv) > argv_idx and sys.argv[argv_idx] == '--load-haketilo':
49
    load_haketilo = True
50
    argv_idx += 1
51

    
52
certdir = Path(sys.argv[argv_idx]).resolve() if len(sys.argv) > argv_idx \
53
    else default_cert_dir
54

    
55
if not certdir.is_dir():
56
    fail('selected certificate directory does not exist.', 2)
57

    
58
argv_idx += 1
59

    
60
port = sys.argv[argv_idx] if len(sys.argv) > argv_idx \
61
    else str(default_proxy_port)
62

    
63
if not port.isnumeric():
64
    fail('port must be an integer.', 3)
65

    
66
httpd = do_an_internet(certdir, int(port))
67
driver = firefox_safe_mode(proxy_port=int(port))
68

    
69
if load_haketilo:
70
    driver.install_addon(str(here.parent / 'mozilla-build.zip'), temporary=True)
71
    driver.get(get_extension_base_url(driver) + 'html/settings.html')
72

    
73
print("You can now control the browser through 'driver' object")
74

    
75
# Here we enable readline-enhanced editing:
76
# https://stackoverflow.com/questions/35115208/is-there-any-way-to-combine-readline-rlcompleter-and-interactiveconsole-in-pytho#answer-35116399
77
readline.parse_and_bind('tab: complete');
78
console_locals = globals()
79
readline.set_completer(Completer(console_locals).complete)
80
code.InteractiveConsole(locals=globals()).interact()
81

    
82
driver.quit()
83
httpd.shutdown()
(2-2/11)