verify-obfs4proxy.py 2.63 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
#!/usr/bin/env python3
import os
import sys
from subprocess import check_call, CalledProcessError

from utils import get_sha256, fail, get_build_versions, get_final_file_name, \
    get_version, get_obfs4_version


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

    verified = verify(version, for_android=False)
    verified_android = verify(version, for_android=True)
    if verified and verified_android:
        sys.exit(0)
    else:
        sys.exit(1)


def verify(version, for_android):
    # get version and versions of its dependencies
    tool_version, versions = get_build_versions('obfs4proxy', version)

    # download reference binary
    file_name = get_final_file_name(versions, for_android)
    os.makedirs('reference', exist_ok=True)
    reference_file_name = os.path.join('reference', file_name)
    try:
        # try downloading from jcenter
        check_call(['wget', '--no-verbose', get_url(versions, for_android), '-O',
                    reference_file_name])
    except CalledProcessError:
        # try fallback to bintray
        print("Warning: Download from jcenter failed. Trying bintray directly...")
        check_call(['wget', '--no-verbose', get_url(versions, for_android, fallback=True), '-O',
                    reference_file_name])

    # check if it was already build
    if not os.path.isfile(file_name):
        # build it first
        if version is None:
            check_call(['./build-obfs4proxy.py'])
        else:
            check_call(['./build-obfs4proxy.py', version])

    # calculate hashes for both files
    reference_hash = get_sha256(reference_file_name)
    build_hash = get_sha256(file_name)
    print("Reference sha256: %s" % reference_hash)
    print("Build sha256:     %s" % build_hash)

    # compare hashes
    suffix = " for Android" if for_android else ""
    if reference_hash == build_hash:
        print("obfs4proxy%s version %s was successfully verified! \o/" % (suffix, tool_version))
        return True
    else:
        print("Hashes for obfs4proxy%s version %s do not match! :(" % (suffix, tool_version))
        return False


def get_url(versions, for_android, fallback=False):
    version = get_obfs4_version(versions)
    directory = "obfs4proxy-android" if for_android else "obfs4proxy"
    file = get_final_file_name(versions, for_android)
    if not fallback:
        return "https://jcenter.bintray.com/org/briarproject/%s/%s/%s" % (directory, version, file)
    else:
        return "https://dl.bintray.com/briarproject/org.briarproject/org/briarproject/%s/%s/%s" % \
               (directory, version, file)


if __name__ == "__main__":
    main()