186 lines
4.4 KiB
Go
186 lines
4.4 KiB
Go
package node
|
|
|
|
/*
|
|
var (
|
|
network = []byte{10, 1, 1, 0}
|
|
serverIP = byte(1)
|
|
clientIP = byte(2)
|
|
port = uint16(5151)
|
|
netName = "testnet"
|
|
pubKey1 = []byte{0x43, 0xde, 0xd4, 0xb2, 0x1d, 0x71, 0x58, 0x9a, 0x96, 0x3a, 0x23, 0xfc, 0x2, 0xe, 0xfa, 0x42, 0x3, 0x94, 0xbc, 0xf8, 0x25, 0xf, 0x54, 0xcc, 0x98, 0x42, 0x8b, 0xe5, 0x27, 0x86, 0x49, 0x33}
|
|
privKey1 = []byte{0xae, 0x4d, 0xc5, 0xaa, 0xc9, 0xbc, 0x65, 0x41, 0x55, 0xb, 0x61, 0x52, 0xc4, 0x6c, 0xce, 0x2f, 0x1b, 0xf5, 0xb3, 0xbf, 0xb5, 0x54, 0x61, 0x7c, 0x26, 0x2e, 0xba, 0x5a, 0x19, 0xe2, 0x9c, 0xe0}
|
|
pubKey2 = []byte{0x8c, 0xfe, 0x12, 0xd9, 0x2d, 0x37, 0x5, 0x43, 0xab, 0x70, 0x59, 0x20, 0x3d, 0x82, 0x93, 0x9b, 0xb3, 0xaa, 0x35, 0x23, 0xc1, 0xb4, 0x4, 0x1f, 0x92, 0x97, 0x6f, 0xfd, 0x55, 0x17, 0x5a, 0x4b}
|
|
privKey2 = []byte{0xd9, 0xe1, 0xc6, 0x64, 0x3e, 0x29, 0x29, 0x78, 0x81, 0x53, 0xc2, 0x31, 0xd9, 0x34, 0x5b, 0x41, 0xf5, 0x80, 0xb0, 0x27, 0x9f, 0x65, 0x85, 0xd4, 0x78, 0xd5, 0x9, 0x2, 0xca, 0x56, 0x42, 0x80}
|
|
)
|
|
|
|
func must(err error) {
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
type TmpNode struct {
|
|
network []byte
|
|
localIP byte
|
|
router *router
|
|
port uint16
|
|
netName string
|
|
iface io.ReadWriteCloser
|
|
pubKey []byte
|
|
privKey []byte
|
|
w *connWriter
|
|
r *connReader
|
|
}
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
func NewTmpNodeServer() *TmpNode {
|
|
n := &TmpNode{
|
|
localIP: serverIP,
|
|
network: network,
|
|
router: &router{table: newPeerRepo()},
|
|
port: port,
|
|
netName: netName,
|
|
pubKey: pubKey1,
|
|
privKey: privKey1,
|
|
}
|
|
|
|
var err error
|
|
n.iface, err = openInterface(n.network, n.localIP, n.netName)
|
|
must(err)
|
|
|
|
myAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf(":%d", n.port))
|
|
must(err)
|
|
|
|
conn, err := net.ListenUDP("udp", myAddr)
|
|
must(err)
|
|
|
|
n.w = newConnWriter(conn, n.localIP, n.router)
|
|
n.r = newConnReader(conn, n.localIP, n.router)
|
|
|
|
n.router.table.Set(clientIP, &peer{
|
|
IP: clientIP,
|
|
SharedKey: computeSharedKey(pubKey2, n.privKey),
|
|
})
|
|
|
|
return n
|
|
}
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
func NewTmpNodeClient(srvAddrStr string) *TmpNode {
|
|
n := &TmpNode{
|
|
localIP: clientIP,
|
|
network: network,
|
|
router: &router{table: newPeerRepo()},
|
|
port: port,
|
|
netName: netName,
|
|
pubKey: pubKey2,
|
|
privKey: privKey2,
|
|
}
|
|
|
|
var err error
|
|
n.iface, err = openInterface(n.network, n.localIP, n.netName)
|
|
must(err)
|
|
|
|
myAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf(":%d", n.port))
|
|
must(err)
|
|
|
|
conn, err := net.ListenUDP("udp", myAddr)
|
|
must(err)
|
|
|
|
n.w = newConnWriter(conn, n.localIP, n.router)
|
|
n.r = newConnReader(conn, n.localIP, n.router)
|
|
|
|
serverAddr, err := netip.ParseAddrPort(fmt.Sprintf("%s:%d", srvAddrStr, port))
|
|
must(err)
|
|
|
|
n.router.table.Set(serverIP, &peer{
|
|
IP: serverIP,
|
|
Addr: &serverAddr,
|
|
SharedKey: computeSharedKey(pubKey1, n.privKey),
|
|
})
|
|
|
|
return n
|
|
}
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
func (n *TmpNode) RunServer() {
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
fmt.Printf("%v", r)
|
|
debug.PrintStack()
|
|
}
|
|
}()
|
|
|
|
// Get remoteAddr from a packet.
|
|
buf := make([]byte, bufferSize)
|
|
remoteAddr, h, _, err := n.r.Read(buf)
|
|
must(err)
|
|
log.Printf("Got remote addr: %d -> %v", h.SourceIP, remoteAddr)
|
|
must(err)
|
|
|
|
n.router.table.Set(h.SourceIP, &peer{
|
|
IP: h.SourceIP,
|
|
Addr: &remoteAddr,
|
|
SharedKey: computeSharedKey(pubKey2, n.privKey),
|
|
})
|
|
|
|
go n.readFromIFace()
|
|
n.readFromConn()
|
|
}
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
func (n *TmpNode) RunClient() {
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
fmt.Printf("%v\n", r)
|
|
debug.PrintStack()
|
|
}
|
|
}()
|
|
|
|
log.Printf("Sending to server...")
|
|
must(n.w.WriteTo(serverIP, 1, []byte{1, 2, 3, 4, 5, 6, 7, 8}))
|
|
|
|
go n.readFromIFace()
|
|
n.readFromConn()
|
|
}
|
|
|
|
func (n *TmpNode) readFromIFace() {
|
|
var (
|
|
buf = make([]byte, bufferSize)
|
|
packet []byte
|
|
remoteIP byte
|
|
err error
|
|
)
|
|
|
|
for {
|
|
packet, remoteIP, err = readNextPacket(n.iface, buf)
|
|
must(err)
|
|
must(n.w.WriteTo(remoteIP, 1, packet))
|
|
}
|
|
}
|
|
|
|
func (node *TmpNode) readFromConn() {
|
|
var (
|
|
buf = make([]byte, bufferSize)
|
|
packet []byte
|
|
err error
|
|
)
|
|
|
|
for {
|
|
_, _, packet, err = node.r.Read(buf)
|
|
must(err)
|
|
// We assume that we're only receiving packets from one source.
|
|
|
|
_, err = node.iface.Write(packet)
|
|
if err != nil {
|
|
log.Printf("Got error: %v", err)
|
|
}
|
|
//must(err)
|
|
}
|
|
}
|
|
*/
|