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 var Names T_Domains
type T_Domains struct { 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 { type T_Domain struct {
DomainName string `json:"DomainName"` DomainName string `json:"DomainName"`
RealServer string `json:"RealServer"` RealServer string `json:"RealServer"`
RealPort string `json:"RealPort"` 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 ( const (

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

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

@ -9,21 +9,22 @@ import (
"net/http/httputil" "net/http/httputil"
"net/url" "net/url"
logger "github.com/cr3a70r/shield/Utils" "github.com/cr3a70r/shield/internal/utils"
) )
// ---
type ProxyHttpServer struct { type ProxyHttpServer struct {
ctx context.Context ctx context.Context
http.Server http.Server
AddressAndPort string AddressAndPort string
Name string Name string
logger *logger.Logger logger *utils.Logger
} }
type ReverseProxy struct { type ReverseProxy struct {
url *url.URL url *url.URL
proxy *httputil.ReverseProxy proxy *httputil.ReverseProxy
logger *logger.Logger logger *utils.Logger
port string port string
} }
@ -40,7 +41,7 @@ type Config struct {
Override Override 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) providedUrl, err := url.Parse(urlString)
if err != nil { 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) p.proxy.ServeHTTP(w, r)
} }

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

@ -1,4 +1,4 @@
package Utils package utils
import ( import (
"encoding/json" "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" "runtime"
"time" "time"
management "github.com/cr3a70r/shield/Management" "github.com/cr3a70r/shield/internal/management"
proxy "github.com/cr3a70r/shield/Proxy" "github.com/cr3a70r/shield/internal/proxy"
"github.com/cr3a70r/shield/Utils" "github.com/cr3a70r/shield/internal/utils"
"github.com/xlab/closer" "github.com/xlab/closer"
) )
@ -27,7 +27,7 @@ func main() {
closer.Bind(cleanup) 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") test := workingProxyForDomain(logger, "test.dev.corp.daydev.org", "10.10.20.40", "9090")
http.HandleFunc("/", test.Handler) http.HandleFunc("/", test.Handler)
@ -46,6 +46,11 @@ func main() {
management.Settings.Initialize() 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()) fmt.Println(runtime.NumGoroutine())
go management.StartWebserver("0.0.0.0:8080") go management.StartWebserver("0.0.0.0:8080")
@ -57,7 +62,7 @@ func cleanup() {
fmt.Println("Closing Application") 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{ transport := &http.Transport{
DialContext: (&net.Dialer{ DialContext: (&net.Dialer{
Timeout: 5 * time.Second, Timeout: 5 * time.Second,

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

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

Loading…
Cancel
Save