HKExConn captures net.Conn

This commit is contained in:
Russ Magee 2018-01-06 20:26:08 +00:00
parent 663f2f6d1f
commit c8b4fa3596
5 changed files with 38 additions and 4 deletions

View file

@ -20,6 +20,7 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
hkex "blitter.com/herradurakex" hkex "blitter.com/herradurakex"
) )

Binary file not shown.

View file

@ -4,13 +4,19 @@ import (
"bufio" "bufio"
"fmt" "fmt"
"net" "net"
hkex "blitter.com/herradurakex"
) )
func main() { func main() {
conn, err := net.Dial("tcp", "localhost:2000") bareconn, err := net.Dial("tcp", "localhost:2000")
if err != nil { if err != nil {
// handle error // handle error
} }
conn := hkex.NewHKExConn(&bareconn)
fmt.Printf("conn: %v\n", conn)
fmt.Fprintf(conn, "GET / HTTP/1.0\r\n\r\n") fmt.Fprintf(conn, "GET / HTTP/1.0\r\n\r\n")
// status, err := bufio.NewReader(conn).ReadString('\n') // status, err := bufio.NewReader(conn).ReadString('\n')
_, err = bufio.NewReader(conn).ReadString('\n') _, err = bufio.NewReader(conn).ReadString('\n')

View file

@ -5,6 +5,7 @@ import (
"io" "io"
"log" "log"
"net" "net"
// "blitter.com/herradurakex"
) )
func main() { func main() {
@ -26,7 +27,6 @@ func main() {
fmt.Println("Accepted client") fmt.Println("Accepted client")
// Handle the connection in a new goroutine. // Handle the connection in a new goroutine.
// The loop then returns to accepting, so that // The loop then returns to accepting, so that
// multiple connections may be served concurrently. // multiple connections may be served concurrently.

View file

@ -21,6 +21,7 @@ import (
"fmt" "fmt"
"math/big" "math/big"
"math/rand" "math/rand"
"net"
"time" "time"
) )
@ -130,3 +131,29 @@ func (h *HerraduraKEx) String() string {
h.PeerD.Text(16), h.PeerD.Text(16),
h.fa.Text(16)) h.fa.Text(16))
} }
type HKExConn struct {
c net.Conn
}
// Return c coerced into a HKExConn (which implements interface net.Conn)
func NewHKExConn(c *net.Conn) (hc *HKExConn) {
fmt.Println("** NewHKExConn wrapping net.Conn **")
return &HKExConn{*c}
}
func (hc HKExConn) Read(b []byte) (n int, err error) {
n, err = hc.c.Read(b)
if n > 0 {
fmt.Println("** hc.Read() wraps c.Read() **")
}
return
}
func (hc HKExConn) Write(b []byte) (n int, err error) {
n, err = hc.c.Write(b)
if n > 0 {
fmt.Printf("** hc.Write('%s') wraps c.Write() **\n", b)
}
return
}