File size: 7,393 Bytes
8ede856
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<template>
    <h5>{{ tm('network.proxySelector.title') }}</h5>
    <v-radio-group class="mt-2" v-model="radioValue" hide-details="true">
        <v-radio :label="tm('network.proxySelector.noProxy')" value="0"></v-radio>
        <v-radio value="1">
            <template v-slot:label>
                <span>{{ tm('network.proxySelector.useProxy') }}</span>
                <v-btn v-if="radioValue === '1'" class="ml-2" @click="testAllProxies" size="x-small"
                    variant="tonal" :loading="loadingTestingConnection">
                    {{ tm('network.proxySelector.testConnection') }}
                </v-btn>
            </template>
        </v-radio>
    </v-radio-group>
    <v-expand-transition>
        <div v-if="radioValue === '1'" style="margin-left: 16px;">
            <v-radio-group v-model="githubProxyRadioControl" class="mt-2" hide-details="true">
                <v-radio color="success" v-for="(proxy, idx) in githubProxies" :key="proxy" :value="String(idx)">
                    <template v-slot:label>
                        <div class="d-flex align-center">
                            <span class="mr-2">{{ proxy }}</span>
                            <div v-if="proxyStatus[idx]">
                                <v-chip
                                    :color="proxyStatus[idx].available ? 'success' : 'error'"
                                    size="x-small"
                                    class="mr-1">
                                    {{ proxyStatus[idx].available ? tm('network.proxySelector.available') : tm('network.proxySelector.unavailable') }}
                                </v-chip>
                                <v-chip
                                    v-if="proxyStatus[idx].available"
                                    color="info"
                                    size="x-small">
                                    {{ proxyStatus[idx].latency }}ms
                                </v-chip>
                            </div>
                        </div>
                    </template>
                </v-radio>
                <v-radio color="primary" value="-1" :label="tm('network.proxySelector.custom')">
                    <template v-slot:label v-if="String(githubProxyRadioControl) === '-1'">
                        <v-text-field density="compact" v-model="selectedGitHubProxy" variant="outlined"
                            style="width: 100vw;" :placeholder="tm('network.proxySelector.custom')" hide-details="true">
                        </v-text-field>
                    </template>
                </v-radio>
            </v-radio-group>
        </div>
    </v-expand-transition>
</template>


<script>
import axios from 'axios';
import { useModuleI18n } from '@/i18n/composables';

export default {
    setup() {
        const { tm } = useModuleI18n('features/settings');
        return { tm };
    },
    data() {
        return {
            githubProxies: [
                "https://edgeone.gh-proxy.com",
                "https://hk.gh-proxy.com/",
                "https://gh-proxy.com/",
                "https://gh.llkk.cc",
            ],
            githubProxyRadioControl: "0", // the index of the selected proxy
            selectedGitHubProxy: "",
            radioValue: "0", // 0: 不使用, 1: 使用
            loadingTestingConnection: false,
            testingProxies: {},
            proxyStatus: {},
            initializing: true,
        }
    },
    methods: {
        getProxyByControl(control) {
            const normalizedControl = String(control);
            if (normalizedControl === "-1") {
                return "";
            }
            const index = Number.parseInt(normalizedControl, 10);
            if (Number.isNaN(index)) {
                return "";
            }
            return this.githubProxies[index] || "";
        },
        async testSingleProxy(idx) {
            this.testingProxies[idx] = true;
            
            const proxy = this.githubProxies[idx];
            
            try {
                const response = await axios.post('/api/stat/test-ghproxy-connection', {
                    proxy_url: proxy
                });
                console.log(response.data);
                if (response.status === 200) {
                    this.proxyStatus[idx] = {
                        available: true,
                        latency: Math.round(response.data.data.latency)
                    };
                } else {
                    this.proxyStatus[idx] = {
                        available: false,
                        latency: 0
                    };
                }
            } catch (error) {
                this.proxyStatus[idx] = {
                    available: false,
                    latency: 0
                };
            } finally {
                this.testingProxies[idx] = false;
            }
        },
        
        async testAllProxies() {
            this.loadingTestingConnection = true;
            
            const promises = this.githubProxies.map((proxy, idx) => 
                this.testSingleProxy(idx)
            );
            
            await Promise.all(promises);
            this.loadingTestingConnection = false;
        },
    },
    mounted() {
        this.initializing = true;

        const savedProxy = localStorage.getItem('selectedGitHubProxy') || "";
        const savedRadio = localStorage.getItem('githubProxyRadioValue') || "0";
        const savedControl = String(localStorage.getItem('githubProxyRadioControl') || "0");

        this.radioValue = savedRadio;
        this.githubProxyRadioControl = savedControl;

        if (savedRadio === "1") {
            if (savedControl !== "-1") {
                this.selectedGitHubProxy = this.getProxyByControl(savedControl);
            } else {
                this.selectedGitHubProxy = savedProxy;
            }
        } else {
            this.selectedGitHubProxy = "";
        }

        this.initializing = false;
    },
    watch: {
        selectedGitHubProxy: function (newVal, oldVal) {
            if (this.initializing) {
                return;
            }
            if (!newVal) {
                newVal = ""
            }
            localStorage.setItem('selectedGitHubProxy', newVal);
        },
        radioValue: function (newVal) {
            if (this.initializing) {
                return;
            }
            localStorage.setItem('githubProxyRadioValue', newVal);
            if (String(newVal) === "0") {
                this.selectedGitHubProxy = "";
            } else if (String(this.githubProxyRadioControl) !== "-1") {
                this.selectedGitHubProxy = this.getProxyByControl(this.githubProxyRadioControl);
            }
        },
        githubProxyRadioControl: function (newVal) {
            if (this.initializing) {
                return;
            }
            const normalizedVal = String(newVal);
            localStorage.setItem('githubProxyRadioControl', normalizedVal);
            if (String(this.radioValue) !== "1") {
                this.selectedGitHubProxy = "";
                return;
            }
            if (normalizedVal !== "-1") {
                this.selectedGitHubProxy = this.getProxyByControl(normalizedVal);
            }
        }
    }
}
</script>

<style>
.v-label {
    font-size: 0.875rem;
}
</style>