mirror of
https://gogs.blitter.com/RLabs/xs
synced 2024-08-14 10:26:42 +00:00
client/server demo hkex.Dial(), hkex.Listen()/hl.Accept() with auto-KEx
This commit is contained in:
parent
4dd121b10b
commit
60f2cb7e26
5 changed files with 100 additions and 26 deletions
BIN
demo/client
BIN
demo/client
Binary file not shown.
|
@ -2,22 +2,18 @@ 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)
|
||||||
|
|
106
herradurakex.go
106
herradurakex.go
|
@ -47,7 +47,7 @@ func New(i int, p int) (h *HerraduraKEx) {
|
||||||
if i == 0 {
|
if i == 0 {
|
||||||
i = 256
|
i = 256
|
||||||
}
|
}
|
||||||
if p == 0 {
|
if p == 0 {
|
||||||
p = 64
|
p = 64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -143,33 +143,93 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return c coerced into a HKExConn (which implements interface net.Conn)
|
// Dial as net.Dial(), but with implicit HKEx PeerD read on connect
|
||||||
func NewHKExConn(c *net.Conn) (hc *HKExConn) {
|
func Dial(protocol string, ipport string) (hc *HKExConn, err error) {
|
||||||
//fmt.Println("** NewHKExConn wrapping net.Conn **")
|
c, err := net.Dial(protocol, ipport)
|
||||||
hc = new(HKExConn)
|
|
||||||
|
|
||||||
hc.c = *c
|
|
||||||
hc.h = New(0,0)
|
|
||||||
d := big.NewInt(0)
|
|
||||||
_,err := fmt.Fscanln(hc.c, d)
|
|
||||||
if err != nil {
|
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
|
hc.h.PeerD = d
|
||||||
fmt.Printf("** peerD:%s\n", hc.h.PeerD.Text(16))
|
fmt.Printf("**(c)** peerD:%s\n", hc.h.PeerD.Text(16))
|
||||||
|
hc.h.FA()
|
||||||
|
fmt.Printf("**(c)** FA:%s\n", hc.h.fa)
|
||||||
return
|
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) {
|
func (hc HKExConn) Read(b []byte) (n int, err error) {
|
||||||
n, err = hc.c.Read(b)
|
n, err = hc.c.Read(b)
|
||||||
if n > 0 {
|
if n > 0 {
|
||||||
//fmt.Println("** hc.Read() wraps c.Read() **")
|
fmt.Println("** hc.Read() wraps c.Read() **")
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -181,3 +241,21 @@ func (hc HKExConn) Write(b []byte) (n int, err error) {
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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) {
|
||||||
|
hc = new(HKExConn)
|
||||||
|
|
||||||
|
hc.c = *c
|
||||||
|
hc.h = New(0, 0)
|
||||||
|
d := big.NewInt(0)
|
||||||
|
_, err := fmt.Fscanln(hc.c, d)
|
||||||
|
if err != nil {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
hc.h.PeerD = d
|
||||||
|
fmt.Printf("** peerD:%s\n", hc.h.PeerD.Text(16))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue