aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/orbot/OrbotStatusReceiver.java
blob: 743fbdf16e2b3d479ae1897b4d5ea3d5c9d0e145 (plain)
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
77
78
package org.sufficientlysecure.keychain.util.orbot;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import org.sufficientlysecure.keychain.util.Log;

/**
 * Created by vanitas on 11.05.16.
 * BroadcastReceiver that receives Orbots status
 */
public class OrbotStatusReceiver extends BroadcastReceiver {

    private static final String TAG = "OrbStatRec";

    //TODO: These two Strings are missing in older versions of NetCipher.
    //TODO: Once they are present in OrbotHelper (not ProxyHelper) point to OrbotHelpers Strings instead.
    public final static String EXTRA_PROXY_PORT_HTTP = "org.torproject.android.intent.extra.HTTP_PROXY_PORT";
    public final static String EXTRA_PROXY_PORT_SOCKS = "org.torproject.android.intent.extra.SOCKS_PROXY_PORT";

    //Variables representing Orbots status
    private boolean torRunning;
    private int proxy_port_http;
    private int proxy_port_socks;

    private static OrbotStatusReceiver instance;

    public OrbotStatusReceiver() {
        instance = this;
    }

    public static OrbotStatusReceiver getInstance() {
        if(instance == null) instance = new OrbotStatusReceiver();
        return instance;
    }


    @Override
    public void onReceive(Context context, Intent intent) {
        if (OrbotHelper.ACTION_STATUS.equals(intent.getAction())) {
            Log.i(TAG, context.getPackageName() + " received intent : " + intent.getAction() + " " + intent.getPackage());
            String status = intent.getStringExtra(OrbotHelper.EXTRA_STATUS) + " (" + intent.getStringExtra(OrbotHelper.EXTRA_PACKAGE_NAME) + ")";
            this.torRunning = (intent.getStringExtra(OrbotHelper.EXTRA_STATUS).equals(OrbotHelper.STATUS_ON));

            Log.d(TAG, "Orbot status: "+status);
            if(torRunning){
                Bundle extras = intent.getExtras();

                if (extras.containsKey(EXTRA_PROXY_PORT_HTTP)) {
                    this.proxy_port_http = extras.getInt(EXTRA_PROXY_PORT_HTTP, -1);
                    Log.i(TAG, "Http proxy set to "+proxy_port_http);
                }

                if (extras.containsKey(EXTRA_PROXY_PORT_SOCKS)) {
                    this.proxy_port_socks = extras.getInt(EXTRA_PROXY_PORT_SOCKS, -1);
                    Log.i(TAG, "Socks proxy set to "+proxy_port_socks);
                }
            }
        }
    }

    public boolean isTorRunning(Context context) {
        OrbotHelper.requestStartTor(context);
        return this.torRunning;
    }

    public int getProxyPortHttp(Context context) {
        OrbotHelper.requestStartTor(context);
        return this.proxy_port_http;
    }

    public int getProxyPortSocks(Context context) {
        OrbotHelper.requestStartTor(context);
        return this.proxy_port_socks;
    }
}