Skip to content

Commit

Permalink
feat: add traffic limiter
Browse files Browse the repository at this point in the history
  • Loading branch information
haveachin committed Dec 4, 2022
1 parent ae38b3d commit efa81b3
Show file tree
Hide file tree
Showing 32 changed files with 903 additions and 350 deletions.
14 changes: 9 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@ package cmd

import (
"embed"
"errors"
"fmt"
"os"
"os/signal"
"path/filepath"
"sync"
"syscall"

"github.com/haveachin/infrared/internal/plugin/api"
"github.com/haveachin/infrared/internal/plugin/prometheus"

"github.com/haveachin/infrared/internal/app/infrared"
"github.com/haveachin/infrared/internal/pkg/bedrock"
"github.com/haveachin/infrared/internal/pkg/config"
"github.com/haveachin/infrared/internal/pkg/java"
"github.com/haveachin/infrared/internal/plugin/api"
"github.com/haveachin/infrared/internal/plugin/prometheus"
"github.com/haveachin/infrared/internal/plugin/traffic_limiter"
"github.com/haveachin/infrared/internal/plugin/webhook"
"github.com/spf13/cobra"
"go.uber.org/zap"
Expand Down Expand Up @@ -52,8 +53,10 @@ var (
zap.String("config", configPath),
)

if err := safeWriteFromEmbeddedFS("configs", "."); err != nil {
return err
if _, err := os.Stat(configPath); err != nil && errors.Is(err, os.ErrNotExist) {
if err := safeWriteFromEmbeddedFS("configs", "."); err != nil {
return err
}
}

cfg, err := config.New(configPath, onConfigChange, logger)
Expand Down Expand Up @@ -94,6 +97,7 @@ var (
&webhook.Plugin{},
&prometheus.Plugin{},
&api.Plugin{},
&traffic_limiter.Plugin{},
},
Logger: logger,
}
Expand Down
69 changes: 43 additions & 26 deletions configs/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,6 @@ providers:
#
#watch: true

# Storage like a database that Infrared should read from.
#
storage:
# A list of storage IDs that Infrared should read from.
#
ids:
- default

# Config files that Infrared should read from.
#
file:
Expand All @@ -45,18 +37,43 @@ providers:
#
watch: true

# Storage are mainly used to store and read Infrared configs,
# but are also used to enable other features that need persistend storage.
#
storage:
# This is the ID of this database.
#
default:
driver: postgres
address: :5432
database: infrared
username: username
password: password
#trafficLimiters:
#default:
# Server IDs to watch the traffic
#
#serverIds:
# - default

# File is used to persistently store data
#
#file: bandwidth.yml

# This is the amount of traffic until the server gets limited.
# Valid sizes are B, KB, MB, GB, TB, PB and EB.
#
#trafficLimit: 1TB

# Sets the schedule for the job that resets the consumed bytes from the traffic limiter.
# For more info on the Cron sysntax see here: https://en.wikipedia.org/wiki/Cron
#
#resetCron: "@monthly"

# The message that is displayed to a client when they try to connect
# but the server is out of bandwidth.
#
#outOfBandwidthMessage: Sorry {{username}}, but the server is out of bandwidth.

# This is the ping response that clients see of your server when it is out of bandwidth.
#
#outOfBandwidthStatus:
#versionName: Infrared
#protocolNumber: 0
#maxPlayerCount: 0
#playerCount: 0
#iconPath: icons/default.png
#motd: |
# Powered by Infrared
# §6Server at {{serverDomain}} is out of bandwidth.

# Enables the API to access real time data.
#
Expand Down Expand Up @@ -94,12 +111,12 @@ storage:

# Event Topics to listen for.
# Available events are:
# - NewConnEvent - When a new connection to Infrared is made.
# - PreConnProcessingEvent - Before the new connection is processed.
# - PostConnProcessingEvent - After a connection is processed.
# - PreConnConnectingEvent - Before a client is connecting to the target server.
# - PlayerJoinEvent - When a player joins a server.
# - PlayerLeaveEvent - When a player leaves the server.
# - AcceptedConn - When a new connection to Infrared is made.
# - PreProcessing - Before the new connection is processed.
# - PostProcessing - After a connection is processed.
# - PrePlayerJoin - Before a client is connecting to the target server.
# - PlayerJoin - When a player joins a server.
# - PlayerLeave - When a player leaves the server.
#
#events:
# - PlayerJoin
Expand Down
5 changes: 4 additions & 1 deletion configs/proxies/defaults.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ defaults:
#
compression: flate

# This is the ping response that clients see of your sever. This cannot be handled at
# This is the ping response that clients see of your server. This cannot be handled at
# server level due to a limitation of the bedrock protocol. See https://wiki.vg/Raknet_Protocol#Unconnected_Pong
#
pingStatus:
Expand Down Expand Up @@ -63,7 +63,10 @@ defaults:
java:
gateway:
listener:
# The message that is displayed to a client when they try to connect via an invalid domain.
#
serverNotFoundMessage: Sorry {{username}}, but {{serverDomain}} was not found.

# The Java Edition has a different approach to handling status pings, than the Bedrock Edition.
# This makes it possible to display a custom message to clients if the domain that they want
# to connect though has a server configuration behind it.
Expand Down
51 changes: 46 additions & 5 deletions configs/proxies/java-example.yml
Original file line number Diff line number Diff line change
@@ -1,24 +1,65 @@
# This is an example config for a minecraft java proxy.
# Feel free to use this as a template for other proxy configs
# or adapt it to your fit your setup.
#
java:
gateways:
# This is just the name of this gateway.
# Since is your only gateways right now I would call it "default".
# These names ARE global. Make sure to not have duplicates.
#
default:
listeners:
# Same as the gateway. This is just the name of the listener.
# Since is binds to port 25565 it makes sense that it is called
# default for now.
# These names ARE global. Make sure to not have duplicates.
#
default:
bind: :25565

# Optional fields:
# Also look at the defaults.yml file for more optional fields.
#
#receiveProxyProtocol: true
#receiveRealIP: true
servers:
# This is again just the name of the server.
# These names ARE global. Make sure to not have duplicates.
#
default:
# We link this server to our default gateway.
# That means that players that connect through any listener
# in our "default" gateway can connect to this server.
#
gateways:
- default

# 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.
# More about wildcard characters: https://en.wikipedia.org/wiki/Wildcard_character
#
domains:
- "*"

# Address of the server that Infrared sends the players to.
#
address: example.com:25565

# Optional fields:
# Also look at the defaults.yml file for more optional fields.
#
#proxyBind: 0.0.0.0
#sendProxyProtocol: true
#sendRealIP: true
#overrideStatus:
#versionName: Infrared
#maxPlayerCount: 20
#protocolNumber: 0
#maxPlayerCount: 0
#playerCount: 0
#
bandwidth:
trafficLimit: 1TB
resetCron: "0 0 0 1 * *"
#iconPath: icons/default.png
#motd: |
# Powered by Infrared
# §aServer at {{serverDomain}} is online.
26 changes: 3 additions & 23 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/haveachin/infrared
go 1.19

require (
github.com/c2h5oh/datasize v0.0.0-20220606134207-859f65c6625b
github.com/cespare/xxhash/v2 v2.1.2
github.com/df-mc/atomic v1.10.0
github.com/docker/docker v20.10.20+incompatible
Expand All @@ -20,11 +21,9 @@ require (
github.com/mitchellh/mapstructure v1.5.0
github.com/pires/go-proxyproto v0.6.2
github.com/prometheus/client_golang v1.13.0
github.com/robfig/cron/v3 v3.0.1
github.com/sandertv/go-raknet v1.12.0
github.com/spf13/cobra v1.6.0
github.com/uptrace/bun v1.1.8
github.com/uptrace/bun/dialect/sqlitedialect v1.1.8
github.com/uptrace/bun/driver/sqliteshim v1.1.8
go.uber.org/atomic v1.10.0
go.uber.org/multierr v1.8.0
go.uber.org/zap v1.23.0
Expand All @@ -40,13 +39,8 @@ require (
github.com/docker/go-units v0.5.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mattn/go-sqlite3 v1.14.15 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2 // indirect
github.com/moby/term v0.0.0-20220808134915-39b0c02b01ae // indirect
github.com/morikuni/aec v1.0.0 // indirect
Expand All @@ -56,27 +50,13 @@ require (
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect
github.com/uptrace/bun/dialect/pgdialect v1.1.8
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/stretchr/testify v1.8.1 // indirect
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
golang.org/x/net v0.0.0-20221017152216-f25eb7ecb193 // indirect
golang.org/x/sys v0.1.0 // indirect
golang.org/x/tools v0.1.12 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gotest.tools/v3 v3.4.0 // indirect
lukechampine.com/uint128 v1.2.0 // indirect
modernc.org/cc/v3 v3.36.3 // indirect
modernc.org/ccgo/v3 v3.16.9 // indirect
modernc.org/libc v1.17.1 // indirect
modernc.org/mathutil v1.5.0 // indirect
modernc.org/memory v1.2.1 // indirect
modernc.org/opt v0.1.3 // indirect
modernc.org/sqlite v1.18.1 // indirect
modernc.org/strutil v1.1.3 // indirect
modernc.org/token v1.0.1 // indirect
)
Loading

0 comments on commit efa81b3

Please sign in to comment.