#define VERSIONE_PROGRAMMA "MV Smart Downloader v 3.03.01 by mamo139" #define console_mode //#define console_mode_grafica #define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include #include #include #include #pragma comment(lib,"wsock32.lib") #pragma comment(lib,"atk-1.0.lib") #pragma comment(lib,"cairo.lib") #pragma comment(lib,"gio-2.0.lib") #pragma comment(lib,"glib-2.0.lib") #pragma comment(lib,"gmodule-2.0.lib") #pragma comment(lib,"gobject-2.0.lib") #pragma comment(lib,"gthread-2.0.lib") #pragma comment(lib,"gailutil.lib") #pragma comment(lib,"gdk_pixbuf-2.0.lib") #pragma comment(lib,"gdk-win32-2.0.lib") #pragma comment(lib,"gtk-win32-2.0.lib") #pragma comment(lib,"pango-1.0.lib") #pragma comment(lib,"pangocairo-1.0.lib") #pragma comment(lib,"pangoft2-1.0.lib") #pragma comment(lib,"pangowin32-1.0.lib") const int processi_num = 2; const int download_buffer_size = 1024; const int socket_timeout = 10000; //timeout per la conessione, in millisecondi direi const int backup_time = 0; //ogni quanto effettuare il backup in millisecondi const int speed_info_time = 5; //ogni quanto viene comunicata la velocitā const int possibilit_speed = 2; //moltiplicato per speed_info_time da il numero di secondi prima della disconnessione per lentezza const int limite_speed = 1; //disconnette proxy piu lenti, velocitā in kb/s char *proxylist_file = "proxylist.txt"; long proxy_scarica(char *fileremoto, char *host,char *video_buffer, char *proxy_ip, int timeout_sec, long video_time, long video_lunghezza, float *proxy_speed, long *video_current_point, bool *reset); bool proxy_scarica_su_stringa(char *fileremoto, char *host,char *stringa, char *proxy_ip, int timeout_sec); long scarica_mv_video(char *video_id, long video_time, long video_lunghezza, char *proxy_ip, char *video_buffer, float* proxy_speed, long *video_current_point, bool *reset); int mv_decode(char *string, long key1, long key2); long mv_video_lunghezza(char *video_id, char *proxy_ip); int proxylist_counter(char *file); int proxylist_loader(char **proxylist,char * file); int funzione(LPVOID param); int video_backup(LPVOID param); #ifdef console_mode int tell_me(LPVOID param); #endif int main_thread(); int main_grafica_uploader(); char * read_clipboard(); struct connection_data { int processo_id; char processo_status; char ** proxylist; float proxy_speed; long proxy_num; long proxy_point; char * video_id; char * video_buffer; long video_point; long video_starting_point; long video_current_point; long video_lunghezza; bool reset; float percentage; }; //***************** gtk widget GtkWidget *finestra1; GtkWidget *tabella1; GtkWidget *label_mvid, *label_processi_scaricanti, *label_processi_terminati; GtkWidget *label_time, *label_global_speed; GtkWidget *progressbar_main; GtkWidget **progressbar_parziale, **label_download_parziale, *tabella_download_parziale, *scrolledwindow_download_parziale; GtkWidget *textview_console, *scrolledwindow_console; GtkTextBuffer *textview_console_buffer; int console_book_size = 100; char *console_booking, **console_book; int book_manager(); void textview_printf(char *string); //variabili globali char main_grafica_uploader_start = 0; double global_speed = 0; double global_percentage = 0; long global_processi_terminati = 0; long global_processi_scaricanti = 0; clock_t global_time_start, global_time_end; struct connection_data *global_dati; //*********************************************// //******************* MAIN ********************// //*********************************************// int main(int argc, char *argv[]){ long x; GError ** thread_error = NULL; g_type_init(); /* Secure glib */ if( ! g_thread_supported() ) g_thread_init( NULL ); /* Secure gtk */ gdk_threads_init(); /* Obtain gtk's global lock */ gdk_threads_enter(); gtk_init (&argc, &argv);//inizializzo l'ambiente gtk //********** FINESTRA1 *************// finestra1 = gtk_window_new(GTK_WINDOW_TOPLEVEL); //creo la finestra principale gtk_window_set_title(GTK_WINDOW(finestra1), VERSIONE_PROGRAMMA); //setto il titolo della finestra principale gtk_container_set_border_width(GTK_CONTAINER(finestra1), 3); //setto il bordo della finestra principale gtk_window_set_resizable(GTK_WINDOW(finestra1), FALSE); //rendo non resizable la finestra principale gtk_window_set_position(GTK_WINDOW(finestra1), GTK_WIN_POS_CENTER); //posizione iniziale finestra g_signal_connect(G_OBJECT(finestra1), "delete_event", G_CALLBACK(gtk_main_quit), NULL); //evento chiusura finestra princ //********** TABELLA1 *************// tabella1 = gtk_table_new(25, 10, TRUE); //creo la tabella principale gtk_container_add(GTK_CONTAINER(finestra1), tabella1); //inserisco la tabella princ. nella finestra princ. //label_mvid, label_processi_scaricanti, label_processi_terminati label_mvid = gtk_label_new("Waiting for MegaVideo ID..."); gtk_table_attach_defaults(GTK_TABLE(tabella1), label_mvid, 0, 2, 0, 1); label_processi_scaricanti = gtk_label_new("..."); gtk_table_attach_defaults(GTK_TABLE(tabella1), label_processi_scaricanti, 6, 8, 3, 4); label_processi_terminati = gtk_label_new("..."); gtk_table_attach_defaults(GTK_TABLE(tabella1), label_processi_terminati, 8, 10, 3, 4); //label_time global_time_start = clock(); label_time = gtk_label_new("time..."); gtk_table_attach_defaults(GTK_TABLE(tabella1), label_time, 8, 10, 0, 1); //label_global_speed label_global_speed = gtk_label_new("speed..."); gtk_table_attach_defaults(GTK_TABLE(tabella1), label_global_speed, 4, 6, 3, 4); //progressbar_main progressbar_main = gtk_progress_bar_new(); gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progressbar_main), "Initializing..."); gtk_table_attach_defaults(GTK_TABLE(tabella1), progressbar_main, 0, 10, 1, 3); //textview_console textview_console = gtk_text_view_new(); gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(textview_console), GTK_WRAP_WORD_CHAR); gtk_text_view_set_editable(GTK_TEXT_VIEW(textview_console),FALSE); textview_console_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview_console)); gtk_text_buffer_set_text(textview_console_buffer, "textview_console ready...\n", -1); scrolledwindow_console = gtk_scrolled_window_new(NULL,NULL); gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwindow_console), GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS); gtk_container_add(GTK_CONTAINER(scrolledwindow_console), textview_console); gtk_table_attach_defaults(GTK_TABLE(tabella1), scrolledwindow_console, 0, 10, 18, 25); //console booking system console_booking = (char *) malloc(console_book_size * sizeof(char)); console_book = (char **) malloc(console_book_size * sizeof(char *)); for(x=0;x= 0 && global_percentage <=1) gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progressbar_main),((gdouble)global_percentage)); else{ global_percentage = 0; gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progressbar_main),((gdouble)0)); } sprintf(console_string,"%.2f%% (%.2f KB/sec)",global_percentage*100,global_speed); gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progressbar_main),console_string); for(x=0;x1 || global_percentage>=1) global_dati[x].percentage = 1; if(global_dati[x].percentage >= 0 && global_dati[x].percentage <=1) gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progressbar_parziale[x]),((gdouble)global_dati[x].percentage)); else{ global_dati[x].percentage = 0; gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progressbar_parziale[x]),((gdouble)0)); } sprintf(console_string,"%d: proxy %d (%.2f KB/sec)",global_dati[x].processo_id,global_dati[x].proxy_point, global_dati[x].proxy_speed); gtk_label_set_text(GTK_LABEL(label_download_parziale[x]), console_string); //sprintf(console_string,"%.2f%%",global_dati[x].percentage*100); //gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progressbar_parziale[x]),console_string); } while(gtk_events_pending()) gtk_main_iteration(); gdk_threads_leave(); free(console_string); return 1; } int main_thread(){ FILE *stream; long x, proxy_num, video_bytes=0; char **proxylist; char *video_buffer; bool wait=1; int processi_terminati; float overall_speed = 0; float download_percentage = 0; float tot_download_percentage = 0; int download_attivi = 0; struct connection_data dati_b; char *console_string; console_string = (char *) malloc(1000 * sizeof(char)); DWORD ThreadId; char *video_id; //********* VARIABILI *********// video_id = read_clipboard(); if(video_id == 0 || strlen(video_id) != 8){ #ifdef console_mode printf("La stringa che hai in memoria non č un ID megavideo valido!\n"); #endif gtk_label_set_text(GTK_LABEL(label_mvid), "ID megavideo non valido..."); textview_printf("ID megavideo non valido...\n"); return 0; } else{ #ifdef console_mode printf("ID video letto dalla clipboard: %s\n",video_id); #endif sprintf(console_string, "MegaVideo ID: %s",video_id); gtk_label_set_text(GTK_LABEL(label_mvid), console_string); sprintf(console_string, "MegaVideo ID: %s\n",video_id); textview_printf(console_string); } //******************************// //*** CARICAMENTO PROXY LIST ***// //******************************// #ifdef console_mode printf("PROXYLIST: caricamento proxylist...\n"); #endif textview_printf("PROXYLIST: caricamento proxylist...\n"); proxy_num = proxylist_counter(proxylist_file); #ifdef console_mode printf("PROXYLIST: numero dei proxy: %d\n",proxy_num); #endif sprintf(console_string, "PROXYLIST: numero dei proxy: %d\n",proxy_num); textview_printf(console_string); proxylist = (char **) malloc(proxy_num * sizeof(char *)); for(x=0;x 1000000000 || video_bytes < 100){ video_bytes = 0; video_bytes = mv_video_lunghezza(video_id, proxylist[x]); x++; } #ifdef console_mode printf("\n"); #endif textview_printf("\n"); //******************************// //*** CREAZIONE BUFFER VIDEO ***// //******************************// video_buffer = (char *) calloc (video_bytes, sizeof(char)); for(x=0;x0) download_attivi++; //****************************// //***** DOWNLOAD GRAFICA *****// //****************************// #ifdef console_mode_grafica if(dati[x].processo_id < 10) printf(" "); printf("%d: ",dati[x].processo_id); #endif download_percentage = (float)100*(dati[x].video_current_point-dati[x].video_starting_point)/dati[1].video_starting_point; if(dati[x].processo_status == 0) download_percentage = 100; tot_download_percentage = tot_download_percentage+download_percentage/processi_num; dati[x].percentage = (float) download_percentage/100; #ifdef console_mode_grafica printf("|"); for(y=0;y<33;y++) if (download_percentage == 100) printf("X"); else if(y*100/33 < download_percentage) printf("x"); else printf("-"); printf("| %.2f%%\x09%.3f\x09proxy %d\n", download_percentage, dati[x].proxy_speed,dati[x].proxy_point); #endif } #ifdef console_mode_grafica printf("\nCONNECTION MANAGER: %.2f%% SPEED: %.3f kbs (%d su %d scaricanti)\n\n",tot_download_percentage, overall_speed,download_attivi,(processi_num-processi_terminati)); #endif //salvo i dati nelle variabili globali global_speed = overall_speed; global_percentage = ((double)tot_download_percentage/100); global_processi_scaricanti = download_attivi; global_processi_terminati = processi_terminati; #ifdef console_mode_grafica printf("\ntell me:\n"); #endif } #ifdef console_mode printf("CONNECTION MANAGER: Tutti i processi sono terminati.\n\n"); #endif textview_printf("CONNECTION MANAGER: Tutti i processi sono terminati.\n\n"); dati_b.processo_status = 0;//termino il sistema di backup //********************************// //*** SCRITTURA FILE SCARICATO ***// //********************************// #ifdef console_mode printf("BUFFER: scrittura buffer su file in corso...\n"); #endif textview_printf("BUFFER: scrittura buffer su file in corso...\n"); sprintf(console_string, "%s.flv", video_id); stream = fopen(console_string,"wb"); fwrite(video_buffer,1,video_bytes,stream); fclose(stream); #ifdef console_mode printf("BUFFER: buffer scritto!\n\n"); #endif textview_printf("BUFFER: buffer scritto!\n\n"); return 1; } //********************************************************************// #ifdef console_mode int tell_me(LPVOID param){ struct connection_data *dati = (struct connection_data *) param; int proc, proxy; char comando; while(1){ printf(">"); scanf("%c",&comando); if(comando == 'r'){ //chiude connessione del processo e la riapre con il proxy selezionato printf("RESET "); printf("processo:"); scanf("%d",&proc); printf("proxy:",proc); scanf("%d",&proxy); printf("\nRESET OK: processo %d proxy %d\n",proc,proxy); if(proxy>0)proxy=proxy-1; dati[proc].reset = 1; dati[proc].proxy_point = proxy; } } return 1; } #endif //********************************************************************// int funzione(LPVOID param){ long video_point_now; struct connection_data *dati = (struct connection_data *) param; char *console_string; console_string = (char *) malloc(1000 * sizeof(char)); video_point_now = dati->video_point; #ifdef console_mode printf("PROCESSO %d: inizializzazione in corso...\n", dati->processo_id); printf("PROCESSO %d: video id: %s starting point: %d\n", dati->processo_id, dati->video_id, video_point_now); printf("PROCESSO %d: proxy point: %d\n", dati->processo_id, dati->proxy_point); #endif sprintf(console_string, "PROCESSO %d: inizializzazione in corso...\n", dati->processo_id); textview_printf(console_string); sprintf(console_string, "PROCESSO %d: video id: %s starting point: %d\n", dati->processo_id, dati->video_id, video_point_now); textview_printf(console_string); sprintf(console_string, "PROCESSO %d: proxy point: %d\n", dati->processo_id, dati->proxy_point); textview_printf(console_string); while(1){ if(video_point_now < 0) break; if(video_point_now >= dati->video_lunghezza) break; #ifdef console_mode printf("PROCESSO %d: start download, point %d, proxy %d\n", dati->processo_id,video_point_now,dati->proxy_point); #endif sprintf(console_string, "PROCESSO %d: start download, point %d, proxy %d\n", dati->processo_id,video_point_now,dati->proxy_point); textview_printf(console_string); video_point_now = scarica_mv_video(dati->video_id, video_point_now, dati->video_lunghezza, dati->proxylist[dati->proxy_point], dati->video_buffer, &dati->proxy_speed, &dati->video_current_point, &dati->reset); if(video_point_now >= 0)dati->proxy_point++; if(dati->proxy_point >= dati->proxy_num) dati->proxy_point = 0;//riavvolgi lista proxy } dati->processo_status = 0; //segnalo chiusura processo //printf("PROCESSO %d: terminato!\n", dati->processo_id); return 1; } //********************************************************************// int video_backup(LPVOID param){ struct connection_data *dati = (struct connection_data *) param; #ifdef console_mode printf("BACKUP: attivato...\n"); #endif textview_printf("BACKUP: attivato...\n"); while(1){ if(backup_time > 0) Sleep(backup_time); else break; if(dati->processo_status == 0){ #ifdef console_mode printf("BACKUP: TERMINATO!\n"); #endif textview_printf("BACKUP: TERMINATO!\n"); break; } FILE *stream; //printf("\nBACKUP: scrittura buffer su file in corso...\n"); stream = fopen("video_temp.flv","wb"); fwrite(dati->video_buffer,1,dati->video_lunghezza,stream); fclose(stream); //printf("BACKUP: buffer scritto!\n\n"); } #ifdef console_mode printf("BACKUP: terminato!\n"); #endif textview_printf("BACKUP: terminato!\n"); return 1; } //********************************************************************// int proxylist_counter(char *file){ FILE *in; long in_size; in = fopen(file,"rb"); fseek(in,0,SEEK_END); in_size = ftell(in); rewind(in); char *buffer; buffer = (char *) malloc(in_size * sizeof(char)); fread(buffer,1,in_size,in); int numero_proxy=0,x,y=0,z=0,c1=0; for(x=0;x26)printf("ERRORE! Sintassi proxylist non corretta...\n"); if(y>=numero_proxy)break; if(buffer[x]=='\x0a'){y++;c1=0;z=0;} else if( buffer[x] > 58 || buffer[x]< 46 ) c1=1; else if(c1==0){ proxylist[y][z] = buffer[x]; proxylist[y][z+1] = '\0'; z++; } } return numero_proxy; } //********************************************************************// long scarica_mv_video(char *video_id, long video_time, long video_lunghezza, char *proxy_ip, char *video_buffer, float* proxy_speed, long *video_current_point, bool *reset){ char *mv_xml_file; mv_xml_file = (char *) malloc(10000 * sizeof(char)); char ckey1[10]; char ckey2[10]; char hash[40]; char serverid[10]; char svideo_time[20]; if(video_time>0) itoa(video_time,svideo_time,10); else svideo_time[0]='\0'; time_t itempo; char stempo[20]; itempo = time(NULL); itoa(itempo,stempo,10); //printf("time: %s\n",stempo); char url[1000]="http://www.megavideo.com/xml/videolink.php?v="; char url2[]="&width=1278&id="; char url3[]="&u="; strcat(url,video_id); strcat(url,url2); strcat(url,stempo); strcat(url,url3); //printf("%s\n",url); proxy_scarica_su_stringa(url,"www.megavideo.com",mv_xml_file,proxy_ip,15); //ricevo file di configurazione megavideo #ifdef console_mode printf("%s\n\n",mv_xml_file); printf("xml strlen: %d\n",strlen(mv_xml_file)); #endif if(strlen(mv_xml_file) < 100) return video_time; #ifdef console_mode printf("inizio analisi xml file...\n"); #endif int x,ap_counter=0,i=0, key1,key2; for(x=0;x0)ckey1[i-1]=mv_xml_file[x]; i++; } if(ap_counter == 18){ ckey1[i-1] = '\0'; i=0; key1=atoi(ckey1); } if(ap_counter == 19){ //caricamento key2 if(i>0)ckey2[i-1]=mv_xml_file[x]; i++; } if(ap_counter == 20){ ckey2[i-1] = '\0'; i=0; key2=atoi(ckey2); } if(ap_counter == 21){ //caricamento hash if(i>0)hash[i-1]=mv_xml_file[x]; i++; } if(ap_counter == 22){ hash[i-1] = '\0'; i=0; } if(ap_counter == 23){ //caricamento serverid if(i>0)serverid[i-1]=mv_xml_file[x]; i++; } if(ap_counter == 24){ serverid[i-1] = '\0'; i=0; } } //printf("key1: %d key2: %d serverid: %s\nhash: %s %d\n",key1,key2,serverid,hash,strlen(hash)); if(strlen(hash)!=32){ #ifdef console_mode printf("errore estrazione dati per decoding...\n"); #endif return video_time; } #ifdef console_mode printf("decoding in: %s\n",hash); #endif mv_decode(hash, key1, key2); #ifdef console_mode printf("decoding ok: %s\n",hash); #endif char furl[1000]="http://www"; char furl2[]=".megavideo.com/files/"; char furl3[]="/"; strcat(furl,serverid); strcat(furl,furl2); strcat(furl,hash); strcat(furl,furl3); strcat(furl,svideo_time); char fhost[1000]="www"; char fhost2[]=".megavideo.com"; strcat(fhost,serverid); strcat(fhost,fhost2); #ifdef console_mode printf("FINAL DOWNLOAD URL: %s\n",furl); #endif Sleep(5000); video_time = proxy_scarica(furl, fhost,video_buffer, proxy_ip, 15, video_time, video_lunghezza, proxy_speed, video_current_point, reset); free(mv_xml_file); return video_time; } //********************************************************************// long mv_video_lunghezza(char *video_id, char *proxy_ip){ char *mv_xml_file; char *console_string; char *ckey1; mv_xml_file = (char *) malloc(10000 * sizeof(char)); mv_xml_file[0] = '\0'; ckey1 = (char *) malloc(10*sizeof(char)); console_string = (char *) malloc(1000 * sizeof(char)); time_t itempo; char stempo[20]; itempo = time(NULL); itoa(itempo,stempo,10); //printf("time: %s\n",stempo); char *url; url = (char *) malloc(1000*sizeof(char)); sprintf(url,"http://www.megavideo.com/xml/videolink.php?v=%s&width=1278&id=%s&u=",video_id,stempo); #ifdef console_mode printf("VIDEO: %s\n",url); #endif sprintf(console_string, "VIDEO: %s\n",url); textview_printf(console_string); proxy_scarica_su_stringa(url,"www.megavideo.com",mv_xml_file,proxy_ip,15); //ricevo file di configurazione megavideo #ifdef console_mode printf("%s\n\n",mv_xml_file); printf("xml strlen: %d\n",strlen(mv_xml_file)); #endif if(strlen(mv_xml_file) < 10) return 0; int x,ap_counter=0,i=0, key1; for(x=0;x0)ckey1[i-1]=mv_xml_file[x]; i++; } if(ap_counter == 12){ ckey1[i-1] = '\0'; i=0; key1=atoi(ckey1); } } #ifdef console_mode printf("VIDEO: bytes: %d\n",key1); #endif sprintf(console_string, "VIDEO: bytes: %d\n",key1); textview_printf(console_string); return key1; } //********************************************************************// bool proxy_scarica_su_stringa(char *fileremoto, char *host,char *stringa, char *proxy_ip, int timeout_sec){ struct sockaddr_in sock; //la struttura per il socket struct hostent ip; //la struttura per l'indirizzo int s; unsigned long cmd; fd_set fds; struct timeval timeout; bool header = true; //variabili per header bool invio = false; char ultimo_carattere = '\0'; char msg_response[] = "\0"; char modo[4]; //proxy ip char ipn[10]; int k=0,i=0,nip=0; int ip1=0,ip2=0,ip3=0,ip4=0,ipport=0; for(k=0;k<(strlen(proxy_ip)+1);k++){ if(proxy_ip[k] != 46 && proxy_ip[k] != 58 && k!=strlen(proxy_ip)){ ipn[i]=proxy_ip[k]; ipn[i+1]='\0'; i++; } else{ if(nip==0)ip1=atoi(ipn); if(nip==1)ip2=atoi(ipn); if(nip==2)ip3=atoi(ipn); if(nip==3)ip4=atoi(ipn); if(nip==4)ipport=atoi(ipn); nip++; i=0; } } #ifdef console_mode printf("risoluzione indirizzo ip: %d.%d.%d.%d:%d\n",ip1,ip2,ip3,ip4,ipport); #endif //********* CONNESSIONE *********// WORD wVersion; WSADATA wsaData; wVersion = MAKEWORD(2, 2); WSAStartup(wVersion, &wsaData); //winsock startup s = socket(AF_INET, SOCK_STREAM, 0); cmd=1; // imposto modalitā socket non boccante (andrā gestita con select()) ioctlsocket( s, FIONBIO, &cmd); // dati per Select timeout.tv_sec = timeout_sec; // numero di secondi per il timeout timeout.tv_usec = 0; // numero di microsecondi per il timeout FD_ZERO(&fds); FD_SET(s, &fds); // aggiungo sd all'elenco dei socket gestiti da select sock.sin_addr.S_un.S_un_b.s_b1=ip1; // indico l'indirizzo IP sock.sin_addr.S_un.S_un_b.s_b2=ip2; sock.sin_addr.S_un.S_un_b.s_b3=ip3; sock.sin_addr.S_un.S_un_b.s_b4=ip4; sock.sin_port = htons(ipport); //porta //ip = *(gethostbyname(host)); // in dico l'indirizzo //memcpy(&sock.sin_addr, ip.h_addr, ip.h_length); //copia l'indirizzo nella struttura socket sock.sin_family = AF_INET; connect(s, (struct sockaddr *)&sock, sizeof(sock)); //ci connettiamo if (select(s+1, 0, &fds, 0, &timeout)>0){ //riusciamo a stabilire la connessione //printf("Connessione al server riuscita...\n"); cmd=0;//ripristino socket bloccante ioctlsocket( s, FIONBIO, &cmd); struct timeval tv; tv.tv_sec = socket_timeout; tv.tv_usec = 0; setsockopt (s, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv)); //********* MESSAGGIO *********// //messaggio per il server... char msg_enter[600] = "GET "; char protocollo[] = " HTTP/1.1\r\n"; char protocollo2[] = "Host: "; char protocollo3[] = "\r\n"; //char dettagli1[] = "Range: bytes=0-20\r\n"; strcat(msg_enter,fileremoto); strcat(msg_enter,protocollo); strcat(msg_enter,protocollo2); strcat(msg_enter,host); strcat(msg_enter,protocollo3); //strcat(msg_enter,dettagli1); //header strcat(msg_enter,"\r\n"); //printf("HTTP PROTOCOL: %s\n", msg_enter); send(s, msg_enter, strlen(msg_enter), 0); //invia dati //printf("Download in corso...\n\n"); k = 0; stringa[0] = '\0'; while(recv(s, msg_response, 1, 0) > 0) { if (!header){ stringa[k] = msg_response[0]; k++; } //else printf("%c",msg_response[0]); //controllo per non scrivere header nel file. if (ultimo_carattere == 13 && msg_response[0] == 10 && invio==true) header=false; ((ultimo_carattere == 13 && msg_response[0] == 10) || (ultimo_carattere==10 && invio))?invio=true:invio=false; ultimo_carattere = msg_response[0]; //fprintf(fo,"%c",msg_response[0]); } //printf("recv fine!\n"); stringa[k] = '\0'; //printf("File Ricevuto\n"); }//fine if(select()) else{ //timeout //printf("Connessione timeout!\n"); return false; } //operazioni di chiusura! shutdown(s, 2); closesocket(s); WSACleanup(); return true; } //********************************************************************// long proxy_scarica(char *fileremoto, char *host,char *video_buffer, char *proxy_ip, int timeout_sec, long video_time, long video_lunghezza, float *proxy_speed, long *video_current_point, bool *reset){ long vid_byte_count = video_time; struct sockaddr_in sock; //la struttura per il socket struct hostent ip; //la struttura per l'indirizzo int s; unsigned long cmd; fd_set fds; struct timeval timeout; long x; bool header = true; //variabili per header bool invio = false; char ultimo_carattere = '\0'; char msg_response[] = "\0"; char modo[4]; //proxy ip char ipn[10]; int k=0,i=0,nip=0; int ip1=0,ip2=0,ip3=0,ip4=0,ipport=0; for(k=0;k<(strlen(proxy_ip)+1);k++){ if(proxy_ip[k] != 46 && proxy_ip[k] != 58 && k!=strlen(proxy_ip)){ ipn[i]=proxy_ip[k]; ipn[i+1]='\0'; i++; } else{ if(nip==0)ip1=atoi(ipn); if(nip==1)ip2=atoi(ipn); if(nip==2)ip3=atoi(ipn); if(nip==3)ip4=atoi(ipn); if(nip==4)ipport=atoi(ipn); nip++; i=0; } } #ifdef console_mode printf("risoluzione indirizzo ip: %d.%d.%d.%d:%d\n",ip1,ip2,ip3,ip4,ipport); #endif //********* CONNESSIONE *********// WORD wVersion; WSADATA wsaData; wVersion = MAKEWORD(2, 2); WSAStartup(wVersion, &wsaData); //winsock startup s = socket(AF_INET, SOCK_STREAM, 0); cmd=1; // imposto modalitā socket non boccante (andrā gestita con select()) ioctlsocket( s, FIONBIO, &cmd); // dati per Select timeout.tv_sec = timeout_sec; // numero di secondi per il timeout timeout.tv_usec = 0; // numero di microsecondi per il timeout FD_ZERO(&fds); FD_SET(s, &fds); // aggiungo s all'elenco dei socket gestiti da select sock.sin_addr.S_un.S_un_b.s_b1=ip1; // indico l'indirizzo IP sock.sin_addr.S_un.S_un_b.s_b2=ip2; sock.sin_addr.S_un.S_un_b.s_b3=ip3; sock.sin_addr.S_un.S_un_b.s_b4=ip4; sock.sin_port = htons(ipport); //porta //ip = *(gethostbyname(host)); // in dico l'indirizzo //memcpy(&sock.sin_addr, ip.h_addr, ip.h_length); //copia l'indirizzo nella struttura socket sock.sin_family = AF_INET; connect(s, (struct sockaddr *)&sock, sizeof(sock)); //ci connettiamo if (select(s+1, 0, &fds, 0, &timeout)>0){ //riusciamo a stabilire la connessione //printf("Connessione al server riuscita...\n"); cmd=0;//ripristino socket bloccante ioctlsocket( s, FIONBIO, &cmd); struct timeval tv; tv.tv_sec = socket_timeout; tv.tv_usec = 0; setsockopt (s, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv) ); //********* MESSAGGIO *********// //messaggio per il server... char msg_enter[600] = "GET "; char protocollo[] = " HTTP/1.1\r\n"; char protocollo2[] = "Host: "; char protocollo3[] = "\r\n"; //char dettagli1[] = "Range: bytes=0-20\r\n"; strcat(msg_enter,fileremoto); strcat(msg_enter,protocollo); strcat(msg_enter,protocollo2); strcat(msg_enter,host); strcat(msg_enter,protocollo3); //strcat(msg_enter,dettagli1); //header strcat(msg_enter,"\r\n"); #ifdef console_mode printf("Invio al server:\n\n%s\n", msg_enter); #endif send(s, msg_enter, strlen(msg_enter), 0); //invia dati //orologio clock_t start,end; double tempo_trascorso = 0; long cicli=0; long cicli2 = 0; int possib = 0; bool lentezza = 0; long downloaded_bytes = 0; char *download_buffer; download_buffer = (char *) malloc(download_buffer_size * sizeof(char)); char *http_header; long lhead = 0; http_header = (char *) malloc(10000*sizeof(char)); while(downloaded_bytes = recv(s, msg_response, 1, 0) > 0){ http_header[lhead] = msg_response[0]; lhead++; //controllo per non scrivere header nel file. if (ultimo_carattere == 13 && msg_response[0] == 10 && invio==true) header=false; ((ultimo_carattere == 13 && msg_response[0] == 10) || (ultimo_carattere==10 && invio))?invio=true:invio=false; ultimo_carattere = msg_response[0]; if(!header) break; //ricevuto header esci dal ciclo } http_header[lhead] = '\0'; #ifdef console_mode printf("Header risposta:\n\n%s\n", http_header); #endif free(http_header); if(downloaded_bytes > 0) while(downloaded_bytes = recv(s, msg_response, 1, 0) > 0){ if(cicli == 0 && msg_response[0] != '\x46'){ break; }//se non ricevo header flv chiudo download if( cicli>=9 || video_time == 0 ){ video_buffer[vid_byte_count] = msg_response[0]; vid_byte_count++; break; } cicli++; } start = clock(); if(downloaded_bytes > 0){ if(cicli>=9 || video_time == 0) { while(1){ downloaded_bytes = recv(s, download_buffer, download_buffer_size, 0); //0x0008 if(downloaded_bytes < 0){ int wsaerrore = WSAGetLastError(); #ifdef console_mode printf("recv %d error code: %d\n", downloaded_bytes, wsaerrore); #endif } if(downloaded_bytes <= 0) break; if(video_buffer[vid_byte_count] != '\0'){ vid_byte_count = -1; //disattivo processo break; //se i dati sono gia stati scritti mi fermo } for(x=0;x= speed_info_time ){ //sistema di misurazione velocitā //printf("SPEED:%d kbs\n",((cicli-cicli2)/(speed_info_time*1024))); *proxy_speed = ((double)(cicli-cicli2)/(tempo_trascorso*1024)); *video_current_point = vid_byte_count; //limitatore di lentezza //if(((cicli-cicli2)/(speed_info_time*1024)) < limite_speed) printf("SPEED: troppo lento %d avvertimento n %d\n",((cicli-cicli2)/(speed_info_time*1024)),possib); if(*proxy_speed < limite_speed && possib >= possibilit_speed){ lentezza=1; break; } //printf("SPEED: disconnesso per lentezza!!\n"); if(*proxy_speed < limite_speed && possib < possibilit_speed) possib++; if(*proxy_speed >= limite_speed) possib=0; //reset control if(*reset == 1){ *reset = 0; break; } start = clock(); cicli2 = cicli; } } } } //printf("recv fine!\n"); *proxy_speed = 0; //azzero la velocitā //cancello ultimi 200 byte long j,bersaglio; if(video_lunghezza > vid_byte_count && vid_byte_count>=0){ if(vid_byte_count-200 < video_time) bersaglio = video_time; else bersaglio = vid_byte_count-200; for(j=vid_byte_count;j>=bersaglio;j--) video_buffer[j] = '\0'; vid_byte_count=bersaglio; } }//fine if(select()) else{ //timeout #ifdef console_mode printf("Connessione timeout!\n"); #endif return vid_byte_count; } //operazioni di chiusura! shutdown(s, 2); closesocket(s); WSACleanup(); return vid_byte_count; } //********************************************************************// int mv_decode(char *string, long key1, long key2){ int string_len = strlen(string); char *decoded; decoded = (char *) malloc(200 * sizeof(char)); char *binblock; binblock = (char *) malloc(200); binblock[0] = '\0'; int i; for( i=0; i < string_len; ++i ){ switch( string[i] ){ case '0': strcat(binblock,"0000"); break; case '1': strcat(binblock,"0001"); break; case '2': strcat(binblock,"0010"); break; case '3': strcat(binblock,"0011"); break; case '4': strcat(binblock,"0100"); break; case '5': strcat(binblock,"0101"); break; case '6': strcat(binblock,"0110"); break; case '7': strcat(binblock,"0111"); break; case '8': strcat(binblock,"1000"); break; case '9': strcat(binblock,"1001"); break; case 'a': strcat(binblock,"1010"); break; case 'b': strcat(binblock,"1011"); break; case 'c': strcat(binblock,"1100"); break; case 'd': strcat(binblock,"1101"); break; case 'e': strcat(binblock,"1110"); break; case 'f': strcat(binblock,"1111"); break; } } long *ciphers; ciphers = (long *) malloc(384 * sizeof(long)); for( i = 0; i < 384; ++i ){ key1 = (key1 * 11 + 77213) % 81371; key2 = (key2 * 17 + 92717) % 192811; ciphers[i] = (key1 + key2) % 128; } long cipher,offset,tmp; for( i = 256; i >= 0; --i ){ cipher = ciphers[i]; offset = i % 128; tmp = binblock[cipher]; binblock[cipher] = binblock[offset]; binblock[offset] = tmp; } for( i = 0; i < 128; ++i ){ binblock[i] = binblock[i] ^ ciphers[i + 256] & 1; } int a=0; for(i=0;i < string_len*4 ; i=i+4 ){ if(binblock[i]== '0' && binblock[i+1]== '0' && binblock[i+2]== '0' && binblock[i+3]== '0' ) decoded[a] = '0'; else if(binblock[i]== '0' && binblock[i+1]== '0' && binblock[i+2]== '0' && binblock[i+3]== '1' ) decoded[a] = '1'; else if(binblock[i]== '0' && binblock[i+1]== '0' && binblock[i+2]== '1' && binblock[i+3]== '0' ) decoded[a] = '2'; else if(binblock[i]== '0' && binblock[i+1]== '0' && binblock[i+2]== '1' && binblock[i+3]== '1' ) decoded[a] = '3'; else if(binblock[i]== '0' && binblock[i+1]== '1' && binblock[i+2]== '0' && binblock[i+3]== '0' ) decoded[a] = '4'; else if(binblock[i]== '0' && binblock[i+1]== '1' && binblock[i+2]== '0' && binblock[i+3]== '1' ) decoded[a] = '5'; else if(binblock[i]== '0' && binblock[i+1]== '1' && binblock[i+2]== '1' && binblock[i+3]== '0' ) decoded[a] = '6'; else if(binblock[i]== '0' && binblock[i+1]== '1' && binblock[i+2]== '1' && binblock[i+3]== '1' ) decoded[a] = '7'; else if(binblock[i]== '1' && binblock[i+1]== '0' && binblock[i+2]== '0' && binblock[i+3]== '0' ) decoded[a] = '8'; else if(binblock[i]== '1' && binblock[i+1]== '0' && binblock[i+2]== '0' && binblock[i+3]== '1' ) decoded[a] = '9'; else if(binblock[i]== '1' && binblock[i+1]== '0' && binblock[i+2]== '1' && binblock[i+3]== '0' ) decoded[a] = 'a'; else if(binblock[i]== '1' && binblock[i+1]== '0' && binblock[i+2]== '1' && binblock[i+3]== '1' ) decoded[a] = 'b'; else if(binblock[i]== '1' && binblock[i+1]== '1' && binblock[i+2]== '0' && binblock[i+3]== '0' ) decoded[a] = 'c'; else if(binblock[i]== '1' && binblock[i+1]== '1' && binblock[i+2]== '0' && binblock[i+3]== '1' ) decoded[a] = 'd'; else if(binblock[i]== '1' && binblock[i+1]== '1' && binblock[i+2]== '1' && binblock[i+3]== '0' ) decoded[a] = 'e'; else if(binblock[i]== '1' && binblock[i+1]== '1' && binblock[i+2]== '1' && binblock[i+3]== '1' ) decoded[a] = 'f'; else return 0; decoded[a+1] = '\0'; a++; } for(i=0;i