Skip to content

Commit

Permalink
added latency to cached status made protocol of the handshake configu…
Browse files Browse the repository at this point in the history
…rable
  • Loading branch information
realDragonium committed Jul 11, 2021
1 parent c0d683e commit bf98a7f
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Im not planning on writing code to prevent Ultraviolet from crashing if you did
This has implemented [tableflip](https://github.com/cloudflare/tableflip) which should make it able to reload/restart Ultraviolet without closing existing connections on Linux and macOS. Ultraviolet should still be usable on windows (testing purposes only pls).
Check their [documentation](https://pkg.go.dev/github.com/cloudflare/tableflip) to know what or how.

IMPORTANT: There is a limit of one 'parent' process. So when you reload Ultraviolet once you need to wait until the parent process is closed (all previous connections have been closed) before you can reload it again.

## Command-Line Flags

Expand Down Expand Up @@ -48,6 +49,7 @@ time config values are based on go's duration formatting. They can be used in co
|rateCooldown|1s|rateCooldown is the time which it will take before the rateLimit will be reset.|
|stateUpdateCooldown|1s|The time it will assume that the state of the server isnt changed (that server isnt offline now while it was online the last time we checked). |
|cacheStatus|false|Turn on or off whether it should cache the online cache of the server. If the server is recognized as offline it will send the offline status to the player.|
|validProtocol|0|validProtocol is the protocol interger the handshake will have when sending the handshake to the backend. Its only necessary to have this when cacheStatus is on.|
|cacheUpdateCooldown|1s|The time it will assume that the statys of the server isnt changed (including player count). |


Expand Down
1 change: 1 addition & 0 deletions config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type ServerConfig struct {

CacheStatus bool `json:"cacheStatus"`
CacheUpdateCooldown string `json:"cacheUpdateCooldown"`
ValidProtocol int `json:"validProtocol"`
OfflineStatus mc.SimpleStatus `json:"offlineStatus"`

RateLimit int `json:"rateLimit"`
Expand Down
1 change: 1 addition & 0 deletions examples/server-example.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"sendProxyProtocol": false,
"disconnectMessage": "Sorry, but the server is offline.",
"cacheStatus": true,
"validProtocol": 755,
"cacheUpdateCooldown": "5s",
"offlineStatus": {
"name": "Ultraviolet",
Expand Down
1 change: 1 addition & 0 deletions proxy/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ func FileToWorkerConfig(cfg config.ServerConfig) WorkerServerConfig {
DialTimeout: dialTimeout,
SendProxyProtocol: cfg.SendProxyProtocol,
CacheStatus: cfg.CacheStatus,
ValidProtocol: cfg.ValidProtocol,
CacheUpdateCooldown: cacheCooldown,
OfflineStatus: offlineStatusPk,
DisconnectPacket: disconPk,
Expand Down
12 changes: 12 additions & 0 deletions proxy/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"log"
"net"
"time"

"github.com/realDragonium/Ultraviolet/mc"
)
Expand All @@ -17,6 +18,7 @@ const (
PROXY McAction = iota
DISCONNECT
SEND_STATUS
SEND_STATUS_CACHE
CLOSE
ERROR
)
Expand All @@ -30,6 +32,8 @@ func (state McAction) String() string {
text = "Disconnect"
case SEND_STATUS:
text = "Send Status"
case SEND_STATUS_CACHE:
text = "Send Status Cache"
case CLOSE:
text = "Close"
case ERROR:
Expand Down Expand Up @@ -68,6 +72,7 @@ type McAnswer struct {
Action McAction
StatusPk mc.Packet
ProxyCh chan ProxyAction
Latency time.Duration
}

func ServeListener(listener net.Listener, reqCh chan McRequest) {
Expand Down Expand Up @@ -152,6 +157,13 @@ func ReadConnection(c net.Conn, reqCh chan McRequest) {
pingPk, _ := conn.ReadPacket()
conn.WritePacket(pingPk)
conn.netConn.Close()
case SEND_STATUS_CACHE:
conn.ReadPacket()
conn.WritePacket(ans.StatusPk)
pingPk, _ := conn.ReadPacket()
time.Sleep(ans.Latency)
conn.WritePacket(pingPk)
conn.netConn.Close()
case CLOSE:
conn.netConn.Close()
}
Expand Down
19 changes: 17 additions & 2 deletions proxy/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type WorkerServerConfig struct {
StateUpdateCooldown time.Duration
CacheStatus bool
CacheUpdateCooldown time.Duration
ValidProtocol int
OfflineStatus mc.Packet
DisconnectPacket mc.Packet
ProxyTo string
Expand Down Expand Up @@ -130,6 +131,12 @@ func NewPrivateWorker(serverId int, cfg WorkerServerConfig) PrivateWorker {
return serverConn, nil
}
}
handshakePacket := mc.ServerBoundHandshake{
ProtocolVersion: mc.VarInt(cfg.ValidProtocol),
ServerAddress: "Ultraviolet",
ServerPort: 25565,
NextState: 1,
}.Marshal()

return PrivateWorker{
proxyCh: make(chan ProxyAction),
Expand All @@ -142,6 +149,7 @@ func NewPrivateWorker(serverId int, cfg WorkerServerConfig) PrivateWorker {
stateUpdateCh: make(chan ServerState),
disconnectPacket: cfg.DisconnectPacket,
serverConnFactory: createConnFeature,
statusHandshake: handshakePacket,
}
}

Expand All @@ -165,6 +173,8 @@ type PrivateWorker struct {
statusCache bool
statusCooldown time.Duration
statusCacheTime time.Time
statusLatency time.Duration
statusHandshake mc.Packet

serverConnFactory func(net.Addr) func() (net.Conn, error)
disconnectPacket mc.Packet
Expand Down Expand Up @@ -217,8 +227,9 @@ func (worker *PrivateWorker) HandleRequest(request McRequest) McAnswer {
worker.updateCacheStatus()
}
return McAnswer{
Action: SEND_STATUS,
Action: SEND_STATUS_CACHE,
StatusPk: worker.cachedStatus,
Latency: worker.statusLatency,
}
}
var connFunc func() (net.Conn, error)
Expand Down Expand Up @@ -260,7 +271,6 @@ func (worker *PrivateWorker) updateServerState() {
}

func (worker *PrivateWorker) updateCacheStatus() {

connFunc := worker.serverConnFactory(&net.IPAddr{})
conn, err := connFunc()
go func(sleepTime time.Duration, updateCh chan ServerState) {
Expand All @@ -274,8 +284,13 @@ func (worker *PrivateWorker) updateCacheStatus() {
worker.state = ONLINE
}
mcConn := NewMcConn(conn)
mcConn.WritePacket(worker.statusHandshake)
mcConn.WritePacket(mc.ServerBoundRequest{}.Marshal())
worker.cachedStatus, _ = mcConn.ReadPacket()
beginTime := time.Now()
mcConn.WritePacket(mc.NewServerBoundPing().Marshal())
mcConn.ReadPacket()
worker.statusLatency = time.Since(beginTime)
conn.Close()
worker.statusCacheTime = time.Now()
}

0 comments on commit bf98a7f

Please sign in to comment.