From 51a8dd5a86eeca744e0add680b1f4796c4babe2b Mon Sep 17 00:00:00 2001 From: Yawning Angel Date: Mon, 12 May 2014 23:04:39 +0000 Subject: Fix logging again. On second thought instead of using log.Panicf(), panic() and do the logging with recover(). This somewhat centralizes logging in obfs4proxy, which will be easier to change when I invariably decide to do logging differently in the future. --- framing/framing.go | 13 ++++++------- ntor/ntor.go | 5 ++--- obfs4.go | 10 +++++----- obfs4proxy/obfs4proxy.go | 17 ++++++++++++++--- packet.go | 6 ++---- utils.go | 4 ++-- 6 files changed, 31 insertions(+), 24 deletions(-) diff --git a/framing/framing.go b/framing/framing.go index 75b9a73..62170ae 100644 --- a/framing/framing.go +++ b/framing/framing.go @@ -61,7 +61,6 @@ import ( "errors" "fmt" "hash" - "log" "code.google.com/p/go.crypto/nacl/secretbox" @@ -127,7 +126,7 @@ type boxNonce struct { func (nonce *boxNonce) init(prefix []byte) { if noncePrefixLength != len(prefix) { - log.Panicf("BUG: Nonce prefix length invalid: %d", len(prefix)) + panic(fmt.Sprintf("BUG: Nonce prefix length invalid: %d", len(prefix))) } copy(nonce.prefix[:], prefix) @@ -161,7 +160,7 @@ type Encoder struct { // containing exactly KeyLength bytes of keying material. func NewEncoder(key []byte) *Encoder { if len(key) != KeyLength { - log.Panicf("BUG: Invalid encoder key length: %d", len(key)) + panic(fmt.Sprintf("BUG: Invalid encoder key length: %d", len(key))) } encoder := new(Encoder) @@ -223,7 +222,7 @@ type Decoder struct { // containing exactly KeyLength bytes of keying material. func NewDecoder(key []byte) *Decoder { if len(key) != KeyLength { - log.Panicf("BUG: Invalid decoder key length: %d", len(key)) + panic(fmt.Sprintf("BUG: Invalid decoder key length: %d", len(key))) } decoder := new(Decoder) @@ -253,7 +252,7 @@ func (decoder *Decoder) Decode(data *bytes.Buffer) (int, []byte, error) { return 0, nil, err } else if n != lengthLength { // Should *NEVER* happen, since at least 2 bytes exist. - log.Panicf("BUG: Failed to read obfuscated length: %d", n) + panic(fmt.Sprintf("BUG: Failed to read obfuscated length: %d", n)) } // Derive the nonce the peer used. @@ -285,8 +284,8 @@ func (decoder *Decoder) Decode(data *bytes.Buffer) (int, []byte, error) { return 0, nil, err } else if n != int(decoder.nextLength) { // Should *NEVER* happen, since at least 2 bytes exist. - log.Panicf("BUG: Failed to read secretbox, got %d, should have %d", n, - decoder.nextLength) + panic(fmt.Sprintf("BUG: Failed to read secretbox, got %d, should have %d", + n, decoder.nextLength)) } out, ok := secretbox.Open(nil, box, &decoder.nextNonce, &decoder.key) if !ok { diff --git a/ntor/ntor.go b/ntor/ntor.go index b19c4a1..9dbed7f 100644 --- a/ntor/ntor.go +++ b/ntor/ntor.go @@ -45,7 +45,6 @@ import ( "encoding/base64" "fmt" "io" - "log" "code.google.com/p/go.crypto/curve25519" "code.google.com/p/go.crypto/hkdf" @@ -422,9 +421,9 @@ func Kdf(keySeed []byte, okmLen int) []byte { okm := make([]byte, okmLen) n, err := io.ReadFull(kdf, okm) if err != nil { - log.Panicf("BUG: Failed HKDF: %s", err.Error()) + panic(fmt.Sprintf("BUG: Failed HKDF: %s", err.Error())) } else if n != len(okm) { - log.Panicf("BUG: Got truncated HKDF output: %d", n) + panic(fmt.Sprintf("BUG: Got truncated HKDF output: %d", n)) } return okm diff --git a/obfs4.go b/obfs4.go index dae40bd..e69c7b7 100644 --- a/obfs4.go +++ b/obfs4.go @@ -30,7 +30,7 @@ package obfs4 import ( "bytes" - "log" + "fmt" "net" "syscall" "time" @@ -98,7 +98,7 @@ func (c *Obfs4Conn) closeAfterDelay() { func (c *Obfs4Conn) clientHandshake(nodeID *ntor.NodeID, publicKey *ntor.PublicKey) error { if c.isServer { - log.Panicf("BUG: clientHandshake() called for server connection") + panic(fmt.Sprintf("BUG: clientHandshake() called for server connection")) } // Generate/send the client handshake. @@ -156,7 +156,7 @@ func (c *Obfs4Conn) clientHandshake(nodeID *ntor.NodeID, publicKey *ntor.PublicK func (c *Obfs4Conn) serverHandshake(nodeID *ntor.NodeID, keypair *ntor.Keypair) error { if !c.isServer { - log.Panicf("BUG: serverHandshake() called for client connection") + panic(fmt.Sprintf("BUG: serverHandshake() called for client connection")) } hs := newServerHandshake(nodeID, keypair) @@ -220,7 +220,7 @@ func (c *Obfs4Conn) ServerHandshake() error { // Clients handshake as part of Dial. if !c.isServer { - log.Panicf("BUG: ServerHandshake() called for client connection") + panic(fmt.Sprintf("BUG: ServerHandshake() called for client connection")) } // Regardless of what happens, don't need the listener past returning from @@ -297,7 +297,7 @@ func (c *Obfs4Conn) Write(b []byte) (int, error) { c.isOk = false return nSent, err } else if n == 0 { - log.Panicf("BUG: Write(), chopping length was 0") + panic(fmt.Sprintf("BUG: Write(), chopping length was 0")) } nSent += n diff --git a/obfs4proxy/obfs4proxy.go b/obfs4proxy/obfs4proxy.go index 34027fc..42a6740 100644 --- a/obfs4proxy/obfs4proxy.go +++ b/obfs4proxy/obfs4proxy.go @@ -75,28 +75,37 @@ var ptListeners []net.Listener // ends, -1 is written. var handlerChan = make(chan int) +func logAndRecover() { + if err := recover(); err != nil { + log.Println("[ERROR] Panic:", err) + } +} + func copyLoop(a, b net.Conn) { var wg sync.WaitGroup wg.Add(2) - // XXX: Log/propagate errors. go func() { + defer logAndRecover() + defer wg.Done() + _, err := io.Copy(b, a) if err != nil { b.Close() a.Close() log.Printf("[WARN] Connection closed: %s", err) } - wg.Done() }() go func() { + defer logAndRecover() + defer wg.Done() + _, err := io.Copy(a, b) if err != nil { a.Close() b.Close() log.Printf("[WARN] Connection closed: %s", err) } - wg.Done() }() wg.Wait() @@ -104,6 +113,7 @@ func copyLoop(a, b net.Conn) { func serverHandler(conn net.Conn, info *pt.ServerInfo) error { defer conn.Close() + defer logAndRecover() handlerChan <- 1 defer func() { @@ -216,6 +226,7 @@ func clientHandler(conn *pt.SocksConn) error { handlerChan <- -1 }() + defer logAndRecover() remote, err := obfs4.Dial("tcp", conn.Req.Target, nodeID, publicKey) if err != nil { log.Printf("[ERROR] client: Handshake failed: %s", err) diff --git a/packet.go b/packet.go index 98ce426..afccc47 100644 --- a/packet.go +++ b/packet.go @@ -30,7 +30,6 @@ package obfs4 import ( "encoding/binary" "fmt" - "log" "github.com/yawning/obfs4/framing" ) @@ -68,8 +67,8 @@ func makePacket(pkt []byte, pktType uint8, data []byte, padLen uint16) int { pktLen := packetOverhead + len(data) + int(padLen) if len(data)+int(padLen) > maxPacketPayloadLength { - log.Panicf("BUG: makePacket() len(data) + padLen > maxPacketPayloadLength: %d + %d > %d", - len(data), padLen, maxPacketPayloadLength) + panic(fmt.Sprintf("BUG: makePacket() len(data) + padLen > maxPacketPayloadLength: %d + %d > %d", + len(data), padLen, maxPacketPayloadLength)) } // Packets are: @@ -104,7 +103,6 @@ func (c *Obfs4Conn) decodePacket(pkt []byte) error { c.receiveDecodedBuffer.Write(payload) default: // Ignore unrecognised packet types. - log.Printf("[INFO] - Ignoring packet type: %d", pktType) } return nil diff --git a/utils.go b/utils.go index f8a394a..ae7bc41 100644 --- a/utils.go +++ b/utils.go @@ -29,13 +29,13 @@ package obfs4 import ( "crypto/rand" - "log" + "fmt" "math/big" ) func randRange(min, max int64) (int64, error) { if max < min { - log.Panicf("randRange: min > max (%d, %d)", min, max) + panic(fmt.Sprintf("randRange: min > max (%d, %d)", min, max)) } r := (max + 1) - min -- cgit v1.2.3