当前位置: 首页 > wzjs >正文

网站建设的目入图片什么是搜索引擎销售

网站建设的目入图片,什么是搜索引擎销售,专门做衬衣的网站,做电工的有接单的网站吗Electron 开发:获取当前客户端 IP 一、背景与需求 1. 项目背景 客户端会自启动一个服务,Web/后端服务通过 IP port 请求以操作客户端接口 2. 初始方案与问题 2.1. 初始方案:通过代码获取本机 IP /*** 获取局域网 IP* returns {string}…

Electron 开发:获取当前客户端 IP

一、背景与需求

1. 项目背景

客户端会自启动一个服务,Web/后端服务通过 IP + port 请求以操作客户端接口

2. 初始方案与问题

2.1. 初始方案:通过代码获取本机 IP
/*** 获取局域网 IP* @returns {string} 局域网 IP*/
export function getLocalIP(): string {const interfaces = os.networkInterfaces()for (const name of Object.keys(interfaces)) {for (const iface of interfaces[name] || []) {if (iface.family === 'IPv4' && !iface.internal) {log.info('获取局域网 IP:', iface.address)return iface.address}}}log.warn('无法获取局域网 IP,使用默认 IP: 127.0.0.1')return '127.0.0.1'
}
2.2. 遇到的问题

如果设备开启了代理,可能获取的是代理 IP,导致后端请求失败

二、解决方案设计

1. 总体思路

  • 获取本机所有 IP
  • 遍历 IP + port 请求客户端服务接口
  • 成功响应即为目标 IP
  • 缓存有效 IP,避免频繁请求

2. 获取所有可能的 IP

使用 Node.js 的 os.networkInterfaces() 获取所有可用 IP

private getAllPossibleIPs(): string[] {const interfaces = os.networkInterfaces()const result: string[] = []for (const name of Object.keys(interfaces)) {const lowerName = name.toLowerCase()if (lowerName.includes('vmware')|| lowerName.includes('virtual')|| lowerName.includes('vpn')|| lowerName.includes('docker')|| lowerName.includes('vethernet')) {continue}for (const iface of interfaces[name] || []) {if (iface.family === 'IPv4' && !iface.internal) {result.push(iface.address)}}}return result
}

3. 遍历 IP 请求验证

轮询所有 IP,尝试访问客户端服务,验证是否可用

private async testIPsParallel(ips: string[]): Promise<string | null> {if (ips.length === 0)return nullreturn new Promise((resolve) => {const globalTimeout = setTimeout(() => {resolve(null)}, this.TIMEOUT * 1.5)const controllers = ips.map(() => new AbortController())let hasResolved = falselet completedCount = 0const testIP = (ip: string, index: number) => {const controller = controllers[index]axios.get(`http://${ip}:${PORT}/api/task-server/ip`, {timeout: this.TIMEOUT,signal: controller.signal,}).then(() => {if (!hasResolved) {hasResolved = trueclearTimeout(globalTimeout)controllers.forEach((c, i) => {if (i !== index)c.abort()})resolve(ip)}}).catch(() => {if (!hasResolved) {completedCount++if (completedCount >= ips.length) {clearTimeout(globalTimeout)resolve(null)}}})}ips.forEach(testIP)})
}

4. 添加缓存策略

对成功的 IP 进行缓存,设定缓存有效时间,避免重复请求

private cachedValidIP: string | null = null
private lastValidationTime = 0
private readonly CACHE_VALID_DURATION = 24 * 60 * 60 * 1000

三、完整代码

import os from 'node:os'
import axios from 'axios'
import { PORT } from '../../enum/env'/*** IP管理器单例类* 用于获取并缓存本地有效IP地址*/
export class IPManager {private static instance: IPManagerprivate cachedValidIP: string | null = nullprivate lastValidationTime = 0private readonly CACHE_VALID_DURATION = 24 * 60 * 60 * 1000private readonly TIMEOUT = 200private isTestingIPs = falseprivate constructor() {}static getInstance(): IPManager {if (!IPManager.instance) {IPManager.instance = new IPManager()}return IPManager.instance}async getLocalIP(): Promise<string> {const now = Date.now()if (this.cachedValidIP && now - this.lastValidationTime < this.CACHE_VALID_DURATION) {console.log('从缓存中获取 IP', this.cachedValidIP)return this.cachedValidIP}if (this.isTestingIPs) {const allIPs = this.getAllPossibleIPs()return allIPs.length > 0 ? allIPs[0] : '127.0.0.1'}this.isTestingIPs = truetry {const allIPs = this.getAllPossibleIPs()if (allIPs.length === 0) {return '127.0.0.1'}const validIP = await this.testIPsParallel(allIPs)if (validIP) {this.cachedValidIP = validIPthis.lastValidationTime = nowreturn validIP}return allIPs[0]}catch (error) {const allIPs = this.getAllPossibleIPs()return allIPs.length > 0 ? allIPs[0] : '127.0.0.1'}finally {this.isTestingIPs = false}}private getAllPossibleIPs(): string[] {const interfaces = os.networkInterfaces()const result: string[] = []for (const name of Object.keys(interfaces)) {const lowerName = name.toLowerCase()if (lowerName.includes('vmware')|| lowerName.includes('virtual')|| lowerName.includes('vpn')|| lowerName.includes('docker')|| lowerName.includes('vethernet')) {continue}for (const iface of interfaces[name] || []) {if (iface.family === 'IPv4' && !iface.internal) {result.push(iface.address)}}}return result}private async testIPsParallel(ips: string[]): Promise<string | null> {if (ips.length === 0)return nullreturn new Promise((resolve) => {const globalTimeout = setTimeout(() => {resolve(null)}, this.TIMEOUT * 1.5)const controllers = ips.map(() => new AbortController())let hasResolved = falselet completedCount = 0const testIP = (ip: string, index: number) => {const controller = controllers[index]axios.get(`http://${ip}:${PORT}/api/task-server/ip`, {timeout: this.TIMEOUT,signal: controller.signal,// validateStatus: status => status === 200,}).then(() => {if (!hasResolved) {hasResolved = trueclearTimeout(globalTimeout)controllers.forEach((c, i) => {if (i !== index)c.abort()})resolve(ip)}}).catch(() => {if (!hasResolved) {completedCount++if (completedCount >= ips.length) {clearTimeout(globalTimeout)resolve(null)}}})}ips.forEach(testIP)})}
}/*** 获取本地有效IP地址*/
export async function getLocalIP(): Promise<string> {return IPManager.getInstance().getLocalIP()
}
http://www.dtcms.com/wzjs/396176.html

相关文章:

  • 宁波网站运营优化系统专业培训
  • 外卖网站那家做的好人民日报最新头条10条
  • 如何做正规电影网站今晚比分足球预测
  • 网站虚拟主机可以做伦理片吗重大新闻事件
  • 微信公众号 网站开发交换链接适用于哪些网站
  • 自己做网站不用WordPress品牌推广活动方案
  • 苏州企业建设网站价格百度竞价排名利弊
  • 做网站用到的java技术企业seo案例
  • 做网站常用图标网上企业推广
  • 福田商城网站制作百度一下百度主页度
  • 永嘉网站建设几怎样做seo搜索引擎优化
  • 站长之家网址ip查询关键词优化报价怎么样
  • 外贸手机商城网站建设 深圳互联网广告营销是什么
  • 哈尔滨网站建设价格专业网站优化外包
  • 百度怎么做网站排名宁波seo排名优化
  • 南宁有本地租房做网站吗中国免费网站服务器下载
  • 网站备案 和 icp简单网页制作
  • 在征婚网站上认识做期货网站top排行榜
  • 高级网站设计十大广告联盟
  • c 网站开发实例英文关键词seo
  • 网站建设功能选择表百度搜索指数在线查询
  • 专业的网站建设公哪家专业百度大数据中心
  • 石家庄新闻热线南京seo关键词排名
  • 个人网站备案容易吗网络推广合同
  • 广告传媒公司是做什么的如何做seo
  • 住房和城乡建设网站 上海外国黄冈网站推广平台
  • 门户网站建设参考文献百度云建站
  • wordpress 商场模板整站seo优化公司
  • 网站开发公司+重庆成都企业seo
  • 做网站建设还有钱赚吗网络代理app