]> git.lizzy.rs Git - go-anidb.git/commitdiff
anidb: Log UDP traffic
authorDiogo Franco (Kovensky) <diogomfranco@gmail.com>
Tue, 16 Jul 2013 16:46:40 +0000 (13:46 -0300)
committerDiogo Franco (Kovensky) <diogomfranco@gmail.com>
Tue, 16 Jul 2013 16:46:40 +0000 (13:46 -0300)
But only the first line of replies. ReAuth and Logout need special
handling since they don't use the sendQueue.

auth.go
udp.go

diff --git a/auth.go b/auth.go
index 029d5a1f05f8050bf28a8d17001a5416831c2a42..3d346e4d443d86617a4344886b77e42940624e03 100644 (file)
--- a/auth.go
+++ b/auth.go
@@ -92,11 +92,13 @@ func (udp *udpWrap) ReAuth() udpapi.APIReply {
        defer udp.credLock.Unlock()
 
        if c := udp.credentials; c != nil {
+               logRequest(paramSet{cmd: "AUTH", params: paramMap{"user": decrypt(c.username)}})
                r := udp.AniDBUDP.Auth(
                        decrypt(c.username),
                        decrypt(c.password),
                        decrypt(c.udpKey))
                runtime.GC() // any better way to clean the plaintexts?
+               logReply(r)
 
                err := r.Error()
 
@@ -160,6 +162,7 @@ func (adb *AniDB) Logout() error {
 
        if adb.udp.connected {
                adb.udp.connected = false
+               logRequest(paramSet{cmd: "LOGOUT"})
                return adb.udp.Logout()
        }
        return nil
diff --git a/udp.go b/udp.go
index 372aeadf20d8afc2117608f236f9614351a7a4b4..ec8f83d9d16c9c02018dbdca678521c3317bf947 100644 (file)
--- a/udp.go
+++ b/udp.go
@@ -99,6 +99,19 @@ func (r *bannedAPIReply) Error() error {
 
 var bannedReply udpapi.APIReply = &bannedAPIReply{}
 
+func logRequest(set paramSet) {
+       switch set.cmd {
+       case "AUTH":
+               log.Printf("UDP>>> AUTH user=%s\n", set.params["user"])
+       default:
+               log.Printf("UDP>>> %s %s\n", set.cmd, udpapi.ParamMap(set.params).String())
+       }
+}
+
+func logReply(reply udpapi.APIReply) {
+       log.Printf("UDP<<< %d %s\n", reply.Code(), reply.Text())
+}
+
 func (udp *udpWrap) sendQueue() {
        initialWait := 6 * time.Second
        wait := initialWait
@@ -110,6 +123,7 @@ func (udp *udpWrap) sendQueue() {
                        continue
                }
 
+               logRequest(set)
                reply := <-udp.AniDBUDP.SendRecv(set.cmd, udpapi.ParamMap(set.params))
 
                if reply.Error() == udpapi.TimeoutError {
@@ -118,10 +132,13 @@ func (udp *udpWrap) sendQueue() {
                        if wait > time.Minute {
                                wait = time.Minute
                        }
+                       log.Printf("UDP--- Timeout; waiting %s before retry", wait)
 
                        time.Sleep(wait)
                        goto Retry
                }
+               logReply(reply)
+
                wait = initialWait
 
                switch reply.Code() {