diff --git a/account/assets/js/panelHandlers/security/ip_list.js b/account/assets/js/panelHandlers/security/ip_list.js index 3505ed5..211d248 100644 --- a/account/assets/js/panelHandlers/security/ip_list.js +++ b/account/assets/js/panelHandlers/security/ip_list.js @@ -42,7 +42,6 @@ function listFilteredIPs(type) { if(xhr.status == 200) { if(xhr.response != undefined && xhr.response.length != 0) { jsonResponse = JSON.parse(xhr.response) - console.log(xhr.response); jQuery.each(jsonResponse, function(k, v) { ipModal.find('._js_currently-filtered-ips').append("
  • "+v.ip+"
  • "); }); diff --git a/pkg/database/ip_filtering.go b/pkg/database/ip_filtering.go index 92d5784..8b5598f 100644 --- a/pkg/database/ip_filtering.go +++ b/pkg/database/ip_filtering.go @@ -47,3 +47,26 @@ func (ds *Datastore) GetFilteredIPs(iptype string) (map[int]Struct_Filtered_IP, return filtered, nil } + +func (ds *Datastore) IsFiltered(ip string, iptype string) (bool, error) { + var holder Struct_Filtered_IP + found := false + + ds.handle.View(func(tx *bolt.Tx) error { + b := tx.Bucket([]byte(BUCKET_FILTERED_IPS)) + c := b.Cursor() + + for k, v := c.First(); k != nil; k, v = c.Next() { + json.Unmarshal(v, &holder) + + if holder.IP == ip && holder.Type == iptype { + found = true + break + } + } + + return nil + }) + + return found, nil +} diff --git a/pkg/public/filter.go b/pkg/public/filter.go new file mode 100644 index 0000000..60e692a --- /dev/null +++ b/pkg/public/filter.go @@ -0,0 +1,27 @@ +package public + +import ( + "net/http" + + "github.com/Ennovar/gPanel/pkg/database" + "github.com/Ennovar/gPanel/pkg/networking" +) + +func (con *Controller) Filter(req *http.Request, ftype string) bool { + ip := networking.GetClientIP(req) + + ds, err := database.Open(con.Directory + database.DB_MAIN) + if err != nil || ds == nil { + con.PublicLogger.Println(req.URL.Path + "::" + err.Error()) + return false + } + defer ds.Close() + + filtered, err := ds.IsFiltered(ip, ftype) + if err != nil { + con.PublicLogger.Println(req.URL.Path + "::" + err.Error()) + return false + } + + return filtered +} diff --git a/pkg/public/public.go b/pkg/public/public.go index c3ee523..2c3cd2e 100644 --- a/pkg/public/public.go +++ b/pkg/public/public.go @@ -11,7 +11,7 @@ import ( ) type Controller struct { - DocumentRoot string + Directory string Port int GracefulShutdownTimeout time.Duration Status int @@ -38,8 +38,8 @@ func New(dir string, port int) *Controller { loadLogger := log.New(fh, "LOAD :: ", 3) controller = Controller{ - DocumentRoot: dir + "public/", - Port: port, + Directory: dir, + Port: port, GracefulShutdownTimeout: 5 * time.Second, Status: 0, PublicLogger: publicLogger, diff --git a/pkg/public/servehttp.go b/pkg/public/servehttp.go index 0e24b05..b6fa197 100644 --- a/pkg/public/servehttp.go +++ b/pkg/public/servehttp.go @@ -15,6 +15,11 @@ import ( func (con *Controller) ServeHTTP(res http.ResponseWriter, req *http.Request) { startTime := time.Now() + if con.Filter(req, "block") { + http.Error(res, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) + return + } + switch con.Status { case 0: // This will actually never show because this function won't run if the server is off http.Error(res, "The server is currently down and not serving requests.", http.StatusServiceUnavailable) @@ -22,8 +27,10 @@ func (con *Controller) ServeHTTP(res http.ResponseWriter, req *http.Request) { case 1: // Normal break case 2: // Maintenance mode - http.Error(res, "The server is currently maintenance mode and not serving requests.", http.StatusServiceUnavailable) - return + if !con.Filter(req, "maintenance") { + http.Error(res, "The server is currently maintenance mode and not serving requests.", http.StatusServiceUnavailable) + return + } case 3: // This will actually never show because this function won't run if the server is off http.Error(res, "The server is currently restarting.", http.StatusServiceUnavailable) return @@ -31,9 +38,9 @@ func (con *Controller) ServeHTTP(res http.ResponseWriter, req *http.Request) { path := req.URL.Path[1:] if len(path) == 0 { - path = (con.DocumentRoot + "index.html") + path = (con.Directory + "public/" + "index.html") } else { - path = (con.DocumentRoot + path) + path = (con.Directory + "public/" + path) } f, err := os.Open(path) diff --git a/pkg/public/toggle.go b/pkg/public/toggle.go index a6ead45..608ede1 100644 --- a/pkg/public/toggle.go +++ b/pkg/public/toggle.go @@ -15,7 +15,7 @@ func (con *Controller) Start() error { con.Status = 1 go server.ListenAndServe() - fmt.Printf("Public server now serving out of %s on port %d\n", con.DocumentRoot, con.Port) + fmt.Printf("Public server now serving out of %s on port %d\n", con.Directory+"public/", con.Port) return nil }