Skip to content

Commit

Permalink
Merge pull request #15 from cloudoptlab/fix/1.4.1/fix-grade-website
Browse files Browse the repository at this point in the history
Fix abnormal URLs' grades and icon colors
  • Loading branch information
T-baby committed Aug 4, 2019
2 parents 1e331cc + a710f66 commit 4284a99
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 29 deletions.
62 changes: 38 additions & 24 deletions js/cloudopt-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -944,8 +944,8 @@ cloudopt.grade = (function (cloudopt) {

/**
* Decide the secure level of a website
*
* @param result result of website()
*
* @param result result of website()
*/
function classify(result) {
var level = 0; /* no level */
Expand All @@ -955,6 +955,8 @@ cloudopt.grade = (function (cloudopt) {
level = 2; /* normal */
} else if (result.score >= 40) {
level = 3; /* potential threat */
} else if (result.score === 0 && result.safe) {
level = 5; /* unknown */
} else {
level = 4; /* dangerous */
}
Expand All @@ -969,9 +971,12 @@ cloudopt.grade = (function (cloudopt) {
safe = true;
}
if (level === 3) {
if (config.safePotential)
safe = false;
else
if (config.safePotential)
safe = false;
else
safe = true;
}
if (level === 5) {
safe = true;
}
if (config.whiteList.indexOf(result.host) > -1 || config.whiteListAds.indexOf(result.host) > -1) {
Expand All @@ -984,47 +989,63 @@ cloudopt.grade = (function (cloudopt) {
return safe;
}

function couldGrade(url) {
if (!url.startsWith("http://") && !url.startsWith("https://")) {
return false;
}
var host = cloudopt.utils.getHost(url);
if (/^([0-9]{1,3}\.){3}[0-9]{1,3}$/.test(host)
|| "localhost" === host) {
return false;
}

return true;
}

/**
* Check the url and get the web data. Such as score ,type, host
*
* @param website url
* @param callback callback function
*/
function website(website, callback) {
var cacheSuffix = "_grade_1.0";
website = cloudopt.utils.getHost(website);
website = website + cacheSuffix;
// safe=true and score=0 represent that this site has no grade
var result = {
safe: true,
type: "",
date: new Date(),
score: 0,
host: ""
};
if (website.startsWith("192.") || website.startsWith("localhost") || website.startsWith("127.")) {
return result;
if (!couldGrade(website)) {
callback(result);
return;
}
result.host = website.replace(cacheSuffix, "");

cloudopt.store.get(website, function (item) {
var cacheSuffix = "_grade_1.0";
website = cloudopt.utils.getHost(website);
result.host = website;
cacheKey = website + cacheSuffix;

cloudopt.store.get(cacheKey, function (item) {
if (item != undefined && JSON.stringify(item) != "{}" && cloudopt.utils.comparisonDate(item.date, defaultExpireTime.safeWebsite)) {
cloudopt.config.refresh(function () {
item.safe = isSafe(item);
callback(item);
});
return;
} else {
cloudopt.store.remove(website);
cloudopt.http.get(cloudopt.host + 'grade/website/' + website.replace(cacheSuffix, ''),{
cloudopt.store.remove(cacheKey);
cloudopt.http.get(cloudopt.host + 'grade/website/' + website, {
timeout: 30000
}).carryApiKey().then(data=>{
result.safe = isSafe(data.result);
result.type = data.result.type;
result.score = data.result.score;

if (data.result.host != "") {
result.date = new Date().getTime();
cloudopt.store.set(website, result);
cloudopt.store.set(cacheKey, result);
}
callback(result);
},error=>{
Expand Down Expand Up @@ -1161,17 +1182,10 @@ cloudopt.browserIconChange = (function (cloudopt) {

function auto(url) {
cloudopt.grade.website(url, function(result) {
if (url.indexOf("file://") == 0
|| url.indexOf("chrome-extension://") == 0
|| url.indexOf("chrome://") == 0) {
gray();
return;
}

var level = cloudopt.grade.classify(result);
if (result.safe === false) {
danger();
} else if (level == 3) {
} else if (level == 3 || level == 5) {
gray();
} else if (level == 2) {
normal();
Expand Down
6 changes: 1 addition & 5 deletions js/cloudopt-popup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cloudopt.onFinish(function() {

function _checkEvaluateTips(url) {
const key = "evaluate_tip_"+url;
cloudopt.store.get(key, function(lastShown) {
Expand Down Expand Up @@ -33,10 +33,6 @@ cloudopt.onFinish(function() {
active: true,
currentWindow: true
}, function(tabArray) {
if (tabArray[tabArray.length - 1].url.indexOf("file://") == 0 || tabArray[tabArray.length - 1].url.indexOf("chrome-extension://") == 0 || tabArray[tabArray.length - 1].url.indexOf("chrome://") == 0) {
$(".score h1").text("?");
return;
}

url = tabArray[tabArray.length - 1].url;

Expand Down

0 comments on commit 4284a99

Please sign in to comment.