aboutsummaryrefslogtreecommitdiff
path: root/ShareGuard.Web/Components/Pages/Admin.razor
blob: 3884630b5bffd70eb836b050ff8fa788df7831ed (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
@page "/admin/"
@using ShareGuard.Web.DbModels
@rendermode InteractiveServer

<PageTitle>ShareGuard Admin</PageTitle>

<h2>ShareGuard Admin</h2>

<h5>Generate Share-Link</h5>
<div class="field label round border">
    <input type="text" placeholder=" " @onchange="args => _newName = args.Value!.ToString()!">
    <label>Name</label>
</div>
<div class="field label round border">
    <input type="text" placeholder=" " @onchange="args => _newIpAddress = args.Value!.ToString()!">
    <label>IP Address(es)</label>
</div>
<button class="border small-round large small-elevate" style="margin-top: 1em;" @onclick="GenerateNewLink">
    <i>add_link</i>
    <span>Generate</span>
</button>

<h5>WG-Quick Template</h5>
<p>You can use the following placeholders:
    <br/><code>{ip}</code> for the peer's ip address(es)
    <br/><code>{publicKey}</code> for the peer's public key
    <br/><code>{privateKey}</code> for the peer's private key</p>
<div class="field extra border">
    <textarea @onchange="args => _template = args.Value!.ToString()!">@_template</textarea>
    <output>This template will be generated for your users.</output>
</div>
<button class="border small-round large small-elevate" style="margin-top: 1em;" @onclick="SaveTemplate">
    <i>save</i>
    <span>Save</span>
</button>

@if (_links != null)
{
    <h5>History</h5>
    <table class="border medium-space">
        <thead>
        <tr>
            <th>Name</th>
            <th>Token</th>
            <th>Client Public Key</th>
            <th>IP Address(es)</th>
            <th>Actions</th>
        </tr>
        </thead>
        <tbody>
        @foreach (var link in _links)
        {
            <tr>
                <td>@link.Name</td>
                <td>@link.Token</td>
                @if (link.PeerPublicKey == null)
                {
                    <td>Pending</td>
                }
                else
                {
                    <td><code>@link.PeerPublicKey</code></td>
                }
                <td>@link.IpAddress</td>
                <td>
                    <button class="small-round tertiary small" @onclick="() => DeleteLink(link)">Delete link</button>
                </td>
            </tr>
        }
        </tbody>
    </table>
}

@code
{
    private const string TemplateFilePath = "./wg-quick-template.txt";
    private string? _template;

    private void SaveTemplate()
    {
        if (_template != null)
        {
            File.WriteAllText(TemplateFilePath, _template);
        }
    }

    private List<Link>? _links;

    protected override void OnInitialized()
    {
        if (File.Exists(TemplateFilePath)) _template = File.ReadAllText(TemplateFilePath);
        using var db = new Database();
        _links = db.Links.ToList();
    }

    private string _newName = "";
    private string _newIpAddress = "";

    private void GenerateNewLink()
    {
        using var db = new Database();
        db.Links.Add(new Link
        {
            Name = _newName,
            IpAddress = _newIpAddress,
            Token = Link.GenerateToken(db)
        });
        db.SaveChanges();
        OnInitialized();
    }

    private void DeleteLink(Link link)
    {
        using var db = new Database();
        db.Remove(link);
        db.SaveChanges();
        OnInitialized();
    }
}