summaryrefslogtreecommitdiff
path: root/app/src/main/java/se/leap/bitmaskclient/pluggableTransports/ObfsvpnClient.java
blob: dfdfbdd501cc93f264b3f8573d83f5001489cd78 (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
package se.leap.bitmaskclient.pluggableTransports;

import android.util.Log;

import client.Client;
import client.Client_;
import client.EventLogger;
import de.blinkt.openvpn.core.VpnStatus;
import de.blinkt.openvpn.core.connection.Connection;
import se.leap.bitmaskclient.base.models.Constants;
import se.leap.bitmaskclient.pluggableTransports.models.HoppingConfig;
import se.leap.bitmaskclient.pluggableTransports.models.KcpConfig;
import se.leap.bitmaskclient.pluggableTransports.models.Obfs4Options;
import se.leap.bitmaskclient.pluggableTransports.models.ObfsvpnConfig;

public class ObfsvpnClient {

    public static final int PORT = 8080;
    public static final String IP = "127.0.0.1";
    private final Object LOCK = new Object();


    private static final String TAG = ObfsvpnClient.class.getSimpleName();

    public final Client_ client;

    public ObfsvpnClient(Obfs4Options options) throws IllegalStateException {

        //FIXME: use a different strategy here
        //Basically we would want to track if the more performant transport protocol (KCP?/TCP?) usage was successful
        //if so, we stick to it, otherwise we flip the flag
        boolean kcpEnabled = Constants.KCP.equals(options.transport.getProtocols()[0]);
        boolean hoppingEnabled = options.transport.getTransportType() == Connection.TransportType.OBFS4_HOP;
        if (!hoppingEnabled && (options.transport.getPorts() == null || options.transport.getPorts().length == 0)) {
            throw new IllegalStateException("obf4 based transport has no bridge ports configured");
        }
        KcpConfig kcpConfig = new KcpConfig(kcpEnabled);
        HoppingConfig hoppingConfig = new HoppingConfig(hoppingEnabled,IP+":"+PORT, options, 10, 10);
        ObfsvpnConfig obfsvpnConfig = new ObfsvpnConfig(IP+":"+PORT, hoppingConfig, kcpConfig, options.bridgeIP, options.transport.getPorts()[0], options.transport.getOptions().getCert() );
        try {
            Log.d(TAG, obfsvpnConfig.toString());
            client = Client.newFFIClient(obfsvpnConfig.toString());
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }

    public int start() {
        synchronized (LOCK) {
            new Thread(() -> {
                try {
                    client.setEventLogger(new EventLogger() {
                        @Override
                        public void error(String s) {
                            VpnStatus.logError("[obfs4-client] " + s);
                        }

                        @Override
                        public void log(String state, String message) {
                            VpnStatus.logDebug("[obfs4-client] " + state + ": " + message);
                        }
                    });
                    client.start();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
            return PORT;
        }
    }

    public void stop() {
        synchronized (LOCK) {
            try {
                client.stop();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                client.setEventLogger(null);
            }
        }
    }

    public boolean isStarted() {
        return client.isStarted();
    }
}