Skip to content

Commit

Permalink
Merge pull request #10 from r7wx/v1.1.1
Browse files Browse the repository at this point in the history
V1.1.1
  • Loading branch information
r7wx authored May 6, 2022
2 parents 236fccc + a686c25 commit 01d74ef
Show file tree
Hide file tree
Showing 14 changed files with 371 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ web/build/*
npm-debug.log*
yarn-debug.log*
yarn-error.log*
internal/config/config.json
test-config.json
.vscode/
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<h4 align="center">A gate to your self hosted infrastructure</h3>

<p align="center">
<img src="https://img.shields.io/github/v/release/r7wx/easy-gate" alt="Release" />
<img src="https://img.shields.io/docker/pulls/r7wx/easy-gate" alt="Docker Pulls" />
<a href="https://github.com/r7wx/easy-gate/actions/workflows/build.yml">
<img src="https://github.com/r7wx/easy-gate/actions/workflows/build.yml/badge.svg"/></a>
<a href="https://github.com/r7wx/easy-gate/actions/workflows/test.yml">
Expand Down
10 changes: 5 additions & 5 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,34 @@ SOFTWARE.

package config

// Group - Self Gate group configuration struct
// Group - Easy Gate group configuration struct
type Group struct {
Name string `json:"name"`
Subnet string `json:"subnet"`
}

// Service - Self Gate service configuration struct
// Service - Easy Gate service configuration struct
type Service struct {
Icon string `json:"icon"`
Name string `json:"name"`
URL string `json:"url"`
Groups []string `json:"groups"`
}

// Note - Self Gate note configuration struct
// Note - Easy Gate note configuration struct
type Note struct {
Name string `json:"name"`
Text string `json:"text"`
Groups []string `json:"groups"`
}

// Theme - Self Gate theme configuration struct
// Theme - Easy Gate theme configuration struct
type Theme struct {
Background string `json:"background"`
Foreground string `json:"foreground"`
}

// Config - Self Gate configuration struct
// Config - Easy Gate configuration struct
type Config struct {
Theme Theme `json:"theme"`
Addr string `json:"addr"`
Expand Down
94 changes: 92 additions & 2 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,23 @@ import (
)

const (
testConfigFilePath = "./config.json"
testConfigFilePath = "./test-config.json"
)

func TestMain(m *testing.M) {
testCfg := Config{
Addr: ":8080",
UseTLS: false,
CertFile: "",
KeyFile: "",
BehindProxy: false,
Title: "Test",
Icon: "fa-solid fa-cubes",
Motd: "",
Theme: Theme{
Background: "#ffffff",
Foreground: "#000000",
},
Groups: []Group{},
Services: []Service{},
Notes: []Note{},
Expand Down Expand Up @@ -68,10 +80,24 @@ func TestConfig(t *testing.T) {
}

newCfg := Config{
Addr: ":8080",
UseTLS: false,
CertFile: "",
KeyFile: "",
BehindProxy: false,
Title: "Test",
Icon: "fa-solid fa-cubes",
Motd: "",
Theme: Theme{
Background: "#ffffff",
Foreground: "#000000",
},
Groups: []Group{},
Services: []Service{
{
Icon: "fa-solid fa-cube",
Name: time.Now().String(),
URL: "http://example.com",
},
},
Notes: []Note{},
Expand All @@ -87,7 +113,11 @@ func TestConfig(t *testing.T) {
}

time.Sleep(10 * time.Millisecond)
cfg := routine.GetConfiguration()
cfg, err := routine.GetConfiguration()
if err != nil {
t.Fatal(err)
}

if cfg.Services[0].Name != newCfg.Services[0].Name {
t.Fatalf("Expected %v, got %v",
cfg.Services[0].Name, newCfg.Services[0].Name)
Expand Down Expand Up @@ -123,3 +153,63 @@ func TestHexColors(t *testing.T) {
t.Fatal("Expected false, got true")
}
}

func TestURLs(t *testing.T) {
if !isURL("http://example.com") {
t.Fatal("Expected true, got false")
}
if !isURL("https://example.com") {
t.Fatal("Expected true, got false")
}
if !isURL("https://example.com/test/test.xy") {
t.Fatal("Expected true, got false")
}
if !isURL("https://example.com/test/test.xy?test=test") {
t.Fatal("Expected true, got false")
}
if !isURL("https://example.com/test/test.xy?test=test#test") {
t.Fatal("Expected true, got false")
}
if !isURL("ftp://example.com") {
t.Fatal("Expected true, got false")
}
if isURL("example.internal.priv") {
t.Fatal("Expected false, got true")
}
if isURL("test.test") {
t.Fatal("Expected false, got true")
}
if isURL("example") {
t.Fatal("Expected false, got true")
}
if isURL("javascript:void(0)") {
t.Fatal("Expected false, got true")
}
if isURL("javascript:alert(1)") {
t.Fatal("Expected false, got true")
}
if isURL("javascript: alert(1)") {
t.Fatal("Expected false, got true")
}
}

func TestIcons(t *testing.T) {
if !isIcon("fa-brands fa-github") {
t.Fatal("Expected true, got false")
}
if !isIcon("fa-regular fa-cube") {
t.Fatal("Expected true, got false")
}
if !isIcon("fa-solid fa-flask-vial") {
t.Fatal("Expected true, got false")
}
if isIcon("") {
t.Fatal("Expected false, got true")
}
if isIcon("bg-white text-red") {
t.Fatal("Expected false, got true")
}
if isIcon("fa-brands fa-github fa-brands fa-github") {
t.Fatal("Expected false, got true")
}
}
7 changes: 2 additions & 5 deletions internal/config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,8 @@ func LoadConfigFile(filePath string) (*Config, string, error) {
return nil, "", err
}

if !isHexColor(cfg.Theme.Background) {
cfg.Theme.Background = "#FFFFFF"
}
if !isHexColor(cfg.Theme.Foreground) {
cfg.Theme.Foreground = "#000000"
if err := validateConfig(cfg); err != nil {
return nil, "", err
}

return &cfg, checksum, nil
Expand Down
8 changes: 5 additions & 3 deletions internal/config/routine.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
// Routine - Config routine struct
type Routine struct {
mu sync.Mutex
Error error
Config *Config
FilePath string
LastChecksum string
Expand All @@ -53,21 +54,22 @@ func NewRoutine(filePath string, interval time.Duration) *Routine {
}

// GetConfiguration - Get current configuration
func (r *Routine) GetConfiguration() *Config {
func (r *Routine) GetConfiguration() (*Config, error) {
defer r.mu.Unlock()
r.mu.Lock()
return r.Config
return r.Config, r.Error
}

// Start - Start config routine
func (r *Routine) Start() {
for {
cfg, checksum, err := LoadConfigFile(r.FilePath)
if err != nil {
log.Println("[Easy Gate] Error loading configuration file: ", err)
r.Error = err
continue
}

r.Error = nil
if checksum != r.LastChecksum {
r.mu.Lock()
r.Config = cfg
Expand Down
105 changes: 105 additions & 0 deletions internal/config/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
MIT License
Copyright (c) 2022 r7wx
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package config

import (
"regexp"

"github.com/r7wx/easy-gate/internal/errors"
)

func isHexColor(color string) bool {
if len(color) < 4 || len(color) > 7 {
return false
}

if color[0] != '#' {
return false
}

for i := 1; i < len(color); i++ {
c := color[i]
if (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') {
continue
}
return false
}

return true
}

func isURL(url string) bool {
r, _ := regexp.Compile(
`^(https?|ftp)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]`)
return r.MatchString(url)
}

func isIcon(icon string) bool {
r, _ := regexp.Compile(
`^(fa-(solid|regular|brands) (fa-[A-Za-z0-9--]+))$`,
)
return r.MatchString(icon)
}

func validateConfig(cfg Config) error {
if !isIcon(cfg.Icon) {
return errors.NewEasyGateError(
errors.InvalidIcon,
errors.Root, "")
}

for _, service := range cfg.Services {
if !isIcon(service.Icon) {
return errors.NewEasyGateError(
errors.InvalidIcon,
errors.Service,
service.Name,
)
}

if !isURL(service.URL) {
return errors.NewEasyGateError(
errors.InvalidURL,
errors.Service,
service.Name,
)
}
}

if !isHexColor(cfg.Theme.Background) {
return errors.NewEasyGateError(
errors.InvalidColor,
errors.Root,
"background",
)
}
if !isHexColor(cfg.Theme.Foreground) {
return errors.NewEasyGateError(
errors.InvalidColor,
errors.Root,
"foreground",
)
}

return nil
}
Loading

0 comments on commit 01d74ef

Please sign in to comment.