-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.go
126 lines (103 loc) · 3.03 KB
/
Client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"bufio"
"crypto/md5"
"encoding/hex"
"fmt"
"io"
"log"
"net"
"os"
"strconv"
"strings"
"time"
)
const BUFFERSIZE = 1024
func descargar (dstFile string, conn net.Conn, logger *log.Logger) bool {
bufferFileSize := make([]byte, 10)
conn.Read(bufferFileSize)
tamanio, _ := strconv.ParseInt(strings.Trim(string(bufferFileSize), ":"), 10, 64)
// Crea el archivo para luego guardar en este lo que se descarga
archivo, err := os.Create(dstFile)
if err != nil {
logger.Println("Hubo un error al crear el archivo donde se descargará: ", err)
return false
}
antes := time.Now()
var bytes int64
for {
if (tamanio - bytes) < BUFFERSIZE {
io.CopyN(archivo, conn, (tamanio - bytes))
conn.Read(make([]byte, (bytes+BUFFERSIZE)-tamanio))
break
}
io.CopyN(archivo, conn, BUFFERSIZE)
bytes += BUFFERSIZE
}
despues := time.Now()
archivo.Close()
logger.Println("Se recibió un archivo (sin verificar) y tardó ", despues.Sub(antes).Seconds(), " segundos")
_ , err = fmt.Fprintln(conn, "OK")
if err != nil {
logger.Println("Hubo un error al enviar el mensaje de control", err)
return false
}
// Obtiene el hash del servidor
hash, err := bufio.NewReader(conn).ReadString('\n')
if err != nil {
logger.Println("Hubo un error al leer el hash del archivo", err)
return false
}
return verificarIntegridad(dstFile, strings.TrimSuffix(hash, "\n"), logger)
}
func verificarIntegridad (ruta, hashRecibido string, logger *log.Logger) bool {
// Crea una interfaz para realizar el hash
hash := md5.New()
archivo, err := os.Open(ruta)
if err != nil {
logger.Println("Hubo un problema al abrir el archivo para calcular su hash")
return false
}
defer archivo.Close()
// Copia el archivo a la interfaz
_, err = io.Copy(hash, archivo)
if err != nil {
logger.Println("Hubo un problema al copiar el archivo para calcular su hash")
return false
}
// Obtiene el hash en 16 bytes
hashInBytes := hash.Sum(nil)[:16]
hashCalculado := hex.EncodeToString(hashInBytes)
// Convierte el hash a String
if hashCalculado != hashRecibido {
logger.Println("El hash no se pudo verificar")
return false
}
logger.Println("Se verificó el hash del archivo de forma correcta")
return true
}
func main() {
logFile, err := os.Create("data/logClient.txt")
logger := log.New(logFile, ">>", log.LstdFlags)
logger.Println("Inicio")
fmt.Println("Indica la direccion:puerto a la cual desea conectarse")
var puerto string
fmt.Scanln(&puerto)
con, err := net.Dial("tcp", puerto)
if err != nil {
logger.Println("Hubo un error: ", err, " al conectarse al puerto ", puerto)
return
}
defer con.Close()
fmt.Fprintln(con, "Listo")
antes := time.Now()
seDescargo := descargar("data/archivo.mp4", con, logger)
despues := time.Now()
if seDescargo != true {
logger.Println("Hubo un error descargando el archivo desde el servidor " + puerto)
} else {
seg := despues.Sub(antes).Seconds()
fmt.Fprintln(con, "Verificado")
logger.Println("Se descargó y verificó correctamente el archivo y tardó ", seg, " segundos")
}
}