-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
174 lines (144 loc) · 4.17 KB
/
main.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
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"os/user"
"syscall"
"time"
"github.com/gorilla/mux"
"github.com/nebulaengineering/pcsc-agnostic-daemon/internal/app"
"github.com/nebulaengineering/pcsc-agnostic-daemon/internal/handler"
"github.com/nebulaengineering/pcsc-agnostic-daemon/utils"
"github.com/rs/cors"
)
var certpath string
var keypath string
var port int
var notcreate bool
var showversion bool
var isdebug bool
var ssl bool
func init() {
flag.StringVar(&certpath, "certpath", "", "[ssl enable required] path to certificate file, if this option wasn't defined the application will create a new certificate in \"$HOME\"")
flag.StringVar(&keypath, "keypath", "", "[ssl enable required] path to key file, if this option and \"certpath\" option weren't defined the application will create a new pair key in \"$HOME\"")
flag.BoolVar(¬create, "f", false, "don't Create files if they don't exist?")
flag.BoolVar(&ssl, "ssl", false, "enable ssl local service?")
flag.BoolVar(&showversion, "version", false, "show version")
flag.BoolVar(&isdebug, "debug", false, "show APDUs in stdout")
flag.IntVar(&port, "port", 1216, "port in local socket to LISTEN (socket = localhost:port)")
}
func main() {
flag.Parse()
if showversion {
fmt.Printf("version: %s\n", handler.VERSION)
os.Exit(2)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
utils.Debug = isdebug
app.InitInstance(ctx)
svc := &myService{
ctx: ctx,
cancelFunc: cancel,
}
interactive := IsAnInteractiveSession()
go func() {
runService("pcsc-pos", interactive, svc)
}()
router := mux.NewRouter().StrictSlash(true)
corsWrapper := cors.New(cors.Options{
AllowedMethods: []string{"GET", "POST", "PATCH", "DELETE"},
AllowedHeaders: []string{"Content-Type", "Origin", "Accept", "*"},
})
router.
Methods("GET").
Path("/pcsc-daemon/service").
Name("getState").
HandlerFunc(handler.GetState)
router.
Methods("GET").
Path("/pcsc-daemon/readers").
Name("listCardReaderDevices").
HandlerFunc(handler.ListAllReaders)
router.
Methods("GET").
Path("/pcsc-daemon/readers/{id}").
Name("getReaderInformation").
HandlerFunc(handler.ReaderInformation)
router.
Methods("GET").
Path("/pcsc-daemon/readers/{readerIdHex}/smartcard").
Name("getReaderCards").
HandlerFunc(handler.CardInReader)
router.
Methods("POST").
Path("/pcsc-daemon/readers/{readerIdHex}/smartcard/sendApdus").
Name("sendAPDUs").
HandlerFunc(handler.SendAPUs)
router.
Methods("POST").
Path("/pcsc-daemon/readers/{readerIdHex}/smartcard/sendApdu").
Name("sendAPDU").
HandlerFunc(handler.SendAPU)
serverHttp := &http.Server{
Addr: fmt.Sprintf(":%d", port),
Handler: corsWrapper.Handler(router),
}
if len(certpath) <= 0 {
certpath = func() string {
usr, err := user.Current()
if err == nil {
return usr.HomeDir
}
return os.Getenv("HOME")
}()
}
fmt.Println("pcsc-agnostic-daemon starting ...")
fmt.Println("pcsc-agnostic-daemon waiting for requests ...")
var serverSSL *http.Server
if ssl {
cert, key, err := verifyAndCreateFiles(certpath, keypath, !notcreate)
if err != nil {
log.Fatalln(err)
}
go func() {
serverSSL = &http.Server{
Addr: fmt.Sprintf(":%d", port-1),
Handler: corsWrapper.Handler(router),
}
if err := serverSSL.ListenAndServeTLS(cert, key); err != nil && err != http.ErrServerClosed {
log.Fatalf("listenAndServerTLS: %s\n", err)
}
}()
}
finish := make(chan os.Signal, 1)
signal.Notify(finish, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
go func() {
fmt.Println("pcsc-agnostic-daemon net started ...")
if err := serverHttp.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listenAndServer: %s\n", err)
}
}()
for {
select {
case <-finish:
ctxx, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()
if err := serverHttp.Shutdown(ctxx); err != nil {
log.Printf("shutdown serverHttp: %s\n", err)
}
if ssl && serverSSL != nil {
if err := serverSSL.Shutdown(ctxx); err != nil {
log.Printf("shutdown serverSSL: %s\n", err)
}
}
log.Println("Servidor cerrado correctamente")
return
}
}
}