diff --git a/client.go b/client.go index 7d7c299..b23e8fa 100644 --- a/client.go +++ b/client.go @@ -5,6 +5,7 @@ import ( "compress/gzip" "compress/zlib" "encoding/binary" + "fmt" "io" "os" "strings" @@ -462,14 +463,16 @@ func (c *Client) msgGetDate(queue bool) { Error(TAG, "Error while sending GetDateMessage") } } -func (c *Client) msgCreateSession(config *SessionConfig, queue bool) { +func (c *Client) msgCreateSession(config *SessionConfig, queue bool) error { var err error Debug(TAG|PROTOCOL, "Sending CreateSessionMessage") c.messageStream.Reset() config.writeToMessage(c.messageStream) if err = c.sendMessage(I2CP_MSG_CREATE_SESSION, c.messageStream, queue); err != nil { Error(TAG, "Error while sending CreateSessionMessage.") + return err } + return err } func (c *Client) msgDestLookup(hash []byte, queue bool) { Debug(TAG|PROTOCOL, "Sending DestLookupMessage.") @@ -530,30 +533,42 @@ func (c *Client) msgSendMessage(sess *Session, dest *Destination, protocol uint8 Error(TAG, "Error while sending SendMessageMessage") } } -func (c *Client) Connect() { +func (c *Client) Connect() error { Info(0, "Client connecting to i2cp at %s:%s", c.properties["i2cp.tcp.host"], c.properties["i2cp.tcp.host"]) err := c.tcp.Connect() if err != nil { - panic(err) + //panic(err) + return err } c.outputStream.Reset() c.outputStream.WriteByte(I2CP_PROTOCOL_INIT) _, err = c.tcp.Send(c.outputStream) + if err != nil { + return err + } Debug(PROTOCOL, "Sending protocol byte message") c.msgGetDate(false) - c.recvMessage(I2CP_MSG_SET_DATE, c.messageStream, true) + err = c.recvMessage(I2CP_MSG_SET_DATE, c.messageStream, true) + if err != nil { + return err + } + return nil } -func (c *Client) CreateSession(sess *Session) { +func (c *Client) CreateSession(sess *Session) error { if c.n_sessions == I2CP_MAX_SESSIONS_PER_CLIENT { Warning(TAG, "Maximum number of session per client connection reached.") - return + return fmt.Errorf("%d %s", TAG, "Maximum number of session per client connection reached.") } sess.config.SetProperty(SESSION_CONFIG_PROP_I2CP_FAST_RECEIVE, "true") sess.config.SetProperty(SESSION_CONFIG_PROP_I2CP_MESSAGE_RELIABILITY, "none") - c.msgCreateSession(sess.config, false) + err := c.msgCreateSession(sess.config, false) + if err != nil { + return err + } c.currentSession = sess c.recvMessage(I2CP_MSG_ANY, c.messageStream, true) + return nil } func (c *Client) ProcessIO() error { diff --git a/client_test.go b/client_test.go index d667fae..3149328 100644 --- a/client_test.go +++ b/client_test.go @@ -12,7 +12,12 @@ func TestClient(t *testing.T) { func TestClient_CreateSession(t *testing.T) { client := NewClient(nil) - client.Connect() + + err := client.Connect() + if err != nil { + t.Fatal(err) + } + session := NewSession(client, SessionCallbacks{ onDestination: func(session *Session, requestId uint32, address string, dest *Destination) { @@ -27,7 +32,13 @@ func TestClient_CreateSession(t *testing.T) { session.config.SetProperty(SESSION_CONFIG_PROP_I2CP_FAST_RECEIVE, "true") session.config.SetProperty(SESSION_CONFIG_PROP_OUTBOUND_NICKNAME, "test-i2cp") session.config.SetProperty(SESSION_CONFIG_PROP_OUTBOUND_QUANTITY, "4") - session.config.destination, _ = NewDestinationFromBase64("r2zbc34IQSzOIF4N0enKf0xXkJKgsj9yTGGspRnstKZf~4UoAljZOW5aFZywGo-NlaXwt~tIyj4NC0Til0vl1D5N9ip7OMYUCajNNgiXEH~FN33yl-AcJbeTlB-FychSmVfYciTQj6yd19~6wICwkdpy6AYo90bAejSVGpvtFeP5P2pnSwPmcB8m79wyq~C2XjQCe5UcBxnfYolWKgr3uDFrgbhqBVCCkO7zTiARwOWZLVOvZsvKZR4WvYAmQI6CQaxnmT5n1FKO6NBb-HOxVw4onERq86Sc6EQ5d48719Yk-73wq1Mxmr7Y2UwmL~FCnY33rT1FJY2KzUENICL1uEuiVmr9N924CT9RbtldOUUcXmM1gaHlPS40-Hz4AvPxFXHynbyySktN3hBLPwfwhyIQw95ezSNuiBB0xPcujazCw02103n2CO-59rMDmWpttLjpLMggP9IwsAPa9FVLnBqfuCn3NrC4fia50RDwfR41AD1GOOWiUT0avYzbbOdsAAAA") - client.CreateSession(session) + session.config.destination, err = NewDestination() //FromBase64("r2zbc34IQSzOIF4N0enKf0xXkJKgsj9yTGGspRnstKZf~4UoAljZOW5aFZywGo-NlaXwt~tIyj4NC0Til0vl1D5N9ip7OMYUCajNNgiXEH~FN33yl-AcJbeTlB-FychSmVfYciTQj6yd19~6wICwkdpy6AYo90bAejSVGpvtFeP5P2pnSwPmcB8m79wyq~C2XjQCe5UcBxnfYolWKgr3uDFrgbhqBVCCkO7zTiARwOWZLVOvZsvKZR4WvYAmQI6CQaxnmT5n1FKO6NBb-HOxVw4onERq86Sc6EQ5d48719Yk-73wq1Mxmr7Y2UwmL~FCnY33rT1FJY2KzUENICL1uEuiVmr9N924CT9RbtldOUUcXmM1gaHlPS40-Hz4AvPxFXHynbyySktN3hBLPwfwhyIQw95ezSNuiBB0xPcujazCw02103n2CO-59rMDmWpttLjpLMggP9IwsAPa9FVLnBqfuCn3NrC4fia50RDwfR41AD1GOOWiUT0avYzbbOdsAAAA") + if err != nil { + t.Fatal(err) + } + err = client.CreateSession(session) + if err != nil { + t.Fatal(err) + } client.Disconnect() } diff --git a/crypto.go b/crypto.go index 597fd3d..0f74be7 100644 --- a/crypto.go +++ b/crypto.go @@ -8,6 +8,7 @@ import ( "encoding/base32" "encoding/base64" "errors" + "fmt" "hash" "io" "math/big" @@ -88,7 +89,7 @@ func writeDsaSigToStream(r, s *big.Int, stream *Stream) (err error) { bites := stream.Bytes() rs = r.Bytes() if len(rs) > 21 { - Fatal(tAG|FATAL, "DSA digest r > %21 bytes") + Fatal(tAG|FATAL, "DSA digest r > 21 bytes") } else if len(rs) > 20 { copy(bites[:20], rs[len(rs)-20:]) } else if len(rs) == 20 { @@ -98,7 +99,7 @@ func writeDsaSigToStream(r, s *big.Int, stream *Stream) (err error) { } ss = s.Bytes() if len(ss) > 21 { - Fatal(tAG|FATAL, "DSA digest r > %21 bytes") + Fatal(tAG|FATAL, "DSA digest r > 21 bytes") } else if len(ss) > 20 { copy(bites[20:], ss[len(ss)-20:]) } else if len(ss) == 20 { @@ -139,18 +140,29 @@ func (c *Crypto) WritePublicSignatureToStream(sgk *SignatureKeyPair, stream *Str // Write Signature keypair to stream func (c *Crypto) WriteSignatureToStream(sgk *SignatureKeyPair, stream *Stream) (err error) { + if sgk == nil { + Fatal(tAG|FATAL, "Error signature cannot be nil") + return fmt.Errorf("Error, signature cannot be nil") + } + if stream == nil { + Fatal(tAG|FATAL, "Error, stream cannot be nil") + return fmt.Errorf("Error, stream cannot be nil") + } if sgk.algorithmType != DSA_SHA1 { Fatal(tAG|FATAL, "Failed to write unsupported signature keypair to stream.") + return fmt.Errorf("Failed to write unsupported signature keypair to stream.") } var n int err = stream.WriteUint32(sgk.algorithmType) n, err = stream.Write(sgk.priv.X.Bytes()) if n != 20 { Fatal(tAG|FATAL, "Failed to export signature because publickey != 20 bytes") + return err } n, err = stream.Write(sgk.pub.Y.Bytes()) if n != 128 { Fatal(tAG|FATAL, "Failed to export signature because privatekey != 20 bytes") + return err } return } diff --git a/logger.go b/logger.go index a94f33d..7fc1450 100644 --- a/logger.go +++ b/logger.go @@ -54,26 +54,58 @@ func LogInit(callbacks *LoggerCallbacks, level int) { logInstance.setLogLevel(level) } func Debug(tags LoggerTags, message string, args ...interface{}) { + if len(args) == 0 { + logInstance.log(tags|DEBUG, message) + return + } logInstance.log(tags|DEBUG, message, args...) } func Info(tags LoggerTags, message string, args ...interface{}) { + if len(args) == 0 { + logInstance.log(tags|INFO, message) + return + } logInstance.log(tags|INFO, message, args...) + //logInstance.log(tags|INFO, message, args...) } func Warning(tags LoggerTags, message string, args ...interface{}) { + if len(args) == 0 { + logInstance.log(tags|WARNING, message) + return + } logInstance.log(tags|WARNING, message, args...) + //logInstance.log(tags|WARNING, message, args...) } func Error(tags LoggerTags, message string, args ...interface{}) { + if len(args) == 0 { + logInstance.log(tags|ERROR, message) + return + } logInstance.log(tags|ERROR, message, args...) + //logInstance.log(tags|ERROR, message, args...) } func Fatal(tags LoggerTags, message string, args ...interface{}) { + if len(args) == 0 { + logInstance.log(tags|FATAL, message) + return + } logInstance.log(tags|FATAL, message, args...) + //logInstance.log(tags|FATAL, message, args...) } func (l *Logger) log(tags LoggerTags, format string, args ...interface{}) { + if len(args) != 0 { + if l.callbacks == nil { + fmt.Printf(format+"\n", args) + } else { + l.callbacks.onLog(l, tags, fmt.Sprintf(format, args...)) + } + return + } if l.callbacks == nil { - fmt.Printf(format+"\n", args) + fmt.Printf(format + "\n") } else { - l.callbacks.onLog(l, tags, fmt.Sprintf(format, args)) + l.callbacks.onLog(l, tags, fmt.Sprintf(format)) } }