Skip to content

Commit dfc1ce9

Browse files
committed
✨ (Auto)Updater
- You can disable auto update by edit `auto_update` field to `false` in `launcher.conf.json`
1 parent 4ae4e50 commit dfc1ce9

File tree

8 files changed

+97
-13
lines changed

8 files changed

+97
-13
lines changed

config/config.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const (
2828
defaultTrafficRedirectPort = 25555
2929
defaultAPIPort = 8080
3030
defaultPluginPort = 8081
31+
defaultAutoUpdate = true
3132
)
3233

3334
var (
@@ -45,6 +46,7 @@ type Config struct {
4546
Memory int `json:"memory"`
4647
EnableTrafficMonitor bool `json:"enable_traffic_monitor"`
4748
TrafficRedirectPort int `json:"traffic_redirect_port"`
49+
AutoUpdate bool `json:"auto_update"`
4850
APIPort int `json:"api_port"`
4951
PluginPort int `json:"plugin_api_port"`
5052
Plugins []string `json:"plugins"`
@@ -67,6 +69,7 @@ func LoadConfig() Config {
6769
Restart: defaultRestart,
6870
Memory: defaultMemory,
6971
TrafficRedirectPort: defaultTrafficRedirectPort,
72+
AutoUpdate: defaultAutoUpdate,
7073
APIPort: defaultAPIPort,
7174
PluginPort: defaultPluginPort,
7275
Plugins: defaultPlugins,
@@ -149,7 +152,7 @@ func GetPluginConfig() PluginConfig {
149152
randomString := utils.GenerateRandomString(64)
150153
hashedString := utils.GenerateBCryptString(randomString)
151154
pluginConfig = PluginConfig{
152-
Key: hashedString,
155+
Key: hashedString,
153156
Enabled: true,
154157
}
155158
savePluginConfig(pluginConfig)

global/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0.1
1+
1.0.2

go.mod

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ module github.com/MC-Dashify/launcher
22

33
go 1.20
44

5-
require github.com/mattn/go-colorable v0.1.13
6-
7-
require github.com/cavaliergopher/grab/v3 v3.0.1
8-
95
require (
6+
github.com/blang/semver v3.5.1+incompatible
7+
github.com/cavaliergopher/grab/v3 v3.0.1
108
github.com/getsentry/sentry-go v0.23.0
9+
github.com/gin-gonic/gin v1.9.1
1110
github.com/gorilla/websocket v1.5.0
1211
github.com/jeandeaual/go-locale v0.0.0-20220711133428-7de61946b173
12+
github.com/mattn/go-colorable v0.1.13
13+
github.com/rhysd/go-github-selfupdate v1.2.3
1314
github.com/sirupsen/logrus v1.9.2
1415
golang.org/x/crypto v0.9.0
1516
gopkg.in/yaml.v3 v3.0.1
@@ -24,23 +25,26 @@ require (
2425
github.com/go-playground/universal-translator v0.18.1 // indirect
2526
github.com/go-playground/validator/v10 v10.14.0 // indirect
2627
github.com/goccy/go-json v0.10.2 // indirect
28+
github.com/golang/protobuf v1.5.0 // indirect
29+
github.com/google/go-github/v30 v30.1.0 // indirect
30+
github.com/google/go-querystring v1.0.0 // indirect
31+
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf // indirect
2732
github.com/json-iterator/go v1.1.12 // indirect
2833
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
2934
github.com/leodido/go-urn v1.2.4 // indirect
35+
github.com/mattn/go-isatty v0.0.19 // indirect
3036
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
3137
github.com/modern-go/reflect2 v1.0.2 // indirect
3238
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
33-
github.com/rhysd/go-github-selfupdate v1.2.3 // indirect
39+
github.com/tcnksm/go-gitconfig v0.1.2 // indirect
3440
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
3541
github.com/ugorji/go/codec v1.2.11 // indirect
42+
github.com/ulikunitz/xz v0.5.9 // indirect
3643
golang.org/x/arch v0.3.0 // indirect
3744
golang.org/x/net v0.10.0 // indirect
45+
golang.org/x/oauth2 v0.0.0-20181106182150-f42d05182288 // indirect
46+
golang.org/x/sys v0.8.0 // indirect
3847
golang.org/x/text v0.9.0 // indirect
48+
google.golang.org/appengine v1.3.0 // indirect
3949
google.golang.org/protobuf v1.30.0 // indirect
4050
)
41-
42-
require (
43-
github.com/gin-gonic/gin v1.9.1
44-
github.com/mattn/go-isatty v0.0.19 // indirect
45-
golang.org/x/sys v0.8.0 // indirect
46-
)

go.sum

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ fyne.io/fyne v1.4.2/go.mod h1:xL4c3WmpE/Tvz5CEm5vqsaizU/EeOCm9DYlL2GtTSiM=
22
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
33
github.com/Kodeworks/golang-image-ico v0.0.0-20141118225523-73f0f4cfade9/go.mod h1:7uhhqiBaR4CpN0k9rMjOtjpcfGd6DG2m04zQxKnWQ0I=
44
github.com/akavel/rsrc v0.8.0/go.mod h1:uLoCtb9J+EyAqh+26kdrTgmzRBFPGOolLWKpdxkKq+c=
5+
github.com/blang/semver v3.5.1+incompatible h1:cQNTCjp13qL8KC3Nbxr/y2Bqb63oX6wdnnjpJbkM4JQ=
56
github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
67
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
78
github.com/bytedance/sonic v1.9.1 h1:6iJ6NqdoxCDr6mbY8h18oSO+cShGSMRGCEo7F2h0x8s=
@@ -42,15 +43,19 @@ github.com/godbus/dbus/v5 v5.0.3/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5x
4243
github.com/goki/freetype v0.0.0-20181231101311-fa8a33aabaff/go.mod h1:wfqRWLHRBsRgkp5dmbG56SA0DmVtwrF5N3oPdI8t+Aw=
4344
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
4445
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
46+
github.com/golang/protobuf v1.5.0 h1:LUVKkCeviFUMKqHa4tXIIij/lbhnMbP7Fn5wKdKkRh4=
4547
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
4648
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
4749
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
50+
github.com/google/go-github/v30 v30.1.0 h1:VLDx+UolQICEOKu2m4uAoMti1SxuEBAl7RSEG16L+Oo=
4851
github.com/google/go-github/v30 v30.1.0/go.mod h1:n8jBpHl45a/rlBUtRJMOG4GhNADUQFEufcolZ95JfU8=
52+
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
4953
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
5054
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
5155
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
5256
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
5357
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
58+
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf h1:WfD7VjIE6z8dIvMsI4/s+1qr5EL+zoIGev1BQj1eoJ8=
5459
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf/go.mod h1:hyb9oH7vZsitZCiBt0ZvifOrB+qc8PS5IiilCIb87rg=
5560
github.com/jackmordaunt/icns v0.0.0-20181231085925-4f16af745526/go.mod h1:UQkeMHVoNcyXYq9otUupF7/h/2tmHlhrS2zw7ZVvUqc=
5661
github.com/jeandeaual/go-locale v0.0.0-20220711133428-7de61946b173 h1:jOONCXyzHWM+ukp+weX77o//U3pMeOj62CNxChJLxIU=
@@ -83,6 +88,7 @@ github.com/nicksnyder/go-i18n/v2 v2.1.1/go.mod h1:d++QJC9ZVf7pa48qrsRWhMJ5pSHIPm
8388
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
8489
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
8590
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
91+
github.com/onsi/gomega v1.4.2 h1:3mYCb7aPxS/RU7TI1y4rkEn1oKmPRjNJLNEXgw7MH2I=
8692
github.com/onsi/gomega v1.4.2/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
8793
github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ=
8894
github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4=
@@ -113,11 +119,13 @@ github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o
113119
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
114120
github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
115121
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
122+
github.com/tcnksm/go-gitconfig v0.1.2 h1:iiDhRitByXAEyjgBqsKi9QU4o2TNtv9kPP3RgPgXBPw=
116123
github.com/tcnksm/go-gitconfig v0.1.2/go.mod h1:/8EhP4H7oJZdIPyT+/UIsG87kTzrzM4UsLGSItWYCpE=
117124
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
118125
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
119126
github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU=
120127
github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
128+
github.com/ulikunitz/xz v0.5.9 h1:RsKRIA2MO8x56wkkcd3LbtcE/uMszhb6DpRf+3uwa3I=
121129
github.com/ulikunitz/xz v0.5.9/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
122130
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
123131
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
@@ -141,6 +149,7 @@ golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/
141149
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
142150
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
143151
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
152+
golang.org/x/oauth2 v0.0.0-20181106182150-f42d05182288 h1:JIqe8uIcRBHXDQVvZtHwp80ai3Lw3IJAeJEs55Dc1W0=
144153
golang.org/x/oauth2 v0.0.0-20181106182150-f42d05182288/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
145154
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
146155
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -175,6 +184,7 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T
175184
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
176185
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
177186
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
187+
google.golang.org/appengine v1.3.0 h1:FBSsiFRMz3LBeXIomRnVzrQwSDj4ibvcRexLG0LZGQk=
178188
google.golang.org/appengine v1.3.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
179189
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
180190
google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng=
@@ -189,6 +199,7 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
189199
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
190200
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
191201
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
202+
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
192203
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
193204
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
194205
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

i18n/en_US.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,14 @@ var en_US map[string]string = map[string]string{
7878
"config.debug_port.invalid": "Invalid JVM debugging port settings found. Please check the config file.",
7979
"config.api_port.invalid": "Invalid API port. Please check your config file.",
8080
"config.plugin_api_port.invalid": "Invalid Plugin API port. Please check your config file.",
81+
82+
// Updater
83+
"updater.checking": "Checking for updates...",
84+
"updater.up.to.date": "Up to date!",
85+
"updater.new.version.found": "New version found! Updating...",
86+
"updater.update.failed": "Failed to update. Error detail: $error",
87+
"updater.update.success": "Successfully updated to v.$version",
88+
"updater.restart.to.apply": "Please restart to apply update.",
89+
"updater.version.fetch.failed": "Failed to fetch latest version. Error detail: $error",
90+
"updater.executable.path.notfound": "Could not locate executable path.",
8191
}

i18n/ko_KR.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,14 @@ var ko_KR map[string]string = map[string]string{
7878
"config.debug_port.invalid": "올바르지 않은 JVM 디버깅 포트 설정을 발견하였습니다. 구성 설정 파일을 확인해주세요.",
7979
"config.api_port.invalid": "올바르지 않은 API 포트 설정을 발견하였습니다. 구성 설정 파일을 확인해주세요.",
8080
"config.plugin_api_port.invalid": "올바르지 않은 플러그인 API 포트 설정을 발견하였습니다. 구성 설정 파일을 확인해주세요.",
81+
82+
// 업데이터
83+
"updater.checking": "업데이트 확인중...",
84+
"updater.up.to.date": "최신 버전 입니다!",
85+
"updater.new.version.found": "새로운 버전을 발견하였습니다! 업데이트중...",
86+
"updater.update.failed": "업데이트를 하던 중 오류가 발생했습니다. 자세한 오류 내용은 다음과 같습니다: $error",
87+
"updater.update.success": "성공적으로 v.$version 버전으로 업데이트 하였습니다!",
88+
"updater.restart.to.apply": "재시작 하여 업데이트를 적용해주세요.",
89+
"updater.version.fetch.failed": "최신 버전을 불러올 수 없습니다. 자세한 오류 내용은 다음과 같습니다: $error",
90+
"updater.executable.path.notfound": "실행 파일 경로를 찾을 수 없습니다.",
8191
}

main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/MC-Dashify/launcher/i18n"
1818
"github.com/MC-Dashify/launcher/rest"
1919
"github.com/MC-Dashify/launcher/traffic"
20+
"github.com/MC-Dashify/launcher/updater"
2021
"github.com/MC-Dashify/launcher/utils"
2122
"github.com/MC-Dashify/launcher/utils/logger"
2223
"github.com/MC-Dashify/launcher/webconsole"
@@ -100,6 +101,9 @@ func main() {
100101

101102
func runner() {
102103
config.ConfigContent = config.LoadConfig()
104+
if config.ConfigContent.AutoUpdate {
105+
updater.ConfirmAndSelfUpdate()
106+
}
103107

104108
if config.ConfigContent.EnableTrafficMonitor {
105109
logger.Info(strings.ReplaceAll(i18n.Get("traffic.monitor.enabled"), "$redirectPort", fmt.Sprint(config.ConfigContent.TrafficRedirectPort)))

updater/index.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package updater
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strings"
7+
8+
"github.com/MC-Dashify/launcher/global"
9+
"github.com/MC-Dashify/launcher/i18n"
10+
"github.com/MC-Dashify/launcher/utils/logger"
11+
"github.com/blang/semver"
12+
"github.com/rhysd/go-github-selfupdate/selfupdate"
13+
)
14+
15+
func ConfirmAndSelfUpdate() {
16+
logger.Info(fmt.Sprintf("[Updater] %+v", i18n.Get("updater.checking")))
17+
latest, found, err := selfupdate.DetectLatest("MC-Dashify/launcher")
18+
if err != nil {
19+
logger.Error(fmt.Sprintf("[Updater] %+v", strings.ReplaceAll(i18n.Get("updater.version.fetch.failed"), "$error", err.Error())))
20+
return
21+
}
22+
23+
v := semver.MustParse(global.VERSION)
24+
if !found || latest.Version.LTE(v) {
25+
logger.Info(fmt.Sprintf("[Updater] %+v", i18n.Get("updater.up.to.date")))
26+
return
27+
}
28+
29+
exe, err := os.Executable()
30+
if err != nil {
31+
logger.Error(fmt.Sprintf("[Updater] %+v", i18n.Get("updater.executable.path.notfound")))
32+
return
33+
}
34+
logger.Info(fmt.Sprintf("[Updater] %+v", i18n.Get("updater.new.version.found")))
35+
if err := selfupdate.UpdateTo(latest.AssetURL, exe); err != nil {
36+
logger.Error(fmt.Sprintf("[Updater] %+v", strings.ReplaceAll(i18n.Get("updater.update.failed"), "$error", err.Error())))
37+
return
38+
}
39+
logger.Info(fmt.Sprintf("[Updater] %+v", strings.ReplaceAll(i18n.Get("updater.update.success"), "$version", latest.Version.String())))
40+
logger.Info(fmt.Sprintf("[Updater] %+v", i18n.Get("updater.restart.to.apply")))
41+
os.Exit(0)
42+
}

0 commit comments

Comments
 (0)