Skip to content

Commit

Permalink
refactor code & new motd system
Browse files Browse the repository at this point in the history
  • Loading branch information
mikhaillav committed Mar 24, 2024
1 parent 6477e08 commit 2bed19a
Showing 1 changed file with 20 additions and 28 deletions.
48 changes: 20 additions & 28 deletions src/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package server

import (
"errors"
"fmt"
"log"
"net"
"sync"

"context"
"fmt"
"strings"
"time"

conf "github.com/OpenFarLands/TheStoneProxy/src/config"
Expand Down Expand Up @@ -51,6 +50,7 @@ func New(proxyAddr, upstreamAddr string, paramConfig *conf.Config) (*Server, err
func (s *Server) handleConnection(conn net.Conn) {
server, err := raknet.DialTimeout(s.UpstreamAddr, 5*time.Second)
if err != nil {
// Try again, because connection sometimes is unstable
server, err = raknet.DialTimeout(s.UpstreamAddr, 5*time.Second)
if err != nil {
conn.Write([]byte{0x15})
Expand Down Expand Up @@ -83,11 +83,10 @@ func (s *Server) handleConnection(conn net.Conn) {

// Read from client, proxy to server
go func() {
b := make([]byte, 1024*1024*4)
for {
conn.SetDeadline(time.Now().Add(time.Duration(s.Timeout) * time.Second))
raknetConn.SetDeadline(time.Now().Add(time.Duration(s.Timeout) * time.Second))

n, err := conn.Read(b)
packet, err := raknetConn.ReadPacket()
if err != nil {
if !raknet.ErrConnectionClosed(err) && !errors.Is(err, context.DeadlineExceeded) {
log.Printf("Error reading from client connection: %v\n", err)
Expand All @@ -96,8 +95,6 @@ func (s *Server) handleConnection(conn net.Conn) {
return
}

packet := b[:n]

_, err = server.Write(packet)
if err != nil {
if !raknet.ErrConnectionClosed(err) {
Expand All @@ -111,10 +108,9 @@ func (s *Server) handleConnection(conn net.Conn) {

// Read from server, relay to client
go func() {
b := make([]byte, 1024*1024*4)
for {
server.SetDeadline(time.Now().Add(time.Duration(s.Timeout) * time.Second))
n, err := server.Read(b)
packet, err := server.ReadPacket()
if err != nil {
if !raknet.ErrConnectionClosed(err) && !errors.Is(err, context.DeadlineExceeded) {
log.Printf("Error reading from server connection: %v\n", err)
Expand All @@ -123,8 +119,6 @@ func (s *Server) handleConnection(conn net.Conn) {
return
}

packet := b[:n]

_, err = conn.Write(packet)
if err != nil {
if !raknet.ErrConnectionClosed(err) {
Expand All @@ -142,6 +136,7 @@ func (s *Server) StopHandle() {
s.Clients.Range(func(key net.Conn, value *Client) bool {
value.Addr.Write([]byte{0x15})
value.Addr.Close()
key.Write([]byte{0x15})
key.Close()
s.Clients.Delete(key)
return true
Expand All @@ -160,27 +155,24 @@ func (s *Server) StartHandle() {
// Get motd from upstream
go func() {
for {
usingOfflineMotd := false
motd, err := raknet.PingTimeout(s.UpstreamAddr, time.Second)
if err != nil {
motd = []byte(config.Network.OfflinePongMessage)
usingOfflineMotd = true
}
var motd *Motd

arrayMotd := strings.Split(string(motd), ";")
if (!usingOfflineMotd || arrayMotd[6] == "SERVER_UNIQUE_ID") && len(arrayMotd) >= 6 {
arrayMotd[6] = fmt.Sprint(listener.ID())
}
if (!usingOfflineMotd || arrayMotd[10] == "PORT_V_4") && len(arrayMotd) >= 10 {
arrayMotd[10] = fmt.Sprint(s.ProxyAddr.Port)
}
if (!usingOfflineMotd || arrayMotd[11] == "PORT_V_6") && len(arrayMotd) >= 11{
arrayMotd[11] = fmt.Sprint(s.ProxyAddr.Port)
byteMotd, err := raknet.PingTimeout(s.UpstreamAddr, time.Second)
if err != nil {
motd = NewMotd()
motd.motd = config.OfflineMotd.Motd
motd.protocolVersion = config.OfflineMotd.ProtocolVersion
motd.versionName = config.OfflineMotd.VersionName
motd.levelName = config.OfflineMotd.LevelName
} else {
motd = NewMotdFromString(string(byteMotd))
}

stringMotd := strings.Join(arrayMotd, ";")
motd.serverUniqueId = fmt.Sprint(listener.ID())
motd.port4 = s.ProxyAddr.Port
motd.port6 = s.ProxyAddr.Port

listener.PongData([]byte(stringMotd))
listener.PongData([]byte(motd.String()))

time.Sleep(time.Duration(config.Network.MotdGetInterval) * time.Second)
}
Expand Down

0 comments on commit 2bed19a

Please sign in to comment.