diff --git a/proxy/main.go b/proxy/main.go index e47b636..cc722bd 100644 --- a/proxy/main.go +++ b/proxy/main.go @@ -28,7 +28,7 @@ func main() { oldip := flag.String("oldip", "", "IP before change") newip := flag.String("newip", "10.0.0.15", "IP after change") oldmac := flag.String("oldmac", "", "MAC before change") - newmac := flag.String("newmac", "52:54:00:12:34:aa", "MAC after change") + newmac := flag.String("newmac", "", "MAC after change") passthrough := flag.Bool("passthrough", false, "Whether to pass every traffic through") proxy := flag.String("proxy", "1", "Number of the proxy switch") pidfile := flag.String("pidfile", "", "Location to write the pid to") @@ -36,7 +36,7 @@ func main() { flag.Parse() log.SetLevel(log.Level(*logLvl)) OldMac, _ = net.ParseMAC(*oldmac) - NewMac, _ = net.ParseMAC(*newmac) + NewMac = util.GenerateMac(*newmac) OldIP = net.ParseIP(*oldip).To4() NewIP = net.ParseIP(*newip).To4() log.SetFormatter(&log.TextFormatter{ diff --git a/proxy/proxy b/proxy/proxy index ea63b39..562c3d0 100755 Binary files a/proxy/proxy and b/proxy/proxy differ diff --git a/proxy/util/util.go b/proxy/util/util.go index 2f50e28..f5af53a 100644 --- a/proxy/util/util.go +++ b/proxy/util/util.go @@ -1,11 +1,13 @@ package util import ( + "crypto/rand" "github.com/google/gopacket" "github.com/google/gopacket/layers" "github.com/google/gopacket/pcapgo" log "github.com/sirupsen/logrus" "io/ioutil" + "net" "os" "strconv" ) @@ -38,7 +40,25 @@ func WritePIDFile(filename string) { if filename == "" { return } - if err := ioutil.WriteFile(filename, []byte(strconv.Itoa(os.Getpid())), 0644); err != nil { + if err := ioutil.WriteFile(filename, []byte(strconv.Itoa(os.Getpid()) + "\n"), 0644); err != nil { log.Errorf("Error writing PID file %s", filename) } -} \ No newline at end of file +} + +func GenerateMac(customMAC string) net.HardwareAddr { + if customMAC != "" { + ret, _ := net.ParseMAC("52:54:00:12:34:aa") + return ret + } + buf := make([]byte, 6) + var mac net.HardwareAddr + if _, err := rand.Read(buf); err != nil { + log.Error("Error generating random mac") + ret, _ := net.ParseMAC("52:54:00:12:34:aa") + return ret + } + // Set local bit, ensure unicast address + buf[0] = (buf[0] | 2) & 0xfe + mac = append(mac, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]) + return mac +}