# 浏览器获取客户端ip（非远程请求或依赖第三方api）

其次如果场景符合，建议使用flutter/uniapp/electron打包应用获取对应客户端或者设备端的mac/ip/uuid再通过webview嵌入内容并且传入webview中

WebRTCControl控件使用（适用于内部项目，离线项目，线上的项目基本通过请求就可以获取）：

在浏览其中安装WebRTCControl控件

然后网页端执行webRtc

```javascript
import tool from '@/utils/tool'
const getIP = (callback) => {
  let recode = {}
  let RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection
  if (!RTCPeerConnection) {
    let win = iframe.contentWindow
    RTCPeerConnection = win.RTCPeerConnection || win.mozRTCPeerConnection || win.webkitRTCPeerConnection
  }

  //创建实例，生成连接
  let pc = new RTCPeerConnection()
  // 匹配字符串中符合ip地址的字段
  function handleCandidate(candidate) {
    let ip_regexp = /([0-9]{1,3}(\.[0-9]{1,3}){3}|([a-f0-9]{1,4}((:[a-f0-9]{1,4}){7}|:+[a-f0-9]{1,4}){6}))/
    if (candidate.match(ip_regexp)) {
      let ip_isMatch = candidate.match(ip_regexp)[1]
      if (!recode[ip_isMatch]) {
        callback(ip_isMatch)
        recode[ip_isMatch] = true
      }
    }
  }
  //监听icecandidate事件
  pc.onicecandidate = (ice) => {
    if (ice.candidate) {
      handleCandidate(ice.candidate.candidate)
    }
  }
  //建立一个伪数据的通道
  pc.createDataChannel('')
  pc.createOffer(
    (res) => {
      pc.setLocalDescription(res)
    },
    () => {}
  )

  //延迟，让一切都能完成
  setTimeout(() => {
    let lines = pc.localDescription.sdp.split('\n')
    lines.forEach((item) => {
      if (item.indexOf('a=candidate:') === 0) {
        handleCandidate(item)
      }
    })
  }, 1000)
}
getIP((res) => {
  tool.data.set('LOC_IP', res)
})
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://408550179s-organization.gitbook.io/blog/liu-lan-qi-huo-qu-ke-hu-duan-ip-fei-yuan-cheng-qing-qiu-huo-yi-lai-di-san-fang-api.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
