Skip to content

Commit

Permalink
fix some deadlocks with resourcepacks
Browse files Browse the repository at this point in the history
  • Loading branch information
olebeck committed Jul 13, 2024
1 parent 7e90c03 commit ca65de6
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 18 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/OneOfOne/xxhash v1.2.8
github.com/cloudfoundry/jibber_jabber v0.0.0-20151120183258-bcc4c8345a21
github.com/dblezek/tga v0.0.0-20150626111426-80720cbc1017
github.com/denisbrodbeck/machineid v1.0.1
github.com/df-mc/dragonfly v0.9.15
github.com/df-mc/goleveldb v1.1.9
github.com/dop251/goja v0.0.0-20240610225006-393f6d42497b
Expand Down Expand Up @@ -44,6 +45,7 @@ require (
gioui.org/cpu v0.0.0-20220412190645-f1e9e8c3b1f7 // indirect
git.wow.st/gmp/jni v0.0.0-20210610011705-34026c7e22d0 // indirect
github.com/brentp/intintmap v0.0.0-20190211203843-30dc0ade9af9 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/changkun/lockfree v0.0.1 // indirect
github.com/df-mc/atomic v1.10.0 // indirect
github.com/df-mc/worldupgrader v1.0.15 // indirect
Expand All @@ -61,6 +63,7 @@ require (
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/muhammadmuzzammil1998/jsonc v1.0.0 // indirect
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/segmentio/fasthash v1.0.3 // indirect
github.com/tklauser/go-sysconf v0.3.14 // indirect
github.com/tklauser/numcpus v0.8.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ github.com/OneOfOne/xxhash v1.2.8 h1:31czK/TI9sNkxIKfaUfGlU47BAxQ0ztGgd9vPyqimf8
github.com/OneOfOne/xxhash v1.2.8/go.mod h1:eZbhyaAYD41SGSSsnmcpxVoRiQ/MPUTjUdIIOT9Um7Q=
github.com/brentp/intintmap v0.0.0-20190211203843-30dc0ade9af9 h1:/G0ghZwrhou0Wq21qc1vXXMm/t/aKWkALWwITptKbE0=
github.com/brentp/intintmap v0.0.0-20190211203843-30dc0ade9af9/go.mod h1:TOk10ahXejq9wkEaym3KPRNeuR/h5Jx+s8QRWIa2oTM=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/changkun/lockfree v0.0.1 h1:5WefVJLglY4IHRqOQmh6Ao6wkJYaJkarshKU8VUtId4=
github.com/changkun/lockfree v0.0.1/go.mod h1:3bKiaXn/iNzIPlSvSOMSVbRQUQtAp8qUAyBUtzU11s4=
github.com/cloudfoundry/jibber_jabber v0.0.0-20151120183258-bcc4c8345a21 h1:tuijfIjZyjZaHq9xDUh0tNitwXshJpbLkqMOJv4H3do=
Expand All @@ -29,6 +31,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dblezek/tga v0.0.0-20150626111426-80720cbc1017 h1:awJfkE1xXsPK+yOi1JfFBYCrSBkZXWbOgEFL6dmYeUA=
github.com/dblezek/tga v0.0.0-20150626111426-80720cbc1017/go.mod h1:47yJHzYP/+2SCHY45B0eyR1QaecoOhkTTpS7UasE0DY=
github.com/denisbrodbeck/machineid v1.0.1 h1:geKr9qtkB876mXguW2X6TU4ZynleN6ezuMSRhl4D7AQ=
github.com/denisbrodbeck/machineid v1.0.1/go.mod h1:dJUwb7PTidGDeYyUBmXZ2GphQBbjJCrnectwCyxcUSI=
github.com/df-mc/atomic v1.10.0 h1:0ZuxBKwR/hxcFGorKiHIp+hY7hgY+XBTzhCYD2NqSEg=
github.com/df-mc/atomic v1.10.0/go.mod h1:Gw9rf+rPIbydMjA329Jn4yjd/O2c/qusw3iNp4tFGSc=
github.com/df-mc/dragonfly v0.9.15 h1:f76x7IySakQEOANOSXDxPp4dsxNHMEBsu0/ESFO45cI=
Expand Down Expand Up @@ -107,6 +111,8 @@ github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 h1:o4JXh1EVt
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5 h1:mZHayPoR0lNmnHyvtYjDeq0zlVHn9K/ZXoy17ylucdo=
github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5/go.mod h1:GEXHk5HgEKCvEIIrSpFI3ozzG5xOKA2DVlEX/gGnewM=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/sandertv/go-raknet v1.14.0 h1:2vtO1m1DFLFszeCcV7mVZfVgkDcAbSxcjM2BlrVrEGs=
github.com/sandertv/go-raknet v1.14.0/go.mod h1:/yysjwfCXm2+2OY8mBazLzcxJ3irnylKCyG3FLgUPVU=
github.com/sandertv/gophertunnel v1.38.0 h1:hGCq9uhAmIFOLAaw/qcLkmcHVo7dVzf3I9Iqx0GOb+0=
Expand Down
Binary file modified subcommands/resourcepack-d/resourcepack-d.go
Binary file not shown.
34 changes: 28 additions & 6 deletions utils/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"errors"
"io"
"os"

"github.com/sandertv/gophertunnel/minecraft/auth"
Expand Down Expand Up @@ -31,6 +32,9 @@ func (a *authsrv) Startup() (err error) {
if errors.Is(err, os.ErrNotExist) {
return nil
}
if err != nil {
return err
}
a.tokenSource = auth.RefreshTokenSource(a.liveToken)
a.realms = realms.NewClient(a.tokenSource)
_, err = a.TokenSource()
Expand Down Expand Up @@ -85,31 +89,49 @@ func (a *authsrv) refreshLiveToken() (err error) {
return nil
}

var Ver1token func(f io.ReadSeeker) (*oauth2.Token, error)
var Tokene = func(t *oauth2.Token, w io.Writer) error {
return json.NewEncoder(w).Encode(t)
}

// writes the livetoken to storage
func (a *authsrv) writeToken(token *oauth2.Token) error {
f, err := os.Create(TokenFile)
if err != nil {
return err
}
defer f.Close()
e := json.NewEncoder(f)
return e.Encode(token)
return Tokene(token, f)
}

// reads the live token from storage, returns os.ErrNotExist if no token is stored
func (a *authsrv) readToken() (*oauth2.Token, error) {
var token oauth2.Token
f, err := os.Open(TokenFile)
if err != nil {
return nil, err
}
defer f.Close()
e := json.NewDecoder(f)
err = e.Decode(&token)

var b = make([]byte, 1)
_, err = f.ReadAt(b, 0)
if err != nil {
return nil, err
}
return &token, nil

switch b[0] {
case '{':
var token oauth2.Token
e := json.NewDecoder(f)
err = e.Decode(&token)
if err != nil {
return nil, err
}
return &token, nil
case '1':
return Ver1token(f)
}

return nil, errors.New("unsupported token file")
}

var ErrNotLoggedIn = errors.New("not logged in")
Expand Down
4 changes: 4 additions & 0 deletions utils/proxy/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ func (p *Context) connectClient(ctx context.Context, serverAddress string) (err
close(p.haveClientData)
},
EarlyConnHandler: func(c *minecraft.Conn) {
if p.Client != nil {
p.listener.Disconnect(c, "You are Already connected!")
return
}
p.Client = c
p.rpHandler.SetClient(c)
c.ResourcePackHandler = p.rpHandler
Expand Down
19 changes: 7 additions & 12 deletions utils/proxy/resourcepacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ func (r *rpHandler) SetServer(c minecraft.IConn) {

func (r *rpHandler) SetClient(c minecraft.IConn) {
r.Client = c
r.nextPackToClient = make(chan resource.Pack)
r.knowPacksRequestedFromServer = make(chan struct{})
r.clientDone = make(chan struct{})
}
Expand Down Expand Up @@ -212,17 +211,6 @@ func (r *rpHandler) OnResourcePacksInfo(pk *packet.ResourcePacksInfo) error {
r.log.Error(err)
return
}

if r.Client != nil {
select {
case <-r.knowPacksRequestedFromServer:
case <-r.ctx.Done():
return
}
if slices.Contains(r.packsRequestedFromServer, newPack.UUID()) {
r.nextPackToClient <- newPack
}
}
}()
r.downloadQueue.serverPackAmount--
continue
Expand Down Expand Up @@ -451,6 +439,9 @@ func (r *rpHandler) downloadResourcePack(pk *packet.ResourcePackDataInfo) error

// from server
func (r *rpHandler) OnResourcePackDataInfo(pk *packet.ResourcePackDataInfo) error {
if _, ok := r.cache.(*replayCache); ok {
return nil
}
r.packDownloads <- pk
return nil
}
Expand Down Expand Up @@ -714,6 +705,10 @@ func (r *rpHandler) processClientRequest(packs []string) error {
r.log.Errorf("BUG: not enough packs sent to client, client will stall %d + %d %d", len(r.packsFromCache), len(r.packsRequestedFromServer), len(packs))
}

if len(r.packsRequestedFromServer) == 0 {
close(r.nextPackToClient)
}

close(r.knowPacksRequestedFromServer)
return nil
}
Expand Down

0 comments on commit ca65de6

Please sign in to comment.