From 78edf1c1302666e6b223903684bdc13c77e88b78 Mon Sep 17 00:00:00 2001 From: Russ Magee Date: Fri, 12 Jan 2018 22:47:57 -0800 Subject: [PATCH] Tucked dbg{client/server} away for testing --- demo/{ => client}/client.go | 16 +++--- demo/dbgclient/client.go | 37 +++++++++++++ demo/{ => dbgserver}/server.go | 2 +- demo/server/server.go | 96 ++++++++++++++++++++++++++++++++++ 4 files changed, 142 insertions(+), 9 deletions(-) rename demo/{ => client}/client.go (79%) create mode 100644 demo/dbgclient/client.go rename demo/{ => dbgserver}/server.go (98%) create mode 100644 demo/server/server.go diff --git a/demo/client.go b/demo/client/client.go similarity index 79% rename from demo/client.go rename to demo/client/client.go index 0b1b512..f8fc796 100644 --- a/demo/client.go +++ b/demo/client/client.go @@ -3,6 +3,8 @@ package main import ( "flag" "fmt" + "io" + "os" hkex "blitter.com/herradurakex" ) @@ -17,7 +19,7 @@ func main() { var cAlg string var hAlg string var server string - + flag.StringVar(&cAlg, "c", "C_AES_256", "cipher [\"C_AES_256\" | \"C_TWOFISH_128\" | \"C_BLOWFISH_64\"]") flag.StringVar(&hAlg, "h", "H_SHA256", "hmac [\"H_SHA256\"]") flag.StringVar(&server, "s", "localhost:2000", "server hostname/address[:port]") @@ -25,13 +27,11 @@ func main() { conn, err := hkex.Dial("tcp", server, cAlg, hAlg) if err != nil { - // handle error fmt.Println("Err!") + panic(err) + } + _, err = io.Copy(conn, os.Stdin) + if err != nil && err.Error() != "EOF" { + fmt.Println(err) } - fmt.Fprintf(conn, "\x01\x02\x03\x04") - //fmt.Fprintf(conn, "GET / HTTP/1.0\r\n\r\n") - //status, err := bufio.NewReader(conn).ReadString('\n') - //_, err = bufio.NewReader(conn).ReadString('\n') - // ... - } diff --git a/demo/dbgclient/client.go b/demo/dbgclient/client.go new file mode 100644 index 0000000..f8fc796 --- /dev/null +++ b/demo/dbgclient/client.go @@ -0,0 +1,37 @@ +package main + +import ( + "flag" + "fmt" + "io" + "os" + + hkex "blitter.com/herradurakex" +) + +// Demo of a simple client that dials up to a simple test server to +// send data. +// Note this code is identical to standard tcp client code, save for +// declaring a 'hkex' rather than a 'net' Dialer Conn. The KEx and +// encrypt/decrypt is done within the type. +// Compare to 'clientp.go' in this directory to see the equivalence. +func main() { + var cAlg string + var hAlg string + var server string + + flag.StringVar(&cAlg, "c", "C_AES_256", "cipher [\"C_AES_256\" | \"C_TWOFISH_128\" | \"C_BLOWFISH_64\"]") + flag.StringVar(&hAlg, "h", "H_SHA256", "hmac [\"H_SHA256\"]") + flag.StringVar(&server, "s", "localhost:2000", "server hostname/address[:port]") + flag.Parse() + + conn, err := hkex.Dial("tcp", server, cAlg, hAlg) + if err != nil { + fmt.Println("Err!") + panic(err) + } + _, err = io.Copy(conn, os.Stdin) + if err != nil && err.Error() != "EOF" { + fmt.Println(err) + } +} diff --git a/demo/server.go b/demo/dbgserver/server.go similarity index 98% rename from demo/server.go rename to demo/dbgserver/server.go index 99ccade..295d8cb 100644 --- a/demo/server.go +++ b/demo/dbgserver/server.go @@ -62,7 +62,7 @@ func main() { } }(ch, eCh) - ticker := time.Tick(time.Second) + ticker := time.Tick(time.Second/100) Term: // continuously read from the connection for { diff --git a/demo/server/server.go b/demo/server/server.go new file mode 100644 index 0000000..295d8cb --- /dev/null +++ b/demo/server/server.go @@ -0,0 +1,96 @@ +package main + +import ( + "flag" + "fmt" + "log" + "time" + + hkex "blitter.com/herradurakex" +) + +// Demo of a simple server that listens and spawns goroutines for each +// connecting client. Note this code is identical to standard tcp +// server code, save for declaring 'hkex' rather than 'net' +// Listener and Conns. The KEx and encrypt/decrypt is done within the type. +// Compare to 'serverp.go' in this directory to see the equivalence. +func main() { + var laddr string + + flag.StringVar(&laddr, "l", ":2000", "interface[:port] to listen") + flag.Parse() + + // Listen on TCP port 2000 on all available unicast and + // anycast IP addresses of the local system. + l, err := hkex.Listen("tcp", laddr) + if err != nil { + log.Fatal(err) + } + defer l.Close() + + fmt.Println("Serving on", laddr) + for { + // Wait for a connection. + conn, err := l.Accept() + if err != nil { + log.Fatal(err) + } + + fmt.Println("Accepted client") + + // Handle the connection in a new goroutine. + // The loop then returns to accepting, so that + // multiple connections may be served concurrently. + go func(c hkex.Conn) (e error) { + ch := make(chan []byte) + chN := 0 + eCh := make(chan error) + + // Start a goroutine to read from our net connection + go func(ch chan []byte, eCh chan error) { + for { + // try to read the data + data := make([]byte, 512) + chN, err = c.Read(data) + if err != nil { + // send an error if it's encountered + eCh <- err + return + } + // send data if we read some. + ch <- data[0:chN] + } + }(ch, eCh) + + ticker := time.Tick(time.Second/100) + Term: + // continuously read from the connection + for { + select { + // This case means we recieved data on the connection + case data := <-ch: + // Do something with the data + fmt.Printf("Client sent %+v\n", data[0:chN]) + //fmt.Printf("Client sent %s\n", string(data)) + // This case means we got an error and the goroutine has finished + case err := <-eCh: + // handle our error then exit for loop + if err.Error() == "EOF" { + fmt.Printf("[Client disconnected]\n") + } else { + fmt.Printf("Error reading client data! (%+v)\n", err) + } + break Term + // This will timeout on the read. + case <-ticker: + // do nothing? this is just so we can time out if we need to. + // you probably don't even need to have this here unless you want + // do something specifically on the timeout. + } + } + // Shut down the connection. + c.Close() + return + }(conn) + } +}