62 lines
963 B
Go
62 lines
963 B
Go
package main
|
|
|
|
import (
|
|
"io"
|
|
"net"
|
|
goHttp "net/http"
|
|
"os"
|
|
)
|
|
|
|
func isPrivateIP(ip net.Addr) bool {
|
|
// Check if the IP is private
|
|
// https://en.wikipedia.org/wiki/Private_network
|
|
|
|
// IPv4
|
|
if ip4 := ip.(*net.TCPAddr).IP.To4(); ip4 != nil {
|
|
if ip4[0] == 10 {
|
|
return true
|
|
}
|
|
if ip4[0] == 172 && ip4[1] >= 16 && ip4[1] <= 31 {
|
|
return true
|
|
}
|
|
if ip4[0] == 192 && ip4[1] == 168 {
|
|
return true
|
|
}
|
|
return false
|
|
} else {
|
|
// IPv6
|
|
ip6 := ip.(*net.TCPAddr).IP.To16()
|
|
if ip6[0] == 0xfd && ip6[1] == 0x00 {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
}
|
|
|
|
func downloadFile(url, p string) error {
|
|
// download
|
|
out, err := os.Create(p)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
defer out.Close()
|
|
req, err := goHttp.NewRequest("GET", url, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
resp, err := goHttp.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
_, err = io.Copy(out, resp.Body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|