packages/hyprspace: self-destructing streams

This commit is contained in:
Max Headroom 2022-09-26 00:43:57 +02:00
parent e6073a8fa2
commit 35aff0e1ce

View file

@ -203,6 +203,7 @@ func UpRun(r *cmd.Root, c *cmd.Sub) {
// If everyting succeeds continue on to the next packet. // If everyting succeeds continue on to the next packet.
_, err = stream.Write(packet[:plen]) _, err = stream.Write(packet[:plen])
if err == nil { if err == nil {
stream.SetWriteDeadline(time.Now().Add(25 * time.Second))
continue continue
} }
} }
@ -215,10 +216,13 @@ func UpRun(r *cmd.Root, c *cmd.Sub) {
// Check if the destination of the packet is a known peer to // Check if the destination of the packet is a known peer to
// the interface. // the interface.
if peer, ok := peerTable[dst]; ok { if peer, ok := peerTable[dst]; ok {
fmt.Println("[-] Connecting to peer: " + peer.Pretty())
stream, err = host.NewStream(ctx, peer, p2p.Protocol) stream, err = host.NewStream(ctx, peer, p2p.Protocol)
if err != nil { if err != nil {
fmt.Println("[!] Failed to dial peer: " + peer.Pretty())
continue continue
} }
stream.SetWriteDeadline(time.Now().Add(25 * time.Second))
// Write packet length // Write packet length
err = binary.Write(stream, binary.LittleEndian, uint16(plen)) err = binary.Write(stream, binary.LittleEndian, uint16(plen))
if err != nil { if err != nil {
@ -357,6 +361,7 @@ func streamHandler(stream network.Stream) {
return return
} }
} }
stream.SetWriteDeadline(time.Now().Add(25 * time.Second))
tunDev.Iface.Write(packet[:size]) tunDev.Iface.Write(packet[:size])
} }
} }