Skip to content

Commit 16e35de

Browse files
committed
initial import from gitlab... more clean up required.
1 parent c86876f commit 16e35de

File tree

9 files changed

+1633
-6
lines changed

9 files changed

+1633
-6
lines changed

.gitignore

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
1-
# If you prefer the allow list template instead of the deny list, see community template:
2-
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
3-
#
41
# Binaries for programs and plugins
52
*.exe
63
*.exe~
74
*.dll
85
*.so
96
*.dylib
7+
*.tgz
108

119
# Test binary, built with `go test -c`
1210
*.test
1311

1412
# Output of the go coverage tool, specifically when used with LiteIDE
1513
*.out
1614

15+
# Leave out idea
16+
.idea/
17+
1718
# Dependency directories (remove the comment below to include it)
18-
# vendor/
19+
vendor/
20+
# Ignore default recorded_flows dir
21+
recorded_flows/
22+
# Ignore all prod
23+
prod_*
1924

20-
# Go workspace file
21-
go.work
25+
dist/

Dockerfile

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# ---- Base ----
2+
FROM docker.ena.net/alpine/base AS build-stage
3+
LABEL application=flowproxy-build
4+
5+
COPY . /opt/go/src/gitlab.ena.net/netflow/flowproxy
6+
WORKDIR /opt/go/src/gitlab.ena.net/netflow/flowproxy
7+
8+
RUN apk add --update make libpcap-dev
9+
10+
# build
11+
RUN make build
12+
13+
# deploy
14+
FROM docker.ena.net/alpine/deploy AS deploy
15+
LABEL application=flowproxy
16+
17+
# add some alpine deps
18+
RUN apk add --update --no-cache tzdata
19+
20+
ENV CONSUL_HTTP_ADDR http://cnl01.dev.ena.net:8500
21+
22+
WORKDIR /opt/flowproxy/
23+
# copy executable
24+
COPY --from=build-stage /opt/go/src/gitlab.ena.net/netflow/flowproxy/flowproxy ./
25+
# copy docroot
26+
# COPY --from=build-stage /opt/go/src/gitlab.ena.net/netflow/flowproxy/docroot ./docroot

common/common.go

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package common
2+
3+
import (
4+
"bytes"
5+
crand "crypto/rand"
6+
"encoding/binary"
7+
"fmt"
8+
"github.com/mostlygeek/arp"
9+
"log"
10+
"math/big"
11+
"net"
12+
"os/exec"
13+
"runtime"
14+
"strings"
15+
)
16+
17+
func CryptoRandomNumber(max int64) int64 {
18+
n, err := crand.Int(crand.Reader, big.NewInt(max))
19+
if err != nil {
20+
panic(fmt.Errorf("crypto number failed to read bytes %v", err))
21+
}
22+
return n.Int64()
23+
}
24+
25+
// RandomNum Generates a random number between the given min and max
26+
func RandomNum(min, max int) int {
27+
return int(CryptoRandomNumber(int64(max-min))) + min
28+
}
29+
30+
func BinaryReader(reader *bytes.Reader, dests ...interface{}) error {
31+
for _, dest := range dests {
32+
err := binary.Read(reader, binary.BigEndian, dest)
33+
if err != nil {
34+
return err
35+
}
36+
}
37+
return nil
38+
}
39+
40+
func LookupMACAddr(ipAddr net.IP) (macAddr net.HardwareAddr, e error) {
41+
// check if ipAddr is local
42+
// find all devices
43+
devices, err := net.Interfaces()
44+
if err != nil {
45+
return nil, err
46+
}
47+
48+
for _, device := range devices {
49+
//DEBUG
50+
//fmt.Printf("device: %s mac: %s\n", device.Name, device.HardwareAddr)
51+
addresses, _ := device.Addrs()
52+
for _, address := range addresses {
53+
//DEBUG
54+
//fmt.Printf("\taddress: %s\n", address)
55+
deviceIPAddr, _, _ := net.ParseCIDR(address.String())
56+
if deviceIPAddr != nil {
57+
if ipAddr.Equal(deviceIPAddr) {
58+
// found IP locally, grab the interface and return it
59+
return device.HardwareAddr, nil
60+
}
61+
} else {
62+
deviceIPAddr = net.ParseIP(address.String())
63+
if ipAddr.Equal(deviceIPAddr) {
64+
return device.HardwareAddr, nil
65+
}
66+
}
67+
}
68+
}
69+
// give ipAddr was not found locally, so lets move on to ARP
70+
// ping first to force the OS to resolve mac address
71+
cmd := exec.Command("ping", "-c", "1", ipAddr.String())
72+
err = cmd.Run()
73+
if err != nil {
74+
log.Printf("Failed to ping %s: %s", ipAddr, err)
75+
return nil, err
76+
}
77+
mac := arp.Search(ipAddr.String())
78+
if runtime.GOOS == "darwin" {
79+
mac = DarwinMACFormat(mac)
80+
}
81+
macParsed, err := net.ParseMAC(mac)
82+
if err != nil {
83+
fmt.Printf("Error parsing destination MAC: %s - %s\n", mac, err)
84+
return nil, err
85+
}
86+
return macParsed, nil
87+
}
88+
89+
// DarwinMACFormat fixes the issue that macOS returns oddly formatted arp -a results
90+
func DarwinMACFormat(macString string) string {
91+
var group int
92+
var builder strings.Builder
93+
for i := 0; i < len(macString); i++ {
94+
r := macString[i]
95+
if r == ':' {
96+
for chars := group; chars < 2; chars++ {
97+
builder.WriteString("0")
98+
}
99+
builder.WriteString(macString[i-group : i])
100+
builder.WriteString(":")
101+
group = 0
102+
continue
103+
}
104+
group++
105+
}
106+
for chars := group; chars < 2; chars++ {
107+
builder.WriteString("0")
108+
}
109+
builder.WriteString(macString[len(macString)-group:])
110+
return builder.String()
111+
}

0 commit comments

Comments
 (0)