-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
378 lines (334 loc) · 10.1 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package main
import (
"context"
rand2 "crypto/rand"
"encoding/base64"
"encoding/json"
"fmt"
atomic2 "github.com/df-mc/atomic"
"github.com/df-mc/dragonfly/server"
"github.com/df-mc/dragonfly/server/block"
"github.com/df-mc/dragonfly/server/block/cube"
"github.com/df-mc/dragonfly/server/player"
"github.com/df-mc/dragonfly/server/world"
"github.com/df-mc/goleveldb/leveldb"
"github.com/google/uuid"
"github.com/lactyy/idou/internal"
"github.com/lactyy/idou/internal/conf"
"github.com/sandertv/gophertunnel/minecraft"
"github.com/sandertv/gophertunnel/minecraft/auth"
"github.com/sandertv/gophertunnel/minecraft/franchise"
"github.com/sandertv/gophertunnel/minecraft/franchise/signaling"
"github.com/sandertv/gophertunnel/minecraft/nethernet"
"github.com/sandertv/gophertunnel/minecraft/protocol"
"github.com/sandertv/gophertunnel/minecraft/protocol/packet"
"github.com/sandertv/gophertunnel/minecraft/room"
"github.com/sandertv/gophertunnel/xsapi"
"github.com/sandertv/gophertunnel/xsapi/mpsd"
"github.com/sandertv/gophertunnel/xsapi/xal"
"github.com/sirupsen/logrus"
"golang.org/x/oauth2"
"golang.org/x/text/language"
"golang.org/x/text/language/display"
"golang.org/x/text/message"
"log/slog"
"math/rand"
"net"
"os"
"strings"
"sync/atomic"
"time"
_ "unsafe"
)
func main() {
log := logrus.New()
log.Level = logrus.DebugLevel
// var setting webrtc.SettingEngine
// factory := logging.NewDefaultLoggerFactory()
// factory.DefaultLogLevel = logging.LogLevelDebug
// factory.Writer = log.WriterLevel(logrus.DebugLevel)
// setting.LoggerFactory = factory
// nethernet.DefaultDialer.API = webrtc.NewAPI(webrtc.WithSettingEngine(setting))
// May be we can make the default value a function, that is only called
// when the file does not exist.
cfg, err := conf.Read(conf.TOMLEncoding, "config", defaultConfig())
if err != nil {
log.Fatalf("error reading config: %s", err)
}
var profiles internal.LoadStorer[uuid.UUID, profile]
{
db, err := leveldb.OpenFile(cfg.Database.Folder, nil)
if err != nil {
log.Fatalf("error opening database: %s", err)
}
defer db.Close()
log.Infof("Database opened at %q", cfg.Database.Folder)
profiles = internal.LevelDBProvider[uuid.UUID, profile]{
DB: db,
}
}
cred, err := conf.Read(conf.JSONEncoding, "auth.cred", &credentials{
src: auth.TokenSource,
})
if err != nil {
log.Fatalf("error reading credentials: %s", err)
}
src := auth.RefreshTokenSource(cred.Token)
c, err := cfg.Server.Config(log)
if err != nil {
log.Fatalf("error creating config: %s", err)
}
{
log.Infof("Publishing session...")
discovery, err := franchise.Discover(protocol.CurrentVersion)
if err != nil {
log.Fatalf("error retrieving discovery: %s", err)
}
a := new(franchise.AuthorizationEnvironment)
if err := discovery.Environment(a, franchise.EnvironmentTypeProduction); err != nil {
log.Fatalf("error reading environment for authorization: %s", err)
}
s := new(signaling.Environment)
if err := discovery.Environment(s, franchise.EnvironmentTypeProduction); err != nil {
log.Fatalf("error reading environment for signaling: %s", err)
}
refresh, cancel := context.WithCancel(context.Background())
defer cancel()
prov := franchise.PlayFabXBLIdentityProvider{
Environment: a,
TokenSource: xal.RefreshTokenSourceContext(refresh, src, "http://playfab.xboxlive.com/"),
}
d := signaling.Dialer{
NetworkID: rand.Uint64(),
}
dial, cancel := context.WithTimeout(context.Background(), time.Second*15)
defer cancel()
conn, err := d.DialContext(dial, prov, s)
if err != nil {
log.Fatalf("error dialing signaling: %s", err)
}
defer conn.Close()
// A token source that refreshes a token used for generic Xbox Live services.
x := xal.RefreshTokenSourceContext(refresh, src, "http://xboxlive.com")
xt, err := x.Token()
if err != nil {
log.Fatalf("error refreshing xbox live token: %s", err)
}
claimer, ok := xt.(xsapi.DisplayClaimer)
if !ok {
log.Fatalf("xbox live token %T does not implement xsapi.DisplayClaimer", xt)
}
displayClaims := claimer.DisplayClaims()
// The name of the session being published. This seems always to be generated
// randomly, referenced as "GUID" of the session.
name := strings.ToUpper(uuid.NewString())
levelID := make([]byte, 8)
_, _ = rand2.Read(levelID)
custom, err := json.Marshal(room.Status{
Joinability: room.JoinabilityJoinableByFriends,
HostName: displayClaims.GamerTag,
OwnerID: displayClaims.XUID,
RakNetGUID: "",
// This is displayed as the suffix of the world name.
Version: protocol.CurrentVersion,
LevelID: base64.StdEncoding.EncodeToString(levelID),
WorldName: c.Name,
WorldType: room.WorldTypeCreative,
// The game seems checking this field before joining a session, causes
// RequestNetworkSettings packet not being even sent to the remote host.
Protocol: protocol.CurrentProtocol,
MemberCount: 1,
MaxMemberCount: 8,
BroadcastSetting: room.BroadcastSettingFriendsOfFriends,
LanGame: true,
IsEditorWorld: false,
TransportLayer: 2,
WebRTCNetworkID: d.NetworkID,
OnlineCrossPlatformGame: true,
CrossPlayDisabled: false,
TitleID: 0,
SupportedConnections: []room.Connection{
{
ConnectionType: 3, // WebSocketsWebRTCSignaling
HostIPAddress: "",
HostPort: 0,
NetherNetID: d.NetworkID,
WebRTCNetworkID: d.NetworkID,
RakNetGUID: "UNASSIGNED_RAKNET_GUID",
},
},
})
if err != nil {
log.Fatalf("error encoding custom properties: %s", err)
}
cfg := mpsd.PublishConfig{
Description: &mpsd.SessionDescription{
Properties: &mpsd.SessionProperties{
System: &mpsd.SessionPropertiesSystem{
JoinRestriction: mpsd.SessionRestrictionFollowed,
ReadRestriction: mpsd.SessionRestrictionFollowed,
},
Custom: custom,
},
},
}
publish, cancel := context.WithTimeout(context.Background(), time.Second*15)
defer cancel()
session, err := cfg.PublishContext(publish, x, mpsd.SessionReference{
ServiceConfigID: uuid.MustParse("4fc10100-5f7a-4470-899b-280835760c07"),
TemplateName: "MinecraftLobby",
Name: name,
})
if err != nil {
log.Fatalf("error publishing session: %s", err)
}
defer session.Close()
log.Debugf("Session Name: %q", name)
log.Debugf("Network ID: %d", d.NetworkID)
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelDebug,
})))
minecraft.RegisterNetwork("nethernet", &nethernet.Network{
Signaling: conn,
})
}
c.WorldProvider = world.NopProvider{}
c.Generator = func(world.Dimension) world.Generator {
return world.NopGenerator{}
}
c.PlayerProvider = playerProvider{}
c.Listeners = []func(server.Config) (server.Listener, error){
cfg.listenerFunc,
}
srv := c.New()
srv.CloseOnProgramEnd()
srv.World().SetBlock(cube.Pos{0, -2, 0}, block.Barrier{}, nil)
srv.World().StopWeatherCycle()
srv.World().StopTime()
srv.Listen()
for srv.Accept(func(p *player.Player) {
plog := log.WithFields(logrus.Fields{
"uuid": p.UUID(),
"address": p.Addr(),
"name": p.Name(),
})
// TODO
writePacket(sessionOf(p), &packet.GameRulesChanged{
GameRules: []protocol.GameRule{
{
Name: "showTags",
Value: false,
},
},
})
p.SetImmobile()
for _, other := range srv.Players() {
other.HideEntity(p)
p.HideEntity(other)
}
prof, err := internal.LoadDefault(profiles, p.UUID(), profile{
UUID: p.UUID(),
})
if err != nil {
plog.Errorf("error loading profile: %s", err)
p.Disconnect("Sorry, We couldn't load your profile")
return
}
if prof.Language == language.Und {
locale := p.Locale()
base, _ := locale.Base()
for _, tag := range message.DefaultCatalog.Languages() {
if b, _ := tag.Base(); base == b {
prof.Language = tag
p.Message(message.NewPrinter(tag).Sprintf("language.auto", display.Self.Name(tag)))
goto FOUND
}
}
prof.Language = language.English
FOUND:
}
m := message.NewPrinter(prof.Language)
a := new(atomic.Pointer[message.Printer])
a.Store(m)
h := &handler{
p: p,
log: plog.Logger,
m: a,
prof: atomic2.NewValue(prof),
storer: profiles,
}
handlers.Store(p.UUID(), h)
p.Handle(h)
p.SendForm(newServerForm(m, h.prof.Load()))
h.setInventory(p, m)
}) {
}
}
type profile struct {
UUID uuid.UUID `json:"uuid"`
Language language.Tag `json:"language"`
Servers []serverProfile `json:"servers"`
}
type serverProfile struct {
Name string `json:"name,omitempty"`
Address *net.UDPAddr `json:"address"`
RawAddress string `json:"rawAddress"`
}
func serverProfileName(s string) string {
if len(s) > 20 {
s = ""
}
return s
}
func addressEqual(a, b *net.UDPAddr) bool {
if a.String() == b.String() {
return true
}
if a.IP.Equal(b.IP) && a.Port == b.Port {
return true
}
return false
}
func (prof profile) shouldRemember(addr *net.UDPAddr, rawAddr string) bool {
for _, srv := range prof.Servers {
if addressEqual(srv.Address, addr) || rawAddr == srv.RawAddress {
return false
}
}
return true
}
func defaultConfig() (cfg config) {
cfg.Database.Folder = "db"
cfg.Server = server.DefaultConfig()
cfg.Server.World.SaveData = false
cfg.Server.Players.SaveData = false
cfg.Server.Server.JoinMessage = ""
cfg.Server.Server.QuitMessage = ""
cfg.Server.Server.Name = "Testing"
return cfg
}
type config struct {
Listener struct {
NetworkID uint64 `json:"networkID" toml:"networkID"`
} `json:"listener" toml:"listener"`
Database struct {
Folder string `json:"folder" toml:"folder"`
} `json:"db" toml:"db"`
Server server.UserConfig `json:"server" toml:"server"`
}
//go:linkname biomes github.com/df-mc/dragonfly/server.biomes
func biomes() map[string]any
type credentials struct {
*oauth2.Token `json:"token"`
src oauth2.TokenSource
}
func (cred *credentials) MarshalJSON() ([]byte, error) {
if cred.Token == nil {
t, err := cred.src.Token()
if err != nil {
return nil, fmt.Errorf("request token: %w", err)
}
cred.Token = t
}
return json.Marshal(*cred)
}