Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Haproxy Support #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ See the [changelog](/CHANGELOG.md) for the latest updates.

## Table of content

- [**Features**](#features)
- [**Installation**](#installation)
- [**Compiling from source**](#compiling-from-source)
- [**Configuring MinePot**](#configuring-minepot)
- [**Config details**](#config-details)
- [**FAQ**](#faq)
- [**Credits**](#credits)
- [**Copyright**](#copyright)
- [MinePot](#minepot)
- [Table of content](#table-of-content)
- [Features](#features)
- [Installation](#installation)
- [Compiling from source](#compiling-from-source)
- [Configuring MinePot](#configuring-minepot)
- [Config details](#config-details)
- [FAQ](#faq)
- [Credits](#credits)
- [Copyright](#copyright)

## Features

Expand Down Expand Up @@ -63,6 +65,7 @@ If you already used [`install.sh`](/install.sh), the config can be found in `/et
| Item | Values | Meaning |
| ------------------ | ---------------------------------------------------------- | ------------------------------------------------------------------- |
| debug | `boolean` | Enable debug logs |
| haproxy | `boolean` | Enable HAProxy \(Do not touch unless you know what you're doing\) |
| writeLogs | `boolean` | Enable logs file |
| logFile | `text` | Path to the logs file |
| writeHistory | `boolean` | Enable history file |
Expand Down
1 change: 1 addition & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"debug": false,
"haproxy": false,

"writeLogs": false,
"logFile": "/var/log/minepot/minepot.logs",
Expand Down
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ require (
github.com/muesli/cache2go v0.0.0-20221011235721-518229cd8021
)

require github.com/google/uuid v1.3.0 // indirect
require (
github.com/google/uuid v1.3.0 // indirect
github.com/mailgun/proxyproto v1.0.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
)
44 changes: 44 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,50 @@
github.com/Tnze/go-mc v1.20.2 h1:arHCE/WxLCxY73C/4ZNLdOymRYtdwoXE05ohB7HVN6Q=
github.com/Tnze/go-mc v1.20.2/go.mod h1:geoRj2HsXSkB3FJBuhr7wCzXegRlzWsVXd7h7jiJ6aQ=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/go-ini/ini v1.55.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/mailgun/proxyproto v1.0.0 h1:CZTX/NM0qSq2JSatnowAhXmsHCXVu9JY6CDouOxJIQ4=
github.com/mailgun/proxyproto v1.0.0/go.mod h1:4r+sqMZLJWs8HRnFYcpYH/Cb+P2QGAQt+bV76JJkS4I=
github.com/muesli/cache2go v0.0.0-20221011235721-518229cd8021 h1:31Y+Yu373ymebRdJN1cWLLooHH8xAr0MhKTEJGV/87g=
github.com/muesli/cache2go v0.0.0-20221011235721-518229cd8021/go.mod h1:WERUkUryfUWlrHnFSO/BEUZ+7Ns8aZy7iVOGewxKzcc=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.12.0/go.mod h1:oUhWkIvk5aDxtKvDDuw8gItl8pKl42LzjC9KZE0HfGg=
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
github.com/onsi/gomega v1.9.0/go.mod h1:Ho0h+IUsWyvy1OpqCwxlQ/21gkhVunqlU8fDGcoTdcA=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/thrawn01/args v0.3.0/go.mod h1:TnRiOFjyh7Wa6oC8ACFPc7KIvbzCiluphA3mJUiPIEo=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20200320220750-118fecf932d8/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/ini.v1 v1.55.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
2 changes: 1 addition & 1 deletion handler/Connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func HandleConnection(conn types.ConnWrapper) {
remoteAddrString := conn.Conn.Socket.RemoteAddr().String()
remoteAddrString := conn.SrcAddr.String()

defer func() {
log.Println(remoteAddrString + " - Closing connection")
Expand Down
2 changes: 1 addition & 1 deletion handler/Handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func handleHandshake(conn *types.ConnWrapper) int {

if conn.Config.Debug {
log.Println(
conn.Conn.Socket.RemoteAddr().String() +
conn.SrcAddr.String() +
" - Received Handshake packet => Protocol version: " +
fmt.Sprint(Protocol) +
", Server address: " +
Expand Down
2 changes: 1 addition & 1 deletion handler/Ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func handlePing(conn *types.ConnWrapper, p *packet.Packet) {
var Magic packet.Long

remoteAddrString := conn.Conn.Socket.RemoteAddr().String()
remoteAddrString := conn.SrcAddr.String()

// Handle Ping Request packet : https://wiki.vg/Server_List_Ping#Ping_Request
if err := p.Scan(&Magic); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion handler/ServerListPing.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func handleServerListPing(conn *types.ConnWrapper) {
var p packet.Packet
remoteAddrString := conn.Conn.Socket.RemoteAddr().String()
remoteAddrString := conn.SrcAddr.String()

for i := 0; i < 2; i++ {

Expand Down
4 changes: 2 additions & 2 deletions handler/StatusRequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func handleStatusRequest(conn *types.ConnWrapper) {
remoteAddrString := conn.Conn.Socket.RemoteAddr().String()
remoteAddrString := conn.SrcAddr.String()

// Handle Status Request packet : https://wiki.vg/Server_List_Ping#Status_Request
if conn.Config.Debug {
Expand Down Expand Up @@ -48,7 +48,7 @@ func handleStatusRequest(conn *types.ConnWrapper) {
statusResponseData.Description = strings.Replace(
statusResponseData.Description,
ipSubstr,
conn.Conn.Socket.RemoteAddr().(*net.TCPAddr).IP.String(),
conn.SrcAddr.(*net.TCPAddr).IP.String(),
1,
)

Expand Down
31 changes: 29 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/LockBlock-dev/MinePot/types"
"github.com/LockBlock-dev/MinePot/util"
"github.com/Tnze/go-mc/net"
"github.com/mailgun/proxyproto"
"github.com/muesli/cache2go"
)

Expand Down Expand Up @@ -45,6 +46,10 @@ func main() {
}
}

if config.Haproxy {
log.Println("Using HAProxy protocol, make sure to configure the HAProxy to use it!")
}

// Setup the cache
_ = cache2go.Cache("MinePot")

Expand Down Expand Up @@ -83,9 +88,31 @@ func main() {
// Set a timeout of X seconds (see config.json)
conn.Socket.SetDeadline(time.Now().Add(time.Duration(config.IdleTimeoutS) * time.Second))

srcAddr := conn.Socket.RemoteAddr()
DestAddr := conn.Socket.LocalAddr()
if config.Haproxy {

h, err := proxyproto.ReadHeader(conn)
if err != nil {
log.Fatal("Client is not using the PROXY protocol " + srcAddr.String())
conn.Close()
continue
}

if h.IsLocal {
conn.Close()
continue
}

srcAddr = h.Source
DestAddr = h.Destination
}

connWrapper := types.ConnWrapper{
Conn: conn,
Config: config,
Conn: conn,
SrcAddr: srcAddr,
DestAddr: DestAddr,
Config: config,
}

// Start a new goroutine to handle the connection
Expand Down
3 changes: 2 additions & 1 deletion types/Config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package types

type Config struct {
Debug bool `json:"debug"`
Debug bool `json:"debug"`
Haproxy bool `json:"haproxy"`

WriteLogs bool `json:"writeLogs"`
LogFile string `json:"logFile"`
Expand Down
10 changes: 8 additions & 2 deletions types/ConnWrapper.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package types

import "github.com/Tnze/go-mc/net"
import (
"net"

mcNet "github.com/Tnze/go-mc/net"
)

type ConnWrapper struct {
net.Conn
mcNet.Conn
SrcAddr net.Addr
DestAddr net.Addr
Config *Config
PacketsReceived int
ReceivedProtocol int
Expand Down