2019-05-05 02:00:39 -05:00
|
|
|
// inspired from:
|
|
|
|
// https://github.com/mactsouk/opensource.com.git
|
|
|
|
// and
|
|
|
|
// https://coderwall.com/p/wohavg/creating-a-simple-tcp-server-in-go
|
|
|
|
|
|
|
|
package dnssecsocket
|
|
|
|
|
2019-05-09 08:20:57 -05:00
|
|
|
import "os"
|
2019-05-05 02:00:39 -05:00
|
|
|
import "bufio"
|
|
|
|
import "math/rand"
|
|
|
|
import "net"
|
|
|
|
import "strconv"
|
|
|
|
import "strings"
|
2023-02-20 22:22:58 -06:00
|
|
|
// import log "github.com/sirupsen/logrus"
|
2019-05-24 23:41:19 -05:00
|
|
|
// import "github.com/wercker/journalhook"
|
2019-05-09 08:20:57 -05:00
|
|
|
|
2021-08-09 00:46:00 -05:00
|
|
|
import "git.wit.org/wit/shell"
|
2019-05-05 02:00:39 -05:00
|
|
|
|
|
|
|
// will try to get this hosts FQDN
|
|
|
|
// import "github.com/Showmax/go-fqdn"
|
|
|
|
|
|
|
|
import "github.com/miekg/dns"
|
|
|
|
|
2019-05-09 19:24:52 -05:00
|
|
|
// import "github.com/davecgh/go-spew/spew"
|
2019-05-05 02:00:39 -05:00
|
|
|
|
|
|
|
const MIN = 1
|
|
|
|
const MAX = 100
|
|
|
|
|
|
|
|
func random() int {
|
|
|
|
return rand.Intn(MAX-MIN) + MIN
|
|
|
|
}
|
|
|
|
|
2019-05-09 08:20:57 -05:00
|
|
|
func GetRemoteAddr(conn net.TCPConn) string {
|
|
|
|
clientAddr := conn.RemoteAddr().String()
|
2019-05-05 02:00:39 -05:00
|
|
|
parts := strings.Split(clientAddr, "]")
|
|
|
|
ipv6 := parts[0]
|
|
|
|
return ipv6[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Handle each connection
|
|
|
|
// Each client must send it's hostname as the first line
|
|
|
|
// Then each hostname is verified with DNSSEC
|
|
|
|
//
|
2019-05-09 08:20:57 -05:00
|
|
|
func HandleConnection(conn *net.TCPConn) {
|
2019-05-24 23:41:19 -05:00
|
|
|
// Disable journalhook until it builds on Windows
|
|
|
|
// journalhook.Enable()
|
|
|
|
|
2019-05-09 19:24:52 -05:00
|
|
|
// spew.Dump(conn)
|
2019-05-09 08:20:57 -05:00
|
|
|
// ipv6client := GetRemoteAddr(c)
|
|
|
|
ipv6client := conn.RemoteAddr()
|
2023-02-20 22:22:58 -06:00
|
|
|
log(args.VerboseDnssec, "Serving to %s as the IPv6 client", ipv6client)
|
2019-05-24 23:41:19 -05:00
|
|
|
|
2019-05-09 08:20:57 -05:00
|
|
|
// setup this TCP socket as the "standard input"
|
|
|
|
// newStdin, _ := bufio.NewReader(conn.File())
|
|
|
|
newStdin, _ := conn.File()
|
|
|
|
newreader := bufio.NewReader(newStdin)
|
|
|
|
|
2023-02-20 22:22:58 -06:00
|
|
|
log(args.VerboseDnssec, "Waiting for the client to tell me its name")
|
2019-05-09 08:20:57 -05:00
|
|
|
netData, err := newreader.ReadString('\n')
|
2019-05-05 02:00:39 -05:00
|
|
|
if err != nil {
|
2023-02-20 22:22:58 -06:00
|
|
|
log(args.VerboseDnssec, err)
|
2019-05-05 02:00:39 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
clientHostname := strings.TrimSpace(netData)
|
2023-02-20 22:22:58 -06:00
|
|
|
log(args.VerboseDnssec, "Recieved client hostname as:", clientHostname)
|
2019-05-05 02:00:39 -05:00
|
|
|
|
|
|
|
dnsRR := Dnstrace(clientHostname, "AAAA")
|
2019-05-09 22:42:48 -05:00
|
|
|
if (dnsRR == nil) {
|
2023-02-20 22:22:58 -06:00
|
|
|
log(args.VerboseDnssec, "dnsRR IS NIL")
|
|
|
|
log(args.VerboseDnssec, "dnsRR IS NIL")
|
|
|
|
log(args.VerboseDnssec, "dnsRR IS NIL")
|
2019-05-09 22:42:48 -05:00
|
|
|
conn.Close()
|
|
|
|
return
|
|
|
|
}
|
2023-02-19 02:20:09 -06:00
|
|
|
ipaddr := dns.Field(dnsRR[1], 1)
|
2023-02-20 22:22:58 -06:00
|
|
|
log(args.VerboseDnssec, "Client claims to be: ", ipaddr)
|
|
|
|
log(args.VerboseDnssec, "Serving to IPv6 client:", ipv6client)
|
2019-05-09 08:20:57 -05:00
|
|
|
|
|
|
|
/* TODO: figure out how to fix this check
|
2019-05-05 02:00:39 -05:00
|
|
|
if (ipaddr != ipv6client) {
|
2023-02-20 22:22:58 -06:00
|
|
|
log(args.VerboseDnssec)
|
|
|
|
log(args.VerboseDnssec, "DNSSEC ERROR: client IPv6 does not work")
|
|
|
|
log(args.VerboseDnssec, "DNSSEC ERROR: client IPv6 does not work")
|
|
|
|
log(args.VerboseDnssec, "DNSSEC ERROR: client IPv6 does not work")
|
|
|
|
log(args.VerboseDnssec)
|
2019-05-09 08:20:57 -05:00
|
|
|
conn.Close()
|
2019-05-05 02:00:39 -05:00
|
|
|
return
|
|
|
|
}
|
2019-05-09 08:20:57 -05:00
|
|
|
*/
|
2019-05-05 02:00:39 -05:00
|
|
|
|
2019-05-09 19:24:52 -05:00
|
|
|
f, _ := conn.File()
|
|
|
|
// shell.SetStdout(f)
|
|
|
|
// shell.SpewOn() // turn this on if you want to look at the process exit states
|
|
|
|
|
|
|
|
// send all log() output to systemd journalctl
|
|
|
|
// shell.UseJournalctl()
|
|
|
|
|
2019-05-05 02:00:39 -05:00
|
|
|
for {
|
2019-05-09 19:24:52 -05:00
|
|
|
defer shell.SetStdout(os.Stdout)
|
|
|
|
defer conn.Close()
|
2019-05-09 08:20:57 -05:00
|
|
|
netData, err := newreader.ReadString('\n')
|
2019-05-05 02:00:39 -05:00
|
|
|
if err != nil {
|
2023-02-20 22:22:58 -06:00
|
|
|
log(args.VerboseDnssec, err)
|
2019-05-05 02:00:39 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
temp := strings.TrimSpace(string(netData))
|
|
|
|
if temp == "STOP" {
|
|
|
|
break
|
|
|
|
}
|
2023-02-20 22:22:58 -06:00
|
|
|
log(args.VerboseDnssec, "Recieved: ", temp)
|
2019-05-05 02:00:39 -05:00
|
|
|
|
2019-05-09 08:20:57 -05:00
|
|
|
if (temp == "list") {
|
2023-02-20 22:22:58 -06:00
|
|
|
log(args.VerboseDnssec, "Should run list here")
|
2019-05-09 08:20:57 -05:00
|
|
|
shell.SetStdout(f)
|
2019-05-09 19:24:52 -05:00
|
|
|
shell.Run("/root/bin/list.testing.com")
|
2019-05-09 08:20:57 -05:00
|
|
|
shell.SetStdout(os.Stdout)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (temp == "cpuinfo") {
|
2023-02-20 22:22:58 -06:00
|
|
|
log(args.VerboseDnssec, "Should cat /proc/cpuinfo")
|
2019-05-09 19:24:52 -05:00
|
|
|
shell.SetStdout(f)
|
2019-05-09 08:20:57 -05:00
|
|
|
shell.Run("cat /proc/cpuinfo")
|
|
|
|
shell.SetStdout(os.Stdout)
|
|
|
|
}
|
|
|
|
|
2019-05-05 02:00:39 -05:00
|
|
|
result := strconv.Itoa(random()) + "\n"
|
2019-05-09 08:20:57 -05:00
|
|
|
conn.Write([]byte(string(result)))
|
2019-05-05 02:00:39 -05:00
|
|
|
}
|
|
|
|
}
|