fix: 🐛 Solve the problem of IPV6 address connection failure

This commit is contained in:
viarotel 2025-01-02 18:45:03 +08:00
parent d262adf54d
commit 1a95325884
8 changed files with 64 additions and 24 deletions

View File

@ -0,0 +1,8 @@
import ipRegex from 'ip-regex'
/**
* Square brackets for IPV6 address
*/
export function ipv6Wrapper(string) {
return ipRegex.v6().test(string) ? `[${string}]` : string
}

View File

@ -11,6 +11,7 @@ import { uniq } from 'lodash-es'
import adbConnectionMonitor from './helpers/adbConnectionMonitor/index.js'
import { streamToBase64 } from '$electron/helpers/index.js'
import { parseBatteryDump } from './helpers/battery/index.js'
import { ipv6Wrapper } from './helpers/index.js'
const exec = util.promisify(_exec)
@ -105,10 +106,6 @@ const deviceShell = async (id, command) => {
const kill = async (...params) => client.kill(...params)
const connect = async (...params) => client.connect(...params)
const disconnect = async (...params) => client.disconnect(...params)
const getDeviceIP = async (id) => {
try {
const { stdout } = await shell(`-s ${id} shell ip -f inet addr show wlan0`)
@ -276,10 +273,6 @@ async function pull(id, filePath, args = {}) {
})
}
async function pair(host, port, code) {
return shell(`pair ${host}:${port} ${code}`)
}
async function connectCode(password, options = {}) {
return adbConnectionMonitor.startQrCodeScanning({
password,
@ -305,6 +298,18 @@ async function battery(id) {
}
}
async function pair(host, port, code) {
return shell(`pair ${ipv6Wrapper(host)}:${port} ${code}`)
}
async function connect(host, port = 5555) {
return shell(`connect ${ipv6Wrapper(host)}:${port}`)
}
async function disconnect(host, port = 5555) {
return shell(`disconnect ${ipv6Wrapper(host)}:${port}`)
}
function init() {
const bin = appStore.get('common.adbPath') || adbPath

View File

@ -48,6 +48,7 @@
"fix-path": "4.0.0",
"fs-extra": "11.2.0",
"husky": "9.0.11",
"ip-regex": "^5.0.0",
"lodash-es": "4.17.21",
"nanoid": "5.0.7",
"pinia": "2.1.7",

View File

@ -30,7 +30,7 @@
</template>
<script>
import { sleep } from '$/utils'
import { parseDeviceId, sleep } from '$/utils'
export default {
inheritAttrs: false,
@ -82,7 +82,7 @@ export default {
async handleStop(row) {
this.stopLoading = true
const [host, port] = row.id.split(':')
const { host, port } = parseDeviceId(row.id)
try {
await this.$adb.disconnect(host, port)

View File

@ -113,8 +113,7 @@ export default {
async handleSubmit() {
try {
await this.$refs.elForm.validate()
const command = `pair ${this.formData.host}:${this.formData.port} ${this.formData.pair}`
await this.$adb.shell(command)
await this.$adb.pair(this.formData.host, this.formData.port, this.formData.pair)
this.$emit('success')
this.handleClose()
}

View File

@ -71,7 +71,7 @@
</template>
<script>
import { sleep } from '$/utils'
import { parseDeviceId, sleep } from '$/utils'
import PairDialog from './PairDialog/index.vue'
import QrAction from './QrAction/index.vue'
@ -104,14 +104,13 @@ export default {
wirelessList() {
const value = this.$store.device.list.reduce((arr, item) => {
if (item.wifi) {
const [host, port] = item.id.split(':')
if (host && port) {
arr.push({
id: item.id,
host,
port,
})
}
const { host, port } = parseDeviceId(item.id)
arr.push({
id: item.id,
host,
port,
})
}
return arr
@ -126,10 +125,10 @@ export default {
set(value) {
this.formData.id = value
const [host, port] = value.split(':')
const { host, port } = parseDeviceId(value)
this.formData.host = host
this.formData.port = port || 5555
this.formData.port = port
},
},
},

View File

@ -42,7 +42,7 @@ export async function getCurrentDevices() {
id: device.id,
status: device.type,
name: getDeviceName(device),
wifi: device.id.includes('.'),
wifi: device.id.includes(':'),
remark: getRemark(device.id),
}))
}

View File

@ -136,3 +136,31 @@ export function formatFileSize(bytes) {
return `${Number.parseFloat((bytes / k ** i).toFixed(2))} ${sizes[i]}`
}
/**
* Parsing the device ID
* @param {*} string
* @returns
*/
export function parseDeviceId(string = '') {
const splitList = string.split(':')
const value = {
host: string,
port: 5555,
}
if (splitList?.length < 2) {
return value
}
const port = Number.parseInt(splitList[splitList.length - 1])
if (!Number.isNaN(port) && port <= 65535) {
value.port = port
}
value.host = string.replace(/\[|\]/g, '').replace(`:${port}`, '')
return value
}