-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnet.go
212 lines (175 loc) · 4.31 KB
/
net.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package gryphon
import (
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"strconv"
"strings"
"github.com/jackpal/gateway"
"github.com/whiterabb17/gryphon/variables"
)
// GetGlobalIp is used to return the global Ip address of the machine.
func GetGlobalIp() string {
ip := ""
resolvers := []string{
"https://api.ipify.org?format=text",
"http://api.ipify.org",
"http://api.ipify.org?format=text",
"https://api.ipify.org",
}
for {
url := RandomSelectStr(resolvers)
resp, err := http.Get(url)
if err != nil {
log.Printf("%v\n", err)
}
defer resp.Body.Close()
i, _ := ioutil.ReadAll(resp.Body)
ip = string(i)
if resp.StatusCode == 200 {
break
}
}
return ip
}
// GetLocalIp is used to get the local Ip address of the machine.
func GetLocalIp() string {
conn, _ := net.Dial("udp", "8.8.8.8:80")
defer conn.Close()
ip := conn.LocalAddr().(*net.UDPAddr).IP
return fmt.Sprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3])
}
// GetGatewayIP returns the Ip address of the gateway in the network where the machine resides.
func GetGatewayIP() string {
ip, err := gateway.DiscoverGateway()
ExitOnError(err)
return ip.String()
}
// Iface returns the currently used wireless interface and its MAC address.
func Iface() (string, string) {
current_iface := ""
interfaces, err := net.Interfaces()
ExitOnError(err)
for _, interf := range interfaces {
if addrs, err := interf.Addrs(); err == nil {
for _, addr := range addrs {
if strings.Contains(addr.String(), GetLocalIp()) {
current_iface = interf.Name
}
}
}
}
netInterface, err := net.InterfaceByName(current_iface)
ExitOnError(err)
name := netInterface.Name
macAddress := netInterface.HardwareAddr
hwAddr, err := net.ParseMAC(macAddress.String())
ExitOnError(err)
return name, hwAddr.String()
}
// Ifaces returns the names of all local interfaces.
func Ifaces() []string {
ifs := []string{}
interfaces, _ := net.Interfaces()
for _, interf := range interfaces {
ifs = append(ifs, interf.Name)
}
return ifs
}
// SendDataTCP sends data to a given host:port using the TCP protocol.
func SendDataTCP(host string, port int, data string) error {
addr := host + ":" + strconv.Itoa(port)
conn, err := net.Dial("tcp", addr)
if err != nil {
return err
}
_, err = io.WriteString(conn, data+"\n")
if err != nil {
return err
}
defer conn.Close()
return nil
}
// SendDataUDP sends data to a given host:port using the UDP protocol.
func SendDataUDP(host string, port int, data string) error {
addr := host + ":" + strconv.Itoa(port)
conn, err := net.Dial("udp", addr)
if err != nil {
return err
}
_, err = io.WriteString(conn, data+"\n")
if err != nil {
return err
}
defer conn.Close()
return nil
}
// Download downloads a file from a url.
func Download(url string) (string, error) {
splitted := strings.Split(url, "/")
filename := splitted[len(splitted)-1]
f, err := os.Create(filename)
if err != nil {
return "Failed", err
}
defer f.Close()
response, err := http.Get(url)
if err != nil {
return filename, err
}
defer response.Body.Close()
_, err = io.Copy(f, response.Body)
if err != nil {
return filename, err
}
return filename, nil
}
func DownloadAndExecute(url string, cmd string) (string, error) {
return downloadAndExecute(url, cmd)
}
// Networks returns a list of nearby wireless networks.
func Networks() ([]string, error) {
return networks()
}
// ExpandCidr returns a list of Ip addresses within a given CIDR.
func ExpandCidr(cidr string) ([]string, error) {
ip, ipnet, err := net.ParseCIDR(cidr)
if err != nil {
return nil, err
}
var ips []string
for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); variables.IpIncrement(ip) {
ips = append(ips, ip.String())
}
lenIPs := len(ips)
switch {
case lenIPs < 2:
return ips, nil
default:
return ips[1 : len(ips)-1], nil
}
}
// DnsLookup returns the list of Ip adddress associated with the given hostname.
func DnsLookup(hostname string) ([]string, error) {
i := []string{}
ips, err := net.LookupIP(hostname)
if err != nil {
return nil, err
}
for _, ip := range ips {
i = append(i, ip.String())
}
return i, nil
}
// RdnsLookup returns the list of hostnames associated with the given Ip address.
func RdnsLookup(ip string) ([]string, error) {
ips, err := net.LookupAddr(ip)
if err != nil {
return nil, err
}
return ips, nil
}