summaryrefslogtreecommitdiffhomepage
path: root/app/src/main/java/com/wireguard/android/model/Tunnel.java
blob: b196eaa5a1d9ecfbfb29a5e8310654349cce9cb4 (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
package com.wireguard.android.model;

import android.databinding.BaseObservable;
import android.databinding.Bindable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.wireguard.android.BR;
import com.wireguard.android.backend.Backend;
import com.wireguard.android.configStore.ConfigStore;
import com.wireguard.android.util.ExceptionLoggers;
import com.wireguard.config.Config;

import org.threeten.bp.Instant;

import java.util.Objects;
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 Comparable<Tunnel> {
    public static final int NAME_MAX_LENGTH = 16;
    private static final Pattern NAME_PATTERN = Pattern.compile("[a-zA-Z0-9_=+.-]{1,16}");
    private static final String TAG = Tunnel.class.getSimpleName();

    private final Backend backend;
    private final ConfigStore configStore;
    private final String name;
    private Config config;
    private Instant lastStateChange = Instant.EPOCH;
    private State state = State.UNKNOWN;
    private Statistics statistics;

    Tunnel(@NonNull final Backend backend, @NonNull final ConfigStore configStore,
           @NonNull final String name, @Nullable final Config config) {
        this.backend = backend;
        this.configStore = configStore;
        this.name = name;
        this.config = config;
    }

    public static boolean isNameValid(final CharSequence name) {
        return name != null && NAME_PATTERN.matcher(name).matches();
    }

    @Override
    public int compareTo(@NonNull final Tunnel tunnel) {
        return name.compareTo(tunnel.name);
    }

    @Bindable
    public Config getConfig() {
        if (config == null)
            getConfigAsync().whenComplete(ExceptionLoggers.D);
        return config;
    }

    public CompletionStage<Config> getConfigAsync() {
        if (config == null)
            return configStore.load(name).thenApply(this::setConfigInternal);
        return CompletableFuture.completedFuture(config);
    }

    @Bindable
    public Instant getLastStateChange() {
        return lastStateChange;
    }

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

    @Bindable
    public State getState() {
        if (state == State.UNKNOWN)
            getStateAsync().whenComplete(ExceptionLoggers.D);
        return state;
    }

    public CompletionStage<State> getStateAsync() {
        if (state == State.UNKNOWN)
            return backend.getState(this).thenApply(this::setStateInternal);
        return CompletableFuture.completedFuture(state);
    }

    @Bindable
    public Statistics getStatistics() {
        // FIXME: Check age of statistics.
        if (statistics == null)
            getStatisticsAsync().whenComplete(ExceptionLoggers.D);
        return statistics;
    }

    public CompletionStage<Statistics> getStatisticsAsync() {
        // FIXME: Check age of statistics.
        if (statistics == null)
            return backend.getStatistics(this).thenApply(this::setStatisticsInternal);
        return CompletableFuture.completedFuture(statistics);
    }

    private void onStateChanged(final State oldState, final State newState) {
        if (oldState != State.UNKNOWN) {
            lastStateChange = Instant.now();
            notifyPropertyChanged(BR.lastStateChange);
        }
        if (newState != State.UP)
            setStatisticsInternal(null);
    }

    public CompletionStage<Config> setConfig(@NonNull final Config config) {
        if (!config.equals(this.config)) {
            return backend.applyConfig(this, config)
                    .thenCompose(cfg -> configStore.save(name, cfg))
                    .thenApply(this::setConfigInternal);
        }
        return CompletableFuture.completedFuture(this.config);
    }

    private Config setConfigInternal(final Config config) {
        if (Objects.equals(this.config, config))
            return config;
        this.config = config;
        notifyPropertyChanged(BR.config);
        return config;
    }

    public CompletionStage<State> setState(@NonNull final State state) {
        if (state != this.state)
            return backend.setState(this, state)
                    .thenApply(this::setStateInternal);
        return CompletableFuture.completedFuture(this.state);
    }

    private State setStateInternal(final State state) {
        if (Objects.equals(this.state, state))
            return state;
        onStateChanged(this.state, state);
        this.state = state;
        notifyPropertyChanged(BR.state);
        return state;
    }

    private Statistics setStatisticsInternal(final Statistics statistics) {
        if (Objects.equals(this.statistics, statistics))
            return statistics;
        this.statistics = statistics;
        notifyPropertyChanged(BR.statistics);
        return statistics;
    }

    public enum State {
        DOWN,
        TOGGLE,
        UNKNOWN,
        UP
    }

    public static class Statistics extends BaseObservable {
    }
}