forked from trezor/trezord-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trezord.go
101 lines (86 loc) · 1.86 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
package main
import (
"flag"
"io"
"log"
"os"
"strconv"
"github.com/trezor/trezord-go/memorywriter"
"github.com/trezor/trezord-go/server"
"github.com/trezor/trezord-go/usb"
"gopkg.in/natefinch/lumberjack.v2"
)
type udpPorts []int
func (i *udpPorts) String() string {
res := ""
for i, p := range *i {
if i > 0 {
res = res + ","
}
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 main() {
var logfile string
var ports udpPorts
var withusb bool
flag.StringVar(&logfile, "l", "", "Log into a file, rotating after 5MB")
flag.Var(&ports, "e", "Use UDP port for emulator. Can be repeated for more ports. Example: trezord-go -e 21324 -e 21326")
flag.BoolVar(&withusb, "u", true, "Use USB devices. Can be disabled for testing environments. Example: trezord-go -e 21324 -u=false")
flag.Parse()
var logger io.Writer
if logfile != "" {
logger = &lumberjack.Logger{
Filename: logfile,
MaxSize: 5, // megabytes
MaxBackups: 3,
}
} else {
logger = os.Stderr
}
m := memorywriter.New(2000)
logger = io.MultiWriter(logger, m)
log.SetOutput(logger)
log.Println("trezord is starting.")
var bus []usb.Bus
if withusb {
w, err := usb.InitWebUSB()
if err != nil {
log.Fatalf("webusb: %s", err)
}
defer w.Close()
h, err := usb.InitHIDAPI()
if err != nil {
log.Fatalf("hidapi: %s", err)
}
bus = append(bus, w, h)
}
if len(ports) > 0 {
e, errUDP := usb.InitUDP(ports)
if errUDP != nil {
log.Fatalf("emulator: %s", errUDP)
}
bus = append(bus, e)
}
if len(bus) == 0 {
log.Fatalf("No transports enabled")
}
b := usb.Init(bus...)
s, err := server.New(b, logger, m)
if err != nil {
log.Fatalf("https: %s", err)
}
err = s.Run()
if err != nil {
log.Fatalf("https: %s", err)
}
}