Skip to content

Commit

Permalink
✨ feat(macs): custom device name
Browse files Browse the repository at this point in the history
  • Loading branch information
CakeAL committed Nov 12, 2024
1 parent 8cc04bb commit 746de13
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 48 deletions.
2 changes: 2 additions & 0 deletions components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ declare module 'vue' {
NButton: typeof import('naive-ui')['NButton']
NCard: typeof import('naive-ui')['NCard']
NCheckbox: typeof import('naive-ui')['NCheckbox']
NCollapse: typeof import('naive-ui')['NCollapse']
NCollapseItem: typeof import('naive-ui')['NCollapseItem']
NCollapseTransition: typeof import('naive-ui')['NCollapseTransition']
NConfigProvider: typeof import('naive-ui')['NConfigProvider']
NDataTable: typeof import('naive-ui')['NDataTable']
Expand Down
Binary file added public/7a35cda1e5187cf409a1677bf0c58fa3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/QQ_1731405540124.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 22 additions & 1 deletion src-tauri/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,35 @@ pub async fn load_mac_address(app_state: tauri::State<'_, AppState>) -> Result<S
None => return Err("SessionID为空,是否已经登录并单击获取Cookie按钮?".to_string()),
};
let via_vpn = *app_state.login_via_vpn.read().unwrap();
let mac_custom_address = app_state.setting.read().unwrap().mac_custom_name.clone();

match get_mac_address(&session_id, via_vpn).await {
match get_mac_address(&session_id, via_vpn, &mac_custom_address).await {
Ok(Some(value)) => Ok(value.to_string()),
Ok(None) => Err("请确认是否已经登录".to_string()),
Err(e) => Err(format!("Request Error,检查是否在校园网内: {}", e)),
}
}

#[tauri::command(async)]
pub async fn set_mac_custom_name(
app_state: tauri::State<'_, AppState>,
app_handle: tauri::AppHandle,
mac: &str,
name: &str,
) -> Result<(), String> {
app_state
.setting
.write()
.unwrap()
.set_mac_custom_name(mac, name);
app_state
.setting
.write()
.unwrap()
.write_setting(&app_handle)
.map_err(|e| e.to_string())
}

#[tauri::command]
pub fn get_current_device_mac() -> Result<String, String> {
match std::env::consts::OS {
Expand Down
1 change: 1 addition & 0 deletions src-tauri/src/entities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ pub struct EveryLoginData {
pub struct MacAddress {
pub device_name: String,
pub mac_address: String,
pub custom_name: String,
}

pub struct AppState {
Expand Down
3 changes: 2 additions & 1 deletion src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ pub fn run() {
set_background_image,
reset_background_image,
set_background_transparence,
set_background_blur
set_background_blur,
set_mac_custom_name
])
.setup(|app| {
#[cfg(not(any(target_os = "android", target_os = "linux")))]
Expand Down
17 changes: 12 additions & 5 deletions src-tauri/src/requests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{f64, time::Duration};
use std::{collections::HashMap, f64, time::Duration};

use anyhow::{anyhow, Result};
use chrono::NaiveDateTime;
Expand Down Expand Up @@ -451,7 +451,11 @@ pub async fn get_user_login_log(
}))
}

pub async fn get_mac_address(session_id: &str, via_vpn: bool) -> Result<Option<Value>> {
pub async fn get_mac_address(
session_id: &str,
via_vpn: bool,
mac_custom_name: &HashMap<String, String>,
) -> Result<Option<Value>> {
let url = if !via_vpn {
"http://202.204.60.7:8080/nav_unBandMacJsp"
} else {
Expand Down Expand Up @@ -495,12 +499,15 @@ pub async fn get_mac_address(session_id: &str, via_vpn: bool) -> Result<Option<V
let mac_address = device_names_value
.iter()
.zip(mac_address_value.iter())
.map(|(device_name, mac_address)| MacAddress {
.map(|(&device_name, &mac_address)| MacAddress {
device_name: device_name.to_string(),
mac_address: mac_address.to_string(),
custom_name: mac_custom_name
.get(mac_address)
.cloned()
.unwrap_or_default(),
})
.collect::<Vec<_>>();

Ok(Some(serde_json::json!(mac_address)))
}

Expand Down Expand Up @@ -658,7 +665,7 @@ mod tests {
#[tokio::test]
async fn test_get_mac_address() {
let session_id = "session_id";
let res = get_mac_address(session_id, false).await;
let res = get_mac_address(session_id, false, &mut HashMap::new()).await;
dbg!(res.unwrap());
}

Expand Down
13 changes: 12 additions & 1 deletion src-tauri/src/setting.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{
collections::HashMap,
fs::{self, create_dir, File, OpenOptions},
io::{Read, Write},
path::PathBuf,
Expand All @@ -13,9 +14,10 @@ pub struct Setting {
pub username: Option<String>,
pub password: Option<String>,
pub ammeter_number: Option<u32>,
pub mac_custom_name: HashMap<String, String>,
pub background_image_path: Option<String>,
pub background_transparence: Option<u32>,
pub background_blur: Option<u32>
pub background_blur: Option<u32>,
}

impl Setting {
Expand Down Expand Up @@ -92,6 +94,15 @@ impl Setting {
pub fn set_background_blur(&mut self, background_blur: u32) {
self.background_blur = Some(background_blur);
}

pub fn set_mac_custom_name(&mut self, mac: &str, name: &str) {
self.mac_custom_name
.entry(mac.to_owned())
.and_modify(|n| {
*n = name.to_owned();
})
.or_insert(name.to_string());
}
}

fn get_config_path(app: &tauri::AppHandle) -> Result<PathBuf> {
Expand Down
135 changes: 95 additions & 40 deletions src/pages/UnbindMacs.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import { open } from "@tauri-apps/plugin-shell";
interface MacAddress {
device_name: string;
mac_address: string;
custom_name: string;
}
const pop_message = useMessage();
const this_mac = ref<string>("");
const mac_addrs = ref<MacAddress[] | null>(null);
const unbind_macs = ref<Array<boolean>>([]);
const custom_names = ref<Array<string>>([]);
onMounted(() => {
get_current_device_mac();
Expand All @@ -33,6 +35,7 @@ const load_mac_address = async () => {
if (mac_addrs.value !== null) {
for (let i = 0; i < mac_addrs.value.length; i += 1) {
unbind_macs.value.push(false);
custom_names.value.push(mac_addrs.value[i].custom_name);
}
}
};
Expand All @@ -52,50 +55,102 @@ const unbind = async () => {
}).catch((err) => pop_message.error(err));
setTimeout(load_mac_address, 100);
};
const set_mac_custom_name = async (mac: string, index: number) => {
if (custom_names.value[index] === "") {
return;
}
await invoke("set_mac_custom_name", {
mac,
name: custom_names.value[index],
}).catch((err) => pop_message.error(err));
};
</script>

<template>
<div class="container">
<n-h2 prefix="bar" type="success" style="margin-top: 15px">
<n-text type="success"> 解绑 MAC 地址 </n-text>
</n-h2>
<p>MAC Address是什么?简单来说校园网靠这个来识别是否是你的设备。</p>
<p>
所以随机MAC地址开启的话,就会导致你之前的设备被顶掉,详情可看B站视频:<a
@click="open('https://www.bilibili.com/video/av792486473/')"
style="text-underline-offset: 5px; text-decoration: underline; cursor: pointer;"
>BV1JC4y1S7WS</a
>
<!-- 点 bv 会打开 av 的网页🤔 -->
</p>
<p>当前机器的无线MAC地址是(仅供参考):{{ this_mac }}</p>
<p>如果把该地址解绑会导致立刻断网。</p>
<div v-if="mac_addrs !== null" class="show-data">
<n-table :bordered="false" :single-line="false">
<thead>
<tr>
<th>序号</th>
<th>设备名(校园网后台可能获取不到)</th>
<th>MAC Address</th>
<th>是否解绑</th>
</tr>
</thead>
<tbody>
<tr v-for="(mac_addr, index) in mac_addrs" :key="index">
<th>{{ index + 1 }}</th>
<th>{{ mac_addr.device_name }}</th>
<th>{{ mac_addr.mac_address }}</th>
<th>
<n-checkbox size="large" v-model:checked="unbind_macs[index]" />
</th>
</tr>
</tbody>
</n-table>
<br />
<n-button strong secondary type="info" @click="unbind">
确定解绑
</n-button>
</div>
<n-scrollbar style="max-height: 100vh">
<n-h2 prefix="bar" type="success" style="margin-top: 15px">
<n-text type="success"> 解绑 MAC 地址 </n-text>
</n-h2>
<p>MAC Address是什么?简单来说校园网靠这个来识别是否是你的设备。</p>
<p>
所以随机MAC地址开启的话,就会导致你之前的设备被顶掉,详情可看B站视频:<a
@click="open('https://www.bilibili.com/video/av792486473/')"
style="
text-underline-offset: 5px;
text-decoration: underline;
cursor: pointer;
"
>BV1JC4y1S7WS</a
>
<!-- 点 bv 会打开 av 的网页🤔 -->
</p>
<p>当前机器的无线MAC地址是(仅供参考):{{ this_mac }}</p>
<p>如果把该地址解绑会导致立刻断网。</p>
<div v-if="mac_addrs !== null" class="show-data">
<n-table :bordered="false" :single-line="false">
<thead>
<tr>
<th>序号</th>
<th>校园网后台设备名</th>
<th>自定义设备名</th>
<th>MAC Address</th>
<th>是否解绑</th>
</tr>
</thead>
<tbody>
<tr v-for="(mac_addr, index) in mac_addrs" :key="index">
<th>{{ index + 1 }}</th>
<th>{{ mac_addr.device_name }}</th>
<th>
<n-input
v-model:value="custom_names[index]"
type="text"
@blur="set_mac_custom_name(mac_addr.mac_address, index)"
/>
</th>
<th>{{ mac_addr.mac_address }}</th>
<th>
<n-checkbox size="large" v-model:checked="unbind_macs[index]" />
</th>
</tr>
</tbody>
</n-table>
<br />
<n-button strong secondary type="info" @click="unbind">
确定解绑
</n-button>
</div>
<p>
校园网后台的设备名十有八九是获取不到的,但是现在可以自定义设备名啦。这样我们就可以标记我们认识的设备了,如果你知道它本机的MAC地址是什么的话。
</p>
<n-card hoverable>
<n-collapse>
<n-collapse-item title="macOS / iOS 固定 MAC 地址方法" name="1">
<div>
<p>
将私有 Wi-Fi 地址设置为<b>“关闭”</b>(注意,不是固定,macOS
15以上)
</p>
<n-image
width="550"
src="/7a35cda1e5187cf409a1677bf0c58fa3.png"
/>
</div>
</n-collapse-item>
<n-collapse-item title="安卓/鸿蒙设备固定 MAC 地址方法" name="2">
<div>
<p>WLAN 详情 -> 隐私 -> 使用设备 MAC 地址</p>
<n-image width="300" src="/QQ_1731405540124.png" />
</div>
</n-collapse-item>
<n-collapse-item title="Windows" name="3">
<div>d</div>
</n-collapse-item>
</n-collapse>
</n-card>
</n-scrollbar>
</div>
</template>

Expand Down

0 comments on commit 746de13

Please sign in to comment.