Basically nothing just moving stuff out of main.go
This commit is contained in:
parent
50d7b9dc2a
commit
ebec00a4cf
2 changed files with 78 additions and 72 deletions
75
awl.go
75
awl.go
|
@ -1,20 +1,16 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/c-robinson/iplib"
|
||||
"example.com/awl/util"
|
||||
"github.com/miekg/dns"
|
||||
"github.com/urfave/cli/v2"
|
||||
"golang.org/x/net/idna"
|
||||
|
@ -126,7 +122,6 @@ func main() {
|
|||
},
|
||||
Action: func(c *cli.Context) error {
|
||||
var err error
|
||||
|
||||
req := parseArgs(c.Args().Slice())
|
||||
// Set DNS-over-TLS, if enabled
|
||||
port := c.Int("port")
|
||||
|
@ -151,10 +146,7 @@ func main() {
|
|||
if dns.TypeToString[req.request] == "A" {
|
||||
req.request = dns.StringToType["PTR"]
|
||||
}
|
||||
req.name, err = reverseDNS(req.name, dns.TypeToString[req.request])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.name, err = util.ReverseDNS(req.name, dns.TypeToString[req.request])
|
||||
}
|
||||
|
||||
// if the domain is not canonical, make it canonical
|
||||
|
@ -177,7 +169,7 @@ func main() {
|
|||
|
||||
// Make the DNS request
|
||||
if c.Bool("https") {
|
||||
in, err = resolveHTTPS(msg, req.server)
|
||||
in, err = util.ResolveHTTPS(msg, req.server)
|
||||
} else if c.Bool("quic") {
|
||||
return fmt.Errorf("quic: not yet implemented")
|
||||
} else {
|
||||
|
@ -311,64 +303,3 @@ func parseArgs(args []string) request {
|
|||
|
||||
return request{server: server, request: req, name: name}
|
||||
}
|
||||
|
||||
func reverseDNS(dom string, q string) (string, error) {
|
||||
if q == "PTR" {
|
||||
if strings.Contains(dom, ".") {
|
||||
// It's an IPv4 address
|
||||
ip := net.ParseIP(dom)
|
||||
if ip != nil {
|
||||
return iplib.IP4ToARPA(ip), nil
|
||||
} else {
|
||||
return "", errors.New("error: Could not parse IPv4 address")
|
||||
}
|
||||
|
||||
} else if strings.Contains(dom, ":") {
|
||||
// It's an IPv6 address
|
||||
ip := net.ParseIP(dom)
|
||||
if ip != nil {
|
||||
return iplib.IP6ToARPA(ip), nil
|
||||
} else {
|
||||
return "", errors.New("error: Could not parse IPv6 address")
|
||||
}
|
||||
}
|
||||
}
|
||||
return "", errors.New("error: -x flag given but no IP found")
|
||||
}
|
||||
|
||||
func resolveHTTPS(msg *dns.Msg, server string) (*dns.Msg, error) {
|
||||
httpR := &http.Client{}
|
||||
buf, err := msg.Pack()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
query := server + "?dns=" + base64.RawURLEncoding.EncodeToString(buf)
|
||||
req, err := http.NewRequest("GET", query, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Accept", "application/dns-message")
|
||||
|
||||
res, err := httpR.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
if res.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("bad HTTP Request: %d", res.StatusCode)
|
||||
}
|
||||
|
||||
fullRes, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
response := dns.Msg{}
|
||||
err = response.Unpack(fullRes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
|
||||
}
|
||||
|
|
75
util/dns.go
Normal file
75
util/dns.go
Normal file
|
@ -0,0 +1,75 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/c-robinson/iplib"
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
func ReverseDNS(dom string, q string) (string, error) {
|
||||
if q == "PTR" {
|
||||
if strings.Contains(dom, ".") {
|
||||
// It's an IPv4 address
|
||||
ip := net.ParseIP(dom)
|
||||
if ip != nil {
|
||||
return iplib.IP4ToARPA(ip), nil
|
||||
} else {
|
||||
return "", errors.New("error: Could not parse IPv4 address")
|
||||
}
|
||||
|
||||
} else if strings.Contains(dom, ":") {
|
||||
// It's an IPv6 address
|
||||
ip := net.ParseIP(dom)
|
||||
if ip != nil {
|
||||
return iplib.IP6ToARPA(ip), nil
|
||||
} else {
|
||||
return "", errors.New("error: Could not parse IPv6 address")
|
||||
}
|
||||
}
|
||||
}
|
||||
return "", errors.New("error: -x flag given but no IP found")
|
||||
}
|
||||
|
||||
func ResolveHTTPS(msg *dns.Msg, server string) (*dns.Msg, error) {
|
||||
httpR := &http.Client{}
|
||||
buf, err := msg.Pack()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
query := server + "?dns=" + base64.RawURLEncoding.EncodeToString(buf)
|
||||
req, err := http.NewRequest("GET", query, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Accept", "application/dns-message")
|
||||
|
||||
res, err := httpR.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
if res.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("bad HTTP Request: %d", res.StatusCode)
|
||||
}
|
||||
|
||||
fullRes, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
response := dns.Msg{}
|
||||
err = response.Unpack(fullRes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
|
||||
}
|
Loading…
Reference in a new issue