#define VERSIONE_PROGRAMMA "MV Smart Downloader v 3.02.01 by mamo139"
//#define console_mode
#include <gtk/gtk.h>
#include <stdio.h>
#include <winsock2.h>
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
const int processi_num = 20;
const int download_buffer_size = 1024;
const int socket_timeout = 10000; //timeout per la conessione, in millisecondi direi
const int backup_time = 180000; //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
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;
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<console_book_size;x++){
console_book[x] = (char *) malloc(1000 * sizeof(char));
console_booking[x] = 1;
}
g_timeout_add(50, (GSourceFunc) book_manager, NULL);
//scrolledwindow download parziali
tabella_download_parziale = gtk_table_new(processi_num*2, 10, TRUE);
scrolledwindow_download_parziale = gtk_scrolled_window_new(NULL,NULL);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwindow_download_parziale), GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolledwindow_download_parziale), tabella_download_parziale);
gtk_table_attach_defaults(GTK_TABLE(tabella1), scrolledwindow_download_parziale, 0, 4, 3, 18);
progressbar_parziale = (GtkWidget **) malloc(processi_num * sizeof(GtkWidget *));
for(x=0;x<processi_num;x++){
progressbar_parziale[x] = gtk_progress_bar_new();
gtk_table_attach_defaults(GTK_TABLE(tabella_download_parziale), progressbar_parziale[x], 0, 10, x*2, x*2+1);
}
label_download_parziale = (GtkWidget **) malloc(processi_num * sizeof(GtkWidget *));
for(x=0;x<processi_num;x++){
label_download_parziale[x] = gtk_label_new("label...");
gtk_table_attach_defaults(GTK_TABLE(tabella_download_parziale), label_download_parziale[x], 0, 10, x*2+1, x*2+2);
}
gtk_widget_show_all(finestra1);
//********** lancio main_thread() *************//
g_thread_create((GThreadFunc) main_thread, NULL, 0, thread_error);
// main_grafica_uploader() si occupa di aggiornare la grafica durante il download
//g_idle_add((GSourceFunc) main_grafica_uploader, NULL);
g_timeout_add(100, (GSourceFunc) main_grafica_uploader, NULL);
gtk_main();
/* Release gtk's global lock */
gdk_threads_leave();
return 0;
}
int main_grafica_uploader(){
if(main_grafica_uploader_start == 0)
return 1;
long x;
long time;
char *console_string;
if(global_percentage < 1)
global_time_end = clock();
console_string = (char *) malloc(1000*sizeof(char));
time = (global_time_end-global_time_start)/CLOCKS_PER_SEC;
gdk_threads_enter();
//titolo finestra
sprintf(console_string,"%.2f%% %s", global_percentage*100, VERSIONE_PROGRAMMA);
gtk_window_set_title(GTK_WINDOW(finestra1), console_string);
//labels
sprintf(console_string,"%d su %d scaricanti", global_processi_scaricanti, (processi_num-global_processi_terminati));
gtk_label_set_text(GTK_LABEL(label_processi_scaricanti), console_string);
sprintf(console_string,"%d su %d terminati", global_processi_terminati, processi_num);
gtk_label_set_text(GTK_LABEL(label_processi_terminati), console_string);
sprintf(console_string,"%02d:%02d:%02d", (time/60/60),(time/60)%60, time%60);
gtk_label_set_text(GTK_LABEL(label_time), console_string);
sprintf(console_string,"%.2f KB/sec", global_speed);
gtk_label_set_text(GTK_LABEL(label_global_speed), console_string);
//progressbar_main
gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progressbar_main),((gdouble)global_percentage));
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;x<processi_num;x++){
if(global_dati[x].percentage>1) global_dati[x].percentage=1;
gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progressbar_parziale[x]),((gdouble)global_dati[x].percentage));
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, y, 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.txt");
#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<proxy_num;x++)
proxylist[x] = (char *) malloc(25 * sizeof(char));
proxylist_loader(proxylist,"proxylist.txt");
//for(x=0;x<proxy_num;x++)
// printf("%d: %s\n",x,proxylist[x]);
#ifdef console_mode
printf("PROXYLIST: caricamento proxylist completato!\n\n"); #endif
textview_printf("PROXYLIST: caricamento proxylist completato!\n\n");
//*********************************//
//*** CONTROLLO LUNGHEZZA VIDEO ***//
//*********************************//
#ifdef console_mode
printf("VIDEO: id: %s\n",video_id
); #endif
sprintf(console_string, "VIDEO: id: %s\n",video_id);
textview_printf(console_string);
#ifdef console_mode
printf("VIDEO: connessione per trovare lunghezza video...\n"); #endif
textview_printf("VIDEO: connessione per trovare lunghezza video...\n");
x=0;
while(video_bytes > 1000000000 || video_bytes < 100){
video_bytes = 0;
video_bytes = mv_video_lunghezza(video_id, proxylist[x]);
x++;
}
#ifdef console_mode
#endif
textview_printf("\n");
//******************************//
//*** CREAZIONE BUFFER VIDEO ***//
//******************************//
video_buffer = (char *) calloc (video_bytes, sizeof(char));
for(x=0;x<video_bytes;x++)
video_buffer[x] = '\0';
#ifdef console_mode
printf("BUFFER: buffer caricato!\n\n"); #endif
textview_printf("BUFFER: buffer caricato!\n\n");
//**********************************//
//*** CARICAMENTO BACKUP MANAGER ***//
//**********************************//
#ifdef console_mode
printf("BACKUP: attivazione...\n\n"); #endif
textview_printf("BACKUP: attivazione...\n\n");
dati_b.video_buffer = video_buffer;
dati_b.video_lunghezza = video_bytes;
dati_b.processo_status = 1;
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) video_backup, &dati_b, 0, &ThreadId);
//***********************************************//
//*** CARICAMENTO PROXY E DOWNLOAD SIMULTANEI ***//
//***********************************************//
#ifdef console_mode
printf("CONNECTION MANAGER: attivazione in corso...\n"); printf("CONNECTION MANAGER: numero di processi da eseguire: %d\n",processi_num
); #endif
textview_printf("CONNECTION MANAGER: attivazione in corso...\n");
sprintf(console_string, "CONNECTION MANAGER: numero di processi da eseguire: %d\n",processi_num);
textview_printf(console_string);
struct connection_data *dati;//strumento di trasmissione informazioni alle funzioni asincrone
dati = (struct connection_data *) malloc(processi_num * sizeof(struct connection_data));
for(x=0;x<processi_num;x++){//attivazione processi
dati[x].processo_id = x;
dati[x].processo_status = 1;
dati[x].proxylist = proxylist;
dati[x].proxy_speed = 0;
dati[x].proxy_num = proxy_num;
dati[x].proxy_point = (unsigned int) 0;//x*proxy_num/processi_num;
dati[x].video_id = video_id;
dati[x].video_buffer = video_buffer;
dati[x].video_point = x*((unsigned long)video_bytes/processi_num);
dati[x].video_starting_point = dati[x].video_point;
dati[x].video_current_point = dati[x].video_point;
dati[x].video_lunghezza = video_bytes;
dati[x].reset = 0;
dati[x].percentage = 0;
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) funzione, &dati[x], 0, &ThreadId);
#ifdef console_mode
printf("CONNECTION MANAGER: attivazione ID: %d\n",dati
[x
].
processo_id); #endif
sprintf(console_string, "CONNECTION MANAGER: attivazione ID: %d\n",dati[x].processo_id);
textview_printf(console_string);
}
#ifdef console_mode
printf("CONNECTION MANAGER: tutti i processi sono stati attivati\n\n",dati
[x
].
processo_id); #endif
sprintf(console_string, "CONNECTION MANAGER: tutti i processi sono stati attivati\n\n",dati[x].processo_id);
textview_printf(console_string);
#ifdef console_mode
//attivazione tell_me
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) tell_me, dati, 0, &ThreadId);
#endif
global_dati = dati;
main_grafica_uploader_start = 1; //attivo main_grafica_uploader()
while(wait){ //attività di controllo funzioni asincrone
Sleep(1000);
//system("cls");
processi_terminati=0;
for(x=0;x<processi_num;x++)
if(dati[x].processo_status == 0) processi_terminati++;
if(processi_terminati == processi_num){
global_speed = 0;
global_percentage = 1;
break;
}
#ifdef console_mode
printf("\nCONNECTION MANAGER: %d su %d segmenti terminati.\n\n",processi_terminati
,processi_num
); #endif
overall_speed = 0; download_attivi=0;
tot_download_percentage = 0;
for(x=0;x<processi_num;x++){
overall_speed = overall_speed + dati[x].proxy_speed;
if(dati[x].proxy_speed >0) download_attivi++;
//****************************//
//***** DOWNLOAD GRAFICA *****//
//****************************//
#ifdef console_mode
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
for(y=0;y<33;y++)
if (download_percentage
== 100
) printf("X"); else if(y
*100
/33
< download_percentage
) printf("x"); printf("| %.2f%%\x09%.3f\x09proxy %d\n", download_percentage
, dati
[x
].
proxy_speed,dati
[x
].
proxy_point); #endif
}
#ifdef console_mode
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
#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){
scanf("%c",&comando);
if(comando == 'r'){ //chiude connessione del processo e la riapre con il proxy selezionato
scanf("%d",&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;
//printf("PROCESSO %d: start download, point %d, proxy %d\n", dati->processo_id,video_point_now,dati->proxy_point);
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){
Sleep(backup_time);
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;x<in_size;x++)
if(buffer[x]== '\x0a') numero_proxy++;
return numero_proxy;
}
//********************************************************************//
int proxylist_loader(char **proxylist, 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;x<in_size;x++)
if(buffer[x]== '\x0a') numero_proxy++;
for(x=0;x<in_size;x++){
if(z
>26
)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
//printf("%s\n\n",mv_xml_file);
//printf("xml strlen: %d\n",strlen(mv_xml_file));
if(strlen(mv_xml_file) < 100) return video_time;
//printf("inizio analisi xml file...\n");
int x,ap_counter=0,i=0, key1,key2;
for(x=0;x<strlen(mv_xml_file);x++){
if(mv_xml_file[x] == 34) ap_counter++;
if(ap_counter == 17){ //caricamento key1
if(i>0)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) return video_time;
mv_decode(hash, key1, key2);
//printf("decoding ok: %s\n\n",hash);
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);
//printf("FINAL DOWNLOAD URL: %s\n",furl);
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;
mv_xml_file = (char *) malloc(10000 * sizeof(char));
mv_xml_file[0] = '\0';
char *console_string;
console_string = (char *) malloc(1000 * sizeof(char));
char ckey1[10];
char ckey2[10];
char hash[40];
char serverid[10];
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);
#ifdef console_mode
#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
//printf("%s\n\n",mv_xml_file);
//printf("VIDEO: xml strlen: %d\n",strlen(mv_xml_file));
if(strlen(mv_xml_file) < 10)
return 0;
int x,ap_counter=0,i=0, key1,key2;
for(x=0;x<strlen(mv_xml_file);x++){
if(mv_xml_file[x] == 34) ap_counter++;
if(ap_counter == 11){ //caricamento key1
if(i>0)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);
free(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;
}
}
//printf("%d %d %d %d %d",ip1,ip2,ip3,ip4,ipport);
//********* 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;
}
}
//printf("%d %d %d %d %d",ip1,ip2,ip3,ip4,ipport);
//********* 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");
//printf("Inviato al server:\n\n%s\n", msg_enter);
send(s, msg_enter, strlen(msg_enter), 0); //invia dati
//printf("Download in corso...\n\n");
//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));
while(downloaded_bytes = recv(s, msg_response, 1, 0) > 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];
if(!header) break; //ricevuto header esci dal ciclo
}
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, 0x0008 );
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<downloaded_bytes;x++){
video_buffer[vid_byte_count] = download_buffer[x]; //scrivo dati nel video_buffer
vid_byte_count++;
}
cicli= cicli + downloaded_bytes; //byte del file scaricati in questa connessione
end = clock();
tempo_trascorso = ((double)(end-start))/CLOCKS_PER_SEC;
if( tempo_trascorso >= 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
//printf("Connessione timeout!\n");
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){
...
return 1;
}
//********************************************************************//
char * read_clipboard(){
HGLOBAL clip;
char *str;
if (!IsClipboardFormatAvailable(CF_TEXT))
return 0;
OpenClipboard(NULL);
clip = GetClipboardData(CF_TEXT);
str = (char *) GlobalLock(clip);
CloseClipboard();
return str;
}
int book_manager(){
long x;
for(x=0;x<console_book_size;x++){
if(console_booking[x]==0){
GtkTextMark *mark;
GtkTextIter console_end;
gtk_text_buffer_get_end_iter(textview_console_buffer, &console_end);
gtk_text_buffer_insert(textview_console_buffer, &console_end, console_book[x], -1);
gtk_text_buffer_place_cursor(textview_console_buffer, &console_end);
mark = gtk_text_buffer_get_insert(textview_console_buffer);
gtk_text_view_scroll_to_mark(GTK_TEXT_VIEW(textview_console), mark, 0.0, TRUE, 0.0, 1.0);
console_booking[x] = 1;
}
}
return 1;
}
void textview_printf(char *string){
long x;
for(x=0;x<console_book_size;x++){
if(console_booking[x] == 1){
console_booking[x] = 0;
sprintf(console_book[x],"%s",string);
break;
}
if(x == console_book_size-1)
x=0;
}
}