✅ IP(아이피) 주소 체크하기
안녕하세요 TriplexLab 입니다.
오늘은 내 사이트에 접속한 IP 번호 체크하는 부분에 대해서 살펴보도록 하겠습니다.
내 사이트가 특정 컴퓨터만(IP만) 접속이 가능하게 하고 싶으시다면
아래코드를 참고하세요! 👇👇
실제 프로젝트 할때는 아래 코드를 사용하서도 되지만
현재 접속한 컴퓨터 IP번호 조회하는것은 백앤드 개발자 분과 협업할때 문의하시면 될것 같습니다.
(여기서는 이미로 하드코드으로 IP번호 넣었습니다.)
🔍 등록할 IP 여러게 일경우
만약 등록 IP 내역이 : 115.32.141.0 ~ 115.32.141.202 까지 있다고 한다면
다음과같이 등록 하시면 됩니다.
const ipStart = [
{'company':'115.32.141.0'},
];
const ipEnd = [
{'company':'115.32.141.202'},
];
🔍 등록할 IP 한개인 경우
만약 등록 IP 내역이 : 112.22.371.634 있다고 한다면
다음과같이 등록 하시면 됩니다.
const ipStart = [
{'home':'112.22.371.634'},
];
const ipEnd = [
{'home':'112.22.371.634'},
];
👨💻 전체 코드
<script>
(() => {
function pad(n) {
return (n.length < 3) ? pad('0' + n) : n;
};
function atoi(ip) {
return Object.keys(ip).reduce(function(acc, cur){
return parseInt(ip[cur].split('.').map(function (el) {
return pad(el);
}).join(''), 10);
},0)
};
function inRange(ipAddr, startIp, endIp) {
return (atoi(ipAddr) >= atoi(startIp)) && (atoi(ipAddr) <= atoi(endIp));
};
const ip = {'currentIP':'115.32.141.1'};
const ipStart = [
{'home':'112.22.371.634'},
{'company':'115.32.141.0'},
];
const ipEnd = [
{'home':'112.22.371.634'},
{'company':'115.32.141.200'},
];
let allowAccess = false;
ipStart.forEach(function(el, idx){
if (inRange(ip, ipStart[idx], ipEnd[idx])){
allowAccess = true;
}
});
if (!allowAccess) {
location.href="https://www.naver.com/";
}
})();
</script>
파일로도 제공합니다.
필요한 분들은 다운 받으세요!!👏👏