convert to ListenTCP

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2019-05-09 13:20:57 +00:00
parent f437bfec59
commit 17e5b73aa4
2 changed files with 54 additions and 16 deletions

View File

@ -5,23 +5,24 @@
package dnssecsocket
// import "fmt"
// import "os"
import "os"
import "bufio"
import "math/rand"
import "net"
import "strconv"
import "strings"
// import "time"
import "log"
// import "fmt"
// import "time"
import "git.wit.com/jcarr/shell"
// will try to get this hosts FQDN
// import "github.com/Showmax/go-fqdn"
import "github.com/miekg/dns"
// import "github.com/davecgh/go-spew/spew"
import "github.com/davecgh/go-spew/spew"
const MIN = 1
const MAX = 100
@ -30,8 +31,8 @@ func random() int {
return rand.Intn(MAX-MIN) + MIN
}
func GetRemoteAddr(c net.Conn) string {
clientAddr := c.RemoteAddr().String()
func GetRemoteAddr(conn net.TCPConn) string {
clientAddr := conn.RemoteAddr().String()
parts := strings.Split(clientAddr, "]")
ipv6 := parts[0]
return ipv6[1:]
@ -42,12 +43,19 @@ func GetRemoteAddr(c net.Conn) string {
// Each client must send it's hostname as the first line
// Then each hostname is verified with DNSSEC
//
func HandleConnection(c net.Conn) {
ipv6client := GetRemoteAddr(c)
func HandleConnection(conn *net.TCPConn) {
spew.Dump(conn)
// ipv6client := GetRemoteAddr(c)
ipv6client := conn.RemoteAddr()
log.Println("Serving to %s as the IPv6 client", ipv6client)
// setup this TCP socket as the "standard input"
// newStdin, _ := bufio.NewReader(conn.File())
newStdin, _ := conn.File()
newreader := bufio.NewReader(newStdin)
log.Println("Waiting for the client to tell me its name")
netData, err := bufio.NewReader(c).ReadString('\n')
netData, err := newreader.ReadString('\n')
if err != nil {
log.Println(err)
return
@ -59,18 +67,21 @@ func HandleConnection(c net.Conn) {
ipaddr := dns.Field(dnsRR, 1)
log.Println("Client claims to be: ", ipaddr)
log.Println("Serving to IPv6 client:", ipv6client)
/* TODO: figure out how to fix this check
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()
c.Close()
conn.Close()
return
}
*/
for {
netData, err := bufio.NewReader(c).ReadString('\n')
netData, err := newreader.ReadString('\n')
if err != nil {
log.Println(err)
return
@ -82,8 +93,26 @@ func HandleConnection(c net.Conn) {
}
log.Println("Recieved: ", temp)
if (temp == "list") {
log.Println("Should run list here")
f, _ := conn.File()
shell.SetStdout(f)
shell.Run("ls /tmp/")
// close(f)
shell.SetStdout(os.Stdout)
}
if (temp == "cpuinfo") {
log.Println("Should cat /proc/cpuinfo")
// f, _ := conn.File()
// shell.SetStdout(f)
shell.Run("cat /proc/cpuinfo")
// close(f)
shell.SetStdout(os.Stdout)
}
result := strconv.Itoa(random()) + "\n"
c.Write([]byte(string(result)))
conn.Write([]byte(string(result)))
}
c.Close()
conn.Close()
}

View File

@ -31,6 +31,13 @@ const (
CONN_TYPE = "tcp"
)
/*
go func(c net.Conn) {
defer c.Close()
io.Copy(os.Stdout, c)
}(conn)
*/
func main() {
hostname := fqdn.Get()
log.Println("FQDN hostname is", hostname)
@ -44,8 +51,10 @@ func main() {
listenstr := "[" + ipaddr + "]:" + CONN_PORT
log.Println("listenstr", listenstr)
myTCPAddr, err := net.ResolveTCPAddr("tcp", listenstr)
// // Listen for incoming connections on the IPv6 address only
l, err := net.Listen(CONN_TYPE, listenstr)
l, err := net.ListenTCP("tcp", myTCPAddr)
if err != nil {
log.Println("Error listening:", err.Error())
return
@ -60,7 +69,7 @@ func main() {
for {
// Listen for an incoming connection.
conn, err := l.Accept()
conn, err := l.AcceptTCP()
if err != nil {
log.Println("Error accepting: ", err.Error())
return