countersutil.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. 3APA3A simpliest proxy server
  3. (c) 2002-2008 by ZARAZA <3APA3A@security.nnov.ru>
  4. please read License Agreement
  5. */
  6. #include "proxy.h"
  7. struct counter_header {
  8. unsigned char sig[4];
  9. time_t updated;
  10. } cheader = {"3CF", (time_t)0};
  11. struct counter_record {
  12. unsigned long traf;
  13. unsigned long trafgb;
  14. time_t cleared;
  15. time_t updated;
  16. } crecord;
  17. #ifdef _WIN32
  18. struct counter_header_old {
  19. unsigned char sig[4];
  20. DWORD updated;
  21. } cheader_old = {"3CF", (time_t)0};
  22. struct counter_record_old {
  23. unsigned long traf;
  24. unsigned long trafgb;
  25. DWORD cleared;
  26. DWORD updated;
  27. } crecord_old;
  28. #endif
  29. int main(int argc, char *argv[]){
  30. FILE *txt;
  31. int bin;
  32. int i;
  33. long unsigned lu1, lu2;
  34. char buf[256];
  35. if(argc!=4){
  36. fprintf(stderr, "Usage: %s command binary_file text_file\n"
  37. " commands are:\n"
  38. "\texport - dump counterfile to text\n"
  39. #ifdef _WIN32
  40. "\toldexport - export counterfile from older 3proxy version\n"
  41. #endif
  42. "\timport- import counterfile from text\n"
  43. "Examples:\n"
  44. #ifdef _WIN32
  45. " %s oldexport counterfile.3cf tmpfile\n"
  46. #else
  47. " %s export counterfilenew.3cf tmpfile\n"
  48. #endif
  49. " %s import counterfilenew.3cf tmpfile\n"
  50. "text file record format:\n"
  51. "%%d %%10lu %%10lu %%lu %%lu\n"
  52. " 1 - counter number\n"
  53. " 2 - traffic (Bytes)\n"
  54. " 3 - traffic (GB)\n"
  55. " 4 - time counter reset (time_t)\n"
  56. " 5 - time counter updated (time_t)\n"
  57. ,argv[0] , argv[0], argv[0]);
  58. return 1;
  59. }
  60. if(!strcmp(argv[1], "export")){
  61. bin = open((char *)argv[2], O_BINARY|O_RDONLY, 0660);
  62. if(bin < 0){
  63. fprintf(stderr, "Failed to open %s\n", argv[2]);
  64. return 2;
  65. }
  66. if(read(bin, &cheader, sizeof(cheader)) != sizeof(cheader) ||
  67. memcmp(&cheader, "3CF", 4)){
  68. fprintf(stderr, "Invalid counter file\n");
  69. return 3;
  70. }
  71. txt = fopen(argv[3], "w");
  72. if(!txt) txt = stdout;
  73. for(i=1; read(bin, &crecord, sizeof(crecord))==sizeof(crecord); i++)
  74. fprintf(txt,"%d %10lu %10lu %lu %lu\n", i,
  75. crecord.trafgb,
  76. crecord.traf,
  77. (unsigned long) crecord.cleared,
  78. (unsigned long) crecord.updated);
  79. }
  80. #ifdef _WIN32
  81. else if(!strcmp(argv[1], "oldexport")){
  82. bin = open((char *)argv[2], O_BINARY|O_RDONLY, 0660);
  83. if(bin < 0){
  84. fprintf(stderr, "Failed to open %s\n", argv[2]);
  85. return 2;
  86. }
  87. if(read(bin, &cheader_old, sizeof(cheader_old)) != sizeof(cheader_old) ||
  88. memcmp(&cheader, "3CF", 4)){
  89. fprintf(stderr, "Invalid counter file\n");
  90. return 3;
  91. }
  92. txt = fopen(argv[3], "w");
  93. if(!txt) txt = stdout;
  94. for(i=1; read(bin, &crecord_old, sizeof(crecord_old))==sizeof(crecord_old); i++)
  95. fprintf(txt, "%d %10lu %10lu %lu %lu\n", i,
  96. crecord_old.trafgb,
  97. crecord_old.traf,
  98. (unsigned long) crecord_old.cleared,
  99. (unsigned long) crecord_old.updated);
  100. }
  101. #endif
  102. else if(!strcmp(argv[1], "import")){
  103. bin = open((char *)argv[2], O_BINARY|O_WRONLY|O_CREAT|O_EXCL, 0660);
  104. if(bin < 0){
  105. fprintf(stderr, "Failed to open %s\n", argv[2]);
  106. return 2;
  107. }
  108. txt = fopen(argv[3], "r");
  109. if(!txt) {
  110. fprintf(stderr, "Failed to open %s\n", argv[3]);
  111. return 3;
  112. }
  113. cheader.updated = time(0);
  114. write(bin, &cheader, sizeof(cheader));
  115. while(fgets(buf, 256, txt) &&
  116. sscanf(buf, "%d %10lu %10lu %lu %lu\n",
  117. &i, &crecord.trafgb, &crecord.traf,
  118. &lu1, &lu2) == 5){
  119. crecord.cleared = (time_t) lu1;
  120. crecord.updated = (time_t) lu1;
  121. lseek(bin,
  122. sizeof(struct counter_header) + (i-1) * sizeof(crecord),
  123. SEEK_SET);
  124. write(bin, &crecord, sizeof(crecord));
  125. }
  126. }
  127. else {
  128. fprintf(stderr, "Unknown command: %s\n", argv[1]);
  129. return 5;
  130. }
  131. return 0;
  132. }