summaryrefslogtreecommitdiff
path: root/app/src/main/java/se/leap/bitmaskclient/pluggableTransports/Dispatcher.java
blob: 8e787b575f2107007284006f9f190176a715cd98 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/**
 * Copyright (c) 2019 LEAP Encryption Access Project and contributers
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package se.leap.bitmaskclient.pluggableTransports;

import android.content.Context;
import android.support.annotation.WorkerThread;
import android.text.TextUtils;
import android.util.Log;

import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;


/**
 * Created by cyberta on 22.02.19.
 */

public class Dispatcher {
    private static final String ASSET_KEY = "piedispatcher";
    public static final String DISPATCHER_PORT = "4430";
    public static final String DISPATCHER_IP = "127.0.0.1";
    private static final String TAG = Dispatcher.class.getName();
    private final String remoteIP;
    private final String remotePort;
    private final String certificate;
    private final String iatMode;
    private File fileDispatcher;
    private Context context;
    private Thread dispatcherThread = null;
    private int dispatcherPid = -1;

    public Dispatcher(Context context, Obfs4Options obfs4Options) {
        this.context = context.getApplicationContext();
        this.remoteIP = obfs4Options.remoteIP;
        this.remotePort = obfs4Options.remotePort;
        this.certificate = obfs4Options.cert;
        this.iatMode = obfs4Options.iatMode;
    }

    @WorkerThread
    public void initSync() {
        try {
            fileDispatcher = installDispatcher();

            // start dispatcher
            dispatcherThread = new Thread(() -> {
                try {
                    StringBuilder dispatcherLog = new StringBuilder();
                    String dispatcherCommand = fileDispatcher.getCanonicalPath() +
                            " -transparent" +
                            " -client" +
                            " -state " + context.getFilesDir().getCanonicalPath() + "/state" +
                            " -target " + remoteIP + ":" + remotePort +
                            " -transports obfs4" +
                            " -options \"" + String.format("{\\\"cert\\\": \\\"%s\\\", \\\"iatMode\\\": \\\"%s\\\"}\"", certificate, iatMode) +
                            " -logLevel DEBUG -enableLogging" +
                            " -proxylistenaddr "+ DISPATCHER_IP + ":" + DISPATCHER_PORT;

                    Log.d(TAG, "dispatcher command: " + dispatcherCommand);
                    runBlockingCmd(new String[]{dispatcherCommand}, dispatcherLog);
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
            dispatcherThread.start();

            // get pid of dispatcher, try several times in case the dispatcher
            // process is not spawned yet
            StringBuilder log = new StringBuilder();
            String pidCommand = "ps | grep piedispatcher";
            for (int i = 0; i < 5; i++) {
                runBlockingCmd(new String[]{pidCommand}, log);
                if (!TextUtils.isEmpty(log))  {
                    break;
                }
                Thread.sleep(100);
            }

            String output  = log.toString();
            StringTokenizer st = new StringTokenizer(output, " ");
            st.nextToken(); // proc owner
            dispatcherPid = Integer.parseInt(st.nextToken().trim());
        } catch(Exception e){
            if (dispatcherThread.isAlive()) {
                Log.e(TAG, e.getMessage() + ". Shutting down Dispatcher thread.");
                stop();
            }
        }
    }

    public String getPort() {
        return DISPATCHER_PORT;
    }

    public void stop() {
        Log.d(TAG, "Shutting down Dispatcher thread.");
        if (dispatcherThread != null && dispatcherThread.isAlive()) {
            try {
                killProcess(dispatcherPid);
            } catch (Exception e) {
                e.printStackTrace();
            }
            dispatcherThread.interrupt();
        }
    }

    private void killProcess(int pid) throws Exception {
        String killPid = "kill -9 " + pid;
        runCmd(new String[]{killPid}, null, false);
    }

    public boolean isRunning() {
        return dispatcherThread != null && dispatcherThread.isAlive();
    }

    private File installDispatcher(){
        File fileDispatcher = null;
        BinaryInstaller bi = new BinaryInstaller(context,context.getFilesDir());

        String arch = System.getProperty("os.arch");
        if (arch.contains("arm"))
            arch = "armeabi-v7a";
        else
            arch = "x86";

        try {
            fileDispatcher = bi.installResource(arch, ASSET_KEY, false);
        } catch (Exception ioe) {
            Log.d(TAG,"Couldn't install dispatcher: " + ioe);
        }

        return fileDispatcher;
    }

    @WorkerThread
    private void runBlockingCmd(String[] cmds, StringBuilder log) throws Exception {
        runCmd(cmds, log, true);
    }

    @WorkerThread
    private int runCmd(String[] cmds, StringBuilder log,
                       boolean waitFor) throws Exception {

        int exitCode = -1;
        Process proc = Runtime.getRuntime().exec("sh");
        OutputStreamWriter out = new OutputStreamWriter(proc.getOutputStream());

        try {
            for (String cmd : cmds) {
                Log.d(TAG, "executing CMD: " + cmd);
                out.write(cmd);
                out.write("\n");
            }

            out.flush();
            out.write("exit\n");
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            out.close();
        }

        if (waitFor) {
            // Consume the "stdout"
            InputStreamReader reader = new InputStreamReader(proc.getInputStream());
            readToLogString(reader, log);

            // Consume the "stderr"
            reader = new InputStreamReader(proc.getErrorStream());
            readToLogString(reader, log);

            try {
                exitCode = proc.waitFor();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        return exitCode;
    }

    private void readToLogString(InputStreamReader reader, StringBuilder log) throws IOException {
        final char buf[] = new char[10];
        int read = 0;
        try {
            while ((read = reader.read(buf)) != -1) {
                if (log != null)
                    log.append(buf, 0, read);
            }
        } catch (IOException e) {
            reader.close();
            throw new IOException(e);
        }
        reader.close();
    }
}