Merge pull request #132 from george-e-shaw-iv/master

Fixed domain router issues and implemented subdomains
This commit is contained in:
George Shaw 2018-01-23 16:02:36 -06:00 committed by GitHub
commit 441448092d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 399 additions and 15 deletions

View file

@ -0,0 +1,30 @@
jQuery('._js_add-subdomain-form').on('submit', function(e){
e.preventDefault();
if(jQuery('#addSubdomain') && jQuery('#addSubdomain').val() && jQuery('#subdomainRoot') && jQuery('#subdomainRoot').val()) {
var requestData = {};
requestData["name"] = jQuery('#addSubdomain').val();
requestData["root"] = jQuery('#subdomainRoot').val();
var xhr = new XMLHttpRequest();
xhr.open(jQuery(this).attr('method'), jQuery(this).attr('action'), true);
xhr.send(JSON.stringify(requestData));
xhr.onloadend = function() {
if(xhr.status == 204) {
ListSubdomains();
}
else {
if(xhr.response != undefined && xhr.response.length != 0) {
alert('Error: ' + xhr.status);
}
else {
alert('An error has occurred. If problem persists please contact your community administrator.');
}
}
}
}
else {
alert('All fields must be filled out to submit this form.');
}
});

View file

@ -0,0 +1,37 @@
var subdomainModal = jQuery('.subdomain-management-modal');
jQuery('._js_subdomain-management').on('click', function(e){
e.preventDefault();
ListSubdomains();
subdomainModal.modal('show');
});
function ListSubdomains() {
var list = jQuery('._js_registered-subdomains');
var xhr = new XMLHttpRequest();
xhr.open('GET', 'api/subdomain/list', true);
xhr.send();
xhr.onloadend = function() {
list.html('');
if(xhr.status == 200) {
jsonResponse = JSON.parse(xhr.response)
jQuery.each(jsonResponse, function(k, v) {
list.append('<div class="row mt-2"><div class="col-6"><p class="mb-0">'+k+'</p><small>Root: '+v.root+'</small></div><div class="col-6 d-flex justify-content-end"><div class="btn-group" role="group"><button class="btn btn-outline-danger _js_delete-registered-subdomain" data="'+k+'">Delete</button></div></div></div>');
});
}
else if(xhr.status == 204) {
list.html('<div class="row mt-2"><div class="col-6 d-flex align-items-center"><p>No registered subdomains exist for this account.</p></div></div>');
}
else {
if(xhr.response != undefined && xhr.response.length != 0) {
alert('Error: ' + xhr.status);
}
else {
alert('An error has occurred. If problem persists please contact your community administrator.');
}
}
}
}

View file

@ -0,0 +1,29 @@
jQuery(document).on('click', '._js_delete-registered-subdomain', function(e){
e.preventDefault();
var subdomain = jQuery(this).attr('data');
var ensure = confirm("Are you sure you want to delete the subdomain \""+ subdomain +"\" from your account?");
if(ensure) {
var requestData = {};
requestData["name"] = subdomain;
var xhr = new XMLHttpRequest();
xhr.open('DELETE', 'api/subdomain/remove', true);
xhr.send(JSON.stringify(requestData));
xhr.onloadend = function () {
if (xhr.status == 204) {
ListSubdomains();
}
else {
if (xhr.response != undefined && xhr.response.length != 0) {
alert('Error: ' + xhr.status);
}
else {
alert('An error has occurred. If problem persists please contact your community administrator.');
}
}
}
}
});

View file

