-
-
Notifications
You must be signed in to change notification settings - Fork 148
/
trezord.go
253 lines (220 loc) · 5.07 KB
/
trezord.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"runtime"
"runtime/debug"
"strconv"
"strings"
"github.com/trezor/trezord-go/core"
"github.com/trezor/trezord-go/memorywriter"
"github.com/trezor/trezord-go/server"
"github.com/trezor/trezord-go/usb"
"gopkg.in/natefinch/lumberjack.v2"
)
var version = "2.0.34"
var githash = "unknown"
type udpTouples []usb.PortTouple
func (i *udpTouples) String() string {
res := ""
for i, p := range *i {
if i > 0 {
res += ","
}
res += strconv.Itoa(p.Normal) + ":" + strconv.Itoa(p.Debug)
}
return res
}
func (i *udpTouples) Set(value string) error {
split := strings.Split(value, ":")
n, err := strconv.Atoi(split[0])
if err != nil {
return err
}
d, err := strconv.Atoi(split[1])
if err != nil {
return err
}
*i = append(*i, usb.PortTouple{
Normal: n,
Debug: d,
})
return nil
}
type udpPorts []int
func (i *udpPorts) String() string {
res := ""
for i, p := range *i {
if i > 0 {
res += ","
}
res += strconv.Itoa(p)
}
return res
}
func (i *udpPorts) Set(value string) error {
p, err := strconv.Atoi(value)
if err != nil {
return err
}
*i = append(*i, p)
return nil
}
func initUsb(init bool, wr *memorywriter.MemoryWriter, sl *log.Logger) []core.USBBus {
if init {
wr.Log("Initing libusb")
w, err := usb.InitLibUSB(wr, !usb.HIDUse, allowCancel(), detachKernelDriver())
if err != nil {
sl.Fatalf("libusb: %s", err)
}
if !usb.HIDUse {
return []core.USBBus{w}
}
wr.Log("Initing hidapi")
h, err := usb.InitHIDAPI(wr)
if err != nil {
sl.Fatalf("hidapi: %s", err)
}
return []core.USBBus{w, h}
}
return nil
}
func main() {
// set git hash
info, ok := debug.ReadBuildInfo()
if !ok {
log.Fatalf("cannot read build info")
}
for _, v := range info.Settings {
if v.Key == "vcs.revision" {
githash = v.Value[0:7]
}
}
var logfile string
var port int
var ports udpPorts
var touples udpTouples
var withusb bool
var verbose bool
var reset bool
var versionFlag bool
flag.StringVar(
&logfile,
"l",
"",
"Log into a file, rotating after 20MB",
)
flag.IntVar(
&port,
"p",
21325,
"Use a different port for the HTTP server. Default is 21325.",
)
flag.Var(
&ports,
"e",
"Use UDP port for emulator. Can be repeated for more ports. Example: trezord-go -e 21324 -e 21326",
)
flag.Var(
&touples,
"ed",
"Use UDP port for emulator with debug link. Can be repeated for more ports. Example: trezord-go -ed 21324:21326",
)
flag.BoolVar(
&withusb,
"u",
true,
"Use USB devices. Can be disabled for testing environments. Example: trezord-go -e 21324 -u=false",
)
flag.BoolVar(
&verbose,
"v",
false,
"Write verbose logs to either stderr or logfile",
)
flag.BoolVar(
&versionFlag,
"version",
false,
"Write version",
)
flag.BoolVar(
&reset,
"r",
true,
"Reset USB device on session acquiring. Enabled by default (to prevent wrong device states); set to false if you plan to connect to debug link outside of bridge.",
)
flag.Parse()
if versionFlag {
fmt.Printf("trezord version %s (rev %s)", version, githash)
return
}
var stderrWriter io.Writer
if logfile != "" {
stderrWriter = &lumberjack.Logger{
Filename: logfile,
MaxSize: 20, // megabytes
MaxBackups: 3,
}
} else {
stderrWriter = os.Stderr
}
stderrLogger := log.New(stderrWriter, "", log.LstdFlags)
shortMemoryWriter := memorywriter.New(2000, 200, false, nil)
verboseWriter := stderrWriter
if !verbose {
verboseWriter = nil
}
longMemoryWriter := memorywriter.New(90000, 200, true, verboseWriter)
printWelcomeInfo(stderrLogger, port)
bus := initUsb(withusb, longMemoryWriter, stderrLogger)
longMemoryWriter.Log(fmt.Sprintf("UDP port count - %d", len(ports)))
if len(ports)+len(touples) > 0 {
for _, t := range ports {
touples = append(touples, usb.PortTouple{
Normal: t,
Debug: 0,
})
}
e, errUDP := usb.InitUDP(touples, longMemoryWriter)
if errUDP != nil {
panic(errUDP)
}
bus = append(bus, e)
}
if len(bus) == 0 {
stderrLogger.Fatalf("No transports enabled")
}
b := usb.Init(bus...)
defer b.Close()
longMemoryWriter.Log("Creating core")
c := core.New(b, longMemoryWriter, allowCancel(), reset)
longMemoryWriter.Log("Creating HTTP server")
s, err := server.New(c, port, stderrWriter, shortMemoryWriter, longMemoryWriter, version, githash)
if err != nil {
stderrLogger.Fatalf("https: %s", err)
}
longMemoryWriter.Log("Running HTTP server")
err = s.Run()
if err != nil {
stderrLogger.Fatalf("https: %s", err)
}
longMemoryWriter.Log("Main ended successfully")
}
func printWelcomeInfo(stderrLogger *log.Logger, port int) {
stderrLogger.Printf("trezord v%s (rev %s) is starting on port %d", version, githash, port)
if core.IsDebugBinary() {
stderrLogger.Print("!! DEBUG mode enabled! Please contact Trezor support in case you did not initiate this. !!")
}
}
// Does OS allow sync canceling via our custom libusb patches?
func allowCancel() bool {
return runtime.GOOS != "freebsd" && runtime.GOOS != "openbsd"
}
// Does OS detach kernel driver in libusb?
func detachKernelDriver() bool {
return runtime.GOOS == "linux"
}