/
join.go
172 lines (142 loc) · 4.34 KB
/
join.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
package cmd
import (
"context"
"encoding/json"
"fmt"
"net"
"os"
"path/filepath"
"strings"
"time"
"github.com/Tnze/go-mc/bot"
"github.com/minepkg/minepkg/internals/api"
"github.com/minepkg/minepkg/internals/commands"
"github.com/minepkg/minepkg/internals/globals"
"github.com/minepkg/minepkg/internals/instances"
"github.com/minepkg/minepkg/internals/launcher"
"github.com/minepkg/minepkg/pkg/manifest"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func init() {
runner := &joinRunner{}
cmd := commands.New(&cobra.Command{
Use: "join <ip/hostname>",
Short: "Joins a compatible server without any setup",
Long: `Servers have to be started with \"minepkg launch --server\" or include the minepkg-companion mod`,
Example: ` minepkg join demo.minepkg.host`,
Aliases: []string{"i-wanna-play-on", "connect"},
Args: cobra.ExactArgs(1),
}, runner)
cmd.Flags().IntVar(&runner.ramMiB, "ram", 0, "Overwrite the amount of RAM in MiB to use")
rootCmd.AddCommand(cmd.Command)
}
type joinRunner struct {
ramMiB int
}
func (j *joinRunner) RunE(cmd *cobra.Command, args []string) error {
var resolvedModpack *api.Release
ip := "127.0.0.1"
connectionString := strings.Split(args[0], ":")
host := connectionString[0]
port := "25565"
if len(connectionString) == 2 {
port = connectionString[1]
}
if host != "localhost" {
rawIP, err := net.LookupHost(host)
if err != nil {
logger.Fail("Could not resolve host " + host)
}
ip = strings.Join(rawIP, ".")
}
resolvedModpack = resolveViaSLP(ip, port)
if resolvedModpack == nil {
logger.Info("Could not determine the server modpack")
os.Exit(1)
}
// looks like we can join this server, so we start initializing the instance stuff here
instance := instances.New()
instance.Lockfile = manifest.NewLockfile()
instance.MinepkgAPI = globals.ApiClient
instanceDir := filepath.Join(instance.InstancesDir(), "server."+ip+"."+resolvedModpack.Package.Name+"."+resolvedModpack.Package.Platform)
os.MkdirAll(instanceDir, os.ModePerm)
instance.Directory = instanceDir
wd, err := os.Getwd()
if err != nil {
return err
}
// change dir to the instance
if err := os.Chdir(instanceDir); err != nil {
return err
}
defer os.Chdir(wd) // move back to working directory
creds, err := ensureMojangAuth()
if err != nil {
return err
}
instance.MojangCredentials = creds
instance.Manifest = manifest.NewInstanceLike(resolvedModpack.Manifest)
fmt.Println("Using modpack " + resolvedModpack.Identifier())
cliLauncher := launcher.Launcher{
Instance: instance,
ServerMode: false,
MinepkgVersion: rootCmd.Version,
UseSystemJava: viper.GetBool("useSystemJava"),
}
if err := cliLauncher.Prepare(); err != nil {
return err
}
opts := &instances.LaunchOptions{
JoinServer: ip + ":" + port,
RamMiB: j.ramMiB,
}
err = cliLauncher.Run(opts)
if err != nil {
return err
}
return nil
}
type modpackDescription struct {
Name string `json:"name"`
Version string `json:"version"`
Platform string `json:"platform"`
}
type slpData struct {
MinepkgModpack *modpackDescription `json:"minepkgModpack"`
Version struct {
Name string `json:"name"`
Protocol string `json:"protocol"`
} `json:"version"`
}
func resolveViaSLP(ip string, port string) *api.Release {
serverData, _, err := bot.PingAndListTimeout(fmt.Sprintf("%s:%s", ip, port), 10*time.Second)
if err != nil {
fmt.Println("could not reach server")
return nil
}
data := slpData{}
json.Unmarshal(serverData, &data)
if data.MinepkgModpack == nil {
fmt.Println("Server does not use minepkg-companion 0.2.0+ or has no valid modpack")
return nil
}
if data.Version.Name == "" {
logger.Fail("Server has no Minecraft version set. This usually means that the server is still starting up. Try again in a few seconds.")
}
fmt.Println("minepkg compatible server detected. Modpack: " + data.MinepkgModpack.Name)
reqs := &api.RequirementQuery{
Version: data.MinepkgModpack.Version,
Platform: data.MinepkgModpack.Platform,
// raw version from minecraft slp.. might need to check that
Minecraft: data.Version.Name,
}
release, err := globals.ApiClient.FindRelease(context.TODO(), data.MinepkgModpack.Name, reqs)
if err != nil {
logger.Fail("Could not fetch release: " + err.Error())
}
if release == nil {
logger.Fail("Server modpack is not published on minepkg.io")
}
return release
}