-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
194 lines (178 loc) · 8.65 KB
/
api.php
File metadata and controls
194 lines (178 loc) · 8.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header('Access-Control-Allow-Headers: Content-Type');
require_once 'IPSecurityLibrary.php';
require_once 'SimpleCache.php';
use Security\IPSecurityLibrary;
// Log klasörünü kontrol et ve oluştur
$logDir = 'logs';
if (!is_dir($logDir)) {
mkdir($logDir, 0755, true);
}
try {
// IP adresi parametresini al
$ip = $_GET['ip'] ?? null;
// IP adresi kontrolü
if ($ip === null || !filter_var($ip, FILTER_VALIDATE_IP)) {
http_response_code(400); // Bad Request
echo json_encode([
'success' => false,
'message' => 'Geçersiz veya eksik IP adresi.',
'code' => 400
], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
exit; // Hata durumunda işlemi sonlandır
}
// API anahtarı kontrolü (isteğe bağlı)
$apiKey = $_GET['api_key'] ?? null;
if ($apiKey !== null && $apiKey !== 'YOUR_API_KEY') {
throw new Exception('Geçersiz API anahtarı', 403);
}
// Konfigürasyon
$config = [
'cache_enabled' => true,
'geolocation_provider' => 'ip-api',
'log_enabled' => true,
'log_path' => 'security_logs/',
'proxy_check_enabled' => true,
'tor_check_enabled' => true,
'vpn_check_enabled' => true,
'asn_database_path' => 'databases/IP2LOCATION-LITE-ASN.CSV',
'datacenter_database_path' => 'databases/IP2LOCATION-DATACENTER.CSV',
'ip2location_enabled' => true,
'risk_threshold' => [
'low' => 2,
'medium' => 0,
'high' => -2
],
'api_keys' => [
'maxmind' => '',
'ipqualityscore' => '',
'proxycheck' => ''
]
];
// IP Güvenlik Kütüphanesini başlat
$ipSecurity = IPSecurityLibrary::getInstance($config);
// IP analizini gerçekleştir
$result = $ipSecurity->analyzeIP($ip);
// IP Location verilerini al
$ipLocationData = $ipSecurity->getIPLocationData($ip);
// Sadece gerekli bilgileri içeren array oluştur
$response = [
'success' => true,
'data' => [
'ip' => $result['ip'],
'timestamp' => date('Y-m-d H:i:s'),
'location' => $result['geolocation'] ?? null,
'network_info' => [
'is_datacenter' => $result['network_info']['is_datacenter'] ?? false,
'is_isp' => $result['network_info']['is_isp'] ?? false,
'asn_info' => $result['network_info']['asn_info'] ?? null,
'proxy_type' => $result['network_info']['proxy_type'] ?? null,
'usage_type' => $result['network_info']['usage_type'] ?? null,
'fraud_score' => $result['network_info']['fraud_score'] ?? 0,
'ip_location' => [
'is_proxy' => $ipLocationData['is_proxy'] ?? false,
'is_datacenter' => $ipLocationData['is_datacenter'] ?? false,
'is_vpn' => $ipLocationData['is_vpn'] ?? false,
'is_tor' => $ipLocationData['is_tor'] ?? false,
'is_mobile' => $ipLocationData['is_mobile'] ?? false,
'is_satellite' => $ipLocationData['is_satellite'] ?? false,
'company_type' => $ipLocationData['company_type'] ?? null,
'source' => $ipLocationData['source'] ?? null,
'country' => $ipLocationData['country'] ?? null,
'country_code' => $ipLocationData['country_code'] ?? null,
'region' => $ipLocationData['region'] ?? null,
'city' => $ipLocationData['city'] ?? null,
'isp' => $ipLocationData['isp'] ?? null,
'org_name' => $ipLocationData['org_name'] ?? null,
'as_no' => $ipLocationData['as_no'] ?? null,
'postal_code' => $ipLocationData['postal_code'] ?? null,
'latitude' => $ipLocationData['latitude'] ?? null,
'longitude' => $ipLocationData['longitude'] ?? null,
'abuse_score' => $ipLocationData['abuse_score'] ?? null,
'asn_abuse_score' => $ipLocationData['asn_abuse_score'] ?? null,
'connection_type' => $ipLocationData['connection_type'] ?? null,
'proxy_type' => $ipLocationData['proxy_type'] ?? null,
'threat_level' => $ipLocationData['threat_level'] ?? null,
'threat_types' => $ipLocationData['threat_types'] ?? [],
'confidence_score' => $ipLocationData['confidence_score'] ?? null,
'net_speed' => $ipLocationData['net_speed'] ?? null,
'area_code' => $ipLocationData['area_code'] ?? null,
'idd_code' => $ipLocationData['idd_code'] ?? null,
'mobile_brand' => $ipLocationData['mobile_brand'] ?? null,
'mcc' => $ipLocationData['mcc'] ?? null,
'mnc' => $ipLocationData['mnc'] ?? null,
'time_zone' => $ipLocationData['time_zone'] ?? null,
'weather_station_code' => $ipLocationData['weather_station_code'] ?? null,
'weather_station_name' => $ipLocationData['weather_station_name'] ?? null,
'elevation' => $ipLocationData['elevation'] ?? null,
'address_type' => $ipLocationData['address_type'] ?? null,
'category' => $ipLocationData['category'] ?? null,
'domain' => $ipLocationData['domain'] ?? null
]
],
'device_info' => [
'type' => $result['device_info']['type'] ?? 'Unknown',
'brand' => $result['device_info']['brand'] ?? 'Unknown',
'model' => $result['device_info']['model'] ?? 'Unknown',
'is_mobile' => $result['device_info']['is_mobile'] ?? false,
'is_tablet' => $result['device_info']['is_tablet'] ?? false,
'is_desktop' => $result['device_info']['is_desktop'] ?? false
],
'operating_system' => [
'name' => $result['operating_system']['name'] ?? 'Unknown',
'version' => $result['operating_system']['version'] ?? 'Unknown',
'architecture' => $result['operating_system']['architecture'] ?? 'Unknown'
],
'browser' => [
'name' => $result['browser_info']['name'] ?? 'Unknown',
'version' => $result['browser_info']['version'] ?? 'Unknown',
'user_agent' => $result['browser_info']['user_agent'] ?? 'Unknown',
'features' => $result['browser_info']['features'] ?? []
],
'language' => [
'code' => $result['language_info']['primary']['code'] ?? 'Unknown',
'name' => $result['language_info']['primary']['name'] ?? 'Unknown',
'all' => $result['language_info']['all'] ?? []
],
'security' => [
'risk_level' => $result['risk_assessment']['level'] ?? 'unknown',
'risk_score' => $result['risk_assessment']['score'] ?? 0,
'risk_factors' => $result['risk_assessment']['factors'] ?? [],
'is_proxy' => $result['security_checks']['is_proxy'] ?? false,
'is_vpn' => $result['security_checks']['is_vpn'] ?? false,
'is_tor' => $result['security_checks']['is_tor'] ?? false,
'threat_score' => $ipLocationData['threat_level'] ?? 0,
'abuse_confidence_score' => $ipLocationData['abuse_score'] ?? 0
],
'cached' => $config['cache_enabled']
],
'timestamp' => time()
];
// Log dosyası oluştur
$date = date('Y-m-d_H-i-s');
$logFile = "{$logDir}/{$date}.txt";
// Log içeriği
$logContent = [
'timestamp' => date('Y-m-d H:i:s'),
'ip' => $result['ip'],
'request_ip' => $ip ?? 'auto',
'response' => $response
];
// Log dosyasına yaz
file_put_contents(
$logFile,
json_encode($logContent, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)
);
// Yanıtı gönder
echo json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
} catch (Exception $e) {
http_response_code($e->getCode() ?: 500);
echo json_encode([
'success' => false,
'message' => $e->getMessage(),
'code' => $e->getCode()
], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
}