1+ <?php
2+ header ('Content-Type: application/json ' );
3+ header ('Access-Control-Allow-Origin: * ' );
4+ header ('Access-Control-Allow-Methods: GET, OPTIONS ' );
5+ header ('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With ' );
6+
7+ // Handle preflight requests
8+ if ($ _SERVER ['REQUEST_METHOD ' ] === 'OPTIONS ' ) {
9+ http_response_code (200 );
10+ exit ();
11+ }
12+
13+ function getServerIPs () {
14+ $ ips = array ();
15+
16+ // Get local network IP addresses
17+ $ localIPs = array ();
18+
19+ // Method 0: Check if HOST_IP is provided via environment variable
20+ $ hostIP = getenv ('HOST_IP ' );
21+ if ($ hostIP && filter_var ($ hostIP , FILTER_VALIDATE_IP , FILTER_FLAG_IPV4 )) {
22+ $ localIPs [] = $ hostIP ;
23+ }
24+
25+ // Method 1: Use hostname -I command (Linux/Unix)
26+ $ output = shell_exec ('hostname -I 2>/dev/null ' );
27+ if ($ output ) {
28+ $ addresses = explode (' ' , trim ($ output ));
29+ foreach ($ addresses as $ ip ) {
30+ if (filter_var ($ ip , FILTER_VALIDATE_IP , FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE )) {
31+ $ localIPs [] = $ ip ;
32+ }
33+ }
34+ }
35+
36+ // Method 2: Use ifconfig command
37+ if (empty ($ localIPs )) {
38+ $ output = shell_exec ('ifconfig 2>/dev/null | grep -oP "inet \K(\d+\.\d+\.\d+\.\d+)" | grep -v 127.0.0.1 ' );
39+ if ($ output ) {
40+ $ addresses = explode ("\n" , trim ($ output ));
41+ foreach ($ addresses as $ ip ) {
42+ if (filter_var ($ ip , FILTER_VALIDATE_IP , FILTER_FLAG_IPV4 ) && !preg_match ('/^127\./ ' , $ ip ) && !preg_match ('/^169\.254\./ ' , $ ip )) {
43+ $ localIPs [] = $ ip ;
44+ }
45+ }
46+ }
47+ }
48+
49+ // Method 3: Use ip command (modern Linux) - get host IP via route
50+ if (empty ($ localIPs )) {
51+ $ output = shell_exec ("ip route get 8.8.8.8 2>/dev/null | awk '{for(i=1;i<=NF;i++) if( \$i== \"src \") print \$(i+1)}' " );
52+ if ($ output ) {
53+ $ ip = trim ($ output );
54+ if (filter_var ($ ip , FILTER_VALIDATE_IP , FILTER_FLAG_IPV4 )) {
55+ $ localIPs [] = $ ip ;
56+ }
57+ }
58+ }
59+
60+ // Method 3.5: Get host IP from Docker gateway
61+ if (empty ($ localIPs )) {
62+ $ output = shell_exec ("ip route | grep default | awk '{print \$3}' 2>/dev/null " );
63+ if ($ output ) {
64+ $ gateway = trim ($ output );
65+ if (filter_var ($ gateway , FILTER_VALIDATE_IP , FILTER_FLAG_IPV4 )) {
66+ // Try to resolve the local network from the gateway
67+ $ networkBase = implode ('. ' , array_slice (explode ('. ' , $ gateway ), 0 , 3 ));
68+ for ($ i = 1 ; $ i <= 254 ; $ i ++) {
69+ $ testIP = $ networkBase . '. ' . $ i ;
70+ if ($ testIP !== $ gateway ) {
71+ // Check if this IP responds (simplified check)
72+ $ localIPs [] = $ gateway ; // Use gateway as fallback
73+ break ;
74+ }
75+ }
76+ }
77+ }
78+ }
79+
80+ // Method 4: Fallback - get server's hostname and resolve it
81+ if (empty ($ localIPs )) {
82+ $ hostname = gethostname ();
83+ $ ip = gethostbyname ($ hostname );
84+ if ($ ip !== $ hostname && filter_var ($ ip , FILTER_VALIDATE_IP , FILTER_FLAG_IPV4 )) {
85+ $ localIPs [] = $ ip ;
86+ }
87+ }
88+
89+ $ ips ['local ' ] = !empty ($ localIPs ) ? $ localIPs [0 ] : $ _SERVER ['SERVER_ADDR ' ] ?? '127.0.0.1 ' ;
90+
91+ // Get external IP
92+ $ externalIP = getExternalIP ();
93+ $ ips ['external ' ] = $ externalIP ;
94+
95+ return $ ips ;
96+ }
97+
98+ function getExternalIP () {
99+ $ services = [
100+ 'http://ipinfo.io/ip ' ,
101+ 'http://icanhazip.com ' ,
102+ 'http://ident.me ' ,
103+ 'http://whatismyipaddress.com/api '
104+ ];
105+
106+ $ context = stream_context_create ([
107+ 'http ' => [
108+ 'timeout ' => 10 ,
109+ 'user_agent ' => 'Mozilla/5.0 (compatible; ServerInfoBot/1.0) '
110+ ]
111+ ]);
112+
113+ foreach ($ services as $ service ) {
114+ try {
115+ $ ip = @file_get_contents ($ service , false , $ context );
116+ if ($ ip ) {
117+ $ ip = trim ($ ip );
118+ if (filter_var ($ ip , FILTER_VALIDATE_IP , FILTER_FLAG_IPV4 )) {
119+ return $ ip ;
120+ }
121+ }
122+ } catch (Exception $ e ) {
123+ continue ;
124+ }
125+ }
126+
127+ return 'Unable to detect ' ;
128+ }
129+
130+ try {
131+ $ serverIPs = getServerIPs ();
132+
133+ echo json_encode ([
134+ 'success ' => true ,
135+ 'local_ip ' => $ serverIPs ['local ' ],
136+ 'external_ip ' => $ serverIPs ['external ' ],
137+ 'hostname ' => gethostname (),
138+ 'server_addr ' => $ _SERVER ['SERVER_ADDR ' ] ?? 'unknown '
139+ ], JSON_PRETTY_PRINT );
140+
141+ } catch (Exception $ e ) {
142+ http_response_code (500 );
143+ echo json_encode ([
144+ 'success ' => false ,
145+ 'error ' => $ e ->getMessage ()
146+ ], JSON_PRETTY_PRINT );
147+ }
148+ ?>
0 commit comments