Untitled

 avatar
unknown
plain_text
2 months ago
4.4 kB
3
Indexable
#include <iostream>
#include <string>
#include <unordered_map>
#include <ctime>
#include "httplib.h"

using namespace std;
using namespace httplib;

unordered_map<string, int> rate_limits;
const int MAX_REQUESTS = 100;
const int TIME_WINDOW = 15 * 60; // 15 dakika

// Rate Limiting Kontrolü
bool rate_limiter(const string& ip) {
    time_t now = time(0);
    if (rate_limits.find(ip) == rate_limits.end()) {
        rate_limits[ip] = now;
        return true;
    } else {
        time_t last_request_time = rate_limits[ip];
        if (difftime(now, last_request_time) <= TIME_WINDOW) {
            return false; // Rate limit aşılmış
        } else {
            rate_limits[ip] = now;
            return true; // Yeni zaman dilimi, isteğe izin ver
        }
    }
}

int main() {
    Server svr;

    // Compression Middleware - Basit bir yaklaşım kullanıyoruz
    svr.set_filter("/player/login/dashboard", [](const Request& req, Response& res) {
        // Gerçek bir sunucuda burada sıkıştırma uygulanabilir
        return true; // İşlemeye devam et
    });

    // CORS Middleware
    svr.set_content_provider("/player/login/dashboard", [](const Request& req, Response& res) {
        res.set_header("Access-Control-Allow-Origin", "*");
        res.set_header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        return true;
    });

    // Logging Middleware
    svr.set_logger([](const Request& req, const Response& res) {
        cout << "[" << time(0) << "] " << req.method << " " << req.path << " - " << res.status << endl;
    });

    // /player/login/dashboard URL'si için POST isteği işlemi
    svr.Post("/player/login/dashboard", [](const Request& req, Response& res) {
        string tData = "{}"; // Örnek bir yanıt verisi
        try {
            string body = req.body;
            size_t pos = body.find("\"");
            if (pos != string::npos) {
                string uData = body.substr(pos + 1);
                // uData'yı çözümle ve giriş bilgilerini işleme
                size_t delim = uData.find("\\n");
                if (delim != string::npos) {
                    uData = uData.substr(0, delim);
                    tData = "{\"username\":\"" + uData + "\"}";
                }
            }
        } catch (const exception& e) {
            cout << "Uyarı: " << e.what() << endl;
        }
        res.set_content(tData, "application/json");
    });

    // /player/growid/login/validate URL'si için POST isteği işlemi
    svr.Post("/player/growid/login/validate", [](const Request& req, Response& res) {
        string _token = req.get_param_value("_token");
        string growId = req.get_param_value("growId");
        string password = req.get_param_value("password");

        string token = "_token=" + _token + "&growId=" + growId + "&password=" + password;
        string base64_token = "base64encoded_" + token; // Base64 kodlamasını taklit ediyoruz

        string response = "{\"status\":\"success\",\"message\":\"Hesap Doğrulandı.\",\"token\":\"" + base64_token + "\",\"url\":\"\",\"accountType\":\"growtopia\"}";
        res.set_content(response, "application/json");
    });

    // /player/growid/checktoken URL'si için POST isteği işlemi
    svr.Post("/player/growid/checktoken", [](const Request& req, Response& res) {
        string refreshToken = req.body; // Gerçek dünyada, token doğrulaması yapılır
        string response = "{\"status\":\"success\",\"message\":\"Hesap Doğrulandı\",\"token\":\"" + refreshToken + "\",\"url\":\"\",\"accountType\":\"growtopia\"}";
        res.set_content(response, "application/json");
    });

    // / URL'si için GET isteği işlemi
    svr.Get("/", [](const Request& req, Response& res) {
        res.set_content("Merhaba Dünya!", "text/plain");
    });

    // Her istek için Rate Limiting Kontrolü
    svr.set_filter("/player/*", [](const Request& req, Response& res) {
        string client_ip = req.remote_addr;
        if (!rate_limiter(client_ip)) {
            res.status = 429; // Çok fazla istek
            res.set_content("{\"error\":\"Rate limit aşıldı\"}", "application/json");
            return false; // İşlemeyi durdur
        }
        return true;
    });

    // Sunucuyu 5000 numaralı portta başlat
    svr.listen("0.0.0.0", 80);
}
Leave a Comment