Просмотр исходного кода

Add simply TCP check during optsGuard to verify the given github URL

Eric Ripa 7 лет назад
Родитель
Сommit
23d5bf0b51
1 измененных файлов с 12 добавлено и 1 удалено
  1. 12 1
      main.go

+ 12 - 1
main.go

@@ -6,6 +6,7 @@ import (
 	"encoding/json"
 	"fmt"
 	"io/ioutil"
+	"net"
 	"net/http"
 	"net/url"
 	"os"
@@ -15,6 +16,7 @@ import (
 	"regexp"
 	"strings"
 	"sync"
+	"time"
 
 	"gopkg.in/src-d/go-git.v4/plumbing"
 
@@ -748,10 +750,19 @@ func optsGuard() error {
 		if !strings.HasSuffix(opts.GithubURL, "/") {
 			opts.GithubURL += "/"
 		}
-		_, err := url.Parse(opts.GithubURL)
+		ghURL, err := url.Parse(opts.GithubURL)
 		if err != nil {
 			return err
 		}
+		tcpPort := "443"
+		if ghURL.Scheme == "http" {
+			tcpPort = "80"
+		}
+		timeout := time.Duration(1 * time.Second)
+		_, err = net.DialTimeout("tcp", ghURL.Host+":"+tcpPort, timeout)
+		if err != nil {
+			return fmt.Errorf("%s unreachable, error: %s", ghURL.Host, err)
+		}
 	}
 
 	if opts.SingleSearch != "" {