new Flags for timeout, keep alive, and retry + downgrade ssl

nosocat
Jean-Louis Huynen 2019-01-17 10:52:22 +01:00
parent a4a86390cb
commit e035adb07f
1 changed files with 36 additions and 8 deletions

View File

@ -52,6 +52,10 @@ type (
src io.Reader src io.Reader
dst d4Writer dst d4Writer
confdir string confdir string
cka time.Duration
ct time.Duration
ce bool
retry time.Duration
d4error uint8 d4error uint8
errnoCopy uint8 errnoCopy uint8
debug bool debug bool
@ -77,8 +81,16 @@ var (
logger.Output(2, info) logger.Output(2, info)
} }
tmpct, _ = time.ParseDuration("5mn")
tmpcka, _ = time.ParseDuration("2h")
tmpretry, _ = time.ParseDuration("30s")
confdir = flag.String("c", "", "configuration directory") confdir = flag.String("c", "", "configuration directory")
debug = flag.Bool("v", false, "Set to True, true, TRUE, 1, or t to enable verbose output on stdout") debug = flag.Bool("v", false, "Set to True, true, TRUE, 1, or t to enable verbose output on stdout")
ce = flag.Bool("ce", true, "Set to True, true, TRUE, 1, or t to enable TLS on network destination")
ct = flag.Duration("ct", tmpct, "Set timeout in human format")
cka = flag.Duration("cka", tmpcka, "Keep Alive time human format, 0 to disable")
retry = flag.Duration("rt", tmpretry, "Time in human format before retry after connection failure, set to 0 to exit on failure")
) )
func main() { func main() {
@ -107,6 +119,10 @@ func main() {
fmt.Printf("destination - the destination where the data is written to\n") fmt.Printf("destination - the destination where the data is written to\n")
fmt.Printf("\n") fmt.Printf("\n")
fmt.Printf("-v [TRUE] for verbose output on stdout") fmt.Printf("-v [TRUE] for verbose output on stdout")
fmt.Printf("-ce [TRUE] if destination is set to ip:port, use of tls")
fmt.Printf("-ct [300] if destination is set to ip:port, timeout in seconds")
fmt.Printf("-cka [3600] if destination is set to ip:port, keepalive in seconds")
fmt.Printf("-retry [5] if destination is set to ip:port, keepalive in seconds")
flag.PrintDefaults() flag.PrintDefaults()
} }
@ -116,6 +132,10 @@ func main() {
os.Exit(1) os.Exit(1)
} }
d4.confdir = *confdir d4.confdir = *confdir
d4.ce = *ce
d4.ct = *ct
d4.cka = *cka
d4.retry = *retry
// Output logging before closing if debug is enabled // Output logging before closing if debug is enabled
if *debug == true { if *debug == true {
@ -130,8 +150,8 @@ func main() {
go d4Copy(d4p, c) go d4Copy(d4p, c)
} else { } else {
go func() { go func() {
time.Sleep(5 * time.Second) time.Sleep(d4.retry)
fmt.Println("Sleeping.. before retry") infof(fmt.Sprintf("Sleeping for %f seconds before retry.\n", d4.retry.Seconds()))
c <- "done waiting" c <- "done waiting"
}() }()
} }
@ -230,15 +250,23 @@ func setReaderWriters(d4 *d4S) bool {
if isn { if isn {
dial := net.Dialer{ dial := net.Dialer{
DualStack: true, DualStack: true,
Timeout: 5 * time.Second, Timeout: (*d4).ct,
KeepAlive: 2 * time.Hour, KeepAlive: (*d4).cka,
FallbackDelay: 0, FallbackDelay: 0,
} }
conn, errc := tls.DialWithDialer(&dial, "tcp", dstnet[0]+":"+dstnet[1], &tls.Config{InsecureSkipVerify: true}) if (*d4).ce == true {
if errc != nil { conn, errc := tls.DialWithDialer(&dial, "tcp", dstnet[0]+":"+dstnet[1], &tls.Config{InsecureSkipVerify: true})
return false if errc != nil {
return false
}
(*d4).dst = newD4Writer(conn, (*d4).conf.key)
} else {
conn, errc := dial.Dial("tcp", dstnet[0]+":"+dstnet[1])
if errc != nil {
return false
}
(*d4).dst = newD4Writer(conn, (*d4).conf.key)
} }
(*d4).dst = newD4Writer(conn, (*d4).conf.key)
} else { } else {
switch (*d4).conf.destination { switch (*d4).conf.destination {
case "stdout": case "stdout":