summaryrefslogtreecommitdiff
path: root/app/src/main/java/de/blinkt/openvpn/core/connection/Connection.java
blob: 6dc78a309992ba3fd112f053b3c003405c824ee3 (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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
 * Copyright (c) 2012-2016 Arne Schwabe
 * Distributed under the GNU GPL v2 with additional terms. For full terms see the file doc/LICENSE.txt
 */

package de.blinkt.openvpn.core.connection;

import static de.blinkt.openvpn.core.connection.Connection.TransportType.*;

import android.text.TextUtils;

import com.google.gson.annotations.JsonAdapter;

import java.io.Serializable;
import java.util.Locale;

@JsonAdapter(ConnectionAdapter.class)
public abstract class Connection implements Serializable, Cloneable {
    private String mServerName = "openvpn.example.com";
    private String mServerPort = "1194";
    private boolean mUseUdp = true;
    private String mCustomConfiguration = "";
    private boolean mUseCustomConfig = false;
    private boolean mEnabled = true;
    private int mConnectTimeout = 0;
    private static final int CONNECTION_DEFAULT_TIMEOUT = 120;
    private ProxyType mProxyType = ProxyType.NONE;
    private String mProxyName = "proxy.example.com";
    private String mProxyPort = "8080";

    private boolean mUseProxyAuth;
    private String mProxyAuthUser = null;
    private String mProxyAuthPassword = null;

    public enum ProxyType {
        NONE,
        HTTP,
        SOCKS5,
        ORBOT
    }

    public enum TransportProtocol {
        UDP("udp"),
        TCP("tcp"),
        KCP("kcp");

        final String protocol;

        TransportProtocol(String transportProtocol) {
            this.protocol = transportProtocol;
        }

        @Override
        public String toString() {
            return protocol;
        }
    }
    public enum TransportType {
        OBFS4("obfs4"),
        OBFS4_HOP("obfs4Hop"),
        OPENVPN("openvpn"),

        PT("metaTransport");

        final String transport;

        TransportType(String transportType) {
            this.transport = transportType;
        }

        @Override
        public String toString() {
            return transport;
        }

        public int toInt() {
            switch (this) {
                case PT:
                    return 0;
                case OPENVPN:
                    return 1;
                case OBFS4:
                    return 2;
                case OBFS4_HOP:
                    return 3;
                default:
                    return -1;
            }
        }

        public static TransportType fromString(String value) {
            switch (value) {
                case "obfs4":
                    return OBFS4;
                case "obfs4-hop":
                    return OBFS4_HOP;
                case "metaTransport":
                    return PT;
                case "openvpn":
                    return OPENVPN;
                default:
                    throw new IllegalArgumentException(value + " is not a valid value for TransportType.");
            }
        }

        public static TransportType fromInt(int value) {
            switch (value) {
                case 0:
                    return PT;
                case 1:
                    return OPENVPN;
                case 2:
                    return OBFS4;
                case 3:
                    return OBFS4_HOP;
                default:
                    return null;
            }
        }

        public boolean isPluggableTransport() {
            return this == OBFS4 || this == OBFS4_HOP || this == PT;
        }

        public TransportType getMetaType() {
            if (this == OBFS4 || this == OBFS4_HOP || this == PT) {
                return PT;
            }
            return OPENVPN;
        }
    }


    private static final long serialVersionUID = 92031902903829089L;


    public String getConnectionBlock(boolean isOpenVPN3) {
        String cfg = "";

        // Server Address
        cfg += "remote ";
        cfg += mServerName;
        cfg += " ";
        cfg += mServerPort;
        if (mUseUdp)
            cfg += " udp\n";
        else
            cfg += " tcp-client\n";

        if (mConnectTimeout != 0)
            cfg += String.format(Locale.US, " connect-timeout  %d\n", mConnectTimeout);

        // OpenVPN 2.x manages proxy connection via management interface
        if ((isOpenVPN3 || usesExtraProxyOptions()) && mProxyType == ProxyType.HTTP)
        {
            cfg+=String.format(Locale.US,"http-proxy %s %s\n", mProxyName, mProxyPort);
            if (mUseProxyAuth)
                cfg+=String.format(Locale.US, "<http-proxy-user-pass>\n%s\n%s\n</http-proxy-user-pass>\n", mProxyAuthUser, mProxyAuthPassword);
        }
        if (usesExtraProxyOptions() && mProxyType == ProxyType.SOCKS5) {
            cfg+=String.format(Locale.US,"socks-proxy %s %s\n", mProxyName, mProxyPort);
        }

        if (!TextUtils.isEmpty(mCustomConfiguration) && mUseCustomConfig) {
            cfg += mCustomConfiguration;
            cfg += "\n";
        }


        return cfg;
    }

    public boolean usesExtraProxyOptions() {
        return (mUseCustomConfig && mCustomConfiguration.contains("http-proxy-option "));
    }


    @Override
    public Connection clone() throws CloneNotSupportedException {
        return (Connection) super.clone();
    }

    public boolean isOnlyRemote() {
        return TextUtils.isEmpty(mCustomConfiguration) || !mUseCustomConfig;
    }

    public int getTimeout() {
        if (mConnectTimeout <= 0)
            return CONNECTION_DEFAULT_TIMEOUT;
        else
            return mConnectTimeout;
    }

    public String getServerName() {
        return mServerName;
    }

    public void setServerName(String mServerName) {
        this.mServerName = mServerName;
    }

    public String getServerPort() {
        return mServerPort;
    }

    public void setServerPort(String serverPort) {
        this.mServerPort = serverPort;
    }

    public boolean isUseUdp() {
        return mUseUdp;
    }

    public void setUseUdp(boolean useUdp) {
        this.mUseUdp = useUdp;
    }

    public String getCustomConfiguration() {
        return mCustomConfiguration;
    }

    public void setCustomConfiguration(String customConfiguration) {
        this.mCustomConfiguration = customConfiguration;
    }

    public boolean isUseCustomConfig() {
        return mUseCustomConfig;
    }

    public void setUseCustomConfig(boolean useCustomConfig) {
        this.mUseCustomConfig = useCustomConfig;
    }

    public boolean isEnabled() {
        return mEnabled;
    }

    public void setEnabled(boolean enabled) {
        this.mEnabled = enabled;
    }

    public int getConnectTimeout() {
        return mConnectTimeout;
    }

    public void setConnectTimeout(int connectTimeout) {
        this.mConnectTimeout = connectTimeout;
    }

    public ProxyType getProxyType() {
        return mProxyType;
    }

    public void setProxyType(ProxyType proxyType) {
        this.mProxyType = proxyType;
    }

    public String getProxyName() {
        return mProxyName;
    }

    public void setProxyName(String proxyName) {
        this.mProxyName = proxyName;
    }

    public String getProxyPort() {
        return mProxyPort;
    }

    public void setProxyPort(String proxyPort) {
        this.mProxyPort = proxyPort;
    }

    public boolean isUseProxyAuth() {
        return mUseProxyAuth;
    }

    public void setUseProxyAuth(boolean useProxyAuth) {
        this.mUseProxyAuth = useProxyAuth;
    }

    public String getProxyAuthUser() {
        return mProxyAuthUser;
    }

    public void setProxyAuthUser(String proxyAuthUser) {
        this.mProxyAuthUser = proxyAuthUser;
    }

    public String getProxyAuthPassword() {
        return mProxyAuthPassword;
    }

    public void setProxyAuthPassword(String proxyAuthPassword) {
        this.mProxyAuthPassword = proxyAuthPassword;
    }

    public abstract TransportType getTransportType();
}