pax_global_header00006660000000000000000000000064142017045570014517gustar00rootroot0000000000000052 comment=e94ab5863de68947d6e7bfc6c5504785727ed349 xmppsrv-v0.1.1/000077500000000000000000000000001420170455700134235ustar00rootroot00000000000000xmppsrv-v0.1.1/.gitlab-ci.yml000066400000000000000000000025301420170455700160570ustar00rootroot00000000000000# To contribute improvements to CI/CD templates, please follow the Development guide at: # https://docs.gitlab.com/ee/development/cicd/templates.html # This specific template is located at: # https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Go.gitlab-ci.yml image: golang:latest variables: # Please edit to your GitLab project REPO_NAME: salsa.debian.org/mdosch/xmppsrv # The problem is that to be able to use go get, one needs to put # the repository in the $GOPATH. So for example if your gitlab domain # is gitlab.com, and that your repository is namespace/project, and # the default GOPATH being /go, then you'd need to have your # repository in /go/src/gitlab.com/namespace/project # Thus, making a symbolic link corrects this. before_script: - mkdir -p $GOPATH/src/$(dirname $REPO_NAME) - ln -svf $CI_PROJECT_DIR $GOPATH/src/$REPO_NAME - cd $GOPATH/src/$REPO_NAME stages: - test - release format: stage: test script: - go fmt $(go list ./... | grep -v /vendor/) - go vet $(go list ./... | grep -v /vendor/) - go test -race $(go list ./... | grep -v /vendor/) release: stage: release image: registry.gitlab.com/gitlab-org/release-cli:latest only: - tags script: - | release-cli create --name "Release $CI_COMMIT_TAG" --tag-name $CI_COMMIT_TAG --description="`cat CHANGELOG.md`" xmppsrv-v0.1.1/CHANGELOG.md000066400000000000000000000003141420170455700152320ustar00rootroot00000000000000# Changelog ## [0.1.1] ### Changed - Don't sort SRV records with identical priority by weight. - Fix a typo in error message when no server SRV records are found. ## [0.1.0] ### Added - Initial release xmppsrv-v0.1.1/LICENSE000066400000000000000000000024521420170455700144330ustar00rootroot00000000000000BSD 2-Clause License Copyright (c) 2021, Martin Dosch All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. xmppsrv-v0.1.1/README.md000066400000000000000000000013441420170455700147040ustar00rootroot00000000000000# xmppsrv Lookup xmpp srv records ## usage Import the library into your go project by adding `salsa.debian.org/mdosch/xmppsrv` to your imports. All functions return `[]SRV` with `SRV` being the following struct: ``` type SRV struct { Type string Target string Port uint16 Priority uint16 Weight uint16 } ``` Type is either `xmpp-client`, `xmpps-client`, `xmpp-server` or `xmpps-server`. The functions `LookupXmppServer`, `LookupXmppsServer, `LookupXmppClient` and `LookupXmppsClient` are called with the server name and return the respective SRV records. The function `LookupClient` and `LookupServer` are also called with the server name but return `xmpp` and `xmpps` SRV records ordered by priority and weight. xmppsrv-v0.1.1/go.mod000066400000000000000000000000601420170455700145250ustar00rootroot00000000000000module salsa.debian.org/mdosch/xmppsrv go 1.16 xmppsrv-v0.1.1/xmppsrv.go000066400000000000000000000072401420170455700154740ustar00rootroot00000000000000// Copyright 2021 Martin Dosch. // Use of this source code is governed by the BSD-2-clause // license that can be found in the LICENSE file. package xmppsrv import ( "errors" "math/rand" "net" "sort" "time" ) // The SRV type is basically the same as net.SRV (see // https://pkg.go.dev/net#SRV but it adds the string Type // which contains either xmpp-client, xmpps-client, // xmpp-server or xmpps-server. // This is especially useful for the function LookupXmppClient // which returns a mix of xmpp-client and xmpps-client // records. type SRV struct { Type string Target string Port uint16 Priority uint16 Weight uint16 } type byPriority []SRV func (o byPriority) Len() int { return len(o) } func (o byPriority) Swap(i, j int) { o[i], o[j] = o[j], o[i] } func (o byPriority) Less(i, j int) bool { return o[i].Priority < o[j].Priority } func getSRV(server string, srvType string) ([]SRV, error) { var records []SRV var err error // Look up SRV records. _, addr, err := net.LookupSRV(srvType, "tcp", server) if err != nil { return records, err } if len(addr) > 0 { for _, adr := range addr { records = append(records, SRV{srvType, adr.Target, adr.Port, adr.Priority, adr.Weight}) } } return records, err } // LookupXmppServer returns the xmpp-server SRV records. func LookupXmppServer(server string) ([]SRV, error) { // Look up xmpp-server SRV records, err := getSRV(server, "xmpp-server") return records, err } // LookupXmppsServer returns the xmpps-server SRV records. func LookupXmppsServer(server string) ([]SRV, error) { // Look up xmpps-server SRV records, err := getSRV(server, "xmpps-server") return records, err } // LookupXmppClient returns the xmpp-server SRV records. func LookupXmppClient(server string) ([]SRV, error) { // Look up xmpp-client SRV records, err := getSRV(server, "xmpp-client") return records, err } // LookupXmppsClient returns the xmpp-server SRV records. func LookupXmppsClient(server string) ([]SRV, error) { // Look up xmpps-client SRV records, err := getSRV(server, "xmpps-client") return records, err } // LookupClient returns the xmpp-client and xmpps-client SRV records sorted by // priority and weight. func LookupClient(server string) ([]SRV, error) { var err error rand.Seed(time.Now().UnixNano()) // Look up xmpp-client SRV records, _ := getSRV(server, "xmpp-client") // Look up xmpps-client SRV records. records2, _ := getSRV(server, "xmpps-client") switch rand.Intn(2) { case 0: records = append(records, records2...) case 1: records = append(records2, records...) default: records = append(records, records2...) } switch len(records) { case 0: return records, errors.New("No client records found.") case 1: return records, err default: // Sort xmpp- and xmpps-client SRV records according to the priority // and weight. sort.Sort(byPriority(records)) } return records, err } // LookupServer returns the xmpp-server and xmpps-server SRV records sorted by // priority and weight. func LookupServer(server string) ([]SRV, error) { var err error rand.Seed(time.Now().UnixNano()) // Look up xmpp-client SRV records, _ := getSRV(server, "xmpp-server") // Look up xmpps-client SRV records. records2, _ := getSRV(server, "xmpps-server") switch rand.Intn(2) { case 0: records = append(records, records2...) case 1: records = append(records2, records...) default: records = append(records, records2...) } switch len(records) { case 0: return records, errors.New("No server records found.") case 1: return records, err default: // Sort xmpp- and xmpps-server SRV records according to the priority // and weight. sort.Sort(byPriority(records)) } return records, err }