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