Skip to content

Commit

Permalink
feat: add config file loading (#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
haveachin authored Dec 29, 2023
1 parent 42e4859 commit cca5096
Show file tree
Hide file tree
Showing 36 changed files with 2,455 additions and 106 deletions.
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ __debug_bin*

# Local Dev Files
.dev/
cmd/infrared/config.yml

# VSCode
.vscode/

# IntelliJ
.idea/
.idea/

# Docs
docs/node_modules/
docs/.vitepress/cache/
docs/.vitepress/dist/
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.PHONY: docs

test:
go test -race -timeout 10s ./...

Expand All @@ -16,4 +18,7 @@ dev:

dos:
CGO_ENABLED=0 go build -ldflags "-s -w" -o ./out/dos ./tools/dos
./out/dos
./out/dos

docs:
cd ./docs && npm run docs:dev
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ Infrared works as a reverse proxy using a sub-/domains to connect clients to a s
- [X] Reverse Proxy
- [X] Wildcards Support
- [X] Multi-Domain Support
- [X] Status Response Caching
- [ ] Proxy Protocol Support
- [ ] Ratelimiter

## Contributing

Expand Down
30 changes: 30 additions & 0 deletions cmd/infrared/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"errors"
"os"

"github.com/haveachin/infrared/configs"
)

func createConfigIfNotExist() error {
info, err := os.Stat(configPath)
if errors.Is(err, os.ErrNotExist) {
if err := os.Mkdir(proxiesDir, 0755); err != nil {
return err
}

return createDefaultConfigFile()
}

if info.IsDir() {
return errors.New("ir.Config is a directory")
}

return nil
}

func createDefaultConfigFile() error {
bb := configs.DefaultInfraredConfig
return os.WriteFile(configPath, bb, 0664)
}
92 changes: 83 additions & 9 deletions cmd/infrared/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,92 @@ package main

import (
"log"
"os"
"os/signal"
"syscall"

ir "github.com/haveachin/infrared/pkg/infrared"
"github.com/haveachin/infrared/pkg/infrared/config"
"github.com/spf13/pflag"
)

const (
envVarPrefix = "INFRARED"
)

var (
configPath = "config.yml"
workingDir = "."
proxiesDir = "./proxies"
)

func envVarString(p *string, name string) {
key := envVarPrefix + "_" + name
v := os.Getenv(key)
if v == "" {
return
}
*p = v
}

func initEnvVars() {
envVarString(&configPath, "CONFIG")
envVarString(&workingDir, "WORKING_DIR")
envVarString(&proxiesDir, "PROXIES_DIR")
}

func initFlags() {
pflag.StringVarP(&configPath, "config", "c", configPath, "path to the config file")
pflag.StringVarP(&workingDir, "working-dir", "w", workingDir, "changes the current working directory")
pflag.StringVarP(&proxiesDir, "proxies-dir", "p", proxiesDir, "path to the proxies directory")
pflag.Parse()
}

func init() {
initEnvVars()
initFlags()
}

func main() {
srv := ir.New(
ir.WithBindAddr(":25565"),
ir.AddServerConfig(
ir.WithServerDomains("*"),
ir.WithServerAddress(":25566"),
),
)

log.Println(srv.ListenAndServe())
log.Println("Starting Infrared")

if err := run(); err != nil {
log.Fatal(err)
}
}

func run() error {
if err := os.Chdir(workingDir); err != nil {
return err
}

if err := createConfigIfNotExist(); err != nil {
return err
}

srv := ir.NewWithConfigProvider(config.FileProvider{
ConfigPath: configPath,
ProxiesPath: proxiesDir,
})

errChan := make(chan error, 1)
go func() {
errChan <- srv.ListenAndServe()
}()

sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)

log.Println("System is online")

select {
case sig := <-sigChan:
log.Printf("Received %s", sig.String())
case err := <-errChan:
if err != nil {
return err
}
}

return nil
}
19 changes: 19 additions & 0 deletions configs/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Infrared Config

# Address that Infrared bind and listens to
#
bind: 0.0.0.0:25565

proxyProtocol:
# Receive proxy protocol
#
#receive: true

# TODO
#
#trustedProxies:
# - "127.0.0.1"

# Maximum duration between packets before the client gets timed out.
#
keepAliveTimeout: 30s
6 changes: 6 additions & 0 deletions configs/embed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package configs

import _ "embed"

