mirror of https://gogs.blitter.com/RLabs/xs
HKExConn -> Conn for drop-in to net.Conn
This commit is contained in:
parent
11cd7bacfb
commit
2faee8eae1
BIN
demo/client
BIN
demo/client
Binary file not shown.
|
@ -12,8 +12,6 @@ func main() {
|
||||||
// handle error
|
// handle error
|
||||||
fmt.Println("Err!")
|
fmt.Println("Err!")
|
||||||
}
|
}
|
||||||
//fmt.Printf("conn: %v\n", conn)
|
|
||||||
|
|
||||||
fmt.Fprintf(conn, "\x01\x02\x03\x04")
|
fmt.Fprintf(conn, "\x01\x02\x03\x04")
|
||||||
//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')
|
||||||
|
|
BIN
demo/server
BIN
demo/server
Binary file not shown.
|
@ -4,7 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
//"net"
|
|
||||||
hkex "blitter.com/herradurakex"
|
hkex "blitter.com/herradurakex"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -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 hkex.HKExConn) (e error) {
|
go func(c hkex.Conn) (e error) {
|
||||||
ch := make(chan []byte)
|
ch := make(chan []byte)
|
||||||
chN := 0
|
chN := 0
|
||||||
eCh := make(chan error)
|
eCh := make(chan error)
|
||||||
|
|
|
@ -146,18 +146,18 @@ func (h *HerraduraKEx) String() string {
|
||||||
|
|
||||||
/*---------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------*/
|
||||||
|
|
||||||
type HKExConn struct {
|
type Conn 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
|
// Dial as net.Dial(), but with implicit HKEx PeerD read on connect
|
||||||
func Dial(protocol string, ipport string) (hc *HKExConn, err error) {
|
func Dial(protocol string, ipport string) (hc *Conn, err error) {
|
||||||
c, err := net.Dial(protocol, ipport)
|
c, err := net.Dial(protocol, ipport)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
hc = &HKExConn{c, New(0, 0)}
|
hc = &Conn{c, New(0, 0)}
|
||||||
|
|
||||||
// KEx
|
// KEx
|
||||||
fmt.Fprintf(c, "0x%s\n", hc.h.d.Text(16))
|
fmt.Fprintf(c, "0x%s\n", hc.h.d.Text(16))
|
||||||
|
@ -175,7 +175,7 @@ func Dial(protocol string, ipport string) (hc *HKExConn, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hc *HKExConn) Close() (err error) {
|
func (hc *Conn) Close() (err error) {
|
||||||
err = hc.c.Close()
|
err = hc.c.Close()
|
||||||
fmt.Println("[Conn Closing]")
|
fmt.Println("[Conn Closing]")
|
||||||
return
|
return
|
||||||
|
@ -202,14 +202,14 @@ func (hl *HKExListener) Close() {
|
||||||
fmt.Println("[Listener Closed]")
|
fmt.Println("[Listener Closed]")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hl *HKExListener) Accept() (hc HKExConn, err error) {
|
func (hl *HKExListener) Accept() (hc Conn, err error) {
|
||||||
c, err := hl.l.Accept()
|
c, err := hl.l.Accept()
|
||||||
|
|
||||||
fmt.Println("[Accepted]")
|
fmt.Println("[Accepted]")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return HKExConn{nil, nil}, err
|
return Conn{nil, nil}, err
|
||||||
}
|
}
|
||||||
hc = HKExConn{c, New(0, 0)}
|
hc = Conn{c, New(0, 0)}
|
||||||
|
|
||||||
d := big.NewInt(0)
|
d := big.NewInt(0)
|
||||||
_, err = fmt.Fscanln(c, d)
|
_, err = fmt.Fscanln(c, d)
|
||||||
|
@ -230,7 +230,7 @@ func (hl *HKExListener) Accept() (hc HKExConn, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/*---------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------*/
|
||||||
func (hc HKExConn) Read(b []byte) (n int, err error) {
|
func (hc Conn) Read(b []byte) (n int, err error) {
|
||||||
n, err = hc.c.Read(b)
|
n, err = hc.c.Read(b)
|
||||||
fmt.Printf("[Decrypting...]\n")
|
fmt.Printf("[Decrypting...]\n")
|
||||||
fmt.Printf("[ciphertext:%+v]\n", b[0:n])
|
fmt.Printf("[ciphertext:%+v]\n", b[0:n])
|
||||||
|
@ -244,7 +244,7 @@ func (hc HKExConn) Read(b []byte) (n int, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hc HKExConn) Write(b []byte) (n int, err error) {
|
func (hc Conn) Write(b []byte) (n int, err error) {
|
||||||
fmt.Printf("[Encrypting...]\n")
|
fmt.Printf("[Encrypting...]\n")
|
||||||
for i, _ := range b {
|
for i, _ := range b {
|
||||||
// FOR TESTING ONLY!! USE REAL CRYPTO HERE
|
// FOR TESTING ONLY!! USE REAL CRYPTO HERE
|
||||||
|
@ -259,8 +259,8 @@ func (hc HKExConn) Write(b []byte) (n int, err error) {
|
||||||
// Return c coerced into a HKEx Conn (which implements interface net.Conn)
|
// Return c coerced into a HKEx Conn (which implements interface net.Conn)
|
||||||
// Only useful if one wants to convert an open connection later to HKEx
|
// Only useful if one wants to convert an open connection later to HKEx
|
||||||
// (Use Dial() instead to start with HKEx automatically.)
|
// (Use Dial() instead to start with HKEx automatically.)
|
||||||
func NewHKExConn(c *net.Conn) (hc *HKExConn) {
|
func NewHKExConn(c *net.Conn) (hc *Conn) {
|
||||||
hc = new(HKExConn)
|
hc = new(Conn)
|
||||||
|
|
||||||
hc.c = *c
|
hc.c = *c
|
||||||
hc.h = New(0, 0)
|
hc.h = New(0, 0)
|
||||||
|
|
Loading…
Reference in New Issue