This commit is contained in:
AkmalFairuz
2025-12-11 08:08:58 +07:00
parent 1d40d3c7e9
commit 151816f18d
8 changed files with 79 additions and 12 deletions

View File

@@ -7,21 +7,21 @@ import (
"sync"
"time"
"encoding/json"
"fmt"
"github.com/akmalfairuz/legacy-version/legacyver"
"github.com/pelletier/go-toml"
"github.com/sandertv/gophertunnel/minecraft"
"github.com/sandertv/gophertunnel/minecraft/auth"
"golang.org/x/oauth2"
"os/signal"
"syscall"
)
// The following program implements a proxy that forwards players from one local address to a remote address.
func main() {
config := readConfig()
token, err := auth.RequestLiveToken()
if err != nil {
panic(err)
}
src := auth.RefreshTokenSource(token)
src := tokenSource()
p, err := minecraft.NewForeignStatusProvider(config.Connection.RemoteAddress)
if err != nil {
@@ -98,6 +98,12 @@ func handleConn(conn *minecraft.Conn, listener *minecraft.Listener, config confi
}
return
}
fmt.Println(pk.ID())
time.Sleep(time.Millisecond * 1000)
if pk.ID() == 498 {
fmt.Println("skipping packet 498")
continue
}
if err := conn.WritePacket(pk); err != nil {
return
}
@@ -144,3 +150,41 @@ func readConfig() config {
}
return c
}
// tokenSource returns a token source for using with a gophertunnel client. It either reads it from the
// token.tok file if cached or requests logging in with a device code.
func tokenSource() oauth2.TokenSource {
check := func(err error) {
if err != nil {
panic(err)
}
}
token := new(oauth2.Token)
tokenData, err := os.ReadFile("token.tok")
if err == nil {
_ = json.Unmarshal(tokenData, token)
} else {
token, err = auth.RequestLiveToken()
check(err)
}
src := auth.RefreshTokenSource(token)
_, err = src.Token()
if err != nil {
// The cached refresh token expired and can no longer be used to obtain a new token. We require the
// user to log in again and use that token instead.
token, err = auth.RequestLiveToken()
check(err)
src = auth.RefreshTokenSource(token)
}
go func() {
c := make(chan os.Signal, 3)
signal.Notify(c, syscall.SIGTERM, syscall.SIGINT)
<-c
tok, _ := src.Token()
b, _ := json.Marshal(tok)
_ = os.WriteFile("token.tok", b, 0644)
os.Exit(0)
}()
return src
}