From bdf5e13e1ef4b859ca452b684794c211b3a7e9b1 Mon Sep 17 00:00:00 2001 From: Jean-Louis Huynen Date: Tue, 26 May 2020 17:18:48 +0200 Subject: [PATCH] chg: [main] tear down old connections for type 2 --- d4-goclient.go | 41 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/d4-goclient.go b/d4-goclient.go index 8c2769e..ca9fc68 100644 --- a/d4-goclient.go +++ b/d4-goclient.go @@ -420,12 +420,43 @@ func setReaderWriters(d4 *d4S) bool { } isn, dstnet := config.IsNet((*d4).conf.destination) if isn { - // First, we test whether a usable connection already exist + // We test whether a usable connection already exist // (case where the reader run out of data) - switch (*d4).dst.w.(type){ - case net.Conn: - // in this case, it's already set up. - default: + if _, ok := (*d4).dst.w.(net.Conn); !ok { + // We need a connection + dial := net.Dialer{ + Timeout: (*d4).ct, + KeepAlive: (*d4).cka, + FallbackDelay: 0, + } + tlsc := tls.Config{ + InsecureSkipVerify: true, + } + if (*d4).cc { + tlsc = tls.Config{ + InsecureSkipVerify: false, + RootCAs: &(*d4).ca, + } + } + if (*d4).ce == true { + conn, errc := tls.DialWithDialer(&dial, "tcp", dstnet, &tlsc) + if errc != nil { + logger.Println(errc) + return false + } + (*d4).dst = newD4Writer(conn, (*d4).conf.key) + } else { + conn, errc := dial.Dial("tcp", dstnet) + if errc != nil { + return false + } + (*d4).dst = newD4Writer(conn, (*d4).conf.key) + } + }else{ + // The connection can be reused, there is nothing to do + // Except for type 2 - we tear down the old connection, + (*d4).dst.w.(net.Conn).Close() + // and bring up a new one. dial := net.Dialer{ Timeout: (*d4).ct, KeepAlive: (*d4).cka,