@@ -10,6 +10,15 @@ import (
1010 "os"
1111 "sync"
1212 "time"
13+
14+ "github.com/wailsapp/wails/v2/pkg/runtime"
15+ )
16+
17+ // Connection timeouts according to best practices
18+ const (
19+ connectionTimeout = 10 * time .Second
20+ writeTimeout = 30 * time .Second
21+ readTimeout = 30 * time .Second
1322)
1423
1524// App struct
@@ -35,11 +44,23 @@ func (a *App) domReady(ctx context.Context) {}
3544
3645// beforeClose is called when the application is about to quit.
3746func (a * App ) beforeClose (ctx context.Context ) (prevent bool ) {
47+ // Clean up any active connections
48+ a .Disconnect ()
3849 return false
3950}
4051
4152// shutdown is called at application termination
42- func (a * App ) shutdown (ctx context.Context ) {}
53+ func (a * App ) shutdown (ctx context.Context ) {
54+ // Ensure all connections are closed
55+ a .Disconnect ()
56+ }
57+
58+ // IsConnected returns the current connection state
59+ func (a * App ) IsConnected () bool {
60+ a .connMu .Lock ()
61+ defer a .connMu .Unlock ()
62+ return a .isConnected
63+ }
4364
4465// dialTLS establece una conexión TLS al servidor remoto
4566// Si tlsVerify es false, acepta cualquier certificado (útil para certificados autofirmados)
@@ -62,7 +83,7 @@ func dialTLS(address, port string, tlsVerify bool) (net.Conn, error) {
6283
6384 // Establecer conexión TLS con timeout
6485 dialer := & net.Dialer {
65- Timeout : 10 * time . Second ,
86+ Timeout : connectionTimeout ,
6687 }
6788
6889 tlsConn , err := tls .DialWithDialer (dialer , "tcp" , fullAddress , tlsConfig )
@@ -120,7 +141,7 @@ func dialConnection(address, port, protocol string, useTLS, tlsVerify bool) (net
120141 // net.JoinHostPort maneja correctamente IPv4 e IPv6
121142 fullAddress := net .JoinHostPort (address , port )
122143 dialer := & net.Dialer {
123- Timeout : 10 * time . Second ,
144+ Timeout : connectionTimeout ,
124145 }
125146
126147 conn , err := dialer .Dial (protocol , fullAddress )
@@ -225,6 +246,11 @@ func (a *App) SendSyslogMessages(config SyslogConfig) SyslogResponse {
225246 return response
226247 }
227248
249+ // Emit start event
250+ runtime .EventsEmit (a .ctx , "syslog:sending" , map [string ]interface {}{
251+ "total" : len (config .Messages ),
252+ })
253+
228254 // net.JoinHostPort maneja correctamente IPv4 e IPv6
229255 fullAddress := net .JoinHostPort (config .Address , config .Port )
230256
@@ -245,14 +271,14 @@ func (a *App) SendSyslogMessages(config SyslogConfig) SyslogResponse {
245271
246272 // Enviar mensajes según el protocolo
247273 if config .Protocol == "tcp" {
248- return sendTCPMessages (conn , config )
274+ return a . sendTCPMessages (conn , config )
249275 }
250- return sendUDPMessages (conn , config )
276+ return a . sendUDPMessages (conn , config )
251277}
252278
253279// sendTCPMessages envía mensajes TCP con el framing adecuado según RFC 6587
254280// Utiliza el módulo Framer para aplicar el método de framing configurado
255- func sendTCPMessages (conn net.Conn , config SyslogConfig ) SyslogResponse {
281+ func ( a * App ) sendTCPMessages (conn net.Conn , config SyslogConfig ) SyslogResponse {
256282 response := SyslogResponse {
257283 SentMessages : []string {},
258284 Errors : []string {},
@@ -265,7 +291,8 @@ func sendTCPMessages(conn net.Conn, config SyslogConfig) SyslogResponse {
265291 MaxMessageLength : 0 , // Sin límite (el servidor puede imponer límites)
266292 })
267293
268- for _ , message := range config .Messages {
294+ totalMessages := len (config .Messages )
295+ for i , message := range config .Messages {
269296 // Construir mensaje syslog según RFC 5424 o RFC 3164
270297 syslogMsg , err := buildSyslogMessage (config , message )
271298 if err != nil {
@@ -280,6 +307,11 @@ func sendTCPMessages(conn net.Conn, config SyslogConfig) SyslogResponse {
280307 continue
281308 }
282309
310+ // Set write deadline to prevent hanging
311+ if err := conn .SetWriteDeadline (time .Now ().Add (writeTimeout )); err != nil {
312+ log .Printf ("Warning: failed to set write deadline: %v" , err )
313+ }
314+
283315 // Enviar con manejo robusto de escrituras parciales
284316 if err := writeAll (conn , framedMsg ); err != nil {
285317 log .Printf ("Error sending message: %v" , err )
@@ -288,25 +320,44 @@ func sendTCPMessages(conn net.Conn, config SyslogConfig) SyslogResponse {
288320 log .Printf ("Sent message: %s" , syslogMsg )
289321 response .SentMessages = append (response .SentMessages , syslogMsg )
290322 }
323+
324+ // Emit progress event
325+ runtime .EventsEmit (a .ctx , "syslog:progress" , map [string ]interface {}{
326+ "current" : i + 1 ,
327+ "total" : totalMessages ,
328+ "percent" : float64 (i + 1 ) / float64 (totalMessages ) * 100 ,
329+ })
291330 }
292331
332+ // Emit completion event
333+ runtime .EventsEmit (a .ctx , "syslog:complete" , map [string ]interface {}{
334+ "sent" : len (response .SentMessages ),
335+ "errors" : len (response .Errors ),
336+ })
337+
293338 return response
294339}
295340
296341// sendUDPMessages envía mensajes UDP (un mensaje por paquete, sin framing)
297- func sendUDPMessages (conn net.Conn , config SyslogConfig ) SyslogResponse {
342+ func ( a * App ) sendUDPMessages (conn net.Conn , config SyslogConfig ) SyslogResponse {
298343 response := SyslogResponse {
299344 SentMessages : []string {},
300345 Errors : []string {},
301346 }
302347
303- for _ , message := range config .Messages {
348+ totalMessages := len (config .Messages )
349+ for i , message := range config .Messages {
304350 syslogMsg , err := buildSyslogMessage (config , message )
305351 if err != nil {
306352 response .Errors = append (response .Errors , fmt .Sprintf ("Error building message: %v" , err ))
307353 continue
308354 }
309355
356+ // Set write deadline to prevent hanging
357+ if err := conn .SetWriteDeadline (time .Now ().Add (writeTimeout )); err != nil {
358+ log .Printf ("Warning: failed to set write deadline: %v" , err )
359+ }
360+
310361 // UDP no requiere framing, un mensaje por paquete
311362 if err := writeAll (conn , []byte (syslogMsg )); err != nil {
312363 log .Printf ("Error sending message: %v" , err )
@@ -315,8 +366,21 @@ func sendUDPMessages(conn net.Conn, config SyslogConfig) SyslogResponse {
315366 log .Printf ("Sent message: %s" , syslogMsg )
316367 response .SentMessages = append (response .SentMessages , syslogMsg )
317368 }
369+
370+ // Emit progress event
371+ runtime .EventsEmit (a .ctx , "syslog:progress" , map [string ]interface {}{
372+ "current" : i + 1 ,
373+ "total" : totalMessages ,
374+ "percent" : float64 (i + 1 ) / float64 (totalMessages ) * 100 ,
375+ })
318376 }
319377
378+ // Emit completion event
379+ runtime .EventsEmit (a .ctx , "syslog:complete" , map [string ]interface {}{
380+ "sent" : len (response .SentMessages ),
381+ "errors" : len (response .Errors ),
382+ })
383+
320384 return response
321385}
322386
0 commit comments