refactoring to golden standards

main
Evgeny Kovalev 2 years ago
parent f0acc77a4b
commit fcc0b0d089
  1. 18
      internal/domains/domains.go
  2. 0
      internal/healthReporter/healthReporter.go
  3. 10
      internal/management/management.go
  4. 0
      internal/management/memento.go
  5. 22
      internal/management/webserver.go
  6. 12
      internal/proxy/proxy.go
  7. 2
      internal/utils/jsonlog.go
  8. 2
      internal/utils/utils.go
  9. 18
      internal/workerpool/task.go
  10. 6
      internal/workerpool/worker.go
  11. 15
      shield.go
  12. 7
      static/domains.html
  13. 2
      static/template.html

@ -3,14 +3,28 @@ package domains
var Names T_Domains
type T_Domains struct {
Domains []T_Domain `json:"Domains"`
Domains []T_Domain `json:"Domains"`
Backends []T_Backend `json:"Backedns"`
Hosts []string `json:"Hosts"`
}
type T_Domain struct {
DomainName string `json:"DomainName"`
RealServer string `json:"RealServer"`
RealPort string `json:"RealPort"`
Status string `json:"Satus"`
HTTPS bool `json:"HTTPS"`
AddForwarded bool `json:"AddForwarded"`
KeyFile string `json:"KeyFile"`
CertFile string `json:"CertFile"`
Status string `json:"Satus"`
}
type T_Backend struct {
Name string `json:"Name"`
ConnectString string `json:"ConnectString"`
}
const (

@ -10,15 +10,17 @@ import (
"time"
"github.com/asaskevich/govalidator"
domains "github.com/cr3a70r/shield/internal/Domains"
"github.com/cr3a70r/shield/internal/domains"
)
var Settings T_Management
type T_Management struct {
Users []T_User `json:"Users"`
Domains []domains.T_Domain `json:"Names"`
Debug bool `json:"Debug"`
Users []T_User `json:"Users"`
Domains []domains.T_Domain `json:"Names"`
Backends []domains.T_Backend `json:"Backedns"`
Hosts []string `json:"Hosts"`
Debug bool `json:"Debug"`
}
type T_User struct {

@ -10,11 +10,11 @@ import (
"text/template"
"github.com/asaskevich/govalidator"
"github.com/cr3a70r/shield/Utils"
"github.com/cr3a70r/shield/internal/domains"
"github.com/cr3a70r/shield/internal/utils"
"github.com/gorilla/mux"
"github.com/gorilla/securecookie"
domains "github.com/cr3a70r/shield/internal/Domains"
)
var hashKey = []byte("ckjstkldx-rlkjcmskl-rdlskjtmd")
@ -41,7 +41,7 @@ func StartWebserver(addr string) {
}
func health(writer http.ResponseWriter, req *http.Request) {
Utils.RespondJSON("Unknown state", 200, writer)
utils.RespondJSON("Unknown state", 200, writer)
}
func renderIndex(w http.ResponseWriter, r *http.Request) {
@ -231,17 +231,17 @@ func requireAuth(h http.Handler) http.Handler {
func api_domains(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
Utils.RespondJSON(Settings.Domains, http.StatusOK, w)
utils.RespondJSON(Settings.Domains, http.StatusOK, w)
}
if r.Method == "POST" {
var msgJSON Utils.Msg
var msgJSON utils.Msg
var req domains.T_Domain
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
log.Println("webserver.api_domains: " + err.Error())
msgJSON.Msg = err.Error()
Utils.RespondJSON(msgJSON, http.StatusBadRequest, w)
utils.RespondJSON(msgJSON, http.StatusBadRequest, w)
}
Settings.AddDomainByDomain(req)
@ -251,11 +251,11 @@ func api_domains(w http.ResponseWriter, r *http.Request) {
log.Println("webserver.api_domains: " + err.Error())
msgJSON.Msg = err.Error()
Utils.RespondJSON(msgJSON, http.StatusBadRequest, w)
utils.RespondJSON(msgJSON, http.StatusBadRequest, w)
}
msgJSON.Msg = "OK"
Utils.RespondJSON(msgJSON, http.StatusOK, w)
utils.RespondJSON(msgJSON, http.StatusOK, w)
}
}
@ -264,8 +264,8 @@ func api_domain(w http.ResponseWriter, r *http.Request) {
httpVars := mux.Vars(r)
id := httpVars["id"]
var msgJSON Utils.Msg
var msgJSON utils.Msg
msgJSON.Msg = id
Utils.RespondJSON(msgJSON, http.StatusBadRequest, w)
utils.RespondJSON(msgJSON, http.StatusBadRequest, w)
}

@ -9,21 +9,22 @@ import (
"net/http/httputil"
"net/url"
logger "github.com/cr3a70r/shield/Utils"
"github.com/cr3a70r/shield/internal/utils"
)
// ---
type ProxyHttpServer struct {
ctx context.Context
http.Server
AddressAndPort string
Name string
logger *logger.Logger
logger *utils.Logger
}
type ReverseProxy struct {
url *url.URL
proxy *httputil.ReverseProxy
logger *logger.Logger
logger *utils.Logger
port string
}
@ -40,7 +41,7 @@ type Config struct {
Override Override
}
func NewProxy(urlString string, transport http.RoundTripper, director *httputil.ReverseProxy, logger *logger.Logger, port string) *ReverseProxy {
func NewProxy(urlString string, transport http.RoundTripper, director *httputil.ReverseProxy, logger *utils.Logger, port string) *ReverseProxy {
providedUrl, err := url.Parse(urlString)
if err != nil {
@ -80,6 +81,9 @@ func (p *ReverseProxy) Handler(w http.ResponseWriter, r *http.Request) {
})
}
//We may load balance here:
//r.Host = balanced IP.
p.proxy.ServeHTTP(w, r)
}

@ -1,4 +1,4 @@
package Utils
package utils
import (
"encoding/json"

@ -1,4 +1,4 @@
package Utils
package utils
import (
"encoding/json"

@ -0,0 +1,18 @@
package workerpool
import "fmt"
type Task struct {
Err error
Data interface{}
f func(interface{}) error
}
func NewTask(f func(interface{}) error, data interface{}) *Task {
return &Task{f: f, Data: data}
}
func process(workerID int, task *Task) {
fmt.Println("Worker %d processes task %v\n", workerID, task.Data)
task.Err = task.f(task.Data)
}

@ -0,0 +1,6 @@
package workerpool
type Worker struct {
ID int
taskChan chan *Task
}

@ -10,9 +10,9 @@ import (
"runtime"
"time"
management "github.com/cr3a70r/shield/Management"
proxy "github.com/cr3a70r/shield/Proxy"
"github.com/cr3a70r/shield/Utils"
"github.com/cr3a70r/shield/internal/management"
"github.com/cr3a70r/shield/internal/proxy"
"github.com/cr3a70r/shield/internal/utils"
"github.com/xlab/closer"
)
@ -27,7 +27,7 @@ func main() {
closer.Bind(cleanup)
logger := Utils.New(os.Stdout, Utils.LevelInfo)
logger := utils.New(os.Stdout, utils.LevelInfo)
test := workingProxyForDomain(logger, "test.dev.corp.daydev.org", "10.10.20.40", "9090")
http.HandleFunc("/", test.Handler)
@ -46,6 +46,11 @@ func main() {
management.Settings.Initialize()
//Building hosts from configured domains
for _, d := range management.Settings.Domains {
management.Settings.Hosts = append(management.Settings.Hosts, d.DomainName)
}
fmt.Println(runtime.NumGoroutine())
go management.StartWebserver("0.0.0.0:8080")
@ -57,7 +62,7 @@ func cleanup() {
fmt.Println("Closing Application")
}
func workingProxyForDomain(logger *Utils.Logger, domain string, realServer string, port string) *proxy.ReverseProxy {
func workingProxyForDomain(logger *utils.Logger, domain string, realServer string, port string) *proxy.ReverseProxy {
transport := &http.Transport{
DialContext: (&net.Dialer{
Timeout: 5 * time.Second,

@ -9,7 +9,7 @@ Protection | Shield
<thead>
<tr>
<th scope="col">Domain</th>
<th scope="col">Server</th>
<th scope="col">Backend</th>
<th scope="col">Port</th>
<th scope="col">Action</th>
</tr>
@ -57,7 +57,7 @@ Protection | Shield
<div class="modal-body">
<label for="inputEmail" class="sr-only">Domain Name</label>
<input type="text" id="Domain" name="Domain" class="form-control" placeholder="contoso.co" required autofocus>
<label for="inputEmail" class="sr-only">Server</label>
<label for="inputEmail" class="sr-only">Backend</label>
<input type="text" id="Server" name="Server" class="form-control" placeholder="FQDN or IP address" required>
<label for="inputEmail" class="sr-only">Port</label>
<input type="text" id="Port" name="Port" class="form-control" placeholder="443 or any other" required>
@ -82,8 +82,7 @@ function AddDomain() {
"RealPort": $("#Port").val(),
}),
success: function(result) {
console.log("DONE!")
console.log(result)
window.location.replace("/domains");
}
})

@ -33,7 +33,7 @@
<a class="nav-link" href="/domains">Domains</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/routing">Tunnels</a>
<a class="nav-link" href="/backends">Backends</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/acls">ACLs</a>

Loading…
Cancel
Save