summaryrefslogtreecommitdiff
path: root/app/src/main/java/se/leap/bitmaskclient/tor/ClientTransportPlugin.java
blob: 9e3828dee73b4a861e5fb9d4412c32fc4da5b2e0 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package se.leap.bitmaskclient.tor;

import android.content.Context;
import android.util.Log;

import androidx.annotation.Nullable;

import org.torproject.jni.ClientTransportPluginInterface;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.ref.WeakReference;
import java.util.HashMap;

import IPtProxy.IPtProxy;

public class ClientTransportPlugin implements ClientTransportPluginInterface {
    public static String TAG = ClientTransportPlugin.class.getSimpleName();

    private HashMap<String, String> mFronts;
    private final WeakReference<Context> contextRef;
    private long snowflakePort = -1;

    public ClientTransportPlugin(Context context) {
        this.contextRef = new WeakReference<>(context);
        loadCdnFronts(context);
    }

    @Override
    public void start() {
        Context context = contextRef.get();
        if (context == null) {
            return;
        }
        File logfile = new File(context.getApplicationContext().getCacheDir(), "snowflake.log");
        Log.d(TAG, "logfile at " + logfile.getAbsolutePath());
        if (!logfile.exists()) {
            try {
                logfile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        //this is using the current, default Tor snowflake infrastructure
        String target = getCdnFront("snowflake-target");
        String front = getCdnFront("snowflake-front");
        String stunServer = getCdnFront("snowflake-stun");
        Log.d(TAG, "startSnowflake. target: " + target + ", front:" + front + ", stunServer" + stunServer);
        snowflakePort = IPtProxy.startSnowflake( stunServer, target, front, logfile.getAbsolutePath(), false, false, true, 1);
        Log.d(TAG, "startSnowflake running on port: " + snowflakePort);
    }

    @Override
    public void stop() {
        IPtProxy.stopSnowflake();
        snowflakePort = -1;
    }

    @Override
    public String getTorrc() {
        return "UseBridges 1\n" +
                "ClientTransportPlugin snowflake socks5 127.0.0.1:" + snowflakePort + "\n" +
                "Bridge snowflake 192.0.2.3:1";
    }

    private void loadCdnFronts(Context context) {
        if (mFronts == null) {
            mFronts = new HashMap<>();
        }
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(context.getAssets().open("fronts")));
            String line;
            while (true) {
                line = reader.readLine();
                if (line == null) break;
                String[] front = line.split(" ");
                mFronts.put(front[0], front[1]);
                Log.d(TAG, "front: " + front[0] + ", " + front[1]);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Nullable
    private String getCdnFront(String service) {
        if (mFronts != null) {
            return mFronts.get(service);
        }
        return null;
    }
}