From c88ec7a32dcb6bf7b10b1a84527aed6315cc82f5 Mon Sep 17 00:00:00 2001 From: Chteufleur Date: Sat, 23 Jul 2016 12:03:21 +0200 Subject: [PATCH] Add the possibility to connect as a client --- httpAuth.cfg | 7 ++++++- main.go | 12 ++++-------- xmpp/confirmation.go | 7 ------- xmpp/xmpp.go | 34 ++++++++++++++++++++++++++++------ 4 files changed, 38 insertions(+), 22 deletions(-) diff --git a/httpAuth.cfg b/httpAuth.cfg index 6c962ee..54fb7c8 100644 --- a/httpAuth.cfg +++ b/httpAuth.cfg @@ -1,10 +1,15 @@ -# XMPP informations +# XMPP informations (component) xmpp_server_address=192.168.1.2 xmpp_server_port=5347 xmpp_hostname=xmppsteam.kingpenguin.tk xmpp_secret=xmpp4steam_password xmpp_debug=true +# XMPP informations (client) +#xmpp_hostname=toto@jabber.fr +#xmpp_secret=totoPwd +#xmpp_debug=true + # HTTP informations http_port=9090 https_port=9093 diff --git a/main.go b/main.go index b671766..03d77c1 100644 --- a/main.go +++ b/main.go @@ -17,9 +17,6 @@ import ( const ( Version = "v0.3.1" configurationFilePath = "httpAuth.cfg" - - default_xmpp_server_address = "127.0.0.1" - default_xmpp_server_port = "5347" ) var ( @@ -55,15 +52,14 @@ func init() { // XMPP config xmpp_server_address := mapConfig["xmpp_server_address"] - if xmpp_server_address == "" { - xmpp_server_address = default_xmpp_server_address + if xmpp_server_address != "" { + xmpp.Addr = xmpp_server_address } xmpp_server_port := mapConfig["xmpp_server_port"] - if xmpp_server_port == "" { - xmpp_server_port = default_xmpp_server_port + if xmpp_server_port != "" { + xmpp.Port = xmpp_server_port } - xmpp.Addr = xmpp_server_address + ":" + xmpp_server_port xmpp.JidStr = mapConfig["xmpp_hostname"] xmpp.Secret = mapConfig["xmpp_secret"] xmpp.Debug = mapConfig["xmpp_debug"] == "true" diff --git a/xmpp/confirmation.go b/xmpp/confirmation.go index 2885147..77a7901 100644 --- a/xmpp/confirmation.go +++ b/xmpp/confirmation.go @@ -31,13 +31,6 @@ type Confirmation struct { ChanReply chan string } -/* -type Transport interface { - Run() - Out(chan interface{}) -} -*/ - func (confirmation *Confirmation) SendConfirmation() { log.Printf("%sQuery JID %s", LogInfo, confirmation.JID) isAutoGeneratedTranctionID := false diff --git a/xmpp/xmpp.go b/xmpp/xmpp.go index 06208e1..e280e68 100644 --- a/xmpp/xmpp.go +++ b/xmpp/xmpp.go @@ -8,13 +8,14 @@ import ( ) const ( - LogInfo = "\t[XMPP COMPONENT INFO]\t" - LogError = "\t[XMPP COMPONENT ERROR]\t" - LogDebug = "\t[XMPP COMPONENT DEBUG]\t" + LogInfo = "\t[XMPP INFO]\t" + LogError = "\t[XMPP ERROR]\t" + LogDebug = "\t[XMPP DEBUG]\t" ) var ( - Addr = "127.0.0.1:5347" + Addr = "127.0.0.1" + Port = "5347" JidStr = "" Secret = "" @@ -35,11 +36,32 @@ var ( ) func Run() { + var addr string + var isComponent bool + log.Printf("%sRunning", LogInfo) // Create stream and configure it as a component connection. jid = must(xmpp.ParseJID(JidStr)).(xmpp.JID) - stream = must(xmpp.NewStream(Addr, &xmpp.StreamConfig{LogStanzas: Debug})).(*xmpp.Stream) - comp = must(xmpp.NewComponentXMPP(stream, jid, Secret)).(*xmpp.XMPP) + isComponent = jid.Node == "" + + if isComponent { + // component + addr = Addr + ":" + Port + } else { + // client + addrs := must(xmpp.HomeServerAddrs(jid)).([]string) + addr = addrs[0] + } + + log.Printf("%sConnecting to %s", LogInfo, addr) + stream = must(xmpp.NewStream(addr, &xmpp.StreamConfig{LogStanzas: Debug})).(*xmpp.Stream) + + if isComponent { + comp = must(xmpp.NewComponentXMPP(stream, jid, Secret)).(*xmpp.XMPP) + } else { + comp = must(xmpp.NewClientXMPP(stream, jid, Secret, &xmpp.ClientConfig{InsecureSkipVerify: false})).(*xmpp.XMPP) + comp.Out <- xmpp.Presence{} + } mainXMPP() log.Printf("%sReach main method's end", LogInfo)