-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
133 lines (102 loc) · 2.75 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"bufio"
"fmt"
"github.com/google/gopacket/pcap"
cmap "github.com/orcaman/concurrent-map"
"log"
"net"
"os"
"runtime"
"sort"
"strconv"
"strings"
)
type Interface struct {
Display string
Iface net.Interface
}
func windowsInterfaces() []Interface {
pcapInterfaces, _ := pcap.FindAllDevs()
interfaces := make([]Interface, len(pcapInterfaces))
for i, iface := range pcapInterfaces {
interfaces[i] = Interface{
Display: iface.Description + " => " + iface.Name,
Iface: net.Interface{
Index: 100 + i,
MTU: 1500,
Name: iface.Name,
HardwareAddr: nil,
Flags: net.Flags(iface.Flags),
},
}
}
return interfaces
}
func findInterfaces() []Interface {
if runtime.GOOS == "windows" {
return windowsInterfaces()
}
netInterfaces, _ := net.Interfaces()
interfaces := make([]Interface, len(netInterfaces))
for i, iface := range netInterfaces {
interfaces[i] = Interface{
Display: iface.Name,
Iface: iface,
}
}
return interfaces
}
func main() {
interfaces := findInterfaces()
interfacesByIndex := make(map[int]Interface)
index := 0
for _, iface := range interfaces {
interfacesByIndex[index] = iface
index = index + 1
}
printInterfaces(interfacesByIndex)
ifaceI := selectInterface(interfacesByIndex, "Please select first interface to be bridged")
ifaceII := selectInterface(interfacesByIndex, "Please select second interface to be bridged")
if (ifaceI.HardwareAddr != nil && ifaceI.HardwareAddr.String() == ifaceII.HardwareAddr.String()) || (ifaceI.HardwareAddr == nil && ifaceI.Name == ifaceII.Name) {
log.Fatal("You need to select different interfaces to bridge")
}
macConnections := cmap.New()
go Bridge(ifaceI, ifaceII, &macConnections)
Bridge(ifaceII, ifaceI, &macConnections)
}
func printInterfaces(interfacesByIndex map[int]Interface) {
keys := make([]int, 0)
for key := range interfacesByIndex {
keys = append(keys, key)
}
sort.Ints(keys)
for _, key := range keys {
fmt.Printf("[ %d ] %s => %s\n", key, interfacesByIndex[key].Display, interfacesByIndex[key].Iface.HardwareAddr)
}
}
func selectInterface(interfacesByIndex map[int]Interface, message string) *net.Interface {
for {
input := askUserInput(message)
key, err := strconv.Atoi(input)
if err != nil {
key = -1
}
iface, ok := interfacesByIndex[key]
if ok {
return &iface.Iface
} else {
fmt.Println("Wrong selection!")
}
}
}
func askUserInput(message string) string {
fmt.Println(message)
reader := bufio.NewReader(os.Stdin)
sentence, err := reader.ReadString('\n')
if err != nil {
log.Fatalf("Error occurred while reading user input: %s", err.Error())
}
sentence = strings.Replace(sentence, "\r\n", "", -1)
return strings.TrimSuffix(sentence, "\n")
}