WIP: Apparently working?
This commit is contained in:
parent
ab246b2a90
commit
169231d848
@ -2,12 +2,13 @@
|
||||
|
||||
## TO DO
|
||||
|
||||
* Double buffering in IFReader and ConnReader ?
|
||||
* Replace time-based counter with startup counter
|
||||
* 16 byte startupCounter
|
||||
* (startupCount << 48) + counter
|
||||
* pass startup count to newRoutingTable function (or global?)
|
||||
* write / increment on startup
|
||||
* Use startup counter for trace IDs as well.
|
||||
* Double buffering in IFReader and ConnReader ?
|
||||
* Clean up state machine - one class w/
|
||||
* type stateFunc func(msg any) stateFunc
|
||||
* "init" funcs: func enterDisconnected() stateFunc
|
||||
|
@ -42,16 +42,7 @@ func (r *ConnReader) handleNextPacket() {
|
||||
buf = buf[:n]
|
||||
h := parseHeader(buf)
|
||||
|
||||
remote := r.RemotePeers[h.SourceIP].Load()
|
||||
|
||||
switch h.StreamID {
|
||||
case controlStreamID:
|
||||
remote.handleControlPacket(h, remoteAddr, buf)
|
||||
case dataStreamID:
|
||||
remote.handleDataPacket(h, buf)
|
||||
default:
|
||||
r.logf("Unknown stream ID: %d", h.StreamID)
|
||||
}
|
||||
r.RemotePeers[h.SourceIP].Load().HandlePacket(h, remoteAddr, buf)
|
||||
}
|
||||
|
||||
func (r *ConnReader) logf(format string, args ...any) {
|
||||
|
@ -60,6 +60,8 @@ type Globals struct {
|
||||
|
||||
// Global TUN interface.
|
||||
IFace io.ReadWriteCloser
|
||||
|
||||
// For trace ID.
|
||||
}
|
||||
|
||||
func NewGlobals(
|
||||
|
@ -41,7 +41,6 @@ func runMCReaderInner(g Globals) {
|
||||
logf("Failed to open discovery packet?")
|
||||
continue
|
||||
}
|
||||
log.Printf("Got local discovery from %v: %v", remoteAddr, h)
|
||||
|
||||
g.RemotePeers[h.SourceIP].Load().HandleLocalDiscoveryPacket(h, remoteAddr, buf)
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import (
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// TODO: Initialize w/ startup counter.
|
||||
var traceIDCounter uint64 = uint64(time.Now().Unix()<<30) + 1
|
||||
|
||||
func newTraceID() uint64 {
|
||||
|
@ -98,12 +98,12 @@ func (r *Remote) sendUDP(b []byte, addr netip.AddrPort) {
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
func (r *Remote) encryptData(conf remoteConfig, packet []byte) []byte {
|
||||
func (r *Remote) encryptData(conf remoteConfig, destIP byte, packet []byte) []byte {
|
||||
h := Header{
|
||||
StreamID: dataStreamID,
|
||||
Counter: atomic.AddUint64(&r.sendCounter, 1),
|
||||
SourceIP: r.Globals.LocalPeerIP,
|
||||
DestIP: r.RemotePeerIP,
|
||||
DestIP: destIP,
|
||||
}
|
||||
return conf.DataCipher.Encrypt(h, packet, packet[len(packet):cap(packet)])
|
||||
}
|
||||
@ -144,22 +144,21 @@ func (r *Remote) sendDataRelayed(conf remoteConfig, data []byte) {
|
||||
return
|
||||
}
|
||||
|
||||
relay.relayData(r.encryptData(conf, data))
|
||||
relay.relayData(conf.Peer.PeerIP, r.encryptData(conf, conf.Peer.PeerIP, data))
|
||||
}
|
||||
|
||||
// sendDataDirect sends data to the remote directly.
|
||||
func (r *Remote) sendDataDirect(conf remoteConfig, data []byte) {
|
||||
r.logf("Sending data direct...")
|
||||
r.sendUDP(r.encryptData(conf, data), conf.DirectAddr)
|
||||
r.sendUDP(r.encryptData(conf, conf.Peer.PeerIP, data), conf.DirectAddr)
|
||||
}
|
||||
|
||||
func (r *Remote) relayData(enc []byte) {
|
||||
func (r *Remote) relayData(toIP byte, enc []byte) {
|
||||
conf := r.conf()
|
||||
if !conf.Up || !conf.Direct {
|
||||
r.logf("Cannot relay: not up or not a direct connection")
|
||||
return
|
||||
}
|
||||
r.sendDataDirect(conf, enc)
|
||||
r.sendUDP(r.encryptData(conf, toIP, enc), conf.DirectAddr)
|
||||
}
|
||||
|
||||
func (r *Remote) sendControl(conf remoteConfig, data []byte) {
|
||||
@ -176,13 +175,11 @@ func (r *Remote) sendControlToAddr(buf []byte, addr netip.AddrPort) {
|
||||
}
|
||||
|
||||
func (r *Remote) sendControlDirect(conf remoteConfig, data []byte) {
|
||||
r.logf("Sending control direct...")
|
||||
enc := r.encryptControl(conf, data)
|
||||
r.sendUDP(enc, conf.DirectAddr)
|
||||
}
|
||||
|
||||
func (r *Remote) sendControlRelayed(conf remoteConfig, data []byte) {
|
||||
r.logf("Sending control relayed...")
|
||||
relay := r.RelayHandler.Load()
|
||||
|
||||
if relay == nil {
|
||||
@ -190,7 +187,7 @@ func (r *Remote) sendControlRelayed(conf remoteConfig, data []byte) {
|
||||
return
|
||||
}
|
||||
|
||||
relay.relayData(r.encryptControl(conf, data))
|
||||
relay.relayData(conf.Peer.PeerIP, r.encryptControl(conf, data))
|
||||
}
|
||||
|
||||
func (r *Remote) forwardPacket(data []byte) {
|
||||
@ -268,7 +265,9 @@ func (r *Remote) handleDataPacket(h Header, data []byte) {
|
||||
// For local.
|
||||
if h.DestIP == r.LocalPeerIP {
|
||||
if _, err := r.IFace.Write(dec); err != nil {
|
||||
log.Fatalf("Failed to write to interface: %v", err)
|
||||
// This could be a malformed packet from a peer, so we don't crash if it
|
||||
// happens.
|
||||
r.logf("Failed to write to interface: %v", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
@ -297,7 +296,6 @@ func (r *Remote) HandleLocalDiscoveryPacket(h Header, srcAddr netip.AddrPort, da
|
||||
SrcIP: h.SourceIP,
|
||||
SrcAddr: srcAddr,
|
||||
}
|
||||
r.logf("Got local discovery packet from %v.", srcAddr)
|
||||
|
||||
select {
|
||||
case r.messages <- msg:
|
||||
|
@ -156,16 +156,13 @@ func (r *remoteFSM) stateServer_onInit(msg controlMsg[packetInit]) {
|
||||
}
|
||||
|
||||
func (r *remoteFSM) stateServer_onSyn(msg controlMsg[packetSyn]) {
|
||||
r.logf("Got SYN: %v", msg.Packet)
|
||||
r.lastSeen = time.Now()
|
||||
p := msg.Packet
|
||||
|
||||
// Before we can respond to this packet, we need to make sure the
|
||||
// route is setup properly.
|
||||
conf := r.conf()
|
||||
if !conf.Up || conf.Direct != p.Direct {
|
||||
r.logf("Got SYN.")
|
||||
}
|
||||
logSyn := !conf.Up || conf.Direct != p.Direct
|
||||
|
||||
conf.Up = true
|
||||
conf.Direct = p.Direct
|
||||
@ -179,6 +176,10 @@ func (r *remoteFSM) stateServer_onSyn(msg controlMsg[packetSyn]) {
|
||||
|
||||
r.updateConf(conf)
|
||||
|
||||
if logSyn {
|
||||
r.logf("Got SYN.")
|
||||
}
|
||||
|
||||
r.sendControl(conf, packetAck{
|
||||
TraceID: p.TraceID,
|
||||
ToAddr: conf.DirectAddr,
|
||||
@ -429,7 +430,6 @@ func (r *remoteFSM) stateClient_onPingTimer() stateFunc {
|
||||
return r.enterClientInit()
|
||||
}
|
||||
|
||||
r.traceID = newTraceID()
|
||||
r.stateClient_sendSyn(conf)
|
||||
return r.stateClient
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user