@ -349,6 +349,48 @@
</div>
</div>
<!-- Sub-domain Management Modal -->
<div class="modal fade subdomain-management-modal" tabindex="-1" role="dialog" aria-labelledby="subdomain-management-modal" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Sub-domain Management</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<h4>Add Sub-domain</h4>
<form method="POST" action="api/subdomain/add" class="_js_add-subdomain-form">
<div class="form-group">
<label for="addSubdomain" class="">Sub-domain</label>
<div class="input-group">
<input type="text" id="addSubdomain" class="form-control" placeholder="Sub-domain">
</div>
</div>
<div class="form-group">
<label for="subdomainRoot" class="">Sub-domain Root</label>
<div class="input-group">
<input type="text" id="subdomainRoot" aria-describedby="subdomainRootHelp" class="form-control" placeholder="Sub-domain Root">
</div>
<small id="subdomainRootHelp" class="form-text text-muted">A directory within your /home/document_root/ to serve as the root for this sub-domain</small>
</div>
<div class="btn-group" role="group">
<button type="submit" class="btn btn-primary">Add Sub-domain</button>
</div>
</form>
<h4 class="mt-3 mb-0">Registered Sub-domains</h4>
<div class="container-full _js_registered-subdomains">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-12">
@ -447,6 +489,7 @@
<h6 class="card-subtitle mb-4 text-muted">View registered domains, register new domains, and set up or manage existing document roots for sub-domains.</h6>
<div class="btn-group" role="group">
<button type="button" class="btn btn-outline-primary _js_domain-management">Domain Management</button>
<button type="button" class="btn btn-outline-primary _js_subdomain-management">Sub-domain Management</button>
</div>
</div>
</div>
@ -501,6 +544,9 @@
<script type="text/javascript" src="assets/js/panelHandlers/domains_subdomains/domain_management.js"></script>
<script type="text/javascript" src="assets/js/panelHandlers/domains_subdomains/domain_link.js"></script>
<script type="text/javascript" src="assets/js/panelHandlers/domains_subdomains/domain_unlink.js"></script>
<script type="text/javascript" src="assets/js/panelHandlers/domains_subdomains/subdomain_management.js"></script>
<script type="text/javascript" src="assets/js/panelHandlers/domains_subdomains/subdomain_add.js"></script>
<script type="text/javascript" src="assets/js/panelHandlers/domains_subdomains/subdomain_remove.js"></script>
<!-- KEEP AT BOTTOM OF BODY TAGS -->
</body>
</html>

48
pkg/api/subdomain/add.go Normal file
View file

