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"
|
2019-05-09 19:24:52 -05:00
|
|
|
import log "github.com/sirupsen/logrus"
|
|
|
|
import "github.com/wercker/journalhook"
|
2019-05-09 08:20:57 -05:00
|
|
|
|
2019-05-09 22:08:22 -05:00
|
|
|
import "git.wit.com/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-09 19:24:52 -05:00
|
|
|
journalhook.Enable()
|
|
|
|
// spew.Dump(conn)
|
2019-05-09 08:20:57 -05:00
|
|
|
// ipv6client := GetRemoteAddr(c)
|
|
|
|
ipv6client := conn.RemoteAddr()
|
2019-05-05 02:00:39 -05:00
|
|
|
log.Println("Serving to %s as the IPv6 client", ipv6client)
|
|
|
|
|
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)
|
|
|
|
|
2019-05-05 02:00:39 -05:00
|
|
|
log.Println("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 {
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
clientHostname := strings.TrimSpace(netData)
|
|
|
|
log.Println("Recieved client hostname as:", clientHostname)
|
|
|
|
|
|
|
|
dnsRR := Dnstrace(clientHostname, "AAAA")
|
2019-05-09 22:42:48 -05:00
|
|
|
if (dnsRR == nil) {
|
|
|
|
log.Println("dnsRR IS NIL")
|
|
|
|
log.Println("dnsRR IS NIL")
|
|
|
|
log.Println("dnsRR IS NIL")
|
|
|
|
conn.Close()
|
|
|
|
return
|
|
|
|
}
|
2019-05-05 02:00:39 -05:00
|
|
|
ipaddr := dns.Field(dnsRR, 1)
|
|
|
|
log.Println("Client claims to be: ", ipaddr)
|
|
|
|
log.Println("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) {
|
|
|
|
log.Println()
|
|
|
|
log.Println("DNSSEC ERROR: client IPv6 does not work")
|
|
|
|
log.Println("DNSSEC ERROR: client IPv6 does not work")
|
|
|
|
log.Println("DNSSEC ERROR: client IPv6 does not work")
|
|
|
|
log.Println()
|
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 {
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
temp := strings.TrimSpace(string(netData))
|
|
|
|
if temp == "STOP" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
log.Println("Recieved: ", temp)
|
|
|
|
|
2019-05-09 08:20:57 -05:00
|
|
|
if (temp == "list") {
|
|
|
|
log.Println("Should run list here")
|
|
|
|
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") {
|
|
|
|
log.Println("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
|
|
|
}
|
|
|
|
}
|