github.com/zartbot/goflow@v0.0.0-20221001080520-456f7dfad288/service/flowinfo/natmap.go (about)

     1  package flowinfo
     2  
     3  import (
     4  	"net"
     5  
     6  	"github.com/zartbot/goflow/lib/iputil"
     7  	"github.com/zartbot/goflow/service/identity"
     8  )
     9  
    10  type NATinfo struct {
    11  	Direction  bool
    12  	IPAddressA net.IP
    13  	PortA      uint16
    14  	IPAddressB net.IP
    15  	PortB      uint16
    16  }
    17  
    18  /*
    19  natEvent :1 Create  :2 Delete
    20  [sourceIPv4Address][sourceTransportPort] <->[destinationIPv4Address][destinationTransportPort]
    21  [postNATSourceIPv4Address][postNAPTSourceTransportPort] <->[postNATDestinationIPv4Address][postNAPTDestinationTransportPort]
    22  */
    23  
    24  func FetchNATAddress(f iputil.FlowInfo) (iputil.FlowInfo, bool) {
    25  	var v iputil.FlowInfo
    26  	natlog, ok := identity.Service["NAT"].Load(f.FlowIDWithPort)
    27  	if ok {
    28  		v := natlog.(iputil.FlowInfo)
    29  		if f.Direction != v.Direction {
    30  			//preNAT src/dst also need to swap
    31  			tempIP := v.IPAddress_A
    32  			tempPort := v.Port_A
    33  			v.IPAddress_A = v.IPAddress_B
    34  			v.Port_A = v.Port_B
    35  			v.IPAddress_B = tempIP
    36  			v.Port_B = tempPort
    37  		}
    38  		return v, true
    39  	} else {
    40  		return v, false
    41  	}
    42  }