Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,331 +1,154 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
from fastapi import FastAPI, Request, HTTPException
|
| 6 |
from fastapi.responses import HTMLResponse
|
| 7 |
-
from
|
| 8 |
-
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 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 |
-
# สแกนรูปแบบคำสั่งแฮกเกอร์ XSS, SQLi, RCE, Path Traversal, LFI/RFI
|
| 38 |
-
EXPLOIT_PAYLOADS = [
|
| 39 |
-
r"select\s+.*\s+from", r"insert\s+into", r"union\s+select", r"drop\s+table",
|
| 40 |
-
r"<\s*script", r"javascript:", r"onerror\s*=", r"onload\s*=", r"\'\s*or\s*.*=.*",
|
| 41 |
-
r"\.\.\/", r"etc\/passwd", r"boot\.ini", r"\{\s*\{\s*.*\}\s*\}", r"\$\{.*\}",
|
| 42 |
-
r"exec\s*\(\s*", r"eval\s*\(\s*", r"union\s+all\s+select"
|
| 43 |
-
]
|
| 44 |
-
|
| 45 |
-
def get_client_ip(request: Request):
|
| 46 |
-
for header in ["cf-connecting-ip", "x-real-ip", "x-forwarded-for"]:
|
| 47 |
-
value = request.headers.get(header)
|
| 48 |
-
if value:
|
| 49 |
-
return value.split(',')[0].strip()
|
| 50 |
-
return request.client.host
|
| 51 |
-
|
| 52 |
-
def generate_token(ip):
|
| 53 |
-
# เปลี่ยนแปลงค่า Token ทุกๆ 10 นาที ผูกกับเวลาปัจจุบันของระบบ
|
| 54 |
-
time_window = int(time.time() / 600)
|
| 55 |
-
return hashlib.md5(f"{ip}{SECRET_KEY}{time_window}".encode()).hexdigest()
|
| 56 |
-
|
| 57 |
-
def is_blocked(ip):
|
| 58 |
-
with SqliteDict(FIREWALL_DB, autocommit=True) as db:
|
| 59 |
-
if ip in db:
|
| 60 |
-
blocked_until = db[ip].get("blocked_until", 0)
|
| 61 |
-
if blocked_until > time.time(): return True
|
| 62 |
-
else: del db[ip]
|
| 63 |
-
return False
|
| 64 |
-
|
| 65 |
-
def check_rate_limit_and_block(ip):
|
| 66 |
-
with SqliteDict(FIREWALL_DB, autocommit=True) as db:
|
| 67 |
-
data = db.get(ip, {"count": 0, "first_request": time.time()})
|
| 68 |
-
data["count"] += 1
|
| 69 |
-
db[ip] = data
|
| 70 |
-
if time.time() - data["first_request"] > 60:
|
| 71 |
-
del db[ip]
|
| 72 |
-
return
|
| 73 |
-
if data["count"] > MAX_REQUESTS_PER_MINUTE:
|
| 74 |
-
data["blocked_until"] = time.time() + BLOCK_TIME_SECONDS
|
| 75 |
-
db[ip] = data
|
| 76 |
-
|
| 77 |
-
def force_block_ip(ip):
|
| 78 |
-
with SqliteDict(FIREWALL_DB, autocommit=True) as db:
|
| 79 |
-
db[ip] = {
|
| 80 |
-
"count": 999,
|
| 81 |
-
"first_request": time.time(),
|
| 82 |
-
"blocked_until": time.time() + BLOCK_TIME_SECONDS
|
| 83 |
-
}
|
| 84 |
-
|
| 85 |
-
def cyber_security_waf(request: Request):
|
| 86 |
-
user_agent = request.headers.get("user-agent", "").lower()
|
| 87 |
-
|
| 88 |
-
# 1. ตรวจสอบลักษณะพื้นฐานและบัญชีดำของบอท
|
| 89 |
-
if not user_agent or len(user_agent) < 12:
|
| 90 |
-
return False
|
| 91 |
-
for bot in BOT_BLACKLIST:
|
| 92 |
-
if bot in user_agent:
|
| 93 |
-
return False
|
| 94 |
-
|
| 95 |
-
# ตรวจสอบว่ามี Headers ที่เบราว์เซอร์ปกติควรมีหรือไม่
|
| 96 |
-
if not request.headers.get("accept") or not request.headers.get("accept-language"):
|
| 97 |
-
return False
|
| 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 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
if is_blocked(ip):
|
| 220 |
-
return HTMLResponse(content='<div style="font-family:-apple-system,sans-serif;background:#0f172a;color:#f8fafc;display:flex;align-items:center;justify-content:center;height:100vh;margin:0;text-align:center;"><div style="max-width:450px;width:90%;background:#1e1b4b;border:2px solid #ef4444;padding:40px;border-radius:16px;"><h1 style="color:#ef4444;">เกิดข้อผิดพลาด</h1><p>ไม่สามารถเชื่อมต่อกับเซิร์ฟเวอร์ได้ (IP Blocked)</p></div></div>', status_code=403)
|
| 221 |
-
check_rate_limit_and_block(ip)
|
| 222 |
-
return generate_challenge_html(ip)
|
| 223 |
-
|
| 224 |
-
@app.get("/verify")
|
| 225 |
-
async def verify(token: str, request: Request):
|
| 226 |
-
ip = get_client_ip(request)
|
| 227 |
-
if is_blocked(ip):
|
| 228 |
-
return {"status": "error", "valid": False, "banned": True}
|
| 229 |
-
is_valid = (token == generate_token(ip))
|
| 230 |
-
return {"status": "ok" if is_valid else "error", "valid": is_valid, "banned": False}
|
| 231 |
-
|
| 232 |
-
@app.get("/get_token")
|
| 233 |
-
async def get_token(request: Request):
|
| 234 |
-
ip = get_client_ip(request)
|
| 235 |
-
if is_blocked(ip):
|
| 236 |
-
raise HTTPException(status_code=403, detail="Blocked")
|
| 237 |
-
return {"token": generate_token(ip)}
|
| 238 |
-
|
| 239 |
-
@app.get("/report_spam")
|
| 240 |
-
async def report_spam(request: Request):
|
| 241 |
-
ip = get_client_ip(request)
|
| 242 |
-
force_block_ip(ip)
|
| 243 |
-
return {"status": "banned"}
|
| 244 |
-
|
| 245 |
-
# --- แทร็กสคริปต์ส่งไปฝังหน้าเว็บหลัก Vercel (ระบบความปลอดภัยขั้นสูง + ดักจับเซิร์ฟเวอร์หลับ + หน้าต่างยินยอมคุกกี้) ---
|
| 246 |
-
@app.get("/js")
|
| 247 |
-
async def get_js_script(request: Request):
|
| 248 |
-
ip = get_client_ip(request)
|
| 249 |
-
hf_url = f"{request.url.scheme}://{request.url.netloc}"
|
| 250 |
-
|
| 251 |
-
error_layout = """<div style='font-family:-apple-system,sans-serif;background:#0f172a;color:#f8fafc;display:flex;align-items:center;justify-content:center;height:100vh;margin:0;text-align:center;position:fixed;top:0;left:0;width:100vw;z-index:999999;'><div style='max-width:450px;width:90%;background:#1e1b4b;border:2px solid #ef4444;padding:40px;border-radius:16px;box-shadow:0 10px 25px rgba(239,68,68,0.2);'><div style='font-size:4rem;margin-bottom:20px;'>⚠️</div><h1 style='color:#ef4444;font-size:1.8rem;margin:0 0 12px 0;'>เกิดข้อผิดพลาด</h1><p style='color:#cbd5e1;font-size:1.1rem;margin:0 0 10px 0;'>ไม่สามารถเชื่อมต่อกับเซิร์ฟเวอร์ได้</p><p style='color:#64748b;font-size:0.9rem;margin-bottom:25px;'>เซิร์ฟเวอร์อาจจะกำลังหลับหรือมีปัญหาชั่วคราว</p><button onclick='window.location.reload()' style='background:#ef4444;color:white;border:none;padding:12px 24px;border-radius:6px;font-size:1rem;font-weight:bold;cursor:pointer;'>ลองใหม่อีกครั้ง</button></div></div>"""
|
| 252 |
-
|
| 253 |
-
if is_blocked(ip):
|
| 254 |
-
js_code = f"""
|
| 255 |
-
(function() {{
|
| 256 |
-
localStorage.clear();
|
| 257 |
-
document.body.innerHTML="{error_layout}";
|
| 258 |
-
}})();
|
| 259 |
-
"""
|
| 260 |
-
return HTMLResponse(content=js_code, media_type="application/javascript")
|
| 261 |
-
|
| 262 |
-
js_code = f"""
|
| 263 |
-
(function() {{
|
| 264 |
-
const h='{hf_url}',u=new URL(location),t=u.searchParams.get('tz_token')||localStorage.tz;let c=[];
|
| 265 |
-
|
| 266 |
-
function showErrPage() {{
|
| 267 |
-
localStorage.clear();
|
| 268 |
-
document.body.innerHTML="{error_layout}";
|
| 269 |
-
}}
|
| 270 |
-
|
| 271 |
-
// ฟังก์ชันสร้างหน้าต่างยอมรับคุกกี้ (Cookie Consent Banner)
|
| 272 |
-
function initCookieBanner() {{
|
| 273 |
-
if (localStorage.cookieAccepted === 'true') return;
|
| 274 |
-
const b = document.createElement('div');
|
| 275 |
-
b.id = 'tz-cookie-banner';
|
| 276 |
-
b.style = 'position:fixed;bottom:20px;left:50%;transform:translateX(-50%);width:90%;max-width:400px;background:#1e293b;color:#f8fafc;padding:16px;border-radius:12px;box-shadow:0 10px 25px rgba(0,0,0,0.3);z-index:99999;font-family:-apple-system,sans-serif;display:flex;flex-direction:column;gap:12px;border:1px solid #334155;';
|
| 277 |
-
b.innerHTML = '<div style="font-size:14px;line-height:1.4;">🍪 เว็บไซต์นี้ใช้คุกกี้และระบบจัดเก็บข้อมูลในเบราว์เซอร์เพื่อความปลอดภัยและมอบประสบการณ์การใช้งานที่ดีที่สุดให้กับคุณ</div><button id="tz-accept-cookie" style="background:#10b981;color:white;border:none;padding:8px 16px;border-radius:6px;font-weight:bold;cursor:pointer;font-size:13px;transition:background 0.2s;">ยอมรับทั้งหมด</button>';
|
| 278 |
-
document.body.appendChild(b);
|
| 279 |
-
document.getElementById('tz-accept-cookie').onclick = function() {{
|
| 280 |
-
localStorage.cookieAccepted = 'true';
|
| 281 |
-
b.remove();
|
| 282 |
-
}};
|
| 283 |
-
}}
|
| 284 |
-
|
| 285 |
-
// 1. ตรวจสอบความปลอดภัยระดับลึกบนเบราว์เซอร์เป้าหมาย
|
| 286 |
-
const isSpamBot = navigator.webdriver ||
|
| 287 |
-
window.outerWidth === 0 ||
|
| 288 |
-
self !== top ||
|
| 289 |
-
(window.Firebug && window.Firebug.chrome && window.Firebug.chrome.isInitialized);
|
| 290 |
-
|
| 291 |
-
if (isSpamBot) {{ showErrPage(); return; }}
|
| 292 |
-
|
| 293 |
-
// 2. ดักจับการสแปมคลิกถี่เกินความจริง (Anti-Click Spam)
|
| 294 |
-
window.addEventListener('click',()=>{{
|
| 295 |
-
c=c.filter(x=>Date.now()-x<1000);c.push(Date.now());
|
| 296 |
-
if(c.length>=5){'{'}
|
| 297 |
-
fetch(h+'/report_spam').then(()=>{{ showErrPage(); }}).catch(()=>{{ showErrPage(); }});
|
| 298 |
-
{'}'}
|
| 299 |
-
}});
|
| 300 |
-
|
| 301 |
-
if(t){{
|
| 302 |
-
if(t.length !== 32) {{ showErrPage(); return; }}
|
| 303 |
-
localStorage.tz=t;
|
| 304 |
-
if(u.searchParams.has('tz_token')){{u.searchParams.delete('tz_token');history.replaceState({{}},'',u);}}
|
| 305 |
-
|
| 306 |
-
const controller = new AbortController();
|
| 307 |
-
const timeoutId = setTimeout(() => {{ controller.abort(); showErrPage(); }}, 5000);
|
| 308 |
-
|
| 309 |
-
fetch(h+'/verify?token='+t, {{ signal: controller.signal }})
|
| 310 |
-
.then(r=>{{
|
| 311 |
-
clearTimeout(timeoutId);
|
| 312 |
-
if(r.status !== 200) {{ showErrPage(); return; }}
|
| 313 |
-
return r.json();
|
| 314 |
-
}})
|
| 315 |
-
.then(d=>{{
|
| 316 |
-
if(!d || !d.valid || d.banned){{ showErrPage(); }}
|
| 317 |
-
else {{ initCookieBanner(); }} // เมื่อ Token ผ่าน ให้รันระบบคุกกี้ขึ้นมา
|
| 318 |
-
}})
|
| 319 |
-
.catch(()=>{{ showErrPage(); }});
|
| 320 |
-
}}else{{
|
| 321 |
-
// ระบบตรวจสอบและปลุกเซิร์ฟเวอร์ (ดักจับ Error/หลับ)
|
| 322 |
-
const controller = new AbortController();
|
| 323 |
-
const timeoutId = setTimeout(() => {{ controller.abort(); showErrPage(); }}, 5000);
|
| 324 |
-
|
| 325 |
-
fetch(h, {{ method: 'HEAD', mode: 'no-cors', signal: controller.signal }})
|
| 326 |
-
.then(() => {{ window.location.href = h; }})
|
| 327 |
-
.catch(() => {{ showErrPage(); }});
|
| 328 |
-
}}
|
| 329 |
-
})();
|
| 330 |
-
"""
|
| 331 |
-
return HTMLResponse(content=js_code, media_type="application/javascript")
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import io
|
| 3 |
+
import json
|
| 4 |
+
from fastapi import FastAPI, File, UploadFile, Form
|
|
|
|
| 5 |
from fastapi.responses import HTMLResponse
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import imagehash
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
| 11 |
+
DATABASE_DIR = "scam_database"
|
| 12 |
+
VOTES_FILE = "votes.json"
|
| 13 |
+
|
| 14 |
+
# ฟังก์ชันโหลดข้อมูลโหวตจากไฟล์ JSON
|
| 15 |
+
def load_votes():
|
| 16 |
+
if os.path.exists(VOTES_FILE):
|
| 17 |
+
try:
|
| 18 |
+
with open(VOTES_FILE, "r", encoding="utf-8") as f:
|
| 19 |
+
return json.load(f)
|
| 20 |
+
except Exception:
|
| 21 |
+
return {}
|
| 22 |
+
return {}
|
| 23 |
+
|
| 24 |
+
# ฟังก์ชันบันทึกข้อมูลโหวตลงไฟล์ JSON
|
| 25 |
+
def save_votes(votes_data):
|
| 26 |
+
with open(VOTES_FILE, "w", encoding="utf-8") as f:
|
| 27 |
+
json.dump(votes_data, f, ensure_ascii=False, indent=2)
|
| 28 |
+
|
| 29 |
+
# ฟังก์ชันดึง Hash ของรูปทั้งหมดในฐานข้อมูล
|
| 30 |
+
def get_database_hashes():
|
| 31 |
+
hashes = {}
|
| 32 |
+
if not os.path.exists(DATABASE_DIR):
|
| 33 |
+
os.makedirs(DATABASE_DIR)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
for filename in os.listdir(DATABASE_DIR):
|
| 36 |
+
if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.webp')):
|
| 37 |
+
filepath = os.path.join(DATABASE_DIR, filename)
|
| 38 |
+
try:
|
| 39 |
+
img = Image.open(filepath)
|
| 40 |
+
h = str(imagehash.phash(img))
|
| 41 |
+
hashes[filename] = h
|
| 42 |
+
except Exception as e:
|
| 43 |
+
print(f"Error loading {filename}: {e}")
|
| 44 |
+
return hashes
|
| 45 |
+
|
| 46 |
+
@app.get("/", response_class=HTMLResponse)
|
| 47 |
+
def serve_index():
|
| 48 |
+
if os.path.exists("index.html"):
|
| 49 |
+
with open("index.html", "r", encoding="utf-8") as f:
|
| 50 |
+
return f.read()
|
| 51 |
+
return "<h1>index.html not found</h1>"
|
| 52 |
+
|
| 53 |
+
@app.post("/check")
|
| 54 |
+
async def check_image(file: UploadFile = File(...)):
|
| 55 |
+
try:
|
| 56 |
+
contents = await file.read()
|
| 57 |
+
uploaded_img = Image.open(io.BytesIO(contents))
|
| 58 |
+
uploaded_hash_obj = imagehash.phash(uploaded_img)
|
| 59 |
+
uploaded_hash_str = str(uploaded_hash_obj)
|
| 60 |
+
|
| 61 |
+
db_hashes = get_database_hashes()
|
| 62 |
+
votes_data = load_votes()
|
| 63 |
+
|
| 64 |
+
MATCH_THRESHOLD = 12
|
| 65 |
+
matched_hash_key = None
|
| 66 |
+
is_in_db = False
|
| 67 |
+
|
| 68 |
+
# เปรียบเทียบกับภาพในฐานข้อมูล
|
| 69 |
+
for filename, db_hash_str in db_hashes.items():
|
| 70 |
+
db_hash_obj = imagehash.hex_to_hash(db_hash_str)
|
| 71 |
+
distance = uploaded_hash_obj - db_hash_obj
|
| 72 |
+
if distance <= MATCH_THRESHOLD:
|
| 73 |
+
matched_hash_key = db_hash_str
|
| 74 |
+
is_in_db = True
|
| 75 |
+
break
|
| 76 |
+
|
| 77 |
+
# ถ้าไม่เจอในฐานข้อมูล ใช้ Hash ของภาพที่อัปโหลดเป็น Key
|
| 78 |
+
if not matched_hash_key:
|
| 79 |
+
matched_hash_key = uploaded_hash_str
|
| 80 |
+
|
| 81 |
+
# ดึงคะแนนโหวต
|
| 82 |
+
image_vote = votes_data.get(matched_hash_key, {"likes": 0, "dislikes": 0})
|
| 83 |
+
likes = image_vote.get("likes", 0)
|
| 84 |
+
dislikes = image_vote.get("dislikes", 0)
|
| 85 |
+
|
| 86 |
+
# เงื่อนไขตรวจสอบสถานะ
|
| 87 |
+
if likes >= 100:
|
| 88 |
+
is_scam = False
|
| 89 |
+
result_text = "ปลอดภัย"
|
| 90 |
+
detail_text = f"รูปภาพนี้ได้รับการยืนยันว่าปลอดภัยจากผู้ใช้งาน (กดไลก์ {likes} คน)"
|
| 91 |
+
elif is_in_db:
|
| 92 |
+
is_scam = True
|
| 93 |
+
result_text = "มิจแน่นอน100%"
|
| 94 |
+
detail_text = "พบรูปภาพตรงกับฐานข้อมูลรูปภาพโปรโมทหลอกลวง"
|
| 95 |
+
else:
|
| 96 |
+
is_scam = False
|
| 97 |
+
result_text = "ไม่สามารถตรวจสอบได้"
|
| 98 |
+
detail_text = "ยังไม่พบรูปภาพนี้ในฐานข้อมูลมิจฉาชีพ"
|
| 99 |
+
|
| 100 |
+
return {
|
| 101 |
+
"is_scam": is_scam,
|
| 102 |
+
"result": result_text,
|
| 103 |
+
"detail": detail_text,
|
| 104 |
+
"image_hash": matched_hash_key,
|
| 105 |
+
"likes": likes,
|
| 106 |
+
"dislikes": dislikes
|
| 107 |
+
}
|
| 108 |
+
except Exception as e:
|
| 109 |
+
return {"error": f"เกิดข้อผิดพลาดในการประมวลผล: {str(e)}"}
|
| 110 |
+
|
| 111 |
+
@app.post("/vote")
|
| 112 |
+
async def vote_image(image_hash: str = Form(...), vote_type: str = Form(...)):
|
| 113 |
+
try:
|
| 114 |
+
votes_data = load_votes()
|
| 115 |
+
if image_hash not in votes_data:
|
| 116 |
+
votes_data[image_hash] = {"likes": 0, "dislikes": 0}
|
| 117 |
+
|
| 118 |
+
if vote_type == "like":
|
| 119 |
+
votes_data[image_hash]["likes"] += 1
|
| 120 |
+
elif vote_type == "dislike":
|
| 121 |
+
votes_data[image_hash]["dislikes"] += 1
|
| 122 |
+
|
| 123 |
+
save_votes(votes_data)
|
| 124 |
+
|
| 125 |
+
likes = votes_data[image_hash]["likes"]
|
| 126 |
+
dislikes = votes_data[image_hash]["dislikes"]
|
| 127 |
+
|
| 128 |
+
# คำนวณสถานะใหม่หลังกดโหวต
|
| 129 |
+
db_hashes = get_database_hashes()
|
| 130 |
+
is_in_db = image_hash in db_hashes.values()
|
| 131 |
+
|
| 132 |
+
if likes >= 100:
|
| 133 |
+
is_scam = False
|
| 134 |
+
result_text = "ปลอดภัย"
|
| 135 |
+
detail_text = f"รูปภาพนี้ได้รับการยืนยันว่าปลอดภัยจากผู้ใช้งาน (กดไลก์ {likes} คน)"
|
| 136 |
+
elif is_in_db:
|
| 137 |
+
is_scam = True
|
| 138 |
+
result_text = "มิจแน่นอน100%"
|
| 139 |
+
detail_text = "พบรูปภาพตรงกับฐานข้อมูลรูปภาพโ���รโมทหลอกลวง"
|
| 140 |
+
else:
|
| 141 |
+
is_scam = False
|
| 142 |
+
result_text = "ไม่สามารถตรวจสอบได้"
|
| 143 |
+
detail_text = "ยังไม่พบรูปภาพนี้ในฐานข้อมูลมิจฉาชีพ"
|
| 144 |
+
|
| 145 |
+
return {
|
| 146 |
+
"success": True,
|
| 147 |
+
"likes": likes,
|
| 148 |
+
"dislikes": dislikes,
|
| 149 |
+
"is_scam": is_scam,
|
| 150 |
+
"result": result_text,
|
| 151 |
+
"detail": detail_text
|
| 152 |
+
}
|
| 153 |
+
except Exception as e:
|
| 154 |
+
return {"error": f"เกิดข้อผิดพลาดในการโหวต: {str(e)}"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|