summaryrefslogtreecommitdiff
path: root/app/src/main/java/de/blinkt/openvpn/core/connection/Connection.java
blob: f60e7333f3564f6b816c15c886ffcbb9bb3bb626 (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
/*
 * 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 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 TransportType {
        OBFS4("obfs4"),
        OPENVPN("openvpn"),
        OBFS4_KCP("obfs4-1"),

        PT("metaTransport");

        String transport;

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

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

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

        public TransportType getMetaType() {
            if (this == OBFS4 || this == OBFS4_KCP || 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();
}