summaryrefslogtreecommitdiffstats
path: root/src/components/ProxyDialog.vue
blob: fc92359b9a64315bf774bf3cfe0174beec1a7bd4 (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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<template>
    <form @submit.prevent="submit">
        <div ref="modal" class="modal fade" tabindex="-1" data-bs-backdrop="static">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 id="exampleModalLabel" class="modal-title">
                            {{ $t("Setup Proxy") }}
                        </h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close" />
                    </div>
                    <div class="modal-body">
                        <div class="mb-3">
                            <label for="proxy-protocol" class="form-label">{{ $t("Proxy Protocol") }}</label>
                            <select id="proxy-protocol" v-model="proxy.protocol" class="form-select">
                                <option value="https">HTTPS</option>
                                <option value="http">HTTP</option>
                                <option value="socks">SOCKS</option>
                                <option value="socks5">SOCKS v5</option>
                                <option value="socks5h">SOCKS v5 (+DNS)</option>
                                <option value="socks4">SOCKS v4</option>
                            </select>
                        </div>

                        <div class="mb-3">
                            <label for="proxy-host" class="form-label">{{ $t("Proxy Server") }}</label>
                            <div class="d-flex">
                                <input id="proxy-host" v-model="proxy.host" type="text" class="form-control" required :placeholder="$t('Server Address')">
                                <input v-model="proxy.port" type="number" class="form-control ms-2" style="width: 100px;" required min="1" max="65535" :placeholder="$t('Port')">
                            </div>
                        </div>

                        <div class="mb-3">
                            <div class="form-check form-switch">
                                <input id="mark-auth" v-model="proxy.auth" class="form-check-input" type="checkbox">
                                <label for="mark-auth" class="form-check-label">{{ $t("Proxy server has authentication") }}</label>
                            </div>
                        </div>

                        <div v-if="proxy.auth" class="mb-3">
                            <label for="proxy-username" class="form-label">{{ $t("User") }}</label>
                            <input id="proxy-username" v-model="proxy.username" type="text" class="form-control" required>
                        </div>

                        <div v-if="proxy.auth" class="mb-3">
                            <label for="proxy-password" class="form-label">{{ $t("Password") }}</label>
                            <input id="proxy-password" v-model="proxy.password" type="password" class="form-control" required>
                        </div>

                        <div class="mb-3 mt-4">
                            <hr class="dropdown-divider mb-4">

                            <div class="form-check form-switch">
                                <input id="mark-active" v-model="proxy.active" class="form-check-input" type="checkbox">
                                <label for="mark-active" class="form-check-label">{{ $t("enabled") }}</label>
                            </div>
                            <div class="form-text">
                                {{ $t("enableProxyDescription") }}
                            </div>

                            <br />

                            <div class="form-check form-switch">
                                <input id="mark-default" v-model="proxy.default" class="form-check-input" type="checkbox">
                                <label for="mark-default" class="form-check-label">{{ $t("setAsDefault") }}</label>
                            </div>
                            <div class="form-text">
                                {{ $t("setAsDefaultProxyDescription") }}
                            </div>

                            <br />

                            <div class="form-check form-switch">
                                <input id="apply-existing" v-model="proxy.applyExisting" class="form-check-input" type="checkbox">
                                <label class="form-check-label" for="apply-existing">{{ $t("Apply on all existing monitors") }}</label>
                            </div>
                        </div>
                    </div>

                    <div class="modal-footer">
                        <button v-if="id" type="button" class="btn btn-danger" :disabled="processing" @click="deleteConfirm">
                            {{ $t("Delete") }}
                        </button>
                        <button type="submit" class="btn btn-primary" :disabled="processing">
                            <div v-if="processing" class="spinner-border spinner-border-sm me-1"></div>
                            {{ $t("Save") }}
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </form>

    <Confirm ref="confirmDelete" btn-style="btn-danger" :yes-text="$t('Yes')" :no-text="$t('No')" @yes="deleteProxy">
        {{ $t("deleteProxyMsg") }}
    </Confirm>
</template>

<script lang="ts">
import { Modal } from "bootstrap";

import Confirm from "./Confirm.vue";

export default {
    components: {
        Confirm,
    },
    props: {},
    emits: [ "added" ],
    data() {
        return {
            model: null,
            processing: false,
            id: null,
            proxy: {
                protocol: null,
                host: null,
                port: null,
                auth: false,
                username: null,
                password: null,
                active: false,
                default: false,
                applyExisting: false,
            }
        };
    },

    mounted() {
        this.modal = new Modal(this.$refs.modal);
    },

    methods: {
        /**
         * Show dialog to confirm deletion
         * @returns {void}
         */
        deleteConfirm() {
            this.modal.hide();
            this.$refs.confirmDelete.show();
        },

        /**
         * Show settings for specified proxy
         * @param {number} proxyID ID of proxy to show
         * @returns {void}
         */
        show(proxyID) {
            if (proxyID) {
                this.id = proxyID;

                for (let proxy of this.$root.proxyList) {
                    if (proxy.id === proxyID) {
                        this.proxy = proxy;
                        break;
                    }
                }
            } else {
                this.id = null;
                this.proxy = {
                    protocol: "https",
                    host: null,
                    port: null,
                    auth: false,
                    username: null,
                    password: null,
                    active: true,
                    default: false,
                    applyExisting: false,
                };
            }

            this.modal.show();
        },

        /**
         * Submit form data for saving
         * @returns {void}
         */
        submit() {
            this.processing = true;
            this.$root.getSocket().emit("addProxy", this.proxy, this.id, (res) => {
                this.$root.toastRes(res);
                this.processing = false;

                if (res.ok) {
                    this.modal.hide();

                    // Emit added event, doesn't emit edit.
                    if (! this.id) {
                        this.$emit("added", res.id);
                    }
                }
            });
        },

        /**
         * Delete this proxy
         * @returns {void}
         */
        deleteProxy() {
            this.processing = true;
            this.$root.getSocket().emit("deleteProxy", this.id, (res) => {
                this.$root.toastRes(res);
                this.processing = false;

                if (res.ok) {
                    this.modal.hide();
                }
            });
        },
    },
};
</script>

<style lang="scss" scoped>
@import "../assets/vars.scss";

.dark {
    .modal-dialog .form-text, .modal-dialog p {
        color: $dark-font-color;
    }
}
</style>