No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

commands.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /* tsiproxy server commands */
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include <string.h>
  6. #include <arpa/inet.h>
  7. #include <netinet/in.h>
  8. #include <sys/utsname.h>
  9. #include <sys/resource.h>
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12. #include <errno.h>
  13. #include <dirent.h>
  14. #include <fcntl.h>
  15. #include "libfuncs/libfuncs.h"
  16. #include "data.h"
  17. #include "conf.h"
  18. extern CONFIG *config;
  19. extern char *server_ver;
  20. extern LIST *netconf, *clients, *chanconf, *restreamer;
  21. extern STATS allstats;
  22. static void std_headers(int clientsock, char *IP) {
  23. fdputsf(clientsock, "X-IP: %s\n", IP);
  24. send_header_textplain(clientsock);
  25. fdputs(clientsock, "\n");
  26. }
  27. void run_ping(unsigned int clientsock, acl_t acl, uint32_t src_ip, char *IP) {
  28. unused(src_ip);
  29. char flags[6] = "-----";
  30. send_200_ok(clientsock);
  31. if (acl.stats ) flags[1] = 'S';
  32. if (acl.reconf) flags[2] = 'F';
  33. if (acl.access) flags[3] = 'A';
  34. fdputsf(clientsock, "X-Access: %s\n", flags);
  35. std_headers(clientsock, IP);
  36. fdputs(clientsock, "PONG\n");
  37. }
  38. int __show_stats(unsigned int clientsock, acl_t acl, char *IP) {
  39. if (!acl.stats) {
  40. LOGf("DENY : Stats access denied | IP: %s\n", IP);
  41. send_403_forbidden(clientsock);
  42. return 0;
  43. }
  44. send_200_ok(clientsock);
  45. std_headers(clientsock, IP);
  46. return 1;
  47. }
  48. void show_stats(unsigned int clientsock, acl_t acl, char *IP) {
  49. if (!__show_stats(clientsock, acl, IP))
  50. return;
  51. time_t now = time(NULL);
  52. LNODE *l, *tmp;
  53. list_lock(clients);
  54. list_for_each_reverse(clients, l, tmp) {
  55. CLIENT *c = l->data;
  56. // Workaround for unknown bug that leaves records in clients list but not in restreamerX->clients list
  57. // Such a client hangs forever taking memory. The bug was seen on snaps server
  58. if (now - c->ts >= NO_TRAFFIC_SECONDS + 5) {
  59. list_unlock(clients);
  60. stop_client_shutdown(c);
  61. list_lock(clients);
  62. continue;
  63. }
  64. if (c->traffic_out == 0)
  65. continue;
  66. // IP A FD PROV CID TRA TM CHAN AGENT
  67. fdputsf(clientsock,"%s\t%c\t%i\t%-12s\t%lu\t%14llu\t%6lu\t%-22s\t%s\n",
  68. c->IP,
  69. '-',
  70. c->fno,
  71. "-",
  72. c->clientid,
  73. c->traffic_out,
  74. now - c->start,
  75. c->chan,
  76. c->agent);
  77. }
  78. list_unlock(clients);
  79. }
  80. int __show_info(unsigned int clientsock, acl_t acl, char *IP,
  81. unsigned long total_clients,
  82. unsigned long clients_current, unsigned long clients_gone,
  83. unsigned long long traffic_in, unsigned long long traffic_out)
  84. {
  85. int used_fds = -1;
  86. if (!acl.stats) {
  87. LOGf("DENY : Info access denied | IP: %s\n", IP);
  88. send_403_forbidden(clientsock);
  89. return 0;
  90. }
  91. time_t now = time(NULL);
  92. time_t uptime = now - allstats.start_ts;
  93. unsigned int t_sec,t_min,t_hour,t_days;
  94. struct rlimit limit;
  95. double load_avg[3];
  96. t_sec = uptime % 60;
  97. t_min = (uptime - t_sec) / 60;
  98. t_hour = (t_min - t_min % 60) / 60;
  99. t_min = t_min - t_hour * 60;
  100. t_days = (t_hour - t_hour % 24) / 24;
  101. t_hour = t_hour - t_days * 24;
  102. getrlimit(RLIMIT_NOFILE, &limit);
  103. getloadavg(load_avg, 3);
  104. send_200_ok(clientsock);
  105. std_headers(clientsock, IP);
  106. fdputs (clientsock, "Server info\n");
  107. fdputsf(clientsock, " Ident : %s\n", config->ident);
  108. fdputsf(clientsock, " Version : %s\n", server_ver);
  109. fdputsf(clientsock, " Server : %s %s %s %s %s\n",
  110. allstats.utsdata.sysname,
  111. allstats.utsdata.nodename,
  112. allstats.utsdata.release,
  113. allstats.utsdata.version,
  114. allstats.utsdata.machine);
  115. fdputsf(clientsock, " Cores : %ld\n", (long)sysconf(_SC_NPROCESSORS_ONLN));
  116. DIR *d = opendir("/proc/self/fd");
  117. if (d) {
  118. struct dirent *dir;
  119. while ((dir = readdir(d))) {
  120. if (dir->d_name[0] == '.') // Ignore . and ..
  121. continue;
  122. used_fds++;
  123. }
  124. closedir(d);
  125. }
  126. fdputsf(clientsock, " Max FDs : %ld\n",(long)limit.rlim_max);
  127. fdputsf(clientsock, " Used FDs : %ld\n",(long)used_fds);
  128. fdputsf(clientsock, " Load average : %4.2f %4.2f %4.2f\n",load_avg[0],load_avg[1],load_avg[2]);
  129. fdputsf(clientsock, " Localtime : %s", ctime(&now));
  130. fdputsf(clientsock, " Uptime : %d %02d:%02d:%02d\n", t_days, t_hour, t_min, t_sec);
  131. fdputsf(clientsock, " Redirect : %s %s\n\n",
  132. config->redirect_url ? "enabled" : "disabled",
  133. config->redirect_url ? config->redirect_url : ""
  134. );
  135. fdputs (clientsock, "Statistics\n");
  136. fdputsf(clientsock, " Clients current : %lu\n", clients_current);
  137. fdputsf(clientsock, " Clients cur+gone : %lu\n", clients_current + clients_gone);
  138. fdputsf(clientsock, " Traffic in : %llu MB\n", traffic_in / (1024*1024));
  139. fdputsf(clientsock, " Traffic out : %llu MB\n", traffic_out / (1024*1024));
  140. fdputsf(clientsock, " Total clients : %lu\n\n", total_clients);
  141. return 1;
  142. }
  143. void show_info(unsigned int clientsock, acl_t acl, char *IP) {
  144. if (!__show_info(clientsock, acl, IP, allstats.clients, allstats.clients_current, allstats.clients_gone, allstats.traffic_in, allstats.traffic_out))
  145. return;
  146. time_t now = time(NULL);
  147. time_t uptime = now - allstats.start_ts;
  148. fdputs (clientsock, "IPTVD statistics\n");
  149. fdputsf(clientsock, " Proxies current : %u\n", restreamer->items);
  150. fdputsf(clientsock, " Restreamers : %lu\n", allstats.child_servers);
  151. fdputsf(clientsock, " Connects : %lu\n", allstats.clients_all);
  152. fdputsf(clientsock, " Errors : %lu\n", allstats.errors);
  153. fdputs (clientsock, " current cur+gone trafic_in traffic_out clients servers connects errors localtime uptime\n");
  154. fdputsf(clientsock, "AllStats : %lu %lu %llu %llu %lu %lu %lu %lu %lu %lu\n\n",
  155. allstats.clients_current,
  156. allstats.clients_current + allstats.clients_gone,
  157. allstats.traffic_in,
  158. allstats.traffic_out,
  159. allstats.clients,
  160. allstats.child_servers,
  161. allstats.clients_all,
  162. allstats.errors,
  163. now,
  164. uptime);
  165. LNODE *l, *tmp;
  166. unsigned int t_sec,t_min,t_hour,t_days;
  167. fdputs(clientsock, "Running restreamers\n");
  168. list_lock(restreamer);
  169. list_for_each_reverse(restreamer, l, tmp) {
  170. RESTREAMER *r = l->data;
  171. uptime = time(NULL) - r->started;
  172. t_sec = uptime % 60;
  173. t_min = (uptime - t_sec) / 60;
  174. t_hour = (t_min - t_min % 60) / 60;
  175. t_min = t_min - t_hour * 60;
  176. t_days = (t_hour - t_hour % 24) / 24;
  177. t_hour = t_hour - t_days * 24;
  178. fdputsf(clientsock," %-27sClients: %i Served: %li Conn: %li Uptime: %d %02d:%02d:%02d In: %lli %lli MB Out: %lli %lli MB\n",
  179. r->name,
  180. r->clients->items,
  181. r->served,
  182. r->connects,
  183. t_days, t_hour, t_min, t_sec,
  184. r->traffic_in, r->traffic_in / (1024*1024),
  185. r->traffic_out, r->traffic_out / (1024*1024)
  186. );
  187. }
  188. list_unlock(restreamer);
  189. fdputs(clientsock,"\n");
  190. }
  191. void run_livecheck(unsigned int clientsock, acl_t acl, char *IP) {
  192. if (!acl.reconf) {
  193. LOGf("DENY : LiveCheck access denied | IP: %s\n", IP);
  194. send_403_forbidden(clientsock);
  195. return;
  196. }
  197. send_200_ok(clientsock);
  198. std_headers(clientsock, IP);
  199. do {
  200. char outbuf[128];
  201. int size;
  202. memset(outbuf, ' ', sizeof(outbuf));
  203. outbuf[sizeof(outbuf)-1] = '\n';
  204. STATS ls = allstats;
  205. sleep(1);
  206. time_t now = time(NULL);
  207. time_t uptime = now - allstats.start_ts;
  208. /* The traffic passed in the 1 second sleep above */
  209. ls.traffic_in = allstats.traffic_in - ls.traffic_in;
  210. ls.traffic_out = allstats.traffic_out - ls.traffic_out;
  211. size = sprintf(outbuf, "%lu %lu %lu %u %llu %llu",
  212. now,
  213. uptime,
  214. allstats.clients_current,
  215. restreamer ? restreamer->items : 0,
  216. ls.traffic_in,
  217. ls.traffic_out);
  218. outbuf[size]=' ';
  219. if (fdwrite(clientsock, outbuf, sizeof(outbuf)) <= 0)
  220. break;
  221. } while (1);
  222. }
  223. void read_info(unsigned int clientsock, acl_t acl, char *IP) {
  224. if (!acl.stats) {
  225. LOGf("DENY : Info access denied | IP: %s\n", IP);
  226. send_403_forbidden(clientsock);
  227. return;
  228. }
  229. time_t now = time(NULL);
  230. time_t uptime = now - allstats.start_ts;
  231. send_200_ok(clientsock);
  232. std_headers(clientsock, IP);
  233. unsigned int gone = allstats.clients_gone; /* Reset it first because fdputsf can block */
  234. allstats.clients_gone = 0;
  235. fdputsf(clientsock, "%lu %lu %llu %llu %lu %lu %lu %lu %lu %lu\n",
  236. allstats.clients_current,
  237. allstats.clients_current + gone,
  238. allstats.traffic_in,
  239. allstats.traffic_out,
  240. allstats.clients,
  241. allstats.child_servers,
  242. allstats.clients_all,
  243. allstats.errors,
  244. now,
  245. uptime);
  246. }
  247. void run_netconf(unsigned int clientsock, acl_t acl, char *IP) {
  248. if (!acl.reconf) {
  249. LOGf("DENY : NetConf access denied | IP: %s\n", IP);
  250. send_403_forbidden(clientsock);
  251. return;
  252. }
  253. int nets = load_networks(config);
  254. if (nets > -1)
  255. LOGf("CONF : NetConf reloaded | IP: %s Loaded: %i\n", IP, nets);
  256. send_200_ok(clientsock);
  257. std_headers(clientsock, IP);
  258. if (nets > -1)
  259. fdputsf(clientsock,"OK %i networks read\n", nets);
  260. else
  261. fdputs(clientsock,"Networks are already up to date\n");
  262. }
  263. void run_chanconf(unsigned int clientsock, acl_t acl, char *IP) {
  264. if (!acl.reconf) {
  265. LOGf("DENY : ChanConf access denied | IP: %s\n", IP);
  266. send_403_forbidden(clientsock);
  267. return;
  268. }
  269. int j = load_channels(config);
  270. if (j > -1)
  271. LOGf("CONF : ChanConf reloaded | IP: %s Loaded: %i\n", IP, j);
  272. send_200_ok(clientsock);
  273. std_headers(clientsock, IP);
  274. if (j > -1)
  275. fdputsf(clientsock,"OK %i channels read\n",j);
  276. else
  277. fdputs(clientsock,"Channels are already up to date\n");
  278. }
  279. void run_getnetconf(unsigned int clientsock, acl_t acl, char *IP) {
  280. if (!acl.reconf) {
  281. send_403_forbidden(clientsock);
  282. return;
  283. }
  284. send_200_ok(clientsock);
  285. std_headers(clientsock, IP);
  286. LNODE *l, *tmp;
  287. list_lock(netconf);
  288. list_for_each(netconf, l, tmp) {
  289. NETWORK *n = l->data;
  290. fdputsf(clientsock,"%d.%d.%d.%d/%d.%d.%d.%d\t%s%s%s\n",
  291. (n->net >> 0 ) & 0xFF,
  292. (n->net >> 8 ) & 0xFF,
  293. (n->net >> 16) & 0xFF,
  294. (n->net >> 24) & 0xFF,
  295. (n->mask >> 0 ) & 0xFF,
  296. (n->mask >> 8 ) & 0xFF,
  297. (n->mask >> 16) & 0xFF,
  298. (n->mask >> 24) & 0xFF,
  299. n->acl.stats ? "S" : "",
  300. n->acl.reconf ? "F" : "",
  301. n->acl.access ? "A" : ""
  302. );
  303. }
  304. list_unlock(netconf);
  305. }
  306. void run_getchanconf(unsigned int clientsock, acl_t acl, char *IP) {
  307. if (!acl.reconf) {
  308. send_403_forbidden(clientsock);
  309. return;
  310. }
  311. send_200_ok(clientsock);
  312. std_headers(clientsock, IP);
  313. LNODE *l, *tmp;
  314. list_lock(chanconf);
  315. list_for_each(chanconf, l, tmp) {
  316. CHANNEL *c = l->data;
  317. int r = 0;
  318. while (c->sources[r] && r < MAX_CHANNEL_SOURCES) {
  319. fdputsf(clientsock,"%s\t%s\n", c->name, c->sources[r]);
  320. r++;
  321. }
  322. }
  323. list_unlock(chanconf);
  324. }
  325. void run_channels(unsigned int clientsock, acl_t acl, char *IP) {
  326. time_t now = time(NULL);
  327. LNODE *l, *tmp;
  328. if (!acl.stats) {
  329. LOGf("DENY : Channels access denied | IP: %s\n", IP);
  330. send_403_forbidden(clientsock);
  331. return;
  332. }
  333. send_200_ok(clientsock);
  334. std_headers(clientsock, IP);
  335. list_lock(restreamer);
  336. list_for_each_reverse(restreamer, l, tmp) {
  337. RESTREAMER *r = l->data;
  338. time_t uptime = now - r->started;
  339. fdputsf(clientsock,"%-20s\t%5i\t%5li\t%5li\t%7ld\t%12lli\t%12lli\n",
  340. r->name,
  341. r->clients->items,
  342. r->served,
  343. r->connects,
  344. uptime,
  345. r->traffic_in,
  346. r->traffic_out
  347. );
  348. }
  349. list_unlock(restreamer);
  350. }