From 36bbb829b522082090d4989474f5ec9adab276f5 Mon Sep 17 00:00:00 2001 From: Chteufleur Date: Sat, 16 Jul 2016 09:44:54 +0200 Subject: [PATCH] Auto generation for transaction ID if not provided in HTTP request --- main.go | 5 +++++ xmpp/client.go | 16 ++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/main.go b/main.go index 1b1b9ad..343e6ee 100644 --- a/main.go +++ b/main.go @@ -76,6 +76,11 @@ func request() { client.Domain = getChanString(http.ChanRequest) client.Transaction = getChanString(http.ChanRequest) + if client.Transaction == "" { + // Random transaction ID generation + client.Transaction = xmpp.SessionID() + } + chanResult := <-http.ChanRequest if v, ok := chanResult.(chan bool); ok { client.ChanReply = v diff --git a/xmpp/client.go b/xmpp/client.go index 5bf476d..64aed17 100644 --- a/xmpp/client.go +++ b/xmpp/client.go @@ -3,10 +3,15 @@ package xmpp import ( "git.kingpenguin.tk/chteufleur/go-xmpp.git/src/xmpp" + "crypto/rand" "log" "strconv" ) +const ( + dictionary = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" +) + type Client struct { JID string Method string @@ -49,3 +54,14 @@ func (client *Client) askViaMessage() { WaitMessageAnswers[client.Transaction] = client comp.Out <- m } + +func SessionID() string { + var bytes = make([]byte, 8) + if _, err := rand.Read(bytes); err != nil { + panic(err) + } + for k, v := range bytes { + bytes[k] = dictionary[v%byte(len(dictionary))] + } + return string(bytes) +}