From 3a5e9fce02264eeed884b15781593479fda9296a Mon Sep 17 00:00:00 2001 From: xengineering Date: Tue, 4 Jul 2023 14:25:21 +0200 Subject: Rework routing completely --- xmpp/routing.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 xmpp/routing.go (limited to 'xmpp/routing.go') diff --git a/xmpp/routing.go b/xmpp/routing.go new file mode 100644 index 0000000..e8aa4ed --- /dev/null +++ b/xmpp/routing.go @@ -0,0 +1,21 @@ +package xmpp + +import ( + "encoding/xml" + "log" +) + +func route(s *xml.StartElement, d *xml.Decoder, c chan<- any) { + switch (*s).Name { + case xml.Name{`http://etherx.jabber.org/streams`, `features`}: + data := streamFeatures{} + err := d.DecodeElement(&data, s) + if err != nil { + log.Printf("Could not decode stream features: %v\n", err) + } else { + log.Print(data) // TODO + } + default: + d.Skip() + } +} -- cgit v1.2.3-70-g09d2 From 7902764e053eb3b8b4d46f8d9caf47d2d5cddd7c Mon Sep 17 00:00:00 2001 From: xengineering Date: Tue, 4 Jul 2023 16:26:04 +0200 Subject: Implement XML element parsing with Generic This helps to avoid duplicated code when new XML elements are described as custom structs. --- xmpp/routing.go | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'xmpp/routing.go') diff --git a/xmpp/routing.go b/xmpp/routing.go index e8aa4ed..2b1680f 100644 --- a/xmpp/routing.go +++ b/xmpp/routing.go @@ -8,14 +8,17 @@ import ( func route(s *xml.StartElement, d *xml.Decoder, c chan<- any) { switch (*s).Name { case xml.Name{`http://etherx.jabber.org/streams`, `features`}: - data := streamFeatures{} - err := d.DecodeElement(&data, s) - if err != nil { - log.Printf("Could not decode stream features: %v\n", err) - } else { - log.Print(data) // TODO - } + parse(streamFeatures{}, s, d, c) default: d.Skip() } } + +func parse[T any](data T, s *xml.StartElement, d *xml.Decoder, c chan<- any) { + err := d.DecodeElement(&data, s) + if err != nil { + log.Printf("Could not decode stream features: %v\n", err) + } else { + c <- data + } +} -- cgit v1.2.3-70-g09d2 From e529bab2e5df93ff8e9fd415b9d65e9bb6d17695 Mon Sep 17 00:00:00 2001 From: xengineering Date: Tue, 4 Jul 2023 16:37:27 +0200 Subject: Introduce handle() as dummy --- xmpp/routing.go | 9 +++++++++ xmpp/session.go | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) (limited to 'xmpp/routing.go') diff --git a/xmpp/routing.go b/xmpp/routing.go index 2b1680f..b184b1c 100644 --- a/xmpp/routing.go +++ b/xmpp/routing.go @@ -22,3 +22,12 @@ func parse[T any](data T, s *xml.StartElement, d *xml.Decoder, c chan<- any) { c <- data } } + +func handle(element any) { + switch t := element.(type) { + case streamFeatures: + log.Println("Handling stream features ...") + default: + log.Printf("Unknown parsed element: %v", t) + } +} diff --git a/xmpp/session.go b/xmpp/session.go index 6abc343..a4120e9 100644 --- a/xmpp/session.go +++ b/xmpp/session.go @@ -64,7 +64,7 @@ func (s *session) run() { for { select { case e := <-s.rx: - log.Print(e) + handle(e) case signal := <-s.in: switch signal.(type) { case SessionShouldDisconnect: -- cgit v1.2.3-70-g09d2 From 2c71877e392da6c2691827160142e95142f7bea6 Mon Sep 17 00:00:00 2001 From: xengineering Date: Tue, 4 Jul 2023 21:24:04 +0200 Subject: Re-implement SASL Was broken because of switch to new RX concept. --- xmpp/routing.go | 4 ++-- xmpp/session.go | 2 +- xmpp/streams.go | 21 +++++++++++++++++---- 3 files changed, 20 insertions(+), 7 deletions(-) (limited to 'xmpp/routing.go') diff --git a/xmpp/routing.go b/xmpp/routing.go index b184b1c..a9dd8b6 100644 --- a/xmpp/routing.go +++ b/xmpp/routing.go @@ -23,10 +23,10 @@ func parse[T any](data T, s *xml.StartElement, d *xml.Decoder, c chan<- any) { } } -func handle(element any) { +func handle(s *session, element any) { switch t := element.(type) { case streamFeatures: - log.Println("Handling stream features ...") + handleStreamFeatures(s, t) default: log.Printf("Unknown parsed element: %v", t) } diff --git a/xmpp/session.go b/xmpp/session.go index a4120e9..7a07280 100644 --- a/xmpp/session.go +++ b/xmpp/session.go @@ -64,7 +64,7 @@ func (s *session) run() { for { select { case e := <-s.rx: - handle(e) + handle(s, e) case signal := <-s.in: switch signal.(type) { case SessionShouldDisconnect: diff --git a/xmpp/streams.go b/xmpp/streams.go index 3aca8a2..8f6fd03 100644 --- a/xmpp/streams.go +++ b/xmpp/streams.go @@ -5,6 +5,23 @@ import ( "log" ) +type streamFeatures struct { + SaslMechanisms []string `xml:"mechanisms>mechanism"` +} + +func handleStreamFeatures(s *session, f streamFeatures) { + if len(f.SaslMechanisms) > 0 { + for _, v := range f.SaslMechanisms { + if v == "PLAIN" { + s.sasl() + return + } + } + log.Println("No compatible SASL mechanism found!") + return + } +} + func openStream(e *xml.Encoder, jid string) { start := xml.StartElement{ xml.Name{"jabber:client", "stream:stream"}, @@ -63,7 +80,3 @@ func iqHandler(s *session, e []xml.Token) { } } } - -type streamFeatures struct { - SaslMechanisms []string `xml:"mechanisms>mechanism"` -} -- cgit v1.2.3-70-g09d2 From a23ba089a4e715e68b8c8d4179290692215784a2 Mon Sep 17 00:00:00 2001 From: xengineering Date: Tue, 4 Jul 2023 22:04:05 +0200 Subject: Re-implement resource binding and presence This was removed for refactoring. --- xmpp/jid.go | 27 ++++++++++++--------------- xmpp/routing.go | 8 ++++++++ xmpp/sasl.go | 6 ++++++ xmpp/streams.go | 8 ++++++++ 4 files changed, 34 insertions(+), 15 deletions(-) (limited to 'xmpp/routing.go') diff --git a/xmpp/jid.go b/xmpp/jid.go index 83772fd..c732027 100644 --- a/xmpp/jid.go +++ b/xmpp/jid.go @@ -41,21 +41,6 @@ func username(jid string) string { return "" } -func hasBind(e []xml.Token) bool { - bind := xml.Name{`urn:ietf:params:xml:ns:xmpp-bind`, `bind`} - - for _, v := range e { - switch s := v.(type) { - case xml.StartElement: - if s.Name == bind { - return true - } - } - } - - return false -} - type bindRequest struct { Bind struct { Xmlns string `xml:"xmlns,attr"` @@ -86,3 +71,15 @@ func (s *session) sendBind() { log.Println("Could not encode ressource binding!") } } + +type iqResponse struct { + Jid string `xml:"urn:ietf:params:xml:ns:xmpp-bind bind>jid"` +} + +func handleIqResponse(s *session, i iqResponse) { + if i.Jid != "" { + s.jid = i.Jid + s.sendPresence() + return + } +} diff --git a/xmpp/routing.go b/xmpp/routing.go index a9dd8b6..5cd2040 100644 --- a/xmpp/routing.go +++ b/xmpp/routing.go @@ -9,6 +9,10 @@ func route(s *xml.StartElement, d *xml.Decoder, c chan<- any) { switch (*s).Name { case xml.Name{`http://etherx.jabber.org/streams`, `features`}: parse(streamFeatures{}, s, d, c) + case xml.Name{`urn:ietf:params:xml:ns:xmpp-sasl`, `success`}: + parse(saslSuccess{}, s, d, c) + case xml.Name{`jabber:client`, `iq`}: + parse(iqResponse{}, s, d, c) default: d.Skip() } @@ -27,6 +31,10 @@ func handle(s *session, element any) { switch t := element.(type) { case streamFeatures: handleStreamFeatures(s, t) + case saslSuccess: + handleSaslSuccess(s) + case iqResponse: + handleIqResponse(s, t) default: log.Printf("Unknown parsed element: %v", t) } diff --git a/xmpp/sasl.go b/xmpp/sasl.go index a20ae56..5fac934 100644 --- a/xmpp/sasl.go +++ b/xmpp/sasl.go @@ -10,6 +10,12 @@ type saslRequest struct { Payload []byte `xml:",chardata"` } +type saslSuccess struct {} + +func handleSaslSuccess(s *session) { + openStream(s.tx, s.jid) +} + func (s *session) sasl() { start := xml.StartElement{ xml.Name{"urn:ietf:params:xml:ns:xmpp-sasl", "auth"}, diff --git a/xmpp/streams.go b/xmpp/streams.go index b9cd4cd..18a5e6a 100644 --- a/xmpp/streams.go +++ b/xmpp/streams.go @@ -7,9 +7,12 @@ import ( type streamFeatures struct { SaslMechanisms []string `xml:"urn:ietf:params:xml:ns:xmpp-sasl mechanisms>mechanism"` + Bind *bool `xml:"urn:ietf:params:xml:ns:xmpp-bind bind,omitempty"` } func handleStreamFeatures(s *session, f streamFeatures) { + log.Print(f) + if len(f.SaslMechanisms) > 0 { for _, v := range f.SaslMechanisms { if v == "PLAIN" { @@ -20,6 +23,11 @@ func handleStreamFeatures(s *session, f streamFeatures) { log.Println("No compatible SASL mechanism found!") return } + + if f.Bind != nil { + s.sendBind() + return + } } func openStream(e *xml.Encoder, jid string) { -- cgit v1.2.3-70-g09d2