diff options
author | xengineering <me@xengineering.eu> | 2023-05-21 13:37:13 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2023-05-21 13:37:13 +0200 |
commit | 8fbe57909d87e9c9c99f4fc6fab0e6b7a115e824 (patch) | |
tree | ce04660975f91af1a56bf37c80b816063a38425c | |
parent | 1d46cedcbb92edf69f080bf3dca07509a14159e2 (diff) | |
download | limox-8fbe57909d87e9c9c99f4fc6fab0e6b7a115e824.tar limox-8fbe57909d87e9c9c99f4fc6fab0e6b7a115e824.tar.zst limox-8fbe57909d87e9c9c99f4fc6fab0e6b7a115e824.zip |
Include TCP connection in xmpp.Conn
-rw-r--r-- | xmpp/xmpp.go | 50 |
1 files changed, 33 insertions, 17 deletions
diff --git a/xmpp/xmpp.go b/xmpp/xmpp.go index 1d00739..624090b 100644 --- a/xmpp/xmpp.go +++ b/xmpp/xmpp.go @@ -18,27 +18,54 @@ const ( type Conn struct { ch chan Event jid, pwd string + tcp *tls.Conn } func NewConn(ch chan Event, jid string, pwd string) *Conn { - c := Conn{ch, jid, pwd} + c := Conn{ + ch: ch, + jid: jid, + pwd: pwd} return &c } +func (c *Conn) Connect() error { + var err error + + domain := domainpart(c.jid) + + roots, err := x509.SystemCertPool() + if err != nil { + log.Println(err) + return err + } + + c.tcp, err = tls.Dial("tcp", domain+":"+"5223", &tls.Config{RootCAs: roots}) + if err != nil { + log.Println(err) + return err + } + + return nil +} + +func (c *Conn) Disconnect() { + c.tcp.Close() +} + func (c *Conn) Run() { - conn, err := setupConn(c.jid) + err := c.Connect() if err != nil { - log.Print(err) return } - defer conn.Close() + defer c.Disconnect() - decoder := newDecoder(conn) + decoder := newDecoder(c.tcp) go decoder.run() defer decoder.stop() - enc := newEncoder(conn) + enc := newEncoder(c.tcp) defer enc.Close() tr := newTokenRouter(&enc) @@ -68,17 +95,6 @@ func (c *Conn) Run() { } } -func setupConn(jid string) (*tls.Conn, error) { - domain := domainpart(jid) - - roots, err := x509.SystemCertPool() - if err != nil { - return nil, err - } - - return tls.Dial("tcp", domain+":"+"5223", &tls.Config{RootCAs: roots}) -} - func sendStreamStart(enc *encoder, jid string) xml.EndElement { start := xml.StartElement{ xml.Name{"jabber:client", "stream:stream"}, |