implimented IP filtering on public

This commit is contained in:
George Shaw 2017-11-30 16:48:04 -06:00
parent 40e1e3d9d6
commit 8b6a4e65b2
6 changed files with 65 additions and 9 deletions

View file

@ -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("<li>"+v.ip+"</li>");
});

View file

@ -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
}

27
pkg/public/filter.go Normal file
View file

@ -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
}

View file

@ -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,

View file

@ -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)

View file

@ -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
}