git » gofer » commit 3434104

raw: Only log at connection close

author Alberto Bertogli
2017-08-07 21:19:49 UTC
committer Alberto Bertogli
2017-08-07 21:22:34 UTC
parent cb5dcb87b093e275a7095e645ed52038539e1a73

raw: Only log at connection close

Logging on connection open and close results in very verbose logs, for
little gain. HTTP just logs at close.

So this patch makes raw logs only show at close, and print the duration
instead (split into connection, forwarding and total).

proxy/raw.go +7 -5

diff --git a/proxy/raw.go b/proxy/raw.go
index d57902b..32f7f67 100644
--- a/proxy/raw.go
+++ b/proxy/raw.go
@@ -3,6 +3,7 @@ package proxy
 import (
 	"crypto/tls"
 	"net"
+	"time"
 
 	"blitiri.com.ar/go/gofer/config"
 	"blitiri.com.ar/go/gofer/util"
@@ -43,6 +44,7 @@ func Raw(conf config.Raw) {
 func forward(src net.Conn, dstAddr string, dstTLS bool) {
 	defer src.Close()
 
+	start := time.Now()
 	var dst net.Conn
 	var err error
 	if dstTLS {
@@ -57,11 +59,11 @@ func forward(src net.Conn, dstAddr string, dstTLS bool) {
 	}
 	defer dst.Close()
 
-	util.Log.Printf("%s raw %s -> %s: open",
-		src.RemoteAddr(), src.LocalAddr(), dst.RemoteAddr())
-
+	startCopy := time.Now()
 	util.BidirCopy(src, dst)
+	end := time.Now()
 
-	util.Log.Printf("%s raw %s -> %s: close",
-		src.RemoteAddr(), src.LocalAddr(), dst.RemoteAddr())
+	util.Log.Printf("%s raw %s -> %s (%s+%s=%s)",
+		src.RemoteAddr(), src.LocalAddr(), dst.RemoteAddr(),
+		startCopy.Sub(start), end.Sub(startCopy), end.Sub(start))
 }