๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๊ฐœ๋ฐœ์ด์•ผ๊ธฐ

์ฝ”๋“œ์ด๊ทธ๋‚˜์ดํ„ฐCI REST API IP๋Œ€์—ญ๋Œ€ ํ—ˆ์šฉ/์ฐจ๋‹จ

๋ฐ˜์‘ํ˜•


์ฝ”๋“œ์ด๊ทธ๋‚˜์ดํ„ฐ (์ดํ•˜, CI) ๋กœ REST API ๊ฐœ๋ฐœ ์‹œ, ๋งŽ์ด ์“ฐ์ด๋Š” ์˜คํ”ˆ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ


ํŠน์ • IP๋ฅผ ํ—ˆ์šฉ/์ฐจ๋‹จ ํ•˜๋Š” ๊ธฐ๋Šฅ์ด ์žˆ๋Š”๋ฐ,


ํŠน์ • IP๋ฅผ (์˜ˆ- 1.1.1.1, 1.1.1.2) ์ง€์ •ํ•ด์„œ ์จ์•ผ๋˜๋Š” ๋ถˆํŽธํ•จ์ด ์žˆ์–ด 


ํ•ด๋‹น ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ปค์Šคํ„ฐ ๋งˆ์ด์ง• ํ–ˆ๋‹ค


REST API LIBRARY

https://github.com/chriskacerguis/codeigniter-restserver



/application/config/rest.php

$config['rest_ip_whitelist_enabled'] = TRUE;
$config['rest_ip_whitelist'] = '192.168.0.1, 192.168.0.2, 192.168.0/24';

์œ„ ํ™”์ดํŠธ๋ฆฌ์ŠคํŠธ์˜ ์„ค์ •๊ฐ’์„ ๋ณด๋ฉด, 192.168.0.1, 192.168.0.2๋Š” ๋‹น์—ฐํžˆ ํ—ˆ์šฉ์ด ์ž˜๋˜์ง€๋งŒ,


์ด ์™ธ ๋‹ค๋ฅธ ์•„์ดํ”ผ๋กœ ์ ‘๊ทผ ์‹œ, ์ ‘๊ทผ ๋ถˆ๊ฐ€๋ผ๋Š” ๋ฌธ๊ตฌ๊ฐ€ ๋œฌ๋‹ค


ํŠน์ • IP๋งŒ ๊ฑธ๋Ÿฌ์ฃผ์ง€, ๋Œ€์—ญ๋Œ€๋กœ๋Š” ์ ์šฉ์ด ๋ถˆ๊ฐ€ํ•œ๊ฒƒ..


๋งŒ์•ฝ ๋‚ด๋ถ€๋ง์—์„œ ์œ ๋™ IP๋ฅผ ์“ฐ๊ณ  ์žˆ๊ณ , ํŠน์ • ๋Œ€์—ญ๋Œ€๋กœ ์ฐจ๋‹จ/ํ—ˆ์šฉํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด...


์ˆ˜์‹ญ~ ์ˆ˜๋งŒ๊ฐœ์˜ IP๋ฅผ ๋‹ค ์ ์–ด๋‘๋Š” ๋ถˆ์ƒ์‚ฌ๋ฅผ ์—†์• ๊ธฐ ์œ„ํ•ด


์ฒ˜๋ฆฌํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ˆ˜์ •ํ•ด์คฌ๋‹ค


/application/libraries/REST_Controller.php

protected function _check_whitelist_auth()
{
$whitelist = explode(',', $this->config->item('rest_ip_whitelist'));
array_push($whitelist, '127.0.0.1', '0.0.0.0');
$is_ip_chk = FALSE;
foreach ($whitelist as &$ip)
{
$ip = trim($ip);
if(strpos($ip, "/") !== FALSE) {
if( $this->IP_Match($ip, $this->input->ip_address()) == TRUE) {
$is_ip_chk = TRUE;
break;
}
}
}
if (in_array($this->input->ip_address(), $whitelist) === FALSE && $is_ip_chk == FALSE)
{
$this->response([
$this->config->item('rest_status_field_name') => FALSE,
$this->config->item('rest_message_field_name') => $this->lang->line('text_rest_ip_unauthorized')
], self::HTTP_UNAUTHORIZED);
}
}


>>ํ•จ์ˆ˜ ์ถ”๊ฐ€

protected function IP_Match($network, $ip){
$ip_arr = explode("/",$network);
$network_long=ip2long($ip_arr[0]);
$mask_long= pow(2,32)-pow(2,(32-$ip_arr[1]));
$ip_long=ip2long($ip);
if (($ip_long & $mask_long) == $network_long) {
return TRUE;
} else {
return FALSE;
}
}



์ด๋ ‡๊ฒŒ ์ ์šฉํ•˜๋ฉด, IP๋ฅผ ์ญ‰~~~ ์ž‘์„ฑํ•  ํ•„์š” ์—†์ด ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅํ•˜๋‹ค


PHP ip2long

http://php.net/manual/kr/function.ip2long.php


PHP pow

http://php.net/manual/kr/function.pow.php