WIP: Cleanup
This commit is contained in:
		| @@ -79,7 +79,6 @@ func newPeerMain(args mainArgs) *peerMain { | |||||||
|  |  | ||||||
| 	localPeer := state.Peers[config.LocalPeerIP] | 	localPeer := state.Peers[config.LocalPeerIP] | ||||||
|  |  | ||||||
| 	log.Printf("XXXXX %v %v", config.LocalPeerIP, localPeer) |  | ||||||
| 	myAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf(":%d", localPeer.Port)) | 	myAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf(":%d", localPeer.Port)) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		log.Fatalf("Failed to resolve UDP address: %v", err) | 		log.Fatalf("Failed to resolve UDP address: %v", err) | ||||||
|   | |||||||
| @@ -1,114 +0,0 @@ | |||||||
| package peer |  | ||||||
|  |  | ||||||
| import ( |  | ||||||
| 	"bytes" |  | ||||||
| 	"crypto/rand" |  | ||||||
| 	mrand "math/rand" |  | ||||||
| 	"net/netip" |  | ||||||
| 	"sync/atomic" |  | ||||||
| ) |  | ||||||
|  |  | ||||||
| // A test peer. |  | ||||||
| type P struct { |  | ||||||
| 	cryptoKeys |  | ||||||
| 	RT         *atomic.Pointer[routingTable] |  | ||||||
| 	Conn       *TestUDPConn |  | ||||||
| 	IFace      *TestIFace |  | ||||||
| 	ConnReader *ConnReader |  | ||||||
| 	IFReader   *IFReader |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func NewPeerForTesting(n *TestNetwork, ip byte, addr netip.AddrPort) P { |  | ||||||
| 	p := P{ |  | ||||||
| 		cryptoKeys: generateKeys(), |  | ||||||
| 		RT:         &atomic.Pointer[routingTable]{}, |  | ||||||
| 		IFace:      NewTestIFace(), |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	rt := newRoutingTable(ip, addr) |  | ||||||
| 	p.RT.Store(&rt) |  | ||||||
| 	p.Conn = n.NewUDPConn(addr) |  | ||||||
| 	//p.ConnWriter = NewConnWriter(p.Conn.WriteToUDPAddrPort, p.RT) |  | ||||||
|  |  | ||||||
| 	return p |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func ConnectPeers(p1, p2 *P) { |  | ||||||
| 	rt1 := p1.RT.Load() |  | ||||||
| 	rt2 := p2.RT.Load() |  | ||||||
|  |  | ||||||
| 	ip1 := rt1.LocalIP |  | ||||||
| 	ip2 := rt2.LocalIP |  | ||||||
|  |  | ||||||
| 	rt1.Peers[ip2].Up = true |  | ||||||
| 	rt1.Peers[ip2].Direct = true |  | ||||||
| 	rt1.Peers[ip2].Relay = true |  | ||||||
| 	rt1.Peers[ip2].DirectAddr = rt2.LocalAddr |  | ||||||
| 	rt1.Peers[ip2].PubSignKey = p2.PubSignKey |  | ||||||
| 	rt1.Peers[ip2].ControlCipher = newControlCipher(p1.PrivKey, p2.PubKey) |  | ||||||
| 	rt1.Peers[ip2].DataCipher = newDataCipher() |  | ||||||
|  |  | ||||||
| 	rt2.Peers[ip1].Up = true |  | ||||||
| 	rt2.Peers[ip1].Direct = true |  | ||||||
| 	rt2.Peers[ip1].Relay = true |  | ||||||
| 	rt2.Peers[ip1].DirectAddr = rt1.LocalAddr |  | ||||||
| 	rt2.Peers[ip1].PubSignKey = p1.PubSignKey |  | ||||||
| 	rt2.Peers[ip1].ControlCipher = newControlCipher(p2.PrivKey, p1.PubKey) |  | ||||||
| 	rt2.Peers[ip1].DataCipher = rt1.Peers[ip2].DataCipher |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func NewPeersForTesting() (p1, p2, p3 P) { |  | ||||||
| 	n := NewTestNetwork() |  | ||||||
|  |  | ||||||
| 	p1 = NewPeerForTesting( |  | ||||||
| 		n, |  | ||||||
| 		1, |  | ||||||
| 		netip.AddrPortFrom(netip.AddrFrom4([4]byte{1, 1, 1, 1}), 100)) |  | ||||||
|  |  | ||||||
| 	p2 = NewPeerForTesting( |  | ||||||
| 		n, |  | ||||||
| 		2, |  | ||||||
| 		netip.AddrPortFrom(netip.AddrFrom4([4]byte{1, 1, 1, 2}), 200)) |  | ||||||
|  |  | ||||||
| 	p3 = NewPeerForTesting( |  | ||||||
| 		n, |  | ||||||
| 		3, |  | ||||||
| 		netip.AddrPortFrom(netip.AddrFrom4([4]byte{1, 1, 1, 3}), 300)) |  | ||||||
|  |  | ||||||
| 	ConnectPeers(&p1, &p2) |  | ||||||
| 	ConnectPeers(&p1, &p3) |  | ||||||
| 	ConnectPeers(&p2, &p3) |  | ||||||
|  |  | ||||||
| 	return |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func RandPacket() []byte { |  | ||||||
| 	n := mrand.Intn(1200) |  | ||||||
| 	b := make([]byte, n) |  | ||||||
| 	rand.Read(b) |  | ||||||
| 	return b |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func ModifyPacket(in []byte) []byte { |  | ||||||
| 	x := make([]byte, 1) |  | ||||||
|  |  | ||||||
| 	for { |  | ||||||
| 		rand.Read(x) |  | ||||||
| 		out := bytes.Clone(in) |  | ||||||
| 		idx := mrand.Intn(len(out)) |  | ||||||
| 		if out[idx] != x[0] { |  | ||||||
| 			out[idx] = x[0] |  | ||||||
| 			return out |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
| } |  | ||||||
|  |  | ||||||
| // ---------------------------------------------------------------------------- |  | ||||||
|  |  | ||||||
| type UnknownControlPacket struct { |  | ||||||
| 	TraceID uint64 |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func (p UnknownControlPacket) Marshal(buf []byte) []byte { |  | ||||||
| 	return newBinWriter(buf).Byte(255).Uint64(p.TraceID).Build() |  | ||||||
| } |  | ||||||
| @@ -1,138 +0,0 @@ | |||||||
| package peer |  | ||||||
|  |  | ||||||
| import ( |  | ||||||
| 	"net/netip" |  | ||||||
| 	"sync/atomic" |  | ||||||
| 	"time" |  | ||||||
| ) |  | ||||||
|  |  | ||||||
| // TODO: Remove |  | ||||||
| func newRemotePeer(ip byte) *remotePeer { |  | ||||||
| 	counter := uint64(time.Now().Unix()<<30 + 1) |  | ||||||
| 	return &remotePeer{ |  | ||||||
| 		IP:       ip, |  | ||||||
| 		counter:  &counter, |  | ||||||
| 		dupCheck: newDupCheck(0), |  | ||||||
| 	} |  | ||||||
| } |  | ||||||
|  |  | ||||||
| // ---------------------------------------------------------------------------- |  | ||||||
|  |  | ||||||
| type remotePeer struct { |  | ||||||
| 	localIP       byte |  | ||||||
| 	IP            byte           // VPN IP of peer (last byte). |  | ||||||
| 	Up            bool           // True if data can be sent on the peer. |  | ||||||
| 	Relay         bool           // True if the peer is a relay. |  | ||||||
| 	Direct        bool           // True if this is a direct connection. |  | ||||||
| 	DirectAddr    netip.AddrPort // Remote address if directly connected. |  | ||||||
| 	PubSignKey    []byte |  | ||||||
| 	ControlCipher *controlCipher |  | ||||||
| 	DataCipher    *dataCipher |  | ||||||
|  |  | ||||||
| 	counter  *uint64   // For sending to. Atomic access only. |  | ||||||
| 	dupCheck *dupCheck // For receiving from. Not safe for concurrent use. |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func (p remotePeer) EncryptDataPacket(destIP byte, data, out []byte) []byte { |  | ||||||
| 	h := Header{ |  | ||||||
| 		StreamID: dataStreamID, |  | ||||||
| 		Counter:  atomic.AddUint64(p.counter, 1), |  | ||||||
| 		SourceIP: p.localIP, |  | ||||||
| 		DestIP:   destIP, |  | ||||||
| 	} |  | ||||||
| 	return p.DataCipher.Encrypt(h, data, out) |  | ||||||
| } |  | ||||||
|  |  | ||||||
| // Decrypts and de-dups incoming data packets. |  | ||||||
| func (p remotePeer) DecryptDataPacket(h Header, enc, out []byte) ([]byte, error) { |  | ||||||
| 	dec, ok := p.DataCipher.Decrypt(enc, out) |  | ||||||
| 	if !ok { |  | ||||||
| 		return nil, errDecryptionFailed |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	if p.dupCheck.IsDup(h.Counter) { |  | ||||||
| 		return nil, errDuplicateSeqNum |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	return dec, nil |  | ||||||
| } |  | ||||||
|  |  | ||||||
| // Peer must have a ControlCipher. |  | ||||||
| func (p remotePeer) EncryptControlPacket(pkt marshaller, tmp, out []byte) []byte { |  | ||||||
| 	tmp = pkt.Marshal(tmp) |  | ||||||
| 	h := Header{ |  | ||||||
| 		StreamID: controlStreamID, |  | ||||||
| 		Counter:  atomic.AddUint64(p.counter, 1), |  | ||||||
| 		SourceIP: p.localIP, |  | ||||||
| 		DestIP:   p.IP, |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	return p.ControlCipher.Encrypt(h, tmp, out) |  | ||||||
| } |  | ||||||
|  |  | ||||||
| // Returns a controlMsg[PacketType]. Peer must have a non-nil ControlCipher. |  | ||||||
| // |  | ||||||
| // This function also drops packets with duplicate sequence numbers. |  | ||||||
| func (p remotePeer) DecryptControlPacket(fromAddr netip.AddrPort, h Header, enc, tmp []byte) (any, error) { |  | ||||||
| 	out, ok := p.ControlCipher.Decrypt(enc, tmp) |  | ||||||
| 	if !ok { |  | ||||||
| 		return nil, errDecryptionFailed |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	if p.dupCheck.IsDup(h.Counter) { |  | ||||||
| 		return nil, errDuplicateSeqNum |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	msg, err := parseControlMsg(h.SourceIP, fromAddr, out) |  | ||||||
| 	if err != nil { |  | ||||||
| 		return nil, err |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	return msg, nil |  | ||||||
| } |  | ||||||
|  |  | ||||||
| // ---------------------------------------------------------------------------- |  | ||||||
|  |  | ||||||
| type routingTable struct { |  | ||||||
| 	// The LocalIP is the configured IP address of the local peer on the VPN. |  | ||||||
| 	// |  | ||||||
| 	// This value is constant. |  | ||||||
| 	LocalIP byte |  | ||||||
|  |  | ||||||
| 	// The LocalAddr is the configured local public address of the peer on the |  | ||||||
| 	// internet. If LocalAddr.IsValid(), then the local peer has a public |  | ||||||
| 	// address. |  | ||||||
| 	// |  | ||||||
| 	// This value is constant. |  | ||||||
| 	LocalAddr netip.AddrPort |  | ||||||
|  |  | ||||||
| 	// The remote peer configurations. These are updated by |  | ||||||
| 	Peers [256]remotePeer |  | ||||||
|  |  | ||||||
| 	// The current relay's VPN IP address, or zero if no relay is available. |  | ||||||
| 	RelayIP byte |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func newRoutingTable(localIP byte, localAddr netip.AddrPort) routingTable { |  | ||||||
| 	rt := routingTable{ |  | ||||||
| 		LocalIP:   localIP, |  | ||||||
| 		LocalAddr: localAddr, |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	for i := range rt.Peers { |  | ||||||
| 		counter := uint64(time.Now().Unix()<<30 + 1) |  | ||||||
| 		rt.Peers[i] = remotePeer{ |  | ||||||
| 			localIP:  localIP, |  | ||||||
| 			IP:       byte(i), |  | ||||||
| 			counter:  &counter, |  | ||||||
| 			dupCheck: newDupCheck(0), |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	return rt |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func (rt *routingTable) GetRelay() (remotePeer, bool) { |  | ||||||
| 	relay := rt.Peers[rt.RelayIP] |  | ||||||
| 	return relay, relay.Up && relay.Direct |  | ||||||
| } |  | ||||||
		Reference in New Issue
	
	Block a user