-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
147 lines (138 loc) · 4.44 KB
/
main.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
package main
import (
"bytes"
"encoding/json"
"io"
"net/http"
"net/url"
"os"
"path"
"strings"
"text/template"
)
const (
CACHE_DIR = "./.cache/bdsdown"
VERSION_QUERY_URL = "https://www.minecraft.net/en-us/download/server/bedrock"
DEFAULT_MIRROR_HOST = "mcdl.bibk.top"
)
var VERSION_TEMPLATE = map[string]*template.Template{
"windows": template.Must(template.New("windows").Parse("https://www.minecraft.net/bedrockdedicatedserver/bin-win/bedrock-server-{{.}}.zip")),
"linux": template.Must(template.New("linux").Parse("https://www.minecraft.net/bedrockdedicatedserver/bin-linux/bedrock-server-{{.}}.zip")),
"windows-preview": template.Must(template.New("windows-preview").Parse("https://www.minecraft.net/bedrockdedicatedserver/bin-win-preview/bedrock-server-{{.}}.zip")),
"linux-preview": template.Must(template.New("linux-preview").Parse("https://www.minecraft.net/bedrockdedicatedserver/bin-linux-preview/bedrock-server-{{.}}.zip")),
}
func main() {
ParseCommandLine()
log.Info("This software only provides a convenient way to download and install Minecraft Bedrock Edition server")
log.Info("You must agree with the Minecraft End User License Agreement and Privacy Policy to continue")
log.Info("- Minecraft End User License Agreement https://minecraft.net/terms")
log.Info("- Microsoft Privacy Policy https://go.microsoft.com/fwlink/?LinkId=521839")
if !UserConfirm("Do you agree with the above terms? (y/n): ") {
log.Info("You must agree with the above terms to use this software.")
return
}
processHttpPackage := func(u *url.URL) {
_, f := path.Split(u.Path)
f = path.Join(CACHE_DIR, f)
mirrorEnv := os.Getenv("BDSDOWN_MIRROR_URL")
if mirrorEnv != "" || needUseMirror() {
if mirrorEnv == "" {
u.Host = DEFAULT_MIRROR_HOST
} else {
mirrorUrl, err := url.Parse(mirrorEnv)
if err != nil {
log.Error("failed to parse BDSDOWN_MIRROR_URL, use default mirror instead")
u.Host = DEFAULT_MIRROR_HOST
} else {
u.Scheme = mirrorUrl.Scheme
u.Host = mirrorUrl.Host
}
}
log.Info("trying download from mirror")
}
err := DownloadFile(u.String(), f)
if err != nil {
if u.Host != "www.minecraft.net" {
log.Errorf("failed to download from mirror: %v, trying fallback", err)
u.Scheme = "https"
u.Host = "www.minecraft.net"
err := DownloadFile(u.String(), f)
if err != nil {
log.Fatal(err)
}
} else {
log.Fatal(err)
}
}
err = UnzipPackage(f, ".")
if err != nil {
log.Fatal(err)
}
}
if config.TargetPackage.Scheme == "file" {
err := UnzipPackage(path.Join(config.TargetPackage.Host, config.TargetPackage.Path), ".")
if err != nil {
log.Fatal(err)
}
} else if config.TargetPackage.Scheme == "http" || config.TargetPackage.Scheme == "https" {
processHttpPackage(&config.TargetPackage)
} else if config.TargetPackage.Scheme == "version" {
platform := config.TargetPackage.Host
version := strings.TrimLeft(config.TargetPackage.Path, "/")
if version == "" || version == "latest" {
log.Infof("fetching latest version for platform %s", platform)
versions, err := FetchVersions(VERSION_QUERY_URL)
if err != nil {
log.Fatal(err)
}
u, ok := versions[platformMapping[platform]]
if !ok {
log.Fatalf("failed to find version for platform %s", platform)
}
processHttpPackage(u)
} else {
buf := bytes.NewBuffer([]byte{})
VERSION_TEMPLATE[platform].Execute(buf, version)
u, _ := url.Parse(buf.String())
processHttpPackage(u)
}
} else {
log.Fatalf("unsupported scheme for target package %s", config.TargetPackage.Scheme)
}
}
func needUseMirror() bool {
resp, err := http.Get("http://ip-api.com/json/")
if err != nil {
log.Warningf("failed to get IP location: %v, use mirror by default.", err)
return true
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Warningf("failed to get IP location: %v, use mirror by default.", err)
return true
}
type IPData struct {
Status string
Country string
CountryCode string
Region string
RegionName string
City string
Zip string
Lat float32
Lon float32
Timezone string
Isp string
Org string
As string
Query string
}
var data IPData
err = json.Unmarshal(body, &data)
if err != nil {
log.Warningf("failed to get IP location: %v, use mirror by default.", err)
return true
}
return data.CountryCode == "CN"
}