mirror of
https://gogs.blitter.com/RLabs/xs
synced 2024-08-14 10:26:42 +00:00
Working client/server demos w/HEx and trivial XOR crypto test
This commit is contained in:
parent
60f2cb7e26
commit
11cd7bacfb
6 changed files with 82 additions and 19 deletions
BIN
demo/Herradura
BIN
demo/Herradura
Binary file not shown.
BIN
demo/client
BIN
demo/client
Binary file not shown.
|
@ -10,10 +10,14 @@ func main() {
|
||||||
conn, err := hkex.Dial("tcp", "localhost:2000")
|
conn, err := hkex.Dial("tcp", "localhost:2000")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// handle error
|
// handle error
|
||||||
|
fmt.Println("Err!")
|
||||||
}
|
}
|
||||||
fmt.Printf("conn: %v\n", conn)
|
//fmt.Printf("conn: %v\n", conn)
|
||||||
// fmt.Fprintf(conn, "GET / HTTP/1.0\r\n\r\n")
|
|
||||||
// status, err := bufio.NewReader(conn).ReadString('\n')
|
fmt.Fprintf(conn, "\x01\x02\x03\x04")
|
||||||
// _, err = bufio.NewReader(conn).ReadString('\n')
|
//fmt.Fprintf(conn, "GET / HTTP/1.0\r\n\r\n")
|
||||||
|
//status, err := bufio.NewReader(conn).ReadString('\n')
|
||||||
|
//_, err = bufio.NewReader(conn).ReadString('\n')
|
||||||
// ...
|
// ...
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
BIN
demo/server
BIN
demo/server
Binary file not shown.
|
@ -2,8 +2,8 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"log"
|
"log"
|
||||||
|
"time"
|
||||||
//"net"
|
//"net"
|
||||||
hkex "blitter.com/herradurakex"
|
hkex "blitter.com/herradurakex"
|
||||||
)
|
)
|
||||||
|
@ -30,12 +30,56 @@ 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) {
|
go func(c hkex.HKExConn) (e error) {
|
||||||
// Echo all incoming data.
|
ch := make(chan []byte)
|
||||||
io.Copy(c, c)
|
chN := 0
|
||||||
fmt.Println("Client sent:%v\n", c)
|
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, 64)
|
||||||
|
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)
|
||||||
|
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.
|
// Shut down the connection.
|
||||||
c.Close()
|
c.Close()
|
||||||
|
return
|
||||||
}(conn)
|
}(conn)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -143,6 +143,7 @@ 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 {
|
||||||
|
@ -158,7 +159,7 @@ func Dial(protocol string, ipport string) (hc *HKExConn, err error) {
|
||||||
}
|
}
|
||||||
hc = &HKExConn{c, New(0, 0)}
|
hc = &HKExConn{c, New(0, 0)}
|
||||||
|
|
||||||
// Stub KEx
|
// KEx
|
||||||
fmt.Fprintf(c, "0x%s\n", hc.h.d.Text(16))
|
fmt.Fprintf(c, "0x%s\n", hc.h.d.Text(16))
|
||||||
|
|
||||||
d := big.NewInt(0)
|
d := big.NewInt(0)
|
||||||
|
@ -167,6 +168,7 @@ func Dial(protocol string, ipport string) (hc *HKExConn, err error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
hc.h.PeerD = d
|
hc.h.PeerD = d
|
||||||
|
fmt.Printf("** D:%s\n", hc.h.d.Text(16))
|
||||||
fmt.Printf("**(c)** peerD:%s\n", hc.h.PeerD.Text(16))
|
fmt.Printf("**(c)** peerD:%s\n", hc.h.PeerD.Text(16))
|
||||||
hc.h.FA()
|
hc.h.FA()
|
||||||
fmt.Printf("**(c)** FA:%s\n", hc.h.fa)
|
fmt.Printf("**(c)** FA:%s\n", hc.h.fa)
|
||||||
|
@ -174,10 +176,11 @@ func Dial(protocol string, ipport string) (hc *HKExConn, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hc *HKExConn) Close() (err error) {
|
func (hc *HKExConn) Close() (err error) {
|
||||||
err = hc.c.Close()
|
err = hc.c.Close()
|
||||||
fmt.Println("[Conn Closing]")
|
fmt.Println("[Conn Closing]")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
/*---------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------*/
|
||||||
|
|
||||||
type HKExListener struct {
|
type HKExListener struct {
|
||||||
|
@ -211,34 +214,45 @@ func (hl *HKExListener) Accept() (hc HKExConn, err error) {
|
||||||
d := big.NewInt(0)
|
d := big.NewInt(0)
|
||||||
_, err = fmt.Fscanln(c, d)
|
_, err = fmt.Fscanln(c, d)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
fmt.Println("[Error]")
|
||||||
return hc, err
|
return hc, err
|
||||||
}
|
}
|
||||||
hc.h.PeerD = d
|
hc.h.PeerD = d
|
||||||
|
fmt.Printf("** D:%s\n", hc.h.d.Text(16))
|
||||||
fmt.Printf("**(s)** peerD:%s\n", hc.h.PeerD.Text(16))
|
fmt.Printf("**(s)** peerD:%s\n", hc.h.PeerD.Text(16))
|
||||||
hc.h.FA()
|
hc.h.FA()
|
||||||
fmt.Printf("**(s)** FA:%s\n", hc.h.fa)
|
fmt.Printf("**(s)** FA:%s\n", hc.h.fa)
|
||||||
|
|
||||||
// Stub KEx
|
// KEx
|
||||||
fmt.Fprintf(c, "0x%s\n", hc.h.d.Text(16))
|
fmt.Fprintf(c, "0x%s\n", hc.h.d.Text(16))
|
||||||
|
|
||||||
return
|
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 {
|
fmt.Printf("[Decrypting...]\n")
|
||||||
fmt.Println("** hc.Read() wraps c.Read() **")
|
fmt.Printf("[ciphertext:%+v]\n", b[0:n])
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
//for i, _ := range b {
|
||||||
|
// FOR TESTING ONLY!! USE REAL CRYPTO HERE
|
||||||
|
//b[i] ^= byte( hc.h.d.Mod(hc.h.d, big.NewInt(int64(c))).Int64() )
|
||||||
|
b[i] ^= hc.h.fa.Bytes()[0]
|
||||||
}
|
}
|
||||||
|
fmt.Printf("[plaintext:%+v]\n", b[0:n])
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hc HKExConn) Write(b []byte) (n int, err error) {
|
func (hc HKExConn) Write(b []byte) (n int, err error) {
|
||||||
n, err = hc.c.Write(b)
|
fmt.Printf("[Encrypting...]\n")
|
||||||
if n > 0 {
|
for i, _ := range b {
|
||||||
//fmt.Printf("** hc.Write('%s') wraps c.Write() **\n", b)
|
// FOR TESTING ONLY!! USE REAL CRYPTO HERE
|
||||||
|
//b[i] ^= byte( hc.h.d.Mod(hc.h.d, big.NewInt(int64(c))).Int64() )
|
||||||
|
b[i] ^= hc.h.fa.Bytes()[0]
|
||||||
}
|
}
|
||||||
|
fmt.Printf("[ciphertext:%+v]\n", b)
|
||||||
|
n, err = hc.c.Write(b)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -256,6 +270,7 @@ func NewHKExConn(c *net.Conn) (hc *HKExConn) {
|
||||||
//
|
//
|
||||||
}
|
}
|
||||||
hc.h.PeerD = d
|
hc.h.PeerD = d
|
||||||
|
fmt.Printf("** D:%s\n", hc.h.d.Text(16))
|
||||||
fmt.Printf("** peerD:%s\n", hc.h.PeerD.Text(16))
|
fmt.Printf("** peerD:%s\n", hc.h.PeerD.Text(16))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue