H5如何获取内网IP和公网IP

前端H5开发,获取内网IP和公网IP

可直接打开,简答便捷,重要的事情说三遍
直接使用!
直接使用!
直接使用!

获取内网IP

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
		<ul>
			<li>内网ip</li>
		</ul>
		<script>
			function getIPs(callback) {
				var ip_dups = {};

				var RTCPeerConnection = window.RTCPeerConnection ||
					window.mozRTCPeerConnection ||
					window.webkitRTCPeerConnection;
				var useWebKit = !!window.webkitRTCPeerConnection;

				if (!RTCPeerConnection) {

					var iframe = document.createElement('iframe');
					iframe.style.display = 'none';

					iframe.sandbox = 'allow-same-origin';

					iframe.addEventListener("DOMNodeInserted", function(e) {
						e.stopPropagation();
					}, false);
					iframe.addEventListener("DOMNodeInsertedIntoDocument", function(e) {
						e.stopPropagation();
					}, false);

					document.body.appendChild(iframe);
					var win = iframe.contentWindow;
					RTCPeerConnection = win.RTCPeerConnection ||
						win.mozRTCPeerConnection ||
						win.webkitRTCPeerConnection;
					useWebKit = !!win.webkitRTCPeerConnection;
				}

				var mediaConstraints = {
					optional: [{
						RtpDataChannels: true
					}]
				};

				var servers = undefined;

				if (useWebKit)
					servers = {
						iceServers: [{
							urls: "stun:stun.services.mozilla.com"
						}]
					};

				var pc = new RTCPeerConnection(servers, mediaConstraints);

				function handleCandidate(candidate) {

					var ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3})/
					var ip_addr = ip_regex.exec(candidate)[1];

					if (ip_dups[ip_addr] === undefined)
						callback(ip_addr);
					ip_dups[ip_addr] = true;
				}

				pc.onicecandidate = function(ice) {

					if (ice.candidate)
						handleCandidate(ice.candidate.candidate);
				};

				pc.createDataChannel("");

				pc.createOffer(function(result) {

					pc.setLocalDescription(result, function() {}, function() {});
				}, function() {});

				setTimeout(function() {

					var lines = pc.localDescription.sdp.split('\n');
					lines.forEach(function(line) {
						if (line.indexOf('a=candidate:') === 0)
							handleCandidate(line);
					});
				}, 1000);
			}

			getIPs(function(ip) {
				var li = document.createElement("li");
				li.textContent = ip;
				if (ip.match(/^(192\.168\.|169\.254\.|10\.|172\.(1[6-9]|2\d|3[01]))/)) {
					//内网ip
					document.getElementsByTagName("ul")[0].appendChild(li);
				}
			});
		</script>
	</body>
</html>

获取公网IP

通常我们使用搜狐的接口

<!DOCTYPE html>
<html>
	<head>
		<title>公网ip获取</title>
		<meta http-equiv=Content-Type content="text/html; charset=gb2312">
	</head>
	<body>
		<script src="http://pv.sohu.com/cityjson?ie=utf-8"></script>
		<script type="text/javascript">
			document.write(returnCitySN["cip"] + ',' + returnCitySN["cname"])
		</script>
	</body>
</html>
  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值