verify-tor.py 2.46 KB
Newer Older
1 2 3 4 5
#!/usr/bin/env python3
import os
import sys
from subprocess import check_call, CalledProcessError

6
from utils import REPO_DIR, get_sha256, get_build_versions, get_final_file_name, \
Torsten Grote's avatar
Torsten Grote committed
7
    get_version, get_tor_version
8

9 10
REF_DIR = "reference"

11 12 13 14 15

def main():
    # get Tor version from command or show usage information
    version = get_version()

16 17 18 19 20 21 22
    if verify(version, for_android=False) and verify(version, for_android=True):
        sys.exit(0)
    else:
        sys.exit(1)


def verify(version, for_android):
23 24 25 26
    # get Tor version and versions of its dependencies
    versions = get_build_versions(version)

    # download reference binary
27
    os.makedirs(REF_DIR, exist_ok=True)
28
    file_name = get_final_file_name(versions, for_android)
29
    ref_file = os.path.join(REF_DIR, file_name)
30 31
    try:
        # try downloading from jcenter
32
        check_call(['wget', '--no-verbose', get_url(versions, for_android), '-O', ref_file])
33 34 35
    except CalledProcessError:
        # try fallback to bintray
        print("Warning: Download from jcenter failed. Trying bintray directly...")
36
        check_call(['wget', '--no-verbose', get_url(versions, for_android, fallback=True), '-O',
37
                    ref_file])
38 39

    # check if Tor was already build
40
    if not os.path.isfile(file_name):
41 42 43 44 45 46 47
        # build Tor
        if version is None:
            check_call(['./build-tor.py'])
        else:
            check_call(['./build-tor.py', version])

    # calculate hashes for both files
48 49
    reference_hash = get_sha256(ref_file)
    build_hash = get_sha256(file_name)
50 51 52 53
    print("Reference sha256: %s" % reference_hash)
    print("Build sha256:     %s" % build_hash)

    # compare hashes
54
    suffix = " for Android" if for_android else ""
55
    if reference_hash == build_hash:
56 57
        print("Tor%s version %s was successfully verified! \o/" % (suffix, versions['tor']))
        return True
58
    else:
59 60
        print("Hashes for Tor%s version %s do not match! :(" % (suffix, versions['tor']))
        return False
61 62


63
def get_url(versions, for_android, fallback=False):
64
    version = get_tor_version(versions)
65 66
    directory = "tor-android" if for_android else "tor"
    file = get_final_file_name(versions, for_android)
67
    if not fallback:
68
        return "https://jcenter.bintray.com/org/briarproject/%s/%s/%s" % (directory, version, file)
69
    else:
70 71
        return "https://dl.bintray.com/briarproject/org.briarproject/org/briarproject/%s/%s/%s" % \
               (directory, version, file)
72 73 74 75


if __name__ == "__main__":
    main()