Skip to content
Snippets Groups Projects
Select Git revision
  • ccf67c1a189973477a0a6b4a7499c5cd74280393
  • master default protected
  • 69-resize-image-before-upload
  • 60-add-match-salamander-modal-to-edit-salamander
  • 50-fix-server-error-message
  • 48-fix-gradle
  • 31-camera-communicate-with-api-and-delete-from-cache-2
  • 20-changing-verification-step-in-profile-to-modal
  • 4-add-all-basic-views
  • 1-setup
10 results

CustomButton.js

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    google.go 2.66 KiB
    package api
    
    import (
    	"bytes"
    	logging "dcsg2900-threattotal/logs"
    	"dcsg2900-threattotal/utils"
    	"encoding/json"
    	"fmt"
    	"io/ioutil"
    	"net/http"
    	"strings"
    	"sync"
    	//"dcsg2900-threattotal/main"
    )
    
    //Function to call the Google Safe Browsing API.
    //API documentation can be found in: https://developers.google.com/safe-browsing/v4
    // Contacted API Endpoint : https://safebrowsing.googleapis.com/v4/threatMatches
    func CallGoogleUrl(url string, response *utils.FrontendResponse2, wg *sync.WaitGroup) {
    	// Google API returnerer [] om den ikke kjenner til domenet / URL. Kan bruke dette til
    	// å avgjøre om det er malicious eller ikke.
    	defer wg.Done()
    
    	var httpSearchURL, httpsSearchURL string
    
    	if strings.Contains(url, "https://") {
    		httpsSearchURL = url
    		container := strings.SplitAfter(url, "https://")
    
    		httpSearchURL = "http://" + container[1]
    
    	} else if strings.Contains(url, "http://") {
    		httpSearchURL = url
    		container := strings.SplitAfter(url, "http://")
    		httpsSearchURL = "https://" + container[1]
    
    		
    	} else {
    		httpSearchURL = "http://" + url
    		httpsSearchURL = "https://" + url
    		
    	}
    
    	APIKey := utils.APIKeyGoogle
    
    	postURL := "https://safebrowsing.googleapis.com/v4/threatMatches:find?key=" + APIKey
    
    	var jsonData = []byte(`
    		{
    			"client": {
    			  "clientId":      "threattotal",
    			  "clientVersion": "1.5.2"
    			},
    			"threatInfo": {
    			  "threatTypes":      ["MALWARE", "SOCIAL_ENGINEERING", "THREAT_TYPE_UNSPECIFIED", "UNWANTED_SOFTWARE","POTENTIALLY_HARMFUL_APPLICATION"],
    			  "platformTypes":    ["ANY_PLATFORM"],
    			  "threatEntryTypes": ["URL"],
    			  "threatEntries": [
    				{"url": "`+ httpsSearchURL +`" },
    				{"url": "`+ httpSearchURL +`"}
    			  ]
    			}
    		}`)
    
    	req, err := http.NewRequest("POST", postURL, bytes.NewBuffer(jsonData))
    	if err != nil {
    		fmt.Println("Error: reading sending google api request")
    		logging.Logerror(err, "ERROR Sending google api request, Google API")
    
    	}
    	req.Header.Set("Content-Type", "application/json; charset=UTF-8")
    
    	client := &http.Client{}
    
    	res, err := client.Do(req)
    	if err != nil {
    		fmt.Println("Error: in google api response")
    		logging.Logerror(err, "ERROR reading google api response, Google API")
    		utils.SetGenericError(response)
    		return
    	}
    	defer res.Body.Close()
    
    	//fmt.Println("response Status:", res.Status)
    	//fmt.Print("Response Headers:", res.Header)
    	body, err := ioutil.ReadAll(res.Body)
    	if err != nil {
    		fmt.Println("Error: reading google api response")
    		logging.Logerror(err, "ERROR reading google api response, Google API")
    
    	}
    
    	var jsonResponse utils.GoogleSafeBrowsing
    
    	err = json.Unmarshal(body, &jsonResponse)
    	if err != nil {
    		fmt.Println(err)
    	}
    
    	utils.SetResponeObjectGoogle(jsonResponse, response)
    }