Untitled diff
59 lines
// tryConnect binds to the given IP address and ask an internet service to
// tryConnect will bind to the ip address and ask a internet service to try to
// connect to it. binding is the address where we must listen (needed because
// connect to it. binding is the address where we must listen (needed because
// the reachable address might not be the same as the binding address => NAT, ip
// the reachable address might not be the same as the binding address => NAT, ip
// rules etc).
// rules etc).
// In case anything goes wrong, an error is returned.
func tryConnect(ip, binding network.Address) error {
func tryConnect(ip, binding network.Address) error {
stopCh := make(chan bool, 1)
stopCh := make(chan bool, 1)
// let's bind
// let's bind
go func() {
go func() {
ln, err := net.Listen("tcp", binding.NetworkAddress())
ln, err := net.Listen("tcp", binding.NetworkAddress())
if err != nil {
if err != nil {
log.Error("Trouble with binding to the address:", err)
fmt.Println("[-] Trouble with binding to the address:", err)
return
return
}
}
con, _ := ln.Accept()
con, _ := ln.Accept()
<-stopCh
<-stopCh
con.Close()
con.Close()
}()
}()
defer func() { stopCh <- true }()
defer func() { stopCh <- true }()
_, port, err := net.SplitHostPort(ip.NetworkAddress())
port := ip.Port()
if err != nil {
return err
}
values := url.Values{}
values := url.Values{}
values.Set("port", port)
values.Set("port", port)
values.Set("timeout", "default")
values.Set("timeout", "default")
// ask the check
// ask the check
url := whatsMyIP + "port-scanner/scan.php"
url := whatsMyIP + "port-scanner/scan.php"
req, err := http.NewRequest("POST", url, bytes.NewBufferString(values.Encode()))
req, err := http.NewRequest("POST", url, bytes.NewBufferString(values.Encode()))
if err != nil {
if err != nil {
return err
return err
}
}
req.Header.Set("Host", "www.whatsmyip.org")
req.Header.Set("Host", "www.whatsmyip.org")
req.Header.Set("Referer", "http://www.whatsmyip.org/port-scanner/")
req.Header.Set("Referer", "http://www.whatsmyip.org/port-scanner/")
req.Header.Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:46.0) Gecko/20100101 Firefox/46.0")
req.Header.Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:46.0) Gecko/20100101 Firefox/46.0")
req.Header.Set("Accept", "*/*")
req.Header.Set("Accept", "*/*")
req.Header.Set("Accept-Language", "en-US,en;q=0.5")
req.Header.Set("Accept-Language", "en-US,en;q=0.5")
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
req.Header.Set("X-Requested-With", "XMLHttpRequest")
req.Header.Set("X-Requested-With", "XMLHttpRequest")
client := &http.Client{}
client := &http.Client{}
resp, err := client.Do(req)
resp, err := client.Do(req)
if err != nil {
if err != nil {
return err
return err
}
}
buffer, err := ioutil.ReadAll(resp.Body)
buffer, err := ioutil.ReadAll(resp.Body)
if err != nil {
if err != nil {
return err
return err
}
}
if !bytes.Contains(buffer, []byte("1")) {
if !bytes.Contains(buffer, []byte("1")) {
return errors.New("Address unreachable")
return errors.New("Address unrechable")
}
}
return nil
return nil
}
}