-
Notifications
You must be signed in to change notification settings - Fork 1
/
switchhost.go
182 lines (156 loc) · 4.85 KB
/
switchhost.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
173
174
175
176
177
178
179
180
181
182
package main
import (
"fmt"
"os"
"os/signal"
"path"
"path/filepath"
"github.com/ralim/switchhost/versionsdb"
"github.com/ralim/switchhost/library"
"github.com/ralim/switchhost/server"
"github.com/ralim/switchhost/settings"
"github.com/ralim/switchhost/termui"
"github.com/ralim/switchhost/titledb"
"github.com/ralim/switchhost/utilities"
"github.com/rivo/tview"
"github.com/rs/zerolog/log"
)
type SwitchHost struct {
ConfigFilePath string `flag:"config" help:"Path to config file"`
KeysFilePath string `flag:"keys" help:"Path to your switch's keyfile"`
NoCUI bool `flag:"noCUI" help:"Disable the Console UI"`
lib *library.Library `flag:"-"`
ui *termui.TermUI `flag:"-"`
settings *settings.Settings `flag:"-"`
titleDB *titledb.TitlesDB `flag:"-"`
versionDB *versionsdb.VersionDB `flag:"-"`
}
func NewSwitchHost() *SwitchHost {
return &SwitchHost{}
}
func (m *SwitchHost) Run() error {
uiExit := make(chan bool, 1)
settingsPath := "./config.json"
if m.ConfigFilePath != "" {
settingsPath = m.ConfigFilePath
}
m.settings = settings.NewSettings(settingsPath)
m.ui = termui.NewTermUI(m.NoCUI)
if !m.NoCUI {
m.settings.SetupLogging(tview.ANSIWriter(m.ui.LogsView))
go func() {
m.ui.Run()
m.ui.Stop()
uiExit <- true
}()
} else {
m.settings.SetupLogging(os.Stdout)
//Run hook listener for ctrl-c
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for range c {
// sig is a ^C, handle it
log.Warn().Msg("Control-C received, shutting down")
uiExit <- true
}
}()
}
m.loadVersionInfo()
// Download TitlesDB
m.loadTitlesDB()
m.lib = library.NewLibrary(m.titleDB, m.settings, m.ui, m.versionDB)
m.tryAndLoadKeys()
m.lib.Start()
server := server.NewServer(m.lib, m.titleDB, m.settings)
server.Run()
//Wait for exit
<-uiExit
//Rediect logs back to terminal since UI has exited
m.settings.SetupLogging(os.Stdout)
log.Warn().Msg("Ctrl-c pressed, closing up")
fmt.Println("Waiting for tasks to stop")
server.Stop() // stop the servers
m.lib.Stop() // wait for library to close down
return nil
}
func (m *SwitchHost) checkKeys(pathUsed string) {
//So this is a bit of a hack around because nsz.py is inflexible in the prod.keys file location
//So we check, if we have been told to load from a not ~/.switch/ location, and the dest file does not exist, we copy it there for nsz.py
if userHomeDir, err := os.UserHomeDir(); err == nil {
homeFileFolderPath := path.Join(userHomeDir, ".switch")
err := os.MkdirAll(homeFileFolderPath, os.ModePerm)
if err == nil {
homeFilePath := path.Join(homeFileFolderPath, "prod.keys")
if homeFilePath != pathUsed && !utilities.Exists(homeFilePath) {
log.Info().Str("pathUsed", pathUsed).Msg("Path used for keys does not equal the one nsz wants, so going to try and copy it there")
err := utilities.CopyFile(pathUsed, homeFilePath)
if err != nil {
log.Warn().Err(err).Msg("Failed to copy the prod.keys file, nsz.py may not work")
}
}
} else {
log.Warn().Err(err).Msg("Failed to copy the prod.keys file, as could not make folder; nsz.py may not work")
}
}
}
func (m *SwitchHost) loadTitlesDB() {
if m.ui != nil {
titlesDBInfo := m.ui.RegisterTask("TitlesDB")
titlesDBInfo.UpdateStatus("Downloading")
defer titlesDBInfo.UpdateStatus("Done")
}
m.titleDB = titledb.CreateTitlesDB(m.settings)
m.titleDB.UpdateTitlesDB()
}
func (m *SwitchHost) loadVersionInfo() {
if m.ui != nil {
versionInfo := m.ui.RegisterTask("Version Info")
versionInfo.UpdateStatus("Downloading")
defer versionInfo.UpdateStatus("Done")
}
versionInfo := versionsdb.NewVersionDBFromURL(m.settings.VersionsDBURL, m.settings.CacheFolder)
m.versionDB = versionInfo
}
func (m *SwitchHost) tryAndLoadKeys() {
//First try cli arg path if we can
if ok := m.loadKeys(m.KeysFilePath, m.lib); ok {
return // Done loading
}
paths := []string{"."}
//Append user home folder if it exists
if userHomeDir, err := os.UserHomeDir(); err == nil {
paths = append(paths, path.Join(userHomeDir, ".switch"))
}
//Append executable folder if it exists
ex, err := os.Executable()
if err != nil {
panic(err)
}
exPath := filepath.Dir(ex)
paths = append(paths, exPath)
for _, folder := range paths {
filePath := path.Join(folder, "prod.keys")
if ok := m.loadKeys(filePath, m.lib); ok {
return // Done loading
}
}
log.Warn().Msg("No keys could be loaded, functionality will be limited")
}
func (m *SwitchHost) loadKeys(filePath string, lib *library.Library) bool {
if _, err := os.Stat(filePath); err == nil {
log.Info().Str("path", filePath).Msg("Loading keys...")
file, err := os.Open(filePath)
if err != nil {
return false
}
defer file.Close()
if err := lib.LoadKeys(file); err != nil {
log.Info().Err(err).Msg("Could not load keys")
return false
}
m.checkKeys(filePath)
return true
}
return false
}