Browse Source

auto update script executed

Toby Chui 1 year ago
parent
commit
f8b5ed02bc
5 changed files with 133 additions and 32 deletions
  1. 1 1
      api.go
  2. 21 0
      blacklist.go
  3. 43 5
      mod/geodb/geodb.go
  4. 45 6
      web/components/blacklist.html
  5. 23 20
      web/components/redirection.html

+ 1 - 1
api.go

@@ -99,6 +99,6 @@ func initAPIs() {
 	authRouter.HandleFunc("/api/blacklist/country/remove", handleCountryBlacklistRemove)
 	authRouter.HandleFunc("/api/blacklist/ip/add", handleIpBlacklistAdd)
 	authRouter.HandleFunc("/api/blacklist/ip/remove", handleIpBlacklistRemove)
-
+	authRouter.HandleFunc("/api/blacklist/enable", handleBlacklistEnable)
 	//If you got APIs to add, append them here
 }

+ 21 - 0
blacklist.go

@@ -79,3 +79,24 @@ func handleIpBlacklistRemove(w http.ResponseWriter, r *http.Request) {
 
 	utils.SendOK(w)
 }
+
+func handleBlacklistEnable(w http.ResponseWriter, r *http.Request) {
+	enable, err := utils.PostPara(r, "enable")
+	if err != nil {
+		//Return the current enabled state
+		currentEnabled := geodbStore.Enabled
+		js, _ := json.Marshal(currentEnabled)
+		utils.SendJSONResponse(w, string(js))
+	} else {
+		if enable == "true" {
+			geodbStore.ToggleBlacklist(true)
+		} else if enable == "false" {
+			geodbStore.ToggleBlacklist(false)
+		} else {
+			utils.SendErrorResponse(w, "invalid enable state: only true and false is accepted")
+			return
+		}
+
+		utils.SendOK(w)
+	}
+}

+ 43 - 5
mod/geodb/geodb.go

@@ -10,8 +10,9 @@ import (
 )
 
 type Store struct {
-	geodb *geoip2.Reader
-	sysdb *database.Database
+	Enabled bool
+	geodb   *geoip2.Reader
+	sysdb   *database.Database
 }
 
 type CountryInfo struct {
@@ -35,12 +36,26 @@ func NewGeoDb(sysdb *database.Database, dbfile string) (*Store, error) {
 		return nil, err
 	}
 
+	err = sysdb.NewTable("blacklist")
+	if err != nil {
+		return nil, err
+	}
+
+	blacklistEnabled := false
+	sysdb.Read("blacklist", "enabled", &blacklistEnabled)
+
 	return &Store{
-		geodb: db,
-		sysdb: sysdb,
+		Enabled: blacklistEnabled,
+		geodb:   db,
+		sysdb:   sysdb,
 	}, nil
 }
 
