Clone of Akilla's acserver @ https://github.com/deregtd/ACServer

DirServ.cpp 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. // DirServ.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <time.h>
  8. struct stServerInfo {
  9. char Name[64];
  10. char IP[64];
  11. unsigned short Port;
  12. time_t LastUpdate;
  13. int Users;
  14. int Type;
  15. char Speed[16];
  16. char Admin[64];
  17. };
  18. int main()
  19. {
  20. char buff[10000]; memset(buff, 0, 10000);
  21. long contentlength = 0;
  22. bool Quitting = false;
  23. printf("Content-type: text/html\n\n");
  24. char *omg = getenv("QUERY_STRING");
  25. if (omg)
  26. {
  27. contentlength = strlen(omg);
  28. strcpy(buff, omg);
  29. }
  30. char Keys[32][200], *posat = buff; int NumKeys = 0;
  31. memset(Keys, 0, sizeof(Keys));
  32. if (contentlength)
  33. {
  34. while (strchr(posat, '&'))
  35. {
  36. memcpy(Keys[NumKeys], posat, strchr(posat, '&') - posat);
  37. NumKeys++;
  38. posat = strchr(posat, '&') + 1;
  39. }
  40. memcpy(Keys[NumKeys], posat, strlen(posat));
  41. NumKeys++;
  42. }
  43. int i;
  44. stServerInfo Server[256]; int NumServers = 0;
  45. time_t CurTime = time(NULL);
  46. FILE *in = fopen("serverlist.txt", "rb");
  47. if (!in)
  48. in = fopen("serverlist.txt", "wb");
  49. fseek(in, 0, SEEK_END);
  50. int flen = ftell(in);
  51. fseek(in, 0, SEEK_SET);
  52. NumServers = (int) (flen / sizeof(stServerInfo));
  53. if (NumServers > 256)
  54. NumServers = 256;
  55. fread(Server, NumServers * sizeof(stServerInfo), 1, in);
  56. fclose(in);
  57. char KeyLeft[200], KeyRight[200];
  58. bool MiniList = false, Serving = false;;
  59. bool NameSet = false, PortSet = false, TypeSet = false, AdminSet = false, SpeedSet = false, HostSet = false;
  60. Server[NumServers].Users = 0;
  61. for (i=0;i<NumKeys;i++)
  62. {
  63. memset(KeyLeft, 0, 200); memset(KeyRight, 0, 200);
  64. if (strchr(Keys[i], '='))
  65. {
  66. memcpy(KeyLeft, Keys[i], strchr(Keys[i], '=') - Keys[i]);
  67. memcpy(KeyRight, strchr(Keys[i], '=') + 1, strlen(strchr(Keys[i], '=') + 1));
  68. }
  69. // printf("Pair: %s = %s<BR>", KeyLeft, KeyRight);
  70. if (stricmp(KeyLeft, "Name") == 0) {
  71. strcpy(Server[NumServers].Name, KeyRight);
  72. while (strstr(Server[NumServers].Name, "%20"))
  73. {
  74. char *tpi = strstr(Server[NumServers].Name, "%20");
  75. *tpi = ' ';
  76. int tpl = strlen(tpi+3);
  77. memcpy(tpi+1, tpi+3, tpl);
  78. *(tpi + 1 + tpl) = 0;
  79. }
  80. NameSet = true;
  81. }
  82. if (stricmp(KeyLeft, "Admin") == 0) {
  83. strcpy(Server[NumServers].Admin, KeyRight);
  84. while (strstr(Server[NumServers].Admin, "%20"))
  85. {
  86. char *tpi = strstr(Server[NumServers].Admin, "%20");
  87. *tpi = ' ';
  88. int tpl = strlen(tpi+3);
  89. memcpy(tpi+1, tpi+3, tpl);
  90. *(tpi + 1 + tpl) = 0;
  91. }
  92. AdminSet = true;
  93. }
  94. if (stricmp(KeyLeft, "Speed") == 0) {
  95. strcpy(Server[NumServers].Speed, KeyRight);
  96. while (strstr(Server[NumServers].Speed, "%20"))
  97. {
  98. char *tpi = strstr(Server[NumServers].Speed, "%20");
  99. *tpi = ' ';
  100. int tpl = strlen(tpi+3);
  101. memcpy(tpi+1, tpi+3, tpl);
  102. *(tpi + 1 + tpl) = 0;
  103. }
  104. SpeedSet = true;
  105. }
  106. if (stricmp(KeyLeft, "Host") == 0) { strcpy(Server[NumServers].IP, KeyRight); HostSet = true; }
  107. if (stricmp(KeyLeft, "Port") == 0) { sscanf(KeyRight, "%i", &Server[NumServers].Port); PortSet = true; }
  108. if (stricmp(KeyLeft, "Type") == 0) { sscanf(KeyRight, "%i", &Server[NumServers].Type); TypeSet = true; }
  109. if (stricmp(KeyLeft, "Users") == 0) { sscanf(KeyRight, "%i", &Server[NumServers].Users); }
  110. if (stricmp(KeyLeft, "Quit") == 0) { Quitting = true; }
  111. if (stricmp(KeyLeft, "Minilist") == 0) { MiniList = true; }
  112. }
  113. if (PortSet && NameSet && TypeSet && SpeedSet && AdminSet)
  114. {
  115. if (!HostSet)
  116. {
  117. memset(Server[NumServers].IP, 0, 64);
  118. strcpy(Server[NumServers].IP, getenv("REMOTE_ADDR"));
  119. }
  120. Server[NumServers].LastUpdate = time(NULL);
  121. bool SrvF = false;
  122. for (i=0;i<NumServers;i++)
  123. {
  124. if ((stricmp(Server[i].IP, Server[NumServers].IP) == 0) && (Server[i].Port == Server[NumServers].Port))
  125. {
  126. memcpy(&Server[i], &Server[NumServers], sizeof(stServerInfo));
  127. i = NumServers;
  128. SrvF = true;
  129. }
  130. }
  131. if (!SrvF)
  132. NumServers++;
  133. Serving = true;
  134. }
  135. if (Quitting)
  136. {
  137. if (!HostSet)
  138. {
  139. memset(Server[NumServers].IP, 0, 32);
  140. char *tph = getenv("REMOTE_HOST");
  141. if (tph)
  142. strcpy(Server[NumServers].IP, tph);
  143. if (strlen(Server[NumServers].IP) == 0)
  144. strcpy(Server[NumServers].IP, getenv("REMOTE_ADDR"));
  145. }
  146. bool SrvF = false;
  147. for (i=0;i<NumServers;i++)
  148. {
  149. if ((stricmp(Server[i].IP, Server[NumServers].IP) == 0) && (Server[i].Port == Server[NumServers].Port))
  150. Server[i].Port = 0;
  151. }
  152. }
  153. FILE *out = fopen("serverlist.txt", "wb");
  154. if (MiniList)
  155. {
  156. printf("<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=5 BORDERCOLOR=BLUE>\n");
  157. printf("<TR><TD WIDTH=130 ALIGN=CENTER><U>Server List</U></TD></TR>\n");
  158. for (i=0;i<NumServers;i++)
  159. {
  160. // if ((CurTime - Server[i].LastUpdate) > 60)
  161. // Server[i].Port = 0;
  162. if (Server[i].Port > 0)
  163. {
  164. printf("<TR><TD WIDTH=130 ALIGN=CENTER>");
  165. char shortname[21]; memset(shortname, 0, 21);
  166. _snprintf(shortname, 20, "%s", Server[i].Name);
  167. char mult[2] = { 0, 0 };
  168. if (Server[i].Users != 1)
  169. mult[0] = 's';
  170. printf("<FONT SIZE=2><A HREF=\"acserver://%s:%i/\">%.30s</A><BR>%s - %i User%s\n",
  171. Server[i].IP, Server[i].Port, Server[i].Name, Server[i].Speed, Server[i].Users, mult);
  172. printf("</TD></TR>");
  173. fwrite(&Server[i], sizeof(stServerInfo), 1, out);
  174. }
  175. }
  176. printf("</TABLE>\n");
  177. } else {
  178. printf("<HTML><HEAD><TITLE>Test Server List</TITLE></HEAD>\n");
  179. printf("<META HTTP-EQUIV=Refresh CONTENT=""15; URL=dirserv.exe"">\n");
  180. printf("<BODY BGCOLOR=BLACK TEXT=WHITE LINK=LIGHTBLUE>\n");
  181. printf("<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=3 BORDERCOLOR=BLUE><TR><TH WIDTH=50></TH><TH>Server Name</TH><TH>Address:Port</TH><TH>Users</TH><TH>Speed</TH><TH>Admin</TH><TH>TSU</TH></TR>\n");
  182. for (i=0;i<NumServers;i++)
  183. {
  184. if ((CurTime - Server[i].LastUpdate) > 60)
  185. Server[i].Port = 0;
  186. if (Server[i].Port > 0)
  187. {
  188. printf("<TR><TD>");
  189. if (Server[i].Type == 1)
  190. {
  191. //Akilla
  192. printf("<A BORDER=0 HREF=""acserver://%s:%i/"">Connect</A>", Server[i].IP, Server[i].Port);
  193. } else if (Server[i].Type == 2) {
  194. //Sentou
  195. printf("<A HREF=""ace://%s:%i/"">Connect (Sentou)</A>", Server[i].IP, Server[i].Port);
  196. }
  197. char TSUColor[50];
  198. int TSU = CurTime - Server[i].LastUpdate;
  199. if (TSU < 20)
  200. sprintf(TSUColor, "GREEN");
  201. else if (TSU < 30)
  202. sprintf(TSUColor, "YELLOW");
  203. else
  204. sprintf(TSUColor, "RED");
  205. printf("</TD><TD>%s</TD><TD>%s:%i</TD><TD ALIGN=CENTER>%i/128</TD><TD ALIGN=CENTER>%s</TD><TD ALIGN=CENTER>%s</TD><TD ALIGN=CENTER><FONT COLOR=%s>%i</FONT></TD></TR>\n", Server[i].Name, Server[i].IP, Server[i].Port, Server[i].Users, Server[i].Speed, Server[i].Admin, TSUColor, TSU);
  206. fwrite(&Server[i], sizeof(stServerInfo), 1, out);
  207. }
  208. }
  209. printf("</TABLE><BR>\n");
  210. printf("<FONT SIZE=2><B>TSU</B> - Time Since Update. Servers should update the page every 15 seconds. If they lag out or have a problem, when TSU hits 60 seconds, the server is removed from the list.</FONT><P>\n");
  211. printf("<FONT SIZE=2>Hosted by Jubilee at <A HREF=http://dirserv.forotech.com/>http://dirserv.forotech.com/</A></FONT><BR>\n");
  212. printf("<FONT SIZE=2>This page refreshes every 15 seconds. It works nicely as an active desktop item.</FONT>\n");
  213. printf("</BODY></HTML>\n");
  214. }
  215. fclose(out);
  216. return 0;
  217. }