summaryrefslogtreecommitdiffhomepage
path: root/app/src/main/java/com/wireguard/config/Profile.java
blob: e04cab3ea734ce591db3ebbba0da0cabff394fbd (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
package com.wireguard.config;

import android.databinding.BaseObservable;
import android.databinding.Bindable;
import android.databinding.Observable;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableList;

import com.wireguard.android.BR;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

/**
 * Represents a wg-quick profile.
 */

public class Profile extends BaseObservable implements Copyable<Profile>, Observable {
    public static boolean isNameValid(String name) {
        final int IFNAMSIZ = 16;
        return !name.contains(" ") && name.getBytes(StandardCharsets.UTF_8).length <= IFNAMSIZ;
    }

    private final Interface iface = new Interface();
    private boolean isConnected;
    private String name;
    private final ObservableList<Peer> peers = new ObservableArrayList<>();

    public Profile(String name) {
        super();
        this.name = name;
    }

    private Profile(Profile original)
            throws IOException {
        this(original.getName());
        final byte configBytes[] = original.toString().getBytes(StandardCharsets.UTF_8);
        final ByteArrayInputStream configStream = new ByteArrayInputStream(configBytes);
        parseFrom(configStream);
    }

    public Profile copy() {
        try {
            return new Profile(this);
        } catch (IOException e) {
            return null;
        }
    }

    public Interface getInterface() {
        return iface;
    }

    @Bindable
    public boolean getIsConnected() {
        return isConnected;
    }

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

    public ObservableList<Peer> getPeers() {
        return peers;
    }

    public void parseFrom(InputStream stream)
            throws IOException {
        try (BufferedReader reader = new BufferedReader(
                new InputStreamReader(stream, StandardCharsets.UTF_8))) {
            Peer currentPeer = null;
            String line;
            while ((line = reader.readLine()) != null) {
                if (line.equals("[Interface]")) {
                    currentPeer = null;
                } else if (line.equals("[Peer]")) {
                    currentPeer = new Peer();
                    peers.add(currentPeer);
                } else if (currentPeer == null) {
                    iface.parseFrom(line);
                } else {
                    currentPeer.parseFrom(line);
                }
            }
        }
    }

    public void setIsConnected(boolean isConnected) {
        this.isConnected = isConnected;
        notifyPropertyChanged(BR.isConnected);
    }

    public void setName(String name) {
        this.name = name;
        notifyPropertyChanged(BR.name);
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder().append(iface.toString());
        for (Peer peer : peers)
            sb.append('\n').append(peer.toString());
        return sb.toString();
    }
}