From 41843133f95e7ca7ec45c40b88c06e35fd7d901e Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Mon, 21 Nov 2016 14:40:32 -0500 Subject: I don't like that socket syntax --- config-sockets.txt | 12 ++--- tls-getcerts.go | 125 +++++++++++++++++++++++++++-------------------------- 2 files changed, 69 insertions(+), 68 deletions(-) diff --git a/config-sockets.txt b/config-sockets.txt index b152146..f121636 100644 --- a/config-sockets.txt +++ b/config-sockets.txt @@ -1,6 +1,6 @@ -tcp:proton.parabola.nu:443 -tcp:proton.parabola.nu:465 -tcp:winston.parabola.nu:443 -tcp:lukeshu.com:443 -tcp:team4272.com:443 -xmpp:parabola.nu:5222 +tcp://proton.parabola.nu:443 +tcp://proton.parabola.nu:465 +tcp://winston.parabola.nu:443 +tcp://lukeshu.com:443 +tcp://team4272.com:443 +tcp://parabola.nu:5222/xmpp diff --git a/tls-getcerts.go b/tls-getcerts.go index 7032199..c78436a 100644 --- a/tls-getcerts.go +++ b/tls-getcerts.go @@ -8,6 +8,7 @@ import ( "fmt" "io" "net" + "net/url" "os" "strings" ) @@ -20,82 +21,82 @@ type xmppTlsProceed struct { XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-tls proceed"` } -func tlsDial(snet, saddr string) (*tls.Conn, error) { - switch snet { - case "tcp": - conn, err := tls.Dial(snet, saddr, &tls.Config{InsecureSkipVerify: true}) - if err != nil { - return nil, err - } - return conn, nil - case "xmpp": - host, _, err := net.SplitHostPort(saddr) - connTCP, err := net.Dial("tcp", saddr) - if err != nil { - return nil, err - } +func xmppStartTLS(connRaw net.Conn, host string) error { + decoder := xml.NewDecoder(connRaw) - decoder := xml.NewDecoder(connTCP) - - // send start - _, err = fmt.Fprintf(connTCP, "", host) - if err != nil { - return nil, err + // send start + _, err := fmt.Fprintf(connRaw, "", host) + if err != nil { + return err + } + // read start + for { + t, err := decoder.Token() + if err != nil || t == nil { + return err } - // read start - for { - t, err := decoder.Token() - if err != nil || t == nil { - return nil, err - } - if se, ok := t.(xml.StartElement); ok { - if se.Name.Local != "stream" { - return nil, xml.UnmarshalError(fmt.Sprintf("expected element of type <%s> but have <%s>", "stream", se.Name.Local)) - } - break + if se, ok := t.(xml.StartElement); ok { + if se.Name.Local != "stream" { + return xml.UnmarshalError(fmt.Sprintf("expected element of type <%s> but have <%s>", "stream", se.Name.Local)) } + break } - // read - var features xmppStreamsFeatures - err = decoder.DecodeElement(&features, nil) - if err != nil { - return nil, err - } - // send - _, err = io.WriteString(connTCP, "") - if err != nil { - return nil, err - } - // read - var proceed xmppTlsProceed - err = decoder.DecodeElement(&proceed, nil) - if err != nil { - return nil, err - } - - connTLS := tls.Client(connTCP, &tls.Config{InsecureSkipVerify: true}) - err = connTLS.Handshake() - if err != nil { - return nil, err - } - return connTLS, nil - default: - return nil, fmt.Errorf("Unknown TLS network: %q", snet) } + // read + var features xmppStreamsFeatures + err = decoder.DecodeElement(&features, nil) + if err != nil { + return err + } + // send + _, err = io.WriteString(connRaw, "") + if err != nil { + return err + } + // read + var proceed xmppTlsProceed + err = decoder.DecodeElement(&proceed, nil) + if err != nil { + return err + } + return nil } func getcert(socket string) (*x509.Certificate, error){ - snet, saddr := split(socket) - host, _, err := net.SplitHostPort(saddr) + u, err := url.Parse(socket) + if err != nil { + return nil, err + } + host, _, err := net.SplitHostPort(u.Host) if err != nil { return nil, err } - conn, err := tlsDial(snet, saddr) + + connRaw, err := net.Dial(u.Scheme, u.Host) if err != nil { return nil, err } - defer conn.Close() - cstate := conn.ConnectionState() + + switch u.Path { + case "", "/": + // do nothing + case "/xmpp": + err = xmppStartTLS(connRaw, host) + if err != nil { + return nil, err + } + default: + return nil, fmt.Errorf("Unknown negotiation path: %q", u.Path) + } + + connTLS := tls.Client(connRaw, &tls.Config{InsecureSkipVerify: true}) + defer connTLS.Close() + err = connTLS.Handshake() + if err != nil { + return nil, err + } + + cstate := connTLS.ConnectionState() opts := x509.VerifyOptions{ DNSName: host, -- cgit v1.2.3-2-g168b