Skip to content

Commit

Permalink
add bandwith track feature
Browse files Browse the repository at this point in the history
  • Loading branch information
lhridder committed Jul 13, 2022
1 parent d26d6b7 commit e3c56e1
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 5 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ fork from [haveachin/infrared](https://github.com/haveachin/infrared)
- Global .json config
- Removed docker and callback features
- Status packet caching
- Bandwith usage tracking for proxy configs through prometheus

## Command-Line Flags

Expand Down Expand Up @@ -51,7 +52,8 @@ fork from [haveachin/infrared](https://github.com/haveachin/infrared)
"rejoinMessage": "Please rejoin to verify your connection.",
"underAttack": false,
"debug": false,
"connectionTreshold": 50
"connectionTreshold": 50,
"trackBandwith": false
}
```
Values can be left out if they don't deviate from the default, a config.json with just `{}` is still required for startup.
Expand All @@ -75,7 +77,8 @@ Values can be left out if they don't deviate from the default, a config.json wit
- `rejoinMessage` what text response should be sent when a player needs to rejoin to verify they're not a bot.
- `underAttack` if the instance should permanently be in attack mode.
- `debug` if debug logs should be enabled.
- `connectionTreshold` at what amount of packets per second the underAttack mode should trigger.`
- `connectionTreshold` at what amount of packets per second the underAttack mode should trigger.
- `trackBandwith` whether or not bandwith usage should be tracked in prometheus (requires prometheusEnabled).


## Proxy Config
Expand Down
8 changes: 8 additions & 0 deletions cmd/infrared/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ func main() {
log.Println(err)
return
}

if infrared.Config.TrackBandwith {
go func() {
for {
gateway.TrackBandwith()
}
}()
}
}

if !infrared.Config.UnderAttack {
Expand Down
2 changes: 2 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type GlobalConfig struct {
UnderAttack bool `json:"underAttack"`
Debug bool `json:"debug"`
ConnectionTreshold int `json:"connectionTreshold"`
TrackBandwith bool `json:"trackBandwith"`
}

var Config GlobalConfig
Expand All @@ -85,6 +86,7 @@ var DefaultConfig = GlobalConfig{
UnderAttack: false,
Debug: false,
ConnectionTreshold: 50,
TrackBandwith: false,
}

func (cfg *ProxyConfig) Dialer() (*Dialer, error) {
Expand Down
21 changes: 21 additions & 0 deletions gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ var (
Name: "infrared_underAttack",
Help: "Is the proxy under attack",
})
usedBandwith = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "infrared_used_bandwith",
Help: "The total number of used bytes of bandwith per proxy",
}, []string{"host"})
ctx = context.Background()
)

Expand Down Expand Up @@ -223,6 +227,10 @@ func (gateway *Gateway) RegisterProxy(proxy *Proxy) error {

playersConnected.WithLabelValues(proxy.DomainName())

if Config.TrackBandwith {
usedBandwith.WithLabelValues(proxy.DomainName())
}

// Check if a gate is already listening to the Proxy address
addr := proxy.ListenTo()
if _, ok := gateway.listeners.Load(addr); ok {
Expand Down Expand Up @@ -754,6 +762,19 @@ func (gateway *Gateway) ClearCps() {
time.Sleep(time.Second)
}

func (gateway *Gateway) TrackBandwith() {
gateway.Proxies.Range(func(k, v interface{}) bool {
proxy := v.(*Proxy)
name := proxy.DomainName()
proxy.mu.Lock()
usedBandwith.WithLabelValues(name).Add(float64(proxy.usedBandwith))
proxy.usedBandwith = 0
proxy.mu.Unlock()
return false
})
time.Sleep(5 * time.Second)
}

func contains(s []string, str string) bool {
for _, v := range s {
if v == str {
Expand Down
14 changes: 11 additions & 3 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ type Proxy struct {
cacheStatusTime time.Time
cacheResponse status.ClientBoundResponse
cacheOnlineStatus bool

usedBandwith int
}

func (proxy *Proxy) DomainNames() []string {
Expand Down Expand Up @@ -185,8 +187,8 @@ func (proxy *Proxy) handleLoginConnection(conn Conn, session Session) error {
playersConnected.With(prometheus.Labels{"host": proxyDomain}).Inc()
defer playersConnected.With(prometheus.Labels{"host": proxyDomain}).Dec()

go pipe(rconn, conn)
pipe(conn, rconn)
go pipe(rconn, conn, proxy)
pipe(conn, rconn, proxy)
return nil
}

Expand Down Expand Up @@ -339,7 +341,7 @@ func (proxy *Proxy) handleStatusConnection(conn Conn, session Session) error {
return nil
}

func pipe(src, dst Conn) {
func pipe(src, dst Conn, proxy *Proxy) {
buffer := make([]byte, 0xffff)

for {
Expand All @@ -354,6 +356,12 @@ func pipe(src, dst Conn) {
if err != nil {
return
}

if Config.TrackBandwith {
proxy.mu.Lock()
proxy.usedBandwith = proxy.usedBandwith + len(data)
proxy.mu.Unlock()
}
}
}

Expand Down

0 comments on commit e3c56e1

Please sign in to comment.