File: dns.go

package info (click to toggle)
coyim 0.3.7-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 4,064 kB
  • ctags: 4,528
  • sloc: xml: 5,120; sh: 328; python: 286; makefile: 235; ruby: 51
file content (56 lines) | stat: -rw-r--r-- 1,540 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package xmpp implements the XMPP IM protocol, as specified in RFC 6120 and
// 6121.
package xmpp

import (
	"errors"
	"net"
	"strconv"
	"strings"

	"golang.org/x/net/proxy"
	ourNet "github.com/twstrike/coyim/net"
)

var (
	// ErrServiceNotAvailable means that the service is decidedly not available at this domain
	ErrServiceNotAvailable = errors.New("service not available")
)

// Resolve performs a DNS SRV lookup for the XMPP server that serves the given
// domain.
func Resolve(domain string) (hostport []string, err error) {
	return massage(net.LookupSRV("xmpp-client", "tcp", domain))
}

// ResolveSRVWithProxy performs a DNS SRV lookup for the xmpp server that serves the given domain over the given proxy
func ResolveSRVWithProxy(proxy proxy.Dialer, domain string) (hostport []string, err error) {
	return massage(ourNet.LookupSRV(proxy, "xmpp-client", "tcp", domain))
}

func massage(cname string, addrs []*net.SRV, err error) ([]string, error) {
	if err != nil {
		return nil, err
	}

	// https://xmpp.org/rfcs/rfc6120.html#tcp-resolution-prefer
	if len(addrs) == 1 && addrs[0].Target == "." {
		return nil, ErrServiceNotAvailable
	}

	ret := make([]string, 0, len(addrs))
	for _, addr := range addrs {
		hostport := net.JoinHostPort(
			strings.TrimSuffix(addr.Target, "."),
			strconv.Itoa(int(addr.Port)),
		)

		ret = append(ret, hostport)
	}

	return ret, nil
}