当前位置:网站首页>Determine whether the currently requested network is internal or external
Determine whether the currently requested network is internal or external
2022-07-18 01:43:00 【Brother Jin (API call Engineer)】
1. Scene analysis
There's a need recently , There is a function that can only be used by Internet users , Intranet users have no feelings , And in the same reference , Then I need to initialize , For current users IP Judge , Look at this. IP Can I access the intranet
2. The front-end implementation
The front-end implementation is just one idea , use AJAX Call a data or interface that only the Intranet can access . If I run out of time , Then it's the Internet .
// Code body
ping = (address,callback) =>{
var img = new Image();
var start = new Date().getTime();
// An intranet picture Used to judge the network type This picture cannot be loaded on the Internet
img.src =address+"?time="+start;
var flag = false;
img.onload = function(){
flag = true;
// stay 2 Seconds Load out the picture Then it will be regarded as the intranet
console.log(" Intranet ")
}
var timer = setTimeout(function(){
if(!flag){
flag = false;
if (callback && typeof callback == 'function') callback();
}
},2000);
}
// Use the callback function to trigger our event
this.ping("http://neiwang.com/static/img/4345wee68dsfsd.png",()=>{
// Only the Internet will execute this callback
alert(" I'm Internet ")
})
The code is simple , Just respond to our message before the timer arrives , That's intranet , But there is a serious risk
If the resource used to verify the intranet is deleted or the interface hangs , Then all of us IP Will be regarded as an external network , Because you are all overtime . Or if our network vibrates , Or the loaded resources are very large , It can also cause problems . So if you want to use this method, you must ensure , The resources or interfaces used for verification must be fast , Resources are guaranteed not to be deleted by mistake . It's best to write an interface or a small picture to judge the interface timeout . Don't choose any one for dozens M Pictures of the , In case the network explodes, it will be over .
3. The backend implementation
public static boolean test() throws IOException {
String ip = "apaddress";
Runtime runtime = Runtime.getRuntime(); // Get the running object of the current program
Process process = null; // Declare processing class objects
String line = null; // Return line information
InputStream is = null; // Input stream
InputStreamReader isr = null;// Byte stream
BufferedReader br = null;
boolean res = false;// result
try {
process = runtime.exec("ping " + ip); // PING
is = process.getInputStream(); // Instantiate the input stream
isr = new InputStreamReader(is);// Convert the input stream into a byte stream
br = new BufferedReader(isr);// Read text from bytes
while ((line = br.readLine()) != null) {
// Be sure to capitalize here Because in linux In the system , It's lowercase To be compatible with the system, add
if (line.toUpperCase().contains("TTL")) {
res = true;
break;
}
}
if (res) {
return true;
} else {
return false;
}
} catch (IOException e) {
runtime.exit(1);
return false;
} finally {
if (is != null) {
is.close();
}
if (isr != null) {
isr.close();
}
if (br != null) {
br.close();
}
}
}
Personally, I prefer to use the backend to realize , This only needs to be used IP that will do , No additional resources are required , More secure .
4. matters needing attention
If you use the back-end method, please pay attention , because java simulation PING request , If it doesn't work, it may cause a long-term jam , Because the way to get the request is while loop , There is a jam . So when calling this interface ,ajax Be sure to add the timeout . Avoid other problems caused by wireless waiting .
The reference codes are as follows :
ipAddressPing = (ip,callback,timeout=2000)=>{
$.ajax({
url:'/api/demo/ipAddressPing?ipAddress='+ip,
type:'get',
timeout:timeout, // Set the timeout time 2s
success:function(res){
console.log(res)
// The request did not time out , however PING no , It also shows that it is an external network Need to call interface Here you can judge for yourself
if (!res) {
if (callback && typeof callback == 'function') callback(res);
}
},
error:function(err){
console.log(err)
// Request timeout description Extranet Need to call interface
if (callback && typeof callback == 'function') callback(res);
}
})
}
边栏推荐
猜你喜欢
![[C exercise] find the number of 1 of an integer in binary](/img/cc/586b18d56305d6136dbd53fe321b10.png)
[C exercise] find the number of 1 of an integer in binary

【C 练习】输入年月,求出月份天数

How can Volvo be confident to maintain "zero casualties" in traffic safety in the era of electrification?

How Axure achieves screen adaptation

【C 练习】变种水仙花数

Redis transaction and Message Subscription Publishing

Matlab bottom source code to realize image dynamic binarization

结构体相关类型解析

Axure如何做到屏幕自适应

百度搜索基础信息设置规范,有哪些我们需要注意的问题呢?
随机推荐
【HCIA】OSI 模型
Online variety show was cold: "Aiyou tengmang" couldn't be put away, "shake up B" couldn't win
手机浏览器产品分析
nosql概述
蜘蛛抓取频率提高,可以快速增加网站收录吗?
今日睡眠质量记录80分
Input only allows you to input English, Chinese and other symbols
Chrome realizes automated testing: recording and playback web page actions
Say goodbye to Leica, Huawei opens a new era! Netizen: Xiaomi
C# 飞行棋小游戏
[practice C] xiaolele walks up the steps
C语言指针相关问题解析
[exercise C] drink soda
百度搜索基础信息设置规范,有哪些我们需要注意的问题呢?
redis事务和消息订阅发布
Network security (2)
Data Lake (19): SQL API reads Kafka data and writes it to iceberg table in real time
redis配置文件
HashSet和TreeSet
Introduction to redis