aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/tunnel/src/main/java/com/wireguard/android/backend/Tunnel.java
blob: 766df443e67703d2d1aa311654801ef8dcc6bd78 (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
/*
 * Copyright © 2017-2023 WireGuard LLC. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

package com.wireguard.android.backend;

import com.wireguard.util.NonNullForAll;

import java.util.regex.Pattern;

/**
 * Represents a WireGuard tunnel.
 */

@NonNullForAll
public interface Tunnel {
    int NAME_MAX_LENGTH = 15;
    Pattern NAME_PATTERN = Pattern.compile("[a-zA-Z0-9_=+.-]{1,15}");

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

    /**
     * Get the name of the tunnel, which should always pass the !isNameInvalid test.
     *
     * @return The name of the tunnel.
     */
    String getName();

    /**
     * React to a change in state of the tunnel. Should only be directly called by Backend.
     *
     * @param newState The new state of the tunnel.
     */
    void onStateChange(State newState);

    /**
     * Enum class to represent all possible states of a {@link Tunnel}.
     */
    enum State {
        DOWN,
        TOGGLE,
        UP;

        /**
         * Get the state of a {@link Tunnel}
         *
         * @param running boolean indicating if the tunnel is running.
         * @return State of the tunnel based on whether or not it is running.
         */
        public static State of(final boolean running) {
            return running ? UP : DOWN;
        }
    }
}