Browse Source

Added an icap service that runs a series of queries against a list of services just for logging purpuses.

Eliezer Croitoru 6 months ago
parent
commit
b766f72be2
2 changed files with 21 additions and 16 deletions
  1. 11 11
      examples-icap/icap-hostname-logger/icap-main.go
  2. 10 5
      peers.go

+ 11 - 11
examples-icap/icap-hostname-logger/icap-main.go

@@ -171,16 +171,6 @@ func hostnameLogger(w icap.ResponseWriter, req *icap.Request) {
 			}
 		}
 
-		if debug > 0 {
-			fmt.Println("Request hostname:", req.Request.URL.Hostname(), "is not a googlevideo.com match")
-			fmt.Println("Request query UPN value:", req.Request.URL.Query().Get("upn"))
-		}
-
-		if debug > 0 {
-			fmt.Println("204 allowed?", allow204)
-			fmt.Println("modified?", modified)
-			fmt.Println("end of the line 204 response!.. Shouldn't happen.")
-		}
 		var testHostname string
 		switch req.Request.Method {
 		case "CONNECT":
@@ -223,10 +213,19 @@ func hostnameLogger(w icap.ResponseWriter, req *icap.Request) {
 		}
 
 		go func() {
-			_, _ = drblPeers.Check(testHostname)
+			peerMatch, peersRating := drblPeers.Check(testHostname)
+			if debug > 0 {
+				fmt.Println("ended drblpeer check for:", testHostname)
+				fmt.Println("drblpeer check results for:", testHostname, "Match:", peerMatch, "Rating:", peersRating)
+			}
 		}()
 
 		w.WriteHeader(204, nil, false)
+		if debug > 0 {
+			fmt.Println("204 allowed?", allow204)
+			fmt.Println("modified?", modified)
+			fmt.Println("End of REQMOD")
+		}
 		return
 	case "RESPMOD":
 		w.WriteHeader(204, nil, false)
@@ -316,6 +315,7 @@ func init() {
 
 	if debug > 0 {
 		fmt.Println("Peers", drblPeers)
+		fmt.Println("Peers", drblPeers.Peers)
 	}
 }
 

+ 10 - 5
peers.go

@@ -15,6 +15,7 @@ import (
 	"github.com/elico/dns_resolver"
 )
 
+// DrblClient ---
 type DrblClient struct {
 	Peername    string
 	Path        string
@@ -26,6 +27,9 @@ type DrblClient struct {
 	Client      *http.Client
 }
 
+// DRBLClientUA ---
+var DRBLClientUA = "DRBL-Peer Client V1"
+
 func New(peerName, protocol, path string, port int, weight int64, bladdr []string) *DrblClient {
 	return &DrblClient{peerName,
 		path,
@@ -60,6 +64,7 @@ func (instance *DrblClient) Check(hostname string, debug bool) (bool, bool, bool
 
 		request, err := http.NewRequest("HEAD", testurl.String(), nil)
 		//request.SetBasicAuth(*user, *pass)
+		request.Header.Set("User-Agent", DRBLClientUA)
 
 		resp, err := instance.Client.Do(request)
 		if err != nil {
@@ -155,15 +160,15 @@ func (instance *DrblClient) HttpCheckUrlWithSrc(requestUrl, src string, debug bo
 	_, err := url.Parse(requestUrl)
 	if err != nil {
 		if debug {
-			fmt.Fprintln(os.Stderr, "testing =>",requestUrl ,"src =>",src , "error =>",  err )
-	  }
+			fmt.Fprintln(os.Stderr, "testing =>", requestUrl, "src =>", src, "error =>", err)
+		}
 		return found, allow, admin, key, err
 	}
 
 	switch {
 	case instance.Protocol == "http" || instance.Protocol == "https":
 		if debug {
-			fmt.Fprintln(os.Stderr, "testing =>",requestUrl ,"src =>",src , "to blacklisting type =>",  instance.Protocol )
+			fmt.Fprintln(os.Stderr, "testing =>", requestUrl, "src =>", src, "to blacklisting type =>", instance.Protocol)
 		}
 		testurl, _ := url.Parse(instance.Protocol + "://" + instance.Peername + ":" + strconv.Itoa(instance.Port) + instance.Path)
 		testurlVals := url.Values{}
@@ -179,8 +184,8 @@ func (instance *DrblClient) HttpCheckUrlWithSrc(requestUrl, src string, debug bo
 		resp, err := instance.Client.Do(request)
 		if err != nil {
 			if debug {
-				fmt.Fprintln(os.Stderr, "testing =>" , requestUrl ,"testurl =>", testurl.String(), "src =>",src , "error =>",  err )
-				fmt.Fprintln(os.Stderr, "RESPOSE =>", resp )
+				fmt.Fprintln(os.Stderr, "testing =>", requestUrl, "testurl =>", testurl.String(), "src =>", src, "error =>", err)
+				fmt.Fprintln(os.Stderr, "RESPOSE =>", resp)
 			}
 			return found, allow, admin, key, err
 		}