Skip to content

Commit

Permalink
refactored some code... forgot what exactly
Browse files Browse the repository at this point in the history
  • Loading branch information
realDragonium committed Jul 8, 2021
1 parent 68f7459 commit 5731823
Show file tree
Hide file tree
Showing 12 changed files with 610 additions and 457 deletions.
38 changes: 3 additions & 35 deletions config/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,10 @@ import (
"log"
"os"
"path/filepath"

"github.com/realDragonium/Ultraviolet/mc"
"github.com/realDragonium/Ultraviolet/proxy"
)

var (
// Isnt this the proper path to put config files into (for execution without docker)
defaultCfgPath = "/etc/ultraviolet"
defaultServerCfgPath = filepath.Join(defaultCfgPath, "config")
defaultUltravioletCfgPath = filepath.Join(defaultCfgPath, "ultraviolet.json")
)

func ReadServerConfigs(path string) ([]ServerConfig, error) {
var cfgs []ServerConfig
if path == "" {
path = defaultServerCfgPath
}
var filePaths []string
err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
Expand All @@ -39,7 +26,6 @@ func ReadServerConfigs(path string) ([]ServerConfig, error) {
return cfgs, err
}
for _, filePath := range filePaths {
log.Println("Loading", filePath)
cfg, err := LoadServerCfgFromPath(filePath)
if err != nil {
return nil, err
Expand All @@ -66,12 +52,9 @@ func LoadServerCfgFromPath(path string) (ServerConfig, error) {
}

func ReadUltravioletConfig(path string) (UltravioletConfig, error) {
cfg := DefaultUltravioletConfig()
if path == "" {
path = defaultUltravioletCfgPath
}
// Check or file exists and if not write default config file to it
log.Printf("Loading Ultraviolet main config file at: %s", path)
var cfg UltravioletConfig

// TODO: Check or file exists and if not write default config file to it
bb, err := ioutil.ReadFile(path)
if err != nil {
return UltravioletConfig{}, err
Expand All @@ -81,18 +64,3 @@ func ReadUltravioletConfig(path string) (UltravioletConfig, error) {
}
return cfg, nil
}

func FileToWorkerConfig(cfg ServerConfig) proxy.WorkerServerConfig {
disconPk := mc.ClientBoundDisconnect{
Reason: mc.Chat(cfg.DisconnectMessage),
}.Marshal()
offlineStatusPk := cfg.OfflineStatus.Marshal()
return proxy.WorkerServerConfig{
ProxyTo: cfg.ProxyTo,
ProxyBind: cfg.ProxyBind,
SendProxyProtocol: cfg.SendProxyProtocol,
RateLimit: cfg.ConnLimitBackend,
OfflineStatus: offlineStatusPk,
DisconnectPacket: disconPk,
}
}
124 changes: 80 additions & 44 deletions config/file_test.go
Original file line number Diff line number Diff line change
@@ -1,68 +1,104 @@
package config_test

import (
"bytes"
"encoding/json"
"io/ioutil"
"os"
"reflect"
"testing"

"github.com/realDragonium/Ultraviolet/config"
"github.com/realDragonium/Ultraviolet/mc"
)

func samePk(expected, received mc.Packet) bool {
sameID := expected.ID == received.ID
sameData := bytes.Equal(expected.Data, received.Data)
func TestReadServerConfigFile(t *testing.T) {
cfg := config.ServerConfig{
MainDomain: "infrared",
ProxyTo: ":25566",
}
file, _ := json.MarshalIndent(cfg, "", " ")
tmpfile, err := ioutil.TempFile("", "example")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name())
if _, err := tmpfile.Write(file); err != nil {
t.Fatal(err)
}
if err := tmpfile.Close(); err != nil {
t.Fatal(err)
}
loadedCfg, err := config.LoadServerCfgFromPath(tmpfile.Name())
if err != nil {
t.Error(err)
}

if !reflect.DeepEqual(cfg, loadedCfg) {
t.Errorf("Wanted:%v \n got: %v", cfg, loadedCfg)
}
}

return sameID && sameData
func TestReadServerConfigs(t *testing.T) {
cfg := config.ServerConfig{
MainDomain: "infrared",
ProxyTo: ":25566",
}
tmpDir, _ := ioutil.TempDir("", "configs")
for i := 0; i < 3; i++ {
file, _ := json.MarshalIndent(cfg, "", " ")
tmpfile, err := ioutil.TempFile(tmpDir, "example")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name())
if _, err := tmpfile.Write(file); err != nil {
t.Fatal(err)
}
if err := tmpfile.Close(); err != nil {
t.Fatal(err)
}
}
loadedCfgs, _ := config.ReadServerConfigs(tmpDir)
for i, loadedCfg := range loadedCfgs {
if !reflect.DeepEqual(cfg, loadedCfg) {
t.Errorf("index: %d \nWanted:%v \n got: %v", i, cfg, loadedCfg)
}
}
}

func TestFileToWorkerConfig(t *testing.T) {
serverCfg := config.ServerConfig{
MainDomain: "Ultraviolet",
ExtraDomains: []string{"Ultraviolet2", "UltraV", "UV"},
ProxyTo: "127.0.10.5:25565",
ProxyBind: "127.0.0.5",
SendProxyProtocol: true,
DisconnectMessage: "HelloThereWeAreClosed...Sorry",
OfflineStatus: mc.AnotherStatusResponse{
func TestReadUltravioletConfigFile(t *testing.T) {
cfg := config.UltravioletConfig{
ListenTo: ":25565",
ReceiveProxyProtocol: false,
DefaultStatus: mc.AnotherStatusResponse{
Name: "Ultraviolet",
Protocol: 755,
Description: "Some broken proxy",
Description: "One dangerous proxy",
},
ConnLimitBackend: 5,
}

expectedDisconPk := mc.ClientBoundDisconnect{
Reason: mc.String(serverCfg.DisconnectMessage),
}.Marshal()
expectedOfflineStatus := mc.AnotherStatusResponse{
Name: "Ultraviolet",
Protocol: 755,
Description: "Some broken proxy",
}.Marshal()

workerCfg := config.FileToWorkerConfig(serverCfg)

if workerCfg.ProxyTo != serverCfg.ProxyTo {
t.Errorf("expected: %v - got: %v", serverCfg.ProxyTo, workerCfg.ProxyTo)
NumberOfWorkers: 5,
NumberOfConnWorkers: 1,
NumberOfStateWorkers: 1,
NumberOfStatusWorkers: 1,
}
if workerCfg.ProxyBind != serverCfg.ProxyBind {
t.Errorf("expected: %v - got: %v", serverCfg.ProxyBind, workerCfg.ProxyBind)
file, _ := json.MarshalIndent(cfg, "", " ")
tmpfile, err := ioutil.TempFile("", "example")
if err != nil {
t.Fatal(err)
}
if workerCfg.SendProxyProtocol != serverCfg.SendProxyProtocol {
t.Errorf("expected: %v - got: %v", serverCfg.SendProxyProtocol, workerCfg.SendProxyProtocol)
defer os.Remove(tmpfile.Name())
if _, err := tmpfile.Write(file); err != nil {
t.Fatal(err)
}
if workerCfg.RateLimit != serverCfg.ConnLimitBackend {
t.Errorf("expected: %v - got: %v", serverCfg.ConnLimitBackend, workerCfg.RateLimit)
if err := tmpfile.Close(); err != nil {
t.Fatal(err)
}
if !samePk(expectedOfflineStatus, workerCfg.OfflineStatus) {
offlineStatus, _ := mc.UnmarshalClientBoundResponse(expectedOfflineStatus)
receivedStatus, _ := mc.UnmarshalClientBoundResponse(workerCfg.OfflineStatus)
t.Errorf("expcted: %v \ngot: %v", offlineStatus, receivedStatus)
loadedCfg, err := config.ReadUltravioletConfig(tmpfile.Name())
if err != nil {
t.Error(err)
}

if !samePk(expectedDisconPk, workerCfg.DisconnectPacket) {
expectedDiscon, _ := mc.UnmarshalClientDisconnect(expectedDisconPk)
receivedDiscon, _ := mc.UnmarshalClientDisconnect(workerCfg.DisconnectPacket)
t.Errorf("expcted: %v \ngot: %v", expectedDiscon, receivedDiscon)
if !reflect.DeepEqual(cfg, loadedCfg) {
t.Errorf("Wanted:%v \n got: %v", cfg, loadedCfg)
}
}
37 changes: 26 additions & 11 deletions config/types.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package config

import "github.com/realDragonium/Ultraviolet/mc"
import (
"github.com/realDragonium/Ultraviolet/mc"
)

type ServerConfig struct {
MainDomain string `json:"mainDomain"`
ExtraDomains []string `json:"extraDomains"`
ProxyTo string `json:"proxyTo"`
ProxyBind string `json:"proxyBind"`
SendProxyProtocol bool `json:"sendProxyProtocol"`
MainDomain string `json:"mainDomain"`
ExtraDomains []string `json:"extraDomains"`

ProxyTo string `json:"proxyTo"`
ProxyBind string `json:"proxyBind"`
SendProxyProtocol bool `json:"sendProxyProtocol"`

DisconnectMessage string `json:"disconnectMessage"`
OfflineStatus mc.AnotherStatusResponse `json:"offlineStatus"`
ConnLimitBackend int `json:"connPerSec"`

RateLimit int `json:"rateLimit"`
RateDuration string `json:"rateCooldown"`
}

func DefaultServerConfig() ServerConfig {
Expand All @@ -24,26 +30,35 @@ func DefaultServerConfig() ServerConfig {
Protocol: 755,
Description: "Some broken proxy",
},
ConnLimitBackend: 5,
RateLimit: 5,
RateDuration: "1s",
}
}

type UltravioletConfig struct {
ListenTo string `json:"listenTo"`
NumberOfWorkers int `json:"numberOfWorkers"`
ReceiveProxyProtocol bool `json:"receiveProxyProtocol"`
DefaultStatus mc.AnotherStatusResponse `json:"defaultStatus"`

NumberOfWorkers int `json:"numberOfWorkers"`
NumberOfConnWorkers int `json:"numberOfConnWorkers"`
NumberOfStateWorkers int `json:"numberOfStateWorkers"`
NumberOfStatusWorkers int `json:"numberOfStatusWorkers"`
}

func DefaultUltravioletConfig() UltravioletConfig {
return UltravioletConfig{
ListenTo: ":25565",
NumberOfWorkers: 5,
ReceiveProxyProtocol: false,
DefaultStatus: mc.AnotherStatusResponse{
Name: "Ultraviolet",
Protocol: 755,
Description: "Some broken proxy",
Description: "One dangerous proxy",
},

NumberOfWorkers: 5,
NumberOfConnWorkers: 1,
NumberOfStateWorkers: 1,
NumberOfStatusWorkers: 1,
}
}
6 changes: 4 additions & 2 deletions examples/server-example.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
"mainDomain": "localhost",
"extraDomains": ["127.0.0.1", "0.0.0.0"],
"proxyTo": ":25566",
"proxyBind": "0.0.0.0",
"proxyBind": "127.0.0.1",
"sendProxyProtocol": false,
"disconnectMessage": "Sorry {{username}}, but the server is offline.",
"offlineStatus": {
"name": "Ultraviolet",
"protocol": 755,
"text": "Some offline server"
},
"connPerSec": 5
"rateLimit": 5,
"rateCooldown": "4s"

}
7 changes: 5 additions & 2 deletions examples/ultraviolet.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
{
"listenTo": ":25565",
"numberOfWorkers": 5,
"receiveProxyProtocol": false,
"defaultStatus": {
"name": "Ultraviolet",
"protocol": 755,
"text": "Some broken proxy"
}
},
"numberOfWorkers": 5,
"numberOfConnWorkers": 1,
"numberOfStateWorkers": 1,
"numberOfStatusWorkers": 1
}
Loading

0 comments on commit 5731823

Please sign in to comment.