+func (s *Store) ToggleBlacklist(enabled bool) {
+	s.sysdb.Write("blacklist", "enabled", enabled)
+	s.Enabled = enabled
+}
+
 func (s *Store) ResolveCountryCodeFromIP(ipstring string) (*CountryInfo, error) {
 	// If you are using strings that may be invalid, check that ip is not nil
 	ip := net.ParseIP(ipstring)
@@ -97,7 +112,25 @@ func (s *Store) RemoveIPFromBlackList(ipAddr string) {
 func (s *Store) IsIPBlacklisted(ipAddr string) bool {
 	var isBlacklisted bool = false
 	s.sysdb.Read("blacklist-ip", ipAddr, &isBlacklisted)
-	return isBlacklisted
+	if isBlacklisted {
+		return true
+	}
+
+	//Check for IP wildcard and CIRD rules
+	AllBlacklistedIps := s.GetAllBlacklistedIp()
+	for _, blacklistRule := range AllBlacklistedIps {
+		wildcardMatch := MatchIpWildcard(ipAddr, blacklistRule)
+		if wildcardMatch {
+			return true
+		}
+
+		cidrMatch := MatchIpCIDR(ipAddr, blacklistRule)
+		if cidrMatch {
+			return true
+		}
+	}
+
+	return false
 }
 
 func (s *Store) GetAllBlacklistedIp() []string {
@@ -117,6 +150,11 @@ func (s *Store) GetAllBlacklistedIp() []string {
 
 //Check if a IP address is blacklisted, in either country or IP blacklist
 func (s *Store) IsBlacklisted(ipAddr string) bool {
+	if !s.Enabled {
+		//Blacklist not enabled. Always return false
+		return false
+	}
+
 	if ipAddr == "" {
 		//Unable to get the target IP address
 		return false

+ 45 - 6
web/components/blacklist.html

@@ -2,11 +2,20 @@
 <h3><i class="ui ban icon"></i> Blacklist</h3>
 <p>Setup blacklist based on estimated IP geographic location or IP address</p>
 <div class="ui divider"></div>
-<h4>Country Blacklist</h4>
-<div class="ui yellow message">
-    <i class="yellow exclamation triangle icon"></i>
-    This will block all requests from the selected country. The requester's location is estimated from their IP address and may not be 100% accurate.
+<div class="ui toggle checkbox">
+    <input type="checkbox" id="enableBlacklist">
+    <label>Enable Blacklist</label>
+</div>
+<div id="toggleSucc" style="float: right; display:none; color: #2abd4d;" >
+    <i class="ui green checkmark icon"></i> Setting Saved
+</div>
+<div class="ui message">
+    <i class="info circle icon"></i> Blacklist function require complex checking logic to validate each incoming request. Not recommend enabling this feature on servers with low end hardware.
 </div>
+<div class="ui divider"></div>
+<h4>Country Blacklist</h4>
+    <p><i class="yellow exclamation triangle icon"></i>
+    This will block all requests from the selected country. The requester's location is estimated from their IP address and may not be 100% accurate.</p>
 
 <div class="ui form">
     <div class="field">
@@ -263,7 +272,7 @@
     </div>
     <button class="ui basic red button" id="ban-btn" onclick="addCountryToBlacklist();"><i class="ui red ban icon"></i> Blacklist Country</button>
 </div>
-<table class="ui celled table">
+<table class="ui unstackable basic celled table">
     <thead>
         <tr>
             <th>ISO Code</th>
@@ -288,7 +297,7 @@
     </button>
   </div>
   
-  <table class="ui celled table">
+  <table class="ui unstackable basic celled table">
     <thead>
       <tr>
         <th>IP Address</th>
@@ -550,4 +559,34 @@
             });
         }
     }
+
+    //function to check for blacklist enable
+    function enableBlacklist() {
+        var isChecked = $('#enableBlacklist').is(':checked');
+        $.ajax({
+            type: 'POST',
+            url: '/api/blacklist/enable',
+            data: { enable: isChecked },
+            success: function(data){
+                $("#toggleSucc").stop().finish().fadeIn("fast").delay(3000).fadeOut("fast");
+            }
+        });
+    }
+
+    function initBlacklistEnableState(){
+        $.get('/api/blacklist/enable', function(data){
+            if (data == true){
+                $('#enableBlacklist').parent().checkbox("set checked");
+            }
+
+            //Register on change event
+            $("#enableBlacklist").on("change", function(){
+                enableBlacklist();
+            })
+        });
+    }
+    initBlacklistEnableState();
+
+
+
 </script>

+ 23 - 20
web/components/redirection.html

@@ -1,26 +1,29 @@
 <h3><i class="level up alternate icon"></i> Redirection Rules</h3>
 <p>Add exception case for redirecting any matching URLs</p>
 <div class="ui basic segment">
-    <table class="ui sortable unstackable celled table">
-        <thead>
-            <tr>
-                <th>Redirection URL</th>
-                <th class="no-sort"></th>
-                <th>Destination URL</th>
-                <th class="no-sort">Copy Pathname</th>
-                <th class="no-sort">Status Code</th>
-                <th class="no-sort">Remove</th>
-            </tr>
-        </thead>
-        <tbody id="redirectionRuleList">
-            <tr>
-                <td></td>
-                <td></td>
-                <td></td>
-                <td></td>
-            </tr>
-        </tbody>
-    </table>
+  <div style="width: 100%; overflow-x: auto;">
+    <table class="ui sortable unstackable celled table" >
+      <thead>
+          <tr>
+              <th>Redirection URL</th>
+              <th class="no-sort"></th>
+              <th>Destination URL</th>
+              <th class="no-sort">Copy Pathname</th>
+              <th class="no-sort">Status Code</th>
+              <th class="no-sort">Remove</th>
+          </tr>
+      </thead>
+      <tbody id="redirectionRuleList">
+          <tr>
+              <td></td>
+              <td></td>
+              <td></td>
+              <td></td>
+          </tr>
+      </tbody>
+  </table>
+  </div>
+    
     <div class="ui green message" id="delRuleSucc" style="display:none;">
       <i class="ui green checkmark icon"></i> Redirection Rule Deleted
     </div>