support users having multiple realms

This commit is contained in:
olebeck 2022-08-15 02:29:01 +02:00
parent 1c68978e50
commit d026bf01f2
7 changed files with 53 additions and 25 deletions

View File

@ -85,7 +85,7 @@ func (p *CaptureCMD) SetFlags(f *flag.FlagSet) {
f.StringVar(&p.server_address, "address", "", "remote server address")
}
func (c *CaptureCMD) Usage() string {
return c.Name() + ": " + c.Synopsis() + "\n"
return c.Name() + ": " + c.Synopsis() + "\n" + SERVER_ADDRESS_HELP
}
func (c *CaptureCMD) Execute(ctx context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {

View File

@ -108,6 +108,7 @@ func main() {
subcommands.Register(subcommands.HelpCommand(), "")
subcommands.ImportantFlag("debug")
subcommands.ImportantFlag("dns")
subcommands.HelpCommand()
{ // interactive input
if len(os.Args) < 2 {

View File

@ -7,6 +7,7 @@ import (
"fmt"
"io"
"net/http"
"os"
"strings"
"github.com/google/subcommands"
@ -35,7 +36,7 @@ func realms_get(path string) ([]byte, error) {
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("%d", resp.StatusCode)
return nil, fmt.Errorf("HTTP %d", resp.StatusCode)
}
data, err := io.ReadAll(resp.Body)
if err != nil {
@ -82,7 +83,7 @@ func get_realms() ([]Realm, error) {
return realms.Servers, nil
}
func get_realm(realm_name string) (string, string, error) {
func get_realm(realm_name, id string) (string, string, error) {
// returns: name, address, err
realms, err := get_realms()
if err != nil {
@ -90,6 +91,9 @@ func get_realm(realm_name string) (string, string, error) {
}
for _, realm := range realms {
if strings.HasPrefix(realm.Name, realm_name) {
if id != "" && id != fmt.Sprint(id) {
continue
}
address, err := realm.Address()
if err != nil {
return "", "", err
@ -100,17 +104,6 @@ func get_realm(realm_name string) (string, string, error) {
return "", "", fmt.Errorf("realm not found")
}
func list_realms() error {
realms, err := get_realms()
if err != nil {
return err
}
for _, realm := range realms {
fmt.Printf("%s\t\t(%d)\n", realm.Name, realm.Id)
}
return nil
}
type TokenCMD struct{}
func (*TokenCMD) Name() string { return "realms-token" }
@ -126,6 +119,29 @@ func (c *TokenCMD) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{})
return 0
}
type RealmListCMD struct{}
func (*RealmListCMD) Name() string { return "list-realms" }
func (*RealmListCMD) Synopsis() string { return "prints all realms you have access to" }
func (c *RealmListCMD) SetFlags(f *flag.FlagSet) {}
func (c *RealmListCMD) Usage() string {
return c.Name() + ": " + c.Synopsis() + "\n"
}
func (c *RealmListCMD) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
realms, err := get_realms()
if err != nil {
fmt.Fprintln(os.Stderr, err)
return 1
}
for _, realm := range realms {
fmt.Printf("Name: %s\tid: %d\n", realm.Name, realm.Id)
}
return 0
}
func init() {
register_command(&TokenCMD{})
register_command(&RealmListCMD{})
}

Binary file not shown.

View File

@ -24,7 +24,7 @@ func (c *SkinProxyCMD) SetFlags(f *flag.FlagSet) {
f.StringVar(&c.filter, "filter", "", "player name filter prefix")
}
func (c *SkinProxyCMD) Usage() string {
return c.Name() + ": " + c.Synopsis() + "\n"
return c.Name() + ": " + c.Synopsis() + "\n" + SERVER_ADDRESS_HELP
}
func (c *SkinProxyCMD) Execute(ctx context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {

View File

@ -19,6 +19,14 @@ import (
"golang.org/x/oauth2"
)
const SERVER_ADDRESS_HELP = `accepted server address formats:
123.234.123.234
123.234.123.234:19132
realm:Username
realm:Username:Id
`
func send_popup(conn *minecraft.Conn, text string) {
conn.WritePacket(&packet.Text{
TextType: packet.TextTypePopup,
@ -62,10 +70,19 @@ func server_input(server string) (address, name string, err error) {
}
if strings.HasPrefix(server, "realm:") { // for realms use api to get ip address
name, address, err = get_realm(strings.Split(server, ":")[1])
realm_info := strings.Split(server, ":")
id := ""
if len(realm_info) == 3 {
id = realm_info[2]
}
name, address, err = get_realm(realm_info[1], id)
if err != nil {
return "", "", err
}
} else if strings.HasSuffix(server, ".pcap") {
s := strings.Split(server, ".")
name = strings.Join(s[:len(s)-1], ".")
address = server
} else {
// if an actual server address if given
// add port if necessary
@ -135,12 +152,6 @@ 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) {
/*
if strings.HasSuffix(server_address, ".pcap") {
return create_replay_connection(server_address)
}
*/
_status := minecraft.NewStatusProvider("Server")
listener, err := minecraft.ListenConfig{
StatusProvider: _status,
@ -170,9 +181,9 @@ func create_proxy(ctx context.Context, server_address string) (l *minecraft.List
G_exit = append(G_exit, func() {
serverConn.Close()
listener.Disconnect(clientConn, "Closing")
l.Disconnect(clientConn, "Closing")
clientConn.Close()
listener.Close()
l.Close()
})
return l, clientConn, serverConn, nil

View File

@ -102,7 +102,7 @@ func (p *WorldCMD) SetFlags(f *flag.FlagSet) {
f.StringVar(&p.server_address, "address", "", "remote server address")
}
func (c *WorldCMD) Usage() string {
return c.Name() + ": " + c.Synopsis() + "\n"
return c.Name() + ": " + c.Synopsis() + "\n" + SERVER_ADDRESS_HELP
}
func (c *WorldCMD) Execute(ctx context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {