summaryrefslogtreecommitdiff
path: root/app/src/main/java/se/leap/bitmaskclient/base/models/Transport.java
blob: 33fbbf7af31b3aae3fcb23a3401134c08d89a6e3 (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
package se.leap.bitmaskclient.base.models;

import androidx.annotation.Nullable;

import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.SerializedName;

import org.json.JSONObject;

import java.io.Serializable;

import de.blinkt.openvpn.core.connection.Connection;

public class Transport implements Serializable {
    private String type;
    private String[] protocols;
    @Nullable
    private String[] ports;
    @Nullable
    private Options options;

    public Transport(String type, String[] protocols, String[] ports, String cert) {
        this(type, protocols, ports, new Options(cert, "0"));
    }

    public Transport(String type, String[] protocols, String[] ports, Options options) {
        this.type = type;
        this.protocols = protocols;
        this.ports = ports;
        this.options = options;
    }

    public String getType() {
        return type;
    }

    public Connection.TransportType getTransportType() {
        return Connection.TransportType.fromString(type);
    }

    public String[] getProtocols() {
        return protocols;
    }

    @Nullable
    public String[] getPorts() {
        return ports;
    }

    @Nullable
    public Options getOptions() {
        return options;
    }

    @Override
    public String toString() {
        return new Gson().toJson(this);
    }

    public static Transport fromJson(JSONObject json) {
        GsonBuilder builder = new GsonBuilder();
        return builder.
                setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).
                create().
                fromJson(json.toString(), Transport.class);
    }

    public static class Options implements Serializable {
        @Nullable
        private String cert;
        @SerializedName("iatMode")
        private String iatMode;

        @Nullable
        private Endpoint[] endpoints;

        private boolean experimental;

        private int portSeed;

        private int portCount;


        public Options(String cert, String iatMode) {
            this.cert = cert;
            this.iatMode = iatMode;
        }

        public Options(String iatMode, Endpoint[] endpoints, int portSeed, int portCount, boolean experimental) {
            this(iatMode, endpoints, null, portSeed, portCount, experimental);
        }

        public Options(String iatMode, Endpoint[] endpoints, String cert, int portSeed, int portCount, boolean experimental) {
            this.iatMode = iatMode;
            this.endpoints = endpoints;
            this.portSeed = portSeed;
            this.portCount = portCount;
            this.experimental = experimental;
            this.cert = cert;
        }


        @Nullable
        public String getCert() {
            return cert;
        }

        public String getIatMode() {
            return iatMode;
        }

        @Nullable
        public Endpoint[] getEndpoints() {
            return endpoints;
        }

        public boolean isExperimental() {
            return experimental;
        }

        public int getPortSeed() {
            return portSeed;
        }

        public int getPortCount() {
            return portCount;
        }

        @Override
        public String toString() {
            return new Gson().toJson(this);
        }
    }


    public static class Endpoint implements Serializable {
        private String ip;
        private String cert;

        public Endpoint(String ip, String cert) {
            this.ip = ip;
            this.cert = cert;
        }

        @Override
        public String toString() {
            return new Gson().toJson(this);
        }

        public String getIp() {
            return ip;
        }

        public String getCert() {
            return cert;
        }
    }

}