-
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
250 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
package infrared | ||
|
||
import ( | ||
"encoding/base64" | ||
"encoding/json" | ||
_ "image/png" | ||
"os" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/haveachin/infrared/pkg/infrared/protocol" | ||
"github.com/haveachin/infrared/pkg/infrared/protocol/login" | ||
"github.com/haveachin/infrared/pkg/infrared/protocol/status" | ||
) | ||
|
||
type PlayerSampleConfig struct { | ||
Name string `yaml:"name"` | ||
UUID string `yaml:"uuid"` | ||
} | ||
|
||
type PlayerSamples []PlayerSampleConfig | ||
|
||
func (ps PlayerSamples) PlayerSampleJSON() []status.PlayerSampleJSON { | ||
psJSON := make([]status.PlayerSampleJSON, len(ps)) | ||
for i, s := range ps { | ||
psJSON[i] = status.PlayerSampleJSON{ | ||
Name: s.Name, | ||
ID: s.UUID, | ||
} | ||
} | ||
return psJSON | ||
} | ||
|
||
type HandshakeStatusResponseConfig struct { | ||
VersionName string `yaml:"versionName"` | ||
ProtocolNumber int `yaml:"protocolNumber"` | ||
MaxPlayerCount int `yaml:"maxPlayerCount"` | ||
PlayerCount int `yaml:"playerCount"` | ||
PlayerSamples PlayerSamples `yaml:"playerSamples"` | ||
Icon string `yaml:"icon"` | ||
MOTD string `yaml:"motd"` | ||
} | ||
|
||
type HandshakeResponseConfig struct { | ||
StatusConfig HandshakeStatusResponseConfig `yaml:"status"` | ||
Message string `yaml:"message"` | ||
} | ||
|
||
type HandshakeResponse struct { | ||
Config HandshakeResponseConfig | ||
|
||
statusOnce sync.Once | ||
statusRespJSON status.ResponseJSON | ||
statusRespPk protocol.Packet | ||
|
||
loginOnce sync.Once | ||
loginRespPk protocol.Packet | ||
} | ||
|
||
func (r *HandshakeResponse) StatusResponse(protVer protocol.Version) (status.ResponseJSON, protocol.Packet) { | ||
r.statusOnce.Do(func() { | ||
cfg := r.Config.StatusConfig | ||
|
||
protNum := cfg.ProtocolNumber | ||
if protNum < 0 { | ||
protNum = int(protVer) | ||
} | ||
|
||
r.statusRespJSON = status.ResponseJSON{ | ||
Version: status.VersionJSON{ | ||
Name: cfg.VersionName, | ||
Protocol: protNum, | ||
}, | ||
Players: status.PlayersJSON{ | ||
Max: cfg.MaxPlayerCount, | ||
Online: cfg.PlayerCount, | ||
Sample: cfg.PlayerSamples.PlayerSampleJSON(), | ||
}, | ||
Favicon: parseServerIcon(cfg.Icon), | ||
Description: parseJSONTextComponent(cfg.MOTD), | ||
} | ||
|
||
respBytes, err := json.Marshal(r.statusRespJSON) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
statusPk := status.ClientBoundResponse{ | ||
JSONResponse: protocol.String(string(respBytes)), | ||
} | ||
|
||
if err := statusPk.Marshal(&r.statusRespPk); err != nil { | ||
panic(err) | ||
} | ||
}) | ||
|
||
return r.statusRespJSON, r.statusRespPk | ||
} | ||
|
||
func (r *HandshakeResponse) LoginReponse() protocol.Packet { | ||
r.loginOnce.Do(func() { | ||
msg := parseJSONTextComponent(r.Config.Message) | ||
disconnectPk := login.ClientBoundDisconnect{ | ||
Reason: protocol.String(msg), | ||
} | ||
|
||
if err := disconnectPk.Marshal(&r.loginRespPk); err != nil { | ||
panic(err) | ||
} | ||
}) | ||
|
||
return r.loginRespPk | ||
} | ||
|
||
type OverrideStatusResponseConfig struct { | ||
VersionName *string `yaml:"versionName"` | ||
ProtocolNumber *int `yaml:"protocolNumber"` | ||
MaxPlayerCount *int `yaml:"maxPlayerCount"` | ||
PlayerCount *int `yaml:"playerCount"` | ||
PlayerSamples PlayerSamples `yaml:"playerSamples"` | ||
Icon *string `yaml:"icon"` | ||
MOTD *string `yaml:"motd"` | ||
} | ||
|
||
type OverrideStatusResponse struct { | ||
Config OverrideStatusResponseConfig | ||
|
||
once sync.Once | ||
overrideFn func(resp status.ResponseJSON) status.ResponseJSON | ||
} | ||
|
||
func (r *OverrideStatusResponse) OverrideStatusResponseJSON(resp status.ResponseJSON) status.ResponseJSON { | ||
r.once.Do(func() { | ||
cfg := r.Config | ||
icon := parseServerIcon(*cfg.Icon) | ||
playerSamples := cfg.PlayerSamples.PlayerSampleJSON() | ||
motd := parseJSONTextComponent(*cfg.MOTD) | ||
|
||
r.overrideFn = func(resp status.ResponseJSON) status.ResponseJSON { | ||
if cfg.Icon != nil { | ||
resp.Favicon = icon | ||
} | ||
|
||
if cfg.VersionName != nil { | ||
resp.Version.Name = *cfg.VersionName | ||
} | ||
|
||
if cfg.ProtocolNumber != nil { | ||
resp.Version.Protocol = *cfg.ProtocolNumber | ||
} | ||
|
||
if cfg.MaxPlayerCount != nil { | ||
resp.Players.Max = *cfg.MaxPlayerCount | ||
} | ||
|
||
if cfg.PlayerCount != nil { | ||
resp.Players.Online = *cfg.PlayerCount | ||
} | ||
|
||
if len(cfg.PlayerSamples) != 0 { | ||
resp.Players.Sample = playerSamples | ||
} | ||
|
||
if cfg.MOTD != nil { | ||
resp.Description = motd | ||
} | ||
|
||
return resp | ||
} | ||
}) | ||
|
||
return r.overrideFn(resp) | ||
} | ||
|
||
func parseServerIcon(iconStr string) string { | ||
if iconStr == "" { | ||
return "" | ||
} | ||
|
||
const base64PNGHeader = "data:image/png;base64," | ||
if strings.HasPrefix(iconStr, base64PNGHeader) { | ||
return iconStr | ||
} | ||
|
||
iconBytes, err := os.ReadFile(iconStr) | ||
if err != nil { | ||
Log.Error(). | ||
Err(err). | ||
Str("iconPath", iconStr). | ||
Msg("Failed to open icon file") | ||
return "" | ||
} | ||
|
||
iconBase64 := base64.StdEncoding.EncodeToString(iconBytes) | ||
return base64PNGHeader + iconBase64 | ||
} | ||
|
||
func parseJSONTextComponent(s string) json.RawMessage { | ||
var motdJSON json.RawMessage | ||
if err := json.Unmarshal([]byte(s), &motdJSON); err != nil { | ||
motdJSON = []byte(`{"text":"` + s + `"}`) | ||
} | ||
return motdJSON | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.