//go:embed config.yml
var DefaultInfraredConfig []byte
16 changes: 16 additions & 0 deletions configs/proxy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# This is the domain that players enter in their game client.
# You can have multiple domains here or just one.
# Currently this holds just a wildcard character as a domain
# meaning that is accepts every domain that a player uses.
# Supports '*' and '?' wildcards in the pattern string.
#
domains:
- "*"

addresses:
- example.com:25565

# Send a Proxy Protocol v2 Header to the server to
# forward the players IP address
#
#sendProxyProtocol: true
2 changes: 1 addition & 1 deletion deployments/docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ services:
- 25566:25565/tcp
environment:
EULA: "TRUE"
VERSION: "1.20.1"
VERSION: "1.20.4"
TYPE: PAPER
networks:
- infrared
Expand Down
88 changes: 88 additions & 0 deletions docs/.vitepress/config.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { defineConfig} from 'vitepress'

// https://vitepress.dev/reference/site-config
export default defineConfig({
lang: 'en-US',
title: 'Infrared',
titleTemplate: ':title | Minecraft Proxy',
description: 'Minecraft Proxy',
cleanUrls: true,
head: [
[
'link',
{
rel: 'icon',
type: 'image/x-icon',
href: '/assets/logo.svg',
},
],
],
themeConfig: {
// https://vitepress.dev/reference/default-theme-config
logo: '/assets/logo.svg',

nav: [
{
text: 'Guides',
items: [
{ text: 'Forward Player IPs', link: '/guide/forward-player-ips' },
]
},
{
text: 'Config',
items: [
{ text: 'Global', link: '/config/' },
{ text: 'Proxy', link: '/config/proxy' },
{ text: 'CLI & Env Vars', link: '/config/cli-and-env-vars' },
]
},
{
text: 'Donate',
items: [
{ text: 'PayPal', link: 'https://paypal.me/hendrikschlehlein' },
{ text: 'Ko-Fi', link: 'https://ko-fi.com/haveachin' },
]
},
],

sidebar: [
{ text: 'Getting Started', link: '/getting-started' },
{
text: 'Config',
items: [
{ text: 'Global', link: '/config/' },
{ text: 'Proxies', link: '/config/proxies' },
{ text: 'CLI & Env Vars', link: '/config/cli-and-env-vars' },
],
},
{
text: 'Guides',
items: [
{ text: 'Forward Player IPs', link: '/guide/forward-player-ips' },
]
},
{ text: 'Report an Issue', link: 'https://github.com/haveachin/infrared/issues' },
{ text: 'Ask in Discussions', link: 'https://github.com/haveachin/infrared/discussions' },
{ text: 'Join our Discord', link: 'https://discord.gg/r98YPRsZAx' },
{ text: 'Branding', link: '/branding' },
],

socialLinks: [
{ icon: 'github', link: 'https://github.com/haveachin/infrared' },
{ icon: 'discord', link: 'https://discord.gg/r98YPRsZAx' },
],

footer: {
message: 'Released under the <a href="https://www.gnu.org/licenses/agpl-3.0.en.html">AGPL-3.0</a>.',
copyright: 'Copyright © 2019-present Haveachin and Contributors',
},

editLink: {
pattern: 'https://github.com/haveachin/infrared/edit/master/website/:path'
},

search: {
provider: 'local'
},
}
})
24 changes: 24 additions & 0 deletions docs/.vitepress/theme/custom.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* .vitepress/theme/custom.css */
:root {
--vp-c-brand-1: #CC0033;
--vp-c-brand-2: #B7002D;
--vp-c-brand-3: #A30028;
--vp-c-brand-light: #b3002d;
--vp-c-brand-lighter: #990026;
--vp-c-brand-dark: #e60039;
--vp-c-brand-darker: #ff0040;

--vp-home-hero-name-color: transparent;
--vp-home-hero-name-background: linear-gradient(
180deg,
#CC0033ff,
#CC0033B0
);

--vp-home-hero-image-filter: blur(40px);
--vp-home-hero-image-background-image: linear-gradient(
120deg,
#CC003325,
#CC003315
);
}
5 changes: 5 additions & 0 deletions docs/.vitepress/theme/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// .vitepress/theme/index.js
import DefaultTheme from 'vitepress/theme'
import './custom.css'

export default DefaultTheme
File renamed without changes
File renamed without changes
Loading

0 comments on commit cca5096

Please sign in to comment.