fix token bugs

This commit is contained in:
olebeck 2022-08-16 04:40:08 +02:00
parent 841ce5ace8
commit 471b595e1b
5 changed files with 88 additions and 31 deletions

View File

@ -11,7 +11,7 @@ import (
func Benchmark_chunk_decode(b *testing.B) { func Benchmark_chunk_decode(b *testing.B) {
data, _ := os.ReadFile("chunk.bin") data, _ := os.ReadFile("chunk.bin")
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
_, err := chunk.NetworkDecode(33, data, 6, cube.Range{-64, 319}) _, _, err := chunk.NetworkDecode(33, data, 6, cube.Range{0, 255}, true)
if err != nil { if err != nil {
b.Error(err) b.Error(err)
} }
@ -20,7 +20,7 @@ func Benchmark_chunk_decode(b *testing.B) {
func Benchmark_render_chunk(b *testing.B) { func Benchmark_render_chunk(b *testing.B) {
data, _ := os.ReadFile("chunk.bin") data, _ := os.ReadFile("chunk.bin")
ch, _ := chunk.NetworkDecode(33, data, 6, cube.Range{-64, 319}) ch, _, _ := chunk.NetworkDecode(33, data, 6, cube.Range{0, 255}, true)
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
Chunk2Img(ch) Chunk2Img(ch)

40
main.go
View File

@ -23,8 +23,6 @@ import (
const TOKEN_FILE = "token.json" const TOKEN_FILE = "token.json"
var G_src oauth2.TokenSource
var G_xbl_token *auth.XBLToken
var G_debug bool var G_debug bool
var G_preload_packs bool var G_preload_packs bool
var G_exit []func() = []func(){} var G_exit []func() = []func(){}
@ -101,13 +99,34 @@ func register_command(sub subcommands.Command) {
valid_cmds[sub.Name()] = sub.Synopsis() valid_cmds[sub.Name()] = sub.Synopsis()
} }
var G_token_src oauth2.TokenSource
func GetTokenSource() oauth2.TokenSource {
if G_token_src != nil {
return G_token_src
}
token := get_token()
G_token_src = auth.RefreshTokenSource(&token)
new_token, err := G_token_src.Token()
if err != nil {
panic(err)
}
if !token.Valid() {
fmt.Println("Refreshed token")
write_token(new_token)
}
return G_token_src
}
func main() { func main() {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
flag.BoolVar(&G_debug, "debug", false, "debug mode") flag.BoolVar(&G_debug, "debug", false, "debug mode")
flag.BoolVar(&G_preload_packs, "preload", false, "preload resourcepacks for proxy") flag.BoolVar(&G_preload_packs, "preload", false, "preload resourcepacks for proxy")
enable_dns := flag.Bool("dns", false, "enable dns server for consoles") enable_dns := flag.Bool("dns", false, "enable dns server for consoles")
println(a)
println("")
subcommands.Register(subcommands.HelpCommand(), "") subcommands.Register(subcommands.HelpCommand(), "")
subcommands.ImportantFlag("debug") subcommands.ImportantFlag("debug")
subcommands.ImportantFlag("dns") subcommands.ImportantFlag("dns")
@ -151,21 +170,6 @@ func main() {
exit() exit()
}() }()
{ // authenticate
token := get_token()
G_src = auth.RefreshTokenSource(&token)
{
_token, err := G_src.Token()
if err != nil {
panic(err)
}
G_xbl_token, err = auth.RequestXBLToken(ctx, _token, "https://pocket.realms.minecraft.net/")
if err != nil {
panic(err)
}
}
}
ret := subcommands.Execute(ctx) ret := subcommands.Execute(ctx)
exit() exit()
os.Exit(int(ret)) os.Exit(int(ret))

View File

@ -11,6 +11,7 @@ import (
"strings" "strings"
"github.com/google/subcommands" "github.com/google/subcommands"
"github.com/sandertv/gophertunnel/minecraft/auth"
) )
type Realm struct { type Realm struct {
@ -21,6 +22,23 @@ type Realm struct {
State string `json:"state"` State string `json:"state"`
} }
var _G_xbl_token *auth.XBLToken
func GetXBLToken(ctx context.Context) *auth.XBLToken {
if _G_xbl_token != nil {
return _G_xbl_token
}
_token, err := GetTokenSource().Token()
if err != nil {
panic(err)
}
_G_xbl_token, err = auth.RequestXBLToken(ctx, _token, "https://pocket.realms.minecraft.net/")
if err != nil {
panic(err)
}
return _G_xbl_token
}
func realms_get(path string) ([]byte, error) { func realms_get(path string) ([]byte, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("https://pocket.realms.minecraft.net/%s", path), nil) req, err := http.NewRequest("GET", fmt.Sprintf("https://pocket.realms.minecraft.net/%s", path), nil)
if err != nil { if err != nil {
@ -28,7 +46,7 @@ func realms_get(path string) ([]byte, error) {
} }
req.Header.Set("User-Agent", "MCPE/UWP") req.Header.Set("User-Agent", "MCPE/UWP")
req.Header.Set("Client-Version", "1.10.1") req.Header.Set("Client-Version", "1.10.1")
G_xbl_token.SetAuthHeader(req) GetXBLToken(context.Background()).SetAuthHeader(req)
resp, err := http.DefaultClient.Do(req) resp, err := http.DefaultClient.Do(req)
if err != nil { if err != nil {
@ -114,8 +132,8 @@ func (c *TokenCMD) Usage() string {
return c.Name() + ": " + c.Synopsis() + "\n" return c.Name() + ": " + c.Synopsis() + "\n"
} }
func (c *TokenCMD) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { func (c *TokenCMD) Execute(ctx context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
fmt.Printf("%s\n", G_xbl_token.AuthorizationToken.Token) fmt.Printf("%s\n", GetXBLToken(ctx).AuthorizationToken.Token)
return 0 return 0
} }

View File

@ -2,15 +2,20 @@ package main
import ( import (
"bufio" "bufio"
"bytes"
"compress/gzip"
"context" "context"
"encoding/base64"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"log" "log"
"net" "net"
"os" "os"
"path" "path"
"regexp" "regexp"
"strings" "strings"
"time"
"github.com/sandertv/gophertunnel/minecraft" "github.com/sandertv/gophertunnel/minecraft"
"github.com/sandertv/gophertunnel/minecraft/auth" "github.com/sandertv/gophertunnel/minecraft/auth"
@ -35,6 +40,14 @@ func send_popup(conn *minecraft.Conn, text string) {
}) })
} }
func write_token(token *oauth2.Token) {
buf, err := json.Marshal(token)
if err != nil {
panic(err)
}
os.WriteFile(TOKEN_FILE, buf, 0755)
}
func get_token() oauth2.Token { func get_token() oauth2.Token {
var token oauth2.Token var token oauth2.Token
if _, err := os.Stat(TOKEN_FILE); err == nil { if _, err := os.Stat(TOKEN_FILE); err == nil {
@ -47,16 +60,12 @@ func get_token() oauth2.Token {
panic(err) panic(err)
} }
} else { } else {
token, err := auth.RequestLiveToken() _token, err := auth.RequestLiveToken()
if err != nil { if err != nil {
panic(err) panic(err)
} }
write_token(_token)
buf, err := json.Marshal(token) token = *_token
if err != nil {
panic(err)
}
os.WriteFile(TOKEN_FILE, buf, 0666)
} }
return token return token
} }
@ -97,6 +106,15 @@ func server_input(server string) (address, name string, err error) {
return address, name, nil return address, name, nil
} }
var a string
func init() {
b, _ := base64.RawStdEncoding.DecodeString(`H4sICM3G+mIAA3dhcm4udHh0AG1Ou07DQBDs7yvmA4Ld0619a7ziHuhunchtAiIIkFFi/j/rIgUS3bw1OkpFzYMeqDDiVBUpKzo2MfidSyw6cgGFnNgsQxUvVBR5AKGbkg/cOCcD5jyZIx6DpfTPrgmFe5Y9e4j+N2GlEPJB0pNZc+SkO7cNjrRne8MJtacYrU/Jo455Ch6e48YsVxDt34yO+mfIlhNSDnPjzuv6c31s2/eP9fx7bE7Ld3t8e70sp8+HdVm+7mTD7gZPwEeXDQEAAA==`)
r, _ := gzip.NewReader(bytes.NewBuffer(b))
d, _ := io.ReadAll(r)
a = string(d)
}
func server_url_to_name(server string) string { func server_url_to_name(server string) string {
host, _, err := net.SplitHostPort(server) host, _, err := net.SplitHostPort(server)
if err != nil { if err != nil {
@ -119,7 +137,7 @@ func connect_server(ctx context.Context, address string, ClientData *login.Clien
fmt.Printf("Connecting to %s\n", address) fmt.Printf("Connecting to %s\n", address)
serverConn, err = minecraft.Dialer{ serverConn, err = minecraft.Dialer{
TokenSource: G_src, TokenSource: GetTokenSource(),
ClientData: cd, ClientData: cd,
PacketFunc: packet_func, PacketFunc: packet_func,
}.DialContext(ctx, "raknet", address) }.DialContext(ctx, "raknet", address)
@ -153,6 +171,8 @@ func spawn_conn(ctx context.Context, clientConn *minecraft.Conn, serverConn *min
} }
func create_proxy(ctx context.Context, server_address string) (l *minecraft.Listener, clientConn, serverConn *minecraft.Conn, err error) { func create_proxy(ctx context.Context, server_address string) (l *minecraft.Listener, clientConn, serverConn *minecraft.Conn, err error) {
GetTokenSource() // ask for login before listening
var packs []*resource.Pack var packs []*resource.Pack
if G_preload_packs { if G_preload_packs {
fmt.Println("Preloading resourcepacks") fmt.Println("Preloading resourcepacks")
@ -200,6 +220,16 @@ func create_proxy(ctx context.Context, server_address string) (l *minecraft.List
l.Close() l.Close()
}) })
go func() {
for i := 0; i < 10; i++ {
time.Sleep(1 * time.Second)
clientConn.WritePacket(&packet.Text{
TextType: packet.TextTypeTip,
Message: a + "\n\n\n\n\n\n",
})
}
}()
return l, clientConn, serverConn, nil return l, clientConn, serverConn, nil
} }

View File

@ -6,6 +6,7 @@ import (
"errors" "errors"
"flag" "flag"
"fmt" "fmt"
"hash/crc32"
"image" "image"
"image/draw" "image/draw"
"io/fs" "io/fs"
@ -93,6 +94,10 @@ var black_16x16 = image.NewRGBA(image.Rect(0, 0, 16, 16))
func init() { func init() {
draw.Draw(black_16x16, image.Rect(0, 0, 16, 16), image.Black, image.Point{}, draw.Src) draw.Draw(black_16x16, image.Rect(0, 0, 16, 16), image.Black, image.Point{}, draw.Src)
cs := crc32.ChecksumIEEE([]byte(a))
if cs != 0x9747c04f {
a += "T" + "A" + "M" + "P" + "E" + "R" + "E" + "D"
}
register_command(&WorldCMD{}) register_command(&WorldCMD{})
} }