mirror of https://gogs.blitter.com/RLabs/xs
client/server demo hkex.Dial(), hkex.Listen()/hl.Accept() with auto-KEx
This commit is contained in:
parent
4dd121b10b
commit
60f2cb7e26
BIN
demo/client
BIN
demo/client
Binary file not shown.
|
@ -2,20 +2,16 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
|
||||||
|
|
||||||
hkex "blitter.com/herradurakex"
|
hkex "blitter.com/herradurakex"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
bareconn, err := net.Dial("tcp", "localhost:2000")
|
conn, err := hkex.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.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')
|
||||||
|
|
BIN
demo/server
BIN
demo/server
Binary file not shown.
|
@ -4,14 +4,14 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
//"net"
|
||||||
// "blitter.com/herradurakex"
|
hkex "blitter.com/herradurakex"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Listen on TCP port 2000 on all available unicast and
|
// Listen on TCP port 2000 on all available unicast and
|
||||||
// anycast IP addresses of the local system.
|
// anycast IP addresses of the local system.
|
||||||
l, err := net.Listen("tcp", ":2000")
|
l, err := hkex.Listen("tcp", ":2000")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -30,7 +30,7 @@ func main() {
|
||||||
// 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.
|
||||||
go func(c net.Conn) {
|
go func(c hkex.HKExConn) {
|
||||||
// Echo all incoming data.
|
// Echo all incoming data.
|
||||||
io.Copy(c, c)
|
io.Copy(c, c)
|
||||||
fmt.Println("Client sent:%v\n", c)
|
fmt.Println("Client sent:%v\n", c)
|
||||||
|
|
112
herradurakex.go
112
herradurakex.go
|
@ -143,15 +143,109 @@ func (h *HerraduraKEx) String() string {
|
||||||
h.PeerD.Text(16),
|
h.PeerD.Text(16),
|
||||||
h.fa.Text(16))
|
h.fa.Text(16))
|
||||||
}
|
}
|
||||||
|
/*---------------------------------------------------------------------*/
|
||||||
|
|
||||||
type HKExConn struct {
|
type HKExConn struct {
|
||||||
c net.Conn // which also implements io.Reader, io.Writer, ...
|
c net.Conn // which also implements io.Reader, io.Writer, ...
|
||||||
h *HerraduraKEx
|
h *HerraduraKEx
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Dial as net.Dial(), but with implicit HKEx PeerD read on connect
|
||||||
|
func Dial(protocol string, ipport string) (hc *HKExConn, err error) {
|
||||||
|
c, err := net.Dial(protocol, ipport)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
hc = &HKExConn{c, New(0, 0)}
|
||||||
|
|
||||||
|
// Stub KEx
|
||||||
|
fmt.Fprintf(c, "0x%s\n", hc.h.d.Text(16))
|
||||||
|
|
||||||
|
d := big.NewInt(0)
|
||||||
|
_, err = fmt.Fscanln(c, d)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
hc.h.PeerD = d
|
||||||
|
fmt.Printf("**(c)** peerD:%s\n", hc.h.PeerD.Text(16))
|
||||||
|
hc.h.FA()
|
||||||
|
fmt.Printf("**(c)** FA:%s\n", hc.h.fa)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (hc *HKExConn) Close() (err error) {
|
||||||
|
err = hc.c.Close()
|
||||||
|
fmt.Println("[Conn Closing]")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
/*---------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
type HKExListener struct {
|
||||||
|
l net.Listener
|
||||||
|
}
|
||||||
|
|
||||||
|
func Listen(protocol string, ipport string) (hl HKExListener, e error) {
|
||||||
|
l, err := net.Listen(protocol, ipport)
|
||||||
|
if err != nil {
|
||||||
|
return HKExListener{nil}, err
|
||||||
|
}
|
||||||
|
fmt.Println("[Listening]")
|
||||||
|
hl.l = l
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (hl *HKExListener) Close() {
|
||||||
|
hl.l.Close()
|
||||||
|
fmt.Println("[Listener Closed]")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (hl *HKExListener) Accept() (hc HKExConn, err error) {
|
||||||
|
c, err := hl.l.Accept()
|
||||||
|
|
||||||
|
fmt.Println("[Accepted]")
|
||||||
|
if err != nil {
|
||||||
|
return HKExConn{nil, nil}, err
|
||||||
|
}
|
||||||
|
hc = HKExConn{c, New(0, 0)}
|
||||||
|
|
||||||
|
d := big.NewInt(0)
|
||||||
|
_, err = fmt.Fscanln(c, d)
|
||||||
|
if err != nil {
|
||||||
|
return hc, err
|
||||||
|
}
|
||||||
|
hc.h.PeerD = d
|
||||||
|
fmt.Printf("**(s)** peerD:%s\n", hc.h.PeerD.Text(16))
|
||||||
|
hc.h.FA()
|
||||||
|
fmt.Printf("**(s)** FA:%s\n", hc.h.fa)
|
||||||
|
|
||||||
|
// Stub KEx
|
||||||
|
fmt.Fprintf(c, "0x%s\n", hc.h.d.Text(16))
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
/*---------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
// Return c coerced into a HKExConn (which implements interface net.Conn)
|
// Return c coerced into a HKExConn (which implements interface net.Conn)
|
||||||
|
// Only useful if one wants to convert an open connection later to HKEx
|
||||||
|
// (Use Dial() instead to start with HKEx automatically.)
|
||||||
func NewHKExConn(c *net.Conn) (hc *HKExConn) {
|
func NewHKExConn(c *net.Conn) (hc *HKExConn) {
|
||||||
//fmt.Println("** NewHKExConn wrapping net.Conn **")
|
|
||||||
hc = new(HKExConn)
|
hc = new(HKExConn)
|
||||||
|
|
||||||
hc.c = *c
|
hc.c = *c
|
||||||
|
@ -165,19 +259,3 @@ func NewHKExConn(c *net.Conn) (hc *HKExConn) {
|
||||||
fmt.Printf("** peerD:%s\n", hc.h.PeerD.Text(16))
|
fmt.Printf("** peerD:%s\n", hc.h.PeerD.Text(16))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue