Skip to content

Commit

Permalink
添加限速功能
Browse files Browse the repository at this point in the history
  • Loading branch information
ljxi committed Nov 4, 2023
1 parent cb0d1aa commit 1ea4102
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/assets/nodes.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"Apple":"https://store.storevideos.cdn-apple.com/v1/store.apple.com/st/1666383693478/atvloop-video-202210/streams_atvloop-video-202210/1920x1080/fileSequence3.m4s",
"微软Azure":"https://cdn.microsoftstore.com.cn/media/product_long_description/3781-00000/2_dupn50xr/4h0yzz2_360.jpg",
"联通测速":"https://cesubao.chinaunicom.cn/1000.bin",
"电信测速":"https://vipspeedtest8.wuhan.net.cn:8080/download?size=2147483648",
"电信测速":"https://vipspeedtest8.wuhan.net.cn:8080/download?size=1073741824",
"清华大学":"https://iptv.tsinghua.edu.cn/st_new_HB33/garbage.php?ckSize=1024",
"上海交大":"https://mirrors.sjtug.sjtu.edu.cn/speedtest/garbage?ckSize=1024",
"并夕夕":"https://cd.pddpic.com/android_dev/2023-07-28/5c04772968aee57ca690fbe1e7f29467.apk",
Expand Down Expand Up @@ -36,7 +36,7 @@
},
"全球[Global]":{
"Cachefly":"https://web1.cachefly.net/speedtest/downloading",
"Cloudflare":"https://speed.cloudflare.com/__down?bytes=1073741824",
"Cloudflare":"https://speed.cloudflare.com/__down?bytes=104857600",
"Akamai":"https://akamtrans-a.akamaihd.net/delivery/2023/08/30/brand-protector-2500k-00004.ts",
"Cloudflare Workers":"https://gh.con.sh/https://github.com/AaronFeng753/Waifu2x-Extension-GUI/releases/download/v2.21.12/Waifu2x-Extension-GUI-v2.21.12-Portable.7z",
"Steam Akamai":"https://cdn.akamai.steamstatic.com/steam/apps/1063730/extras/NW_Sword_Sorcery_2.gif",
Expand Down
67 changes: 67 additions & 0 deletions src/components/Main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@
</div>
<div class="showItem">
<span class="font-background" style="font-size: larger;">带宽</span>
<el-text size="small" class="mx-1">{{ state.maxSpeed ? '/' + formatter(state.maxSpeed, 2, [0, 0, 0, 0, 0, 0]) : ""
}}</el-text>
<el-button type="primary" style="height: 15px;" :icon="Edit" link @click="EditSpeedVisible = true" />
<div class="state-icon">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor"
class="h-15 w-15 float-right pt-3">
Expand Down Expand Up @@ -186,6 +189,33 @@
</span>
</template>
</el-dialog>
<el-dialog style="width: 90%;max-width: 350px;" v-model="EditSpeedVisible" title="设置带宽上限">
<el-form>
<div class="mt-4">
<el-input type="number" min='1' v-model="maxSpeedInput.num" autocomplete="off" placeholder="留空则无上限"
class="input-with-select">
<template #append>
<el-select v-model="maxSpeedInput.type" placeholder="Select" style="width: 80px">
<el-option label="Mbps" value="Mbps" />
<el-option label="Gbps" value="Gbps" />
</el-select>
</template>
</el-input>
<br><br>
<el-alert title="注意:" type="warning">
浏览器会使用缓存策略<br>只能限制平均速度,无法限制峰值速度!<br>部分链接无法限速,请使用其它限速方法
</el-alert>
</div>
</el-form>
<template #footer>
<span class="dialog-footer">
<el-button @click="EditSpeedVisible = false">取消</el-button>
<el-button type="primary" @click="editSpeedUse()">
确定
</el-button>
</span>
</template>
</el-dialog>
<MarkUI :show="showMark" :loginInfo="loginInfo" />
<audio v-if="isMobile && !isIOS && !isMiuiBrowser && runBackground" @canplay="() => { if (isRunning) audioDom.play() }"
@pause="() => { if (runBackground) isRunning = false }" @play="isRunning = true" controls loop ref="audioDom"
Expand Down Expand Up @@ -275,6 +305,7 @@ const state = reactive({
startUse: 0,
startTime: 0,
maxUse: localStorage.maxUse ? Number(localStorage.maxUse) : 0,
maxSpeed: localStorage.maxSpeed ? Number(localStorage.maxSpeed) : 0,
})
const isRunning = ref(false)
const loginInfo = reactive({ AccessToken: localStorage.AccessToken ? localStorage.AccessToken : "" })
Expand Down Expand Up @@ -519,6 +550,16 @@ function formatter(num: number, desIndex: number, flo: Array<number>) {
return cnum.toFixed(flo[total_index]) + describe[total_index];
}
const speedCtr=()=>{
if(state.bytesUsed-state.recordUse>state.maxSpeed/8){
return new Promise((resolve)=>{
setTimeout(()=>{
resolve(0)
},new Date().getTime()%1000)
})
}
}
async function startThread(index: number) {
try {
var _url = runUrl.value
Expand All @@ -530,6 +571,7 @@ async function startThread(index: number) {
const reader = response.body.getReader();
let decodeLength = 0
while (true) {
if(state.maxSpeed)await speedCtr()
const { value } = await reader.read();
let chunkLength = value?.length
if (!chunkLength || _url != runUrl.value) {
Expand All @@ -556,6 +598,7 @@ async function startThread(index: number) {
const EditTableVisible = ref(false)
const addTableVisible = ref(false)
const EditMaxVisible = ref(false)
const EditSpeedVisible = ref(false)
const addForm = ref({
label: '',
value: '',
Expand Down Expand Up @@ -609,6 +652,30 @@ const editMaxUse = () => {
maxUseInput.value.num = null
EditMaxVisible.value = false
}
const maxSpeedInput: Ref<{
num: number | null;
type: "Gbps" | "Mbps" | "Kbps";
}> = ref({
num: null,
type: 'Mbps',
})
const editSpeedUse = () => {
var map = {
"Kbps": 1024 ,
"Mbps": 1024 * 1024,
"Gbps": 1024 * 1024 * 1024
}
var tmp = 0
if (maxSpeedInput.value.num) {
tmp = Math.floor(maxSpeedInput.value.num * map[maxSpeedInput.value.type])
}
state.maxSpeed = tmp
localStorage.maxSpeed = tmp
maxSpeedInput.value.num = null
EditSpeedVisible.value = false
}
var isMobile = /Mobi|Android|iPhone/i.test(navigator.userAgent)
var isMiuiBrowser = /MiuiBrowser/i.test(navigator.userAgent)
var isIOS = /iPhone/i.test(navigator.userAgent)
Expand Down

0 comments on commit 1ea4102

Please sign in to comment.