aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/app/src/main/java/com/wireguard/android/model/Tunnel.java
blob: 87b607d0133f0b42740468b50ea3a23dd10cbe86 (plain) (blame)
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
/*
 * Copyright © 2017-2019 WireGuard LLC. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

package com.wireguard.android.model;

import android.os.SystemClock;
import android.util.Pair;

import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;
import androidx.annotation.Nullable;

import com.wireguard.android.BR;
import com.wireguard.android.util.ExceptionLoggers;
import com.wireguard.config.Config;
import com.wireguard.crypto.Key;
import com.wireguard.util.Keyed;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

import java9.util.concurrent.CompletableFuture;
import java9.util.concurrent.CompletionStage;

/**
 * Encapsulates the volatile and nonvolatile state of a WireGuard tunnel.
 */

public class Tunnel extends BaseObservable implements Keyed<String> {
    public static final int NAME_MAX_LENGTH = 15;
    private static final Pattern NAME_PATTERN = Pattern.compile("[a-zA-Z0-9_=+.-]{1,15}");

    private final TunnelManager manager;
    @Nullable private Config config;
    private String name;
    private State state;
    @Nullable private Statistics statistics;

    Tunnel(final TunnelManager manager, final String name,
           @Nullable final Config config, final State state) {
        this.manager = manager;
        this.name = name;
        this.config = config;
        this.state = state;
    }

    public static boolean isNameInvalid(final CharSequence name) {
        return !NAME_PATTERN.matcher(name).matches();
    }

    public CompletionStage<Void> delete() {
        return manager.delete(this);
    }

    @Bindable
    @Nullable
    public Config getConfig() {
        if (config == null)
            manager.getTunnelConfig(this).whenComplete(ExceptionLoggers.E);
        return config;
    }

    public CompletionStage<Config> getConfigAsync() {
        if (config == null)
            return manager.getTunnelConfig(this);
        return CompletableFuture.completedFuture(config);
    }

    @Override
    public String getKey() {
        return name;
    }

    @Bindable
    public String getName() {
        return name;
    }

    @Bindable
    public State getState() {
        return state;
    }

    public CompletionStage<State> getStateAsync() {
        return TunnelManager.getTunnelState(this);
    }

    @Bindable
    @Nullable
    public Statistics getStatistics() {
        if (statistics == null || statistics.isStale())
            TunnelManager.getTunnelStatistics(this).whenComplete(ExceptionLoggers.E);
        return statistics;
    }

    public CompletionStage<Statistics> getStatisticsAsync() {
        if (statistics == null || statistics.isStale())
            return TunnelManager.getTunnelStatistics(this);
        return CompletableFuture.completedFuture(statistics);
    }

    Config onConfigChanged(final Config config) {
        this.config = config;
        notifyPropertyChanged(BR.config);
        return config;
    }

    public String onNameChanged(final String name) {
        this.name = name;
        notifyPropertyChanged(BR.name);
        return name;
    }

    State onStateChanged(final State state) {
        if (state != State.UP)
            onStatisticsChanged(null);
        this.state = state;
        notifyPropertyChanged(BR.state);
        return state;
    }

    @Nullable
    Statistics onStatisticsChanged(@Nullable final Statistics statistics) {
        this.statistics = statistics;
        notifyPropertyChanged(BR.statistics);
        return statistics;
    }

    public CompletionStage<Config> setConfig(final Config config) {
        if (!config.equals(this.config))
            return manager.setTunnelConfig(this, config);
        return CompletableFuture.completedFuture(this.config);
    }

    public CompletionStage<String> setName(final String name) {
        if (!name.equals(this.name))
            return manager.setTunnelName(this, name);
        return CompletableFuture.completedFuture(this.name);
    }

    public CompletionStage<State> setState(final State state) {
        if (state != this.state)
            return manager.setTunnelState(this, state);
        return CompletableFuture.completedFuture(this.state);
    }

    public enum State {
        DOWN,
        TOGGLE,
        UP;

        public static State of(final boolean running) {
            return running ? UP : DOWN;
        }
    }

    public static class Statistics extends BaseObservable {
        private long lastTouched = SystemClock.elapsedRealtime();
        private final Map<Key, Pair<Long, Long>> peerBytes = new HashMap<>();

        public void add(final Key key, final long rx, final long tx) {
            peerBytes.put(key, Pair.create(rx, tx));
            lastTouched = SystemClock.elapsedRealtime();
        }

        private boolean isStale() {
            return SystemClock.elapsedRealtime() - lastTouched > 900;
        }

        public Key[] peers() {
            return peerBytes.keySet().toArray(new Key[0]);
        }

        public long peerRx(final Key peer) {
            if (!peerBytes.containsKey(peer))
                return 0;
            return peerBytes.get(peer).first;
        }

        public long peerTx(final Key peer) {
            if (!peerBytes.containsKey(peer))
                return 0;
            return peerBytes.get(peer).second;
        }

        public long totalRx() {
            long rx = 0;
            for (final Pair<Long, Long> val : peerBytes.values()) {
                rx += val.first;
            }
            return rx;
        }

        public long totalTx() {
            long tx = 0;
            for (final Pair<Long, Long> val : peerBytes.values()) {
                tx += val.second;
            }
            return tx;
        }
    }
}