-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
234 lines (208 loc) · 5.02 KB
/
app.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package main
import (
"context"
"fmt"
"io"
"net"
"os"
"time"
"github.com/jmoiron/sqlx"
"github.com/pressly/goose/v3"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"github.com/wailsapp/wails/v2/pkg/runtime"
"golang.org/x/crypto/ssh"
"gopkg.in/natefinch/lumberjack.v2"
)
type App struct {
ctx context.Context
messageService *messageService
db *sqlx.DB
configService *configService
sshClient *ssh.Client
listener net.Listener
}
func NewApp() *App {
return &App{}
}
type atmSwitch interface {
pack(message Message) ([]byte, error)
unpack(r io.Reader) (AtmResponse, error)
build(message *Message, reversal bool)
packEchoTest() ([]byte, error)
}
func (a *App) shutdown(ctx context.Context) {
if a.db != nil {
a.db.Close()
}
if a.listener != nil {
a.listener.Close()
}
if a.sshClient != nil {
a.sshClient.Close()
}
}
func (a *App) startup(ctx context.Context) {
dirname, err := os.UserHomeDir()
if err != nil {
log.Fatal().Err(err).Msg("")
}
logFile := &lumberjack.Logger{
Filename: dirname + "/logfile.log",
MaxSize: 10,
MaxBackups: 3,
MaxAge: 7,
Compress: true,
}
multiWriter := zerolog.MultiLevelWriter(logFile, zerolog.ConsoleWriter{Out: os.Stdout})
logger := zerolog.New(multiWriter).With().Caller().Timestamp().Logger()
log.Logger = logger
a.ctx = ctx
db, err := sqlx.Connect("sqlite", dirname+"/atm.db")
if err != nil {
log.Fatal().Err(err).Msg("")
}
a.db = db
goose.SetBaseFS(embedMigrations)
if err := goose.SetDialect("sqlite"); err != nil {
log.Fatal().Err(err).Msg("")
}
if err := goose.Up(db.DB, "migrations"); err != nil {
log.Fatal().Err(err).Msg("")
}
configService := &configService{db: db}
a.configService = configService
configs, err := configService.loadConfigs()
if err != nil {
log.Fatal().Err(err).Msg("")
}
for _, v := range configs {
viper.SetDefault(v.Key, v.Value)
}
messageService := &messageService{db: db}
a.messageService = messageService
}
func sendMessage(a *App, message Message, reversal bool) (AtmResponse, error) {
atmSwitch, err := getAtmSwitch(message)
if err != nil {
log.Error().Err(err).Msg("")
return AtmResponse{}, err
}
atmSwitch.build(&message, reversal)
err = a.messageService.saveMessage(message)
if err != nil {
log.Error().Err(err).Msg("")
return AtmResponse{}, err
}
b, err := atmSwitch.pack(message)
if err != nil {
log.Error().Err(err).Msg("")
return AtmResponse{}, err
}
serverAddr := fmt.Sprintf("%s:%s", viper.GetString("HOST"), viper.GetString("PORT"))
conn, err := net.DialTimeout("tcp", serverAddr, 30*time.Second)
if err != nil {
return AtmResponse{}, err
}
defer conn.Close()
err = a.messageService.sendTcpMessage(conn, b)
if err != nil {
return AtmResponse{}, err
}
return atmSwitch.unpack(conn)
}
func (a *App) SendFinancialMessage(message Message) (AtmResponse, error) {
return sendMessage(a, message, false)
}
func (a *App) SendReversalMessage(id int) (AtmResponse, error) {
message, err := a.messageService.getMessage(id)
if err != nil {
log.Error().Err(err).Msg("")
return AtmResponse{}, err
}
return sendMessage(a, message, true)
}
func getAtmSwitch(message Message) (atmSwitch, error) {
var atmSwitch atmSwitch
switch message.Switch {
case CORTEX:
atmSwitch = cortex
case POSTBRIDGE:
atmSwitch = postbridge
case NARADA:
atmSwitch = narada
default:
return nil, fmt.Errorf("atm switch not supported")
}
return atmSwitch, nil
}
func (a *App) GetMessages(page int) ([]Message, error) {
messages, err := a.messageService.getMessages(page)
if err != nil {
log.Error().Err(err).Msg("")
return nil, err
}
return messages, nil
}
func (a *App) GetConfigs() ([]Config, error) {
configs, err := a.configService.getConfigs()
if err != nil {
log.Error().Err(err).Msg("")
return nil, err
}
return configs, nil
}
func (a *App) UpdateConfigs(configs []Config) error {
err := a.configService.updateConfigs(configs)
if err != nil {
log.Error().Err(err).Msg("")
return err
}
return nil
}
func (a *App) UseTunnel() error {
sshClient, listener, err := tunnel(a.ctx)
if err != nil {
log.Error().Err(err).Msg("")
return err
}
a.sshClient = sshClient
a.listener = listener
log.Print("tunnel successful")
return nil
}
func (a *App) CloseTunnel() error {
if a.listener != nil {
err := a.listener.Close()
if err != nil {
log.Error().Err(err).Msg("")
return err
}
}
if a.sshClient != nil {
err := a.sshClient.Close()
if err != nil {
log.Error().Err(err).Msg("")
return err
}
}
return nil
}
func (a *App) PingTunnel() {
if a.sshClient != nil {
_, _, err := a.sshClient.SendRequest("keepalive@openssh.com", true, nil)
if err != nil {
log.Error().Err(err).Msg("")
runtime.EventsEmit(a.ctx, "tunnel", false, false)
return
}
runtime.EventsEmit(a.ctx, "tunnel", true, false)
return
}
runtime.EventsEmit(a.ctx, "tunnel", false, false)
}
func (a *App) OpenFileDialog() (string, error) {
path, _ := runtime.OpenFileDialog(a.ctx, runtime.OpenDialogOptions{})
return path, nil
}