@ -0,0 +1,48 @@
package subdomain
import (
"encoding/json"
"log"
"net/http"
"strconv"
"github.com/Ennovar/gPanel/pkg/database"
)
func Add(res http.ResponseWriter, req *http.Request, logger *log.Logger, dir string) bool {
if req.Method != "POST" {
logger.Println(req.URL.Path + "::" + req.Method + "::" + strconv.Itoa(http.StatusMethodNotAllowed) + "::" + http.StatusText(http.StatusMethodNotAllowed))
http.Error(res, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return false
}
var requestData struct {
Name string `json:"name"`
Root string `json:"root"`
}
err := json.NewDecoder(req.Body).Decode(&requestData)
if err != nil {
logger.Println(req.URL.Path + "::" + err.Error())
http.Error(res, err.Error(), http.StatusBadRequest)
return false
}
ds, err := database.Open(dir + database.DB_MAIN)
if err != nil || ds == nil {
logger.Println(req.URL.Path + "::" + err.Error())
http.Error(res, err.Error(), http.StatusInternalServerError)
return false
}
defer ds.Close()
err = ds.Put(database.BUCKET_SUBDOMAINS, []byte(requestData.Name), database.StructSubdomain{Root: requestData.Root})
if err != nil {
logger.Println(req.URL.Path + "::" + err.Error())
http.Error(res, err.Error(), http.StatusInternalServerError)
return false
}
res.WriteHeader(http.StatusNoContent)
return true
}

49
pkg/api/subdomain/list.go Normal file
View file

@ -0,0 +1,49 @@
package subdomain
import (
"encoding/json"
"log"
"net/http"
"strconv"
"github.com/Ennovar/gPanel/pkg/database"
)
func List(res http.ResponseWriter, req *http.Request, logger *log.Logger, dir string) bool {
if req.Method != "GET" {
logger.Println(req.URL.Path + "::" + req.Method + "::" + strconv.Itoa(http.StatusMethodNotAllowed) + "::" + http.StatusText(http.StatusMethodNotAllowed))
http.Error(res, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return false
}
ds, err := database.Open(dir + database.DB_MAIN)
if err != nil || ds == nil {
logger.Println(req.URL.Path + "::" + err.Error())
http.Error(res, err.Error(), http.StatusInternalServerError)
return false
}
defer ds.Close()
subdomains, err := ds.ListSubdomains()
if err != nil {
logger.Println(req.URL.Path + "::" + err.Error())
http.Error(res, err.Error(), http.StatusInternalServerError)
return false
}
if len(subdomains) > 0 {
b, err := json.Marshal(subdomains)
if err != nil {
logger.Println(req.URL.Path + "::" + err.Error())
http.Error(res, err.Error(), http.StatusInternalServerError)
return false
}
res.WriteHeader(http.StatusOK)
res.Write(b)
return true
}
res.WriteHeader(http.StatusNoContent)
return true
}

View file

@ -0,0 +1,47 @@
package subdomain
import (
"encoding/json"
"log"
"net/http"
"strconv"
"github.com/Ennovar/gPanel/pkg/database"
)
func Remove(res http.ResponseWriter, req *http.Request, logger *log.Logger, dir string) bool {
if req.Method != "DELETE" {
logger.Println(req.URL.Path + "::" + req.Method + "::" + strconv.Itoa(http.StatusMethodNotAllowed) + "::" + http.StatusText(http.StatusMethodNotAllowed))
http.Error(res, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return false
}
var requestData struct {
Name string `json:"name"`
}
err := json.NewDecoder(req.Body).Decode(&requestData)
if err != nil {
logger.Println(req.URL.Path + "::" + err.Error())
http.Error(res, err.Error(), http.StatusBadRequest)
return false
}
ds, err := database.Open(dir + database.DB_MAIN)
if err != nil || ds == nil {
logger.Println(req.URL.Path + "::" + err.Error())
http.Error(res, err.Error(), http.StatusInternalServerError)
return false
}
defer ds.Close()
err = ds.Delete(database.BUCKET_SUBDOMAINS, []byte(requestData.Name))
if err != nil {
logger.Println(req.URL.Path + "::" + err.Error())
http.Error(res, err.Error(), http.StatusInternalServerError)
return false
}
res.WriteHeader(http.StatusNoContent)
return true
}

View file

@ -14,7 +14,7 @@ import (
const (
DB_MAIN = "datastore.db"
DB_SETTINGS = "settings.db"
DB_DOMAINS = "domains.db"
DB_DOMAINS = "domains.db"
)
// Bucket constants
@ -23,9 +23,10 @@ const (
BUCKET_USERS = "users"
BUCKET_PORTS = "ports"
BUCKET_FILTERED_IPS = "filtered_ips"
BUCKET_SUBDOMAINS = "subdomains"
// DB_SETTINGS BUCKETS
BUCKET_GENERAL = "general"
BUCKET_GENERAL = "general"
BUCKET_NAMESERVERS = "nameservers"
// DB_DOMAINS BUCKETS
@ -71,6 +72,11 @@ func Open(filepath string) (*Datastore, error) {
if err != nil {
return err
}
_, err = tx.CreateBucketIfNotExists([]byte(BUCKET_SUBDOMAINS))
if err != nil {
return err
}
}
if strings.HasSuffix(filepath, DB_SETTINGS) {

View file

@ -0,0 +1,30 @@
package database
import (
"encoding/json"
"github.com/boltdb/bolt"
)
type StructSubdomain struct {
Root string `json:"root"`
}
func (ds *Datastore) ListSubdomains() (map[string]StructSubdomain, error) {
filtered := make(map[string]StructSubdomain)
var holder StructSubdomain
ds.handle.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(BUCKET_SUBDOMAINS))
c := b.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
json.Unmarshal(v, &holder)
filtered[string(k)] = holder
}
return nil
})
return filtered, nil
}

View file

@ -11,6 +11,7 @@ import (
"github.com/Ennovar/gPanel/pkg/api/server"
"github.com/Ennovar/gPanel/pkg/api/settings"
"github.com/Ennovar/gPanel/pkg/api/ssh"
"github.com/Ennovar/gPanel/pkg/api/subdomain"
"github.com/Ennovar/gPanel/pkg/api/user"
)
@ -74,6 +75,12 @@ func (con *Controller) apiHandler(res http.ResponseWriter, req *http.Request) (b
return true, ssh.DeleteKey(res, req, con.APILogger)
case "/ssh/getkeys":
return true, ssh.GetKeys(res, req, con.APILogger)
case "/subdomain/list":
return true, subdomain.List(res, req, con.APILogger, con.Directory)
case "/subdomain/add":
return true, subdomain.Add(res, req, con.APILogger, con.Directory)
case "/subdomain/remove":
return true, subdomain.Remove(res, req, con.APILogger, con.Directory)
default:
return false, false
}

View file

@ -42,7 +42,7 @@ func New(dir, name string, accPort, pubPort int) *Controller {
DocumentRoot: "account/",
Name: name,
Port: accPort,
Public: public.New("/home/"+name+"/", pubPort),
Public: public.New("/home/"+name+"/", dir, pubPort),
GracefulShutdownTimeout: 5 * time.Second,
Status: 0,
AccountLogger: accountLogger,

View file

@ -6,8 +6,9 @@ import (
"os"
"strconv"
"github.com/Ennovar/gPanel/pkg/routing"
"strings"
"github.com/Ennovar/gPanel/pkg/routing"
)
func (con *Controller) ServeHTTP(res http.ResponseWriter, req *http.Request) {
@ -18,6 +19,11 @@ func (con *Controller) ServeHTTP(res http.ResponseWriter, req *http.Request) {
path = (con.Directory + con.DocumentRoot + path)
}
if strings.HasSuffix(path, "throw400") {
http.Error(res, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
if strings.HasSuffix(path, "index.html") {
if con.checkAuth(res, req) == true {
http.Redirect(res, req, "gPanel.html", http.StatusFound)

View file

@ -12,6 +12,7 @@ import (
type Controller struct {
Directory string
AccountDirectory string
Port int
GracefulShutdownTimeout time.Duration
Status int
@ -23,15 +24,16 @@ var controller Controller
var server http.Server
// New function returns a new PublicWeb type.
func New(dir string, port int) *Controller {
func New(dir, accountDir string, port int) *Controller {
ph, lh, err := getLogHandles(dir)
if err != nil {
log.Fatalf("Error trying to start logging instances within %v: %v", dir, err.Error())
}
controller = Controller{
Directory: dir,
Port: port,
Directory: dir,
AccountDirectory: accountDir,
Port: port,
GracefulShutdownTimeout: 5 * time.Second,
Status: 0,
PublicLogger: ph,

View file

@ -12,6 +12,7 @@ import (
"strings"
"time"
"github.com/Ennovar/gPanel/pkg/database"
"github.com/Ennovar/gPanel/pkg/routing"
)
@ -92,10 +93,46 @@ func (con *Controller) ServeHTTP(res http.ResponseWriter, req *http.Request) {
}
path := req.URL.Path[1:]
if len(path) == 0 {
path = con.Directory + "document_root/" + "index.html"
if strings.HasPrefix(req.Host, "www") {
if len(path) == 0 {
path = con.Directory + "document_root/" + "index.html"
} else {
path = con.Directory + "document_root/" + path
}
} else {
path = con.Directory + "document_root/" + path
if strings.Count(req.Host, ".") == 2 {
subdomain := strings.SplitN(req.Host, ".", 2)[0] //Remove sub-domain
ds, err := database.Open(con.AccountDirectory + database.DB_MAIN)
if err != nil || ds == nil {
con.PublicLogger.Println(path + "::" + strconv.Itoa(http.StatusInternalServerError) + "::" + err.Error())
routing.HttpThrowStatus(http.StatusInternalServerError, res)
return
}
var sdRoot database.StructSubdomain
err = ds.Get(database.BUCKET_SUBDOMAINS, []byte(subdomain), &sdRoot)
if err != nil {
con.PublicLogger.Println(path + "::" + strconv.Itoa(http.StatusInternalServerError) + "::" + err.Error())
routing.HttpThrowStatus(http.StatusInternalServerError, res)
return
}
_ = ds.Close()
if len(path) == 0 {
path = con.Directory + "document_root/" + sdRoot.Root + "/index.html"
} else {
path = con.Directory + "document_root/" + sdRoot.Root + "/" + path
}
} else {
if len(path) == 0 {
path = con.Directory + "document_root/" + "index.html"
} else {
path = con.Directory + "document_root/" + path
}
}
}
contentType, err := routing.GetContentType(path)

View file

@ -6,9 +6,11 @@ import (
"strconv"
"time"
"github.com/Ennovar/gPanel/pkg/database"
"log"
"strings"
"sync"
"github.com/Ennovar/gPanel/pkg/database"
)
type Router struct {
@ -57,14 +59,22 @@ func New() *Router {
Addr: "localhost:" + strconv.Itoa(r.Port),
Handler: &httputil.ReverseProxy{
Director: func(req *http.Request) {
host := req.Host
if strings.Count(host, ".") == 2 {
host = strings.SplitN(host, ".", 2)[1] //Remove sub-domain
}
req.Header.Set("Host", req.Host)
req.URL.Scheme = "http"
mutex.Lock()
if d, ok := domainToPort[req.Host]; ok {
if d, ok := domainToPort[host]; ok {
mutex.Unlock()
req.Header.Set("Host", req.Host)
req.URL.Scheme = "http"
req.URL.Host = "127.0.0.1:" + strconv.Itoa(d)
} else {
mutex.Unlock()
req.URL.Host = "127.0.0.1:2082"
req.URL.Path = "/throw400"
}
},
},
@ -78,7 +88,7 @@ func New() *Router {
go func() {
for {
select {
case <- ticker.C:
case <-ticker.C:
if !RefreshMap() {
ticker.Stop()
log.Fatal("Error refreshing domain/bundle pairing for router")