Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- # g++ -std=c++14 -pthread -lboost_system -lboost_filesystem BotBan.cpp -I/usr/include/mysql -I/usr/include/mysql++ -L/usr/local/lib -lmysqlpp -lmysqlclient -Ofast -flto -march=native -o BotBan
- # # # # # # # # # # # # # # # # # # Bot Ban Programm (C++) # # # # # # # # # # # # # # # # # #
- # (c) 2016, AntiDot
- **/
- #include <iostream>
- #include <cstdlib>
- #include <string>
- #include <sstream>
- #include <vector>
- #include <ctime>
- #include <stdio.h>
- #include <iterator>
- #include <cstring>
- #include <time.h>
- #include <string.h>
- #include <fstream>
- #include <algorithm>
- #include <stdlib.h>
- #include <thread>
- #include <boost/filesystem.hpp>
- #include <mysql.h>
- #include <mysql++.h>
- #include <mysql/mysql.h>
- using namespace std;
- namespace fs = boost::filesystem;
- // just going to input the general details and not the port numbers
- struct connection_details
- {
- char *server;
- char *user;
- char *password;
- char *database;
- };
- MYSQL* mysql_connection_setup(struct connection_details mysql_details)
- {
- // first of all create a mysql instance and initialize the variables within
- MYSQL *connection = mysql_init(NULL);
- // connect to the database with the details attached.
- if (!mysql_real_connect(connection,mysql_details.server, mysql_details.user, mysql_details.password, mysql_details.database, 0, NULL, 0)) {
- printf("Conection error : %s\n", mysql_error(connection));
- exit(1);
- }
- return connection;
- }
- MYSQL_RES* mysql_perform_query(MYSQL *connection, char *sql_query)
- {
- // send the query to the database
- if (mysql_query(connection, sql_query))
- {
- printf("MySQL query error : %s\n", mysql_error(connection));
- exit(1);
- }
- return mysql_use_result(connection);
- }
- // Split
- vector<string> &split(const string &s, char delim, vector<string> &elems) {
- stringstream ss(s);
- string item;
- while (getline(ss, item, delim)) {
- elems.push_back(item);
- }
- return elems;
- }
- vector<string> split(const string &s, char delim) {
- vector<string> elems;
- split(s, delim, elems);
- return elems;
- }
- // Vars
- vector<string> banlist;
- vector<string> whitelist = split("151.80.110.115 94.23.163.192 127.0.0.1 0.0.0.0", ' ');
- string fban;
- string fbots;
- string flow;
- size_t gfound;
- string orgflow;
- size_t orgfound;
- string orgflow1;
- size_t orgfound1;
- // Date
- string datan() {
- time_t rawtime;
- struct tm * timeinfo;
- char buffer [80];
- time ( &rawtime );
- timeinfo = localtime ( &rawtime );
- strftime (buffer,80,"%H:%M:%S",timeinfo);
- return buffer;
- }
- // Write Open File
- void WOpenFile(string name1, string atr1, string data1) {
- const char* name = name1.c_str();
- const char* atr = atr1.c_str();
- const char* data = data1.c_str();
- FILE * file = fopen(name, atr);
- if (file)
- {
- fputs(data, file);
- }
- else
- cout << "File error!" << endl;
- fclose(file);
- }
- // Read Open File
- string ROpenFile(string name1) {
- const char* name = name1.c_str();
- FILE * file = fopen(name, "r");
- if (file)
- {
- char buf[1000];
- string result;
- while (fgets(buf,1000, file)!=NULL) {
- string buf1 = string(buf);
- result = result+buf1;
- }
- fclose(file);
- return result;
- }
- else
- cout << "File error!" << endl;
- fclose(file);
- return 0;
- }
- // Ban
- string Ban(string ip) {
- bool found3;
- bool found2;
- found3 = ( find(whitelist.begin(), whitelist.end(), ip) == whitelist.end() ? false : true );
- found2 = ( find(banlist.begin(), banlist.end(), ip) == banlist.end() ? false : true );
- if (found2 == false and found3 == false) {
- banlist.push_back(ip);
- string t = "["+datan()+"] [\x1b[31mBot\x1b[0m] -> Забанен "+ip+"!";
- string syss = "ipset -A dropips "+ip;
- system(syss.c_str());
- return t;
- } else {
- return "No";
- }
- }
- // BAN thread
- void Thrd() {
- vector<string> bans;
- string tobanstr;
- string line;
- int sll = 0;
- while (true) {
- MYSQL *conn;
- MYSQL_RES *res;
- MYSQL_ROW row;
- struct connection_details mysqlD;
- mysqlD.server = (char *)"localhost";
- mysqlD.user = (char *)"root";
- mysqlD.password = (char *)"rly";
- mysqlD.database = (char *)"lol";
- conn = mysql_connection_setup(mysqlD);
- res = mysql_perform_query(conn, (char *)"SELECT * FROM `Blacklist`");
- while ((row = mysql_fetch_row(res)) !=NULL) {
- tobanstr = Ban(row[1]);
- if (tobanstr != "No") {
- cout << tobanstr << endl;
- }
- bans.clear();
- struct timespec tw = {0,000500000};
- struct timespec tr;
- nanosleep (&tw, &tr);
- }
- mysql_free_result(res);
- mysql_close(conn);
- struct timespec tw1 = {sll,500000000};
- struct timespec tr1;
- nanosleep (&tw1, &tr1);
- }
- }
- // MAIN
- int main()
- {
- string start = "["+datan()+"] [\x1b[34mBot\x1b[0m] -> Старт Ban Bot v.0.1!";
- cout << start << endl;
- string mes;
- int ssize;
- string s;
- stringstream out;
- thread Ban(Thrd);
- while (true) {
- if (banlist.size() > 0) {
- ssize = (int)banlist.size();
- out << ssize;
- s = out.str();
- mes = "["+datan()+"] [\x1b[34mWAF\x1b[0m] -> Забанено всего "+s+" ботов!";
- cout << mes << endl;
- }
- out.str("");
- banlist.clear();
- struct timespec tw = {20,000000000};
- struct timespec tr;
- nanosleep (&tw, &tr);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement