Expose connection ID creation

This commit is contained in:
Daniel
2023-07-20 13:36:09 +02:00
parent 28d3d24988
commit 5d7caeb4bb
2 changed files with 25 additions and 18 deletions

View File

@@ -1,6 +1,7 @@
package packet
import (
"fmt"
"net"
"time"
)
@@ -50,3 +51,26 @@ func (pi *Info) RemotePort() uint16 {
}
return pi.DstPort
}
// CreateConnectionID creates a connection ID.
// In most circumstances, this method should not be used directly, but
// packet.GetConnectionID() should be called instead.
func (pi *Info) CreateConnectionID() string {
return CreateConnectionID(pi.Protocol, pi.Src, pi.SrcPort, pi.Dst, pi.DstPort, pi.Inbound)
}
// CreateConnectionID creates a connection ID.
func CreateConnectionID(protocol IPProtocol, src net.IP, srcPort uint16, dst net.IP, dstPort uint16, inbound bool) string {
// TODO: make this ID not depend on the packet direction for better support for forwarded packets.
if protocol == TCP || protocol == UDP {
if inbound {
return fmt.Sprintf("%d-%s-%d-%s-%d", protocol, dst, dstPort, src, srcPort)
}
return fmt.Sprintf("%d-%s-%d-%s-%d", protocol, src, srcPort, dst, dstPort)
}
if inbound {
return fmt.Sprintf("%d-%s-%s", protocol, dst, src)
}
return fmt.Sprintf("%d-%s-%s", protocol, src, dst)
}