2018-10-31 11:17:51 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2020-04-19 22:29:58 +02:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"sync"
|
|
|
|
"syscall"
|
2020-04-17 19:39:01 +02:00
|
|
|
"time"
|
|
|
|
|
2020-04-17 19:39:06 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"go.bug.st/serial"
|
|
|
|
|
2020-04-19 22:29:58 +02:00
|
|
|
"git.cacert.org/cacert-gosigner/client/processing"
|
|
|
|
"git.cacert.org/cacert-gosigner/client/protocol"
|
2018-10-31 11:17:51 +01:00
|
|
|
"git.cacert.org/cacert-gosigner/datastructures"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2020-04-17 19:38:54 +02:00
|
|
|
var configFile string
|
|
|
|
|
|
|
|
flag.StringVar(&configFile, "c", "client.yaml", "client configuration file in YAML format")
|
2018-10-31 11:17:51 +01:00
|
|
|
flag.Parse()
|
|
|
|
|
2020-04-17 19:38:54 +02:00
|
|
|
var clientConfig *ClientConfig
|
2020-04-17 19:39:06 +02:00
|
|
|
var serialConfig *serial.Mode
|
2020-04-17 19:38:54 +02:00
|
|
|
var err error
|
|
|
|
|
|
|
|
if clientConfig, err = readConfig(configFile); err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
2020-04-17 19:39:06 +02:00
|
|
|
serialConfig = fillSerialMode(clientConfig)
|
2020-04-17 19:38:54 +02:00
|
|
|
if clientConfig.Debug {
|
2020-04-19 22:29:58 +02:00
|
|
|
log.SetLevel(log.TraceLevel)
|
2020-04-17 19:38:54 +02:00
|
|
|
}
|
|
|
|
|
2020-04-17 19:39:06 +02:00
|
|
|
log.Infof("connecting to %s using %+v", clientConfig.SerialAddress, serialConfig)
|
|
|
|
port, err := serial.Open(clientConfig.SerialAddress, serialConfig)
|
2018-10-31 11:17:51 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-04-17 19:39:01 +02:00
|
|
|
log.Debug("serial port connected")
|
2020-04-19 22:29:58 +02:00
|
|
|
|
|
|
|
requestChannel := protocol.NewSignerProtocolRequestChannel()
|
|
|
|
responseChannel := make(chan *datastructures.SignerResponse, 1)
|
|
|
|
|
|
|
|
readWriteCloser := (io.ReadWriteCloser)(port)
|
2020-04-20 22:01:06 +02:00
|
|
|
clientProtocolConfig := protocol.NewSignerProtocolConfig()
|
|
|
|
if clientConfig.BufferSize != 0 {
|
|
|
|
clientProtocolConfig.BufferSize = int(clientConfig.BufferSize)
|
|
|
|
}
|
|
|
|
protocolHandler := protocol.NewProtocolHandler(
|
|
|
|
requestChannel, &responseChannel, &readWriteCloser, clientProtocolConfig,
|
|
|
|
)
|
2020-04-19 22:29:58 +02:00
|
|
|
|
|
|
|
cancelChannel := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(cancelChannel, syscall.SIGTERM, syscall.SIGINT)
|
|
|
|
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(2)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
if err := protocolHandler.HandleSignerProtocol(); err != nil {
|
|
|
|
log.Errorf("terminating because of %v", err)
|
|
|
|
close(cancelChannel)
|
2018-10-31 11:17:51 +01:00
|
|
|
}
|
2020-04-19 22:29:58 +02:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
runMainLoop(requestChannel, &responseChannel)
|
|
|
|
wg.Done()
|
2018-10-31 11:17:51 +01:00
|
|
|
}()
|
|
|
|
|
2020-04-19 22:29:58 +02:00
|
|
|
sig := <-cancelChannel
|
|
|
|
if sig != nil {
|
|
|
|
log.Infof("caught %+v", sig)
|
|
|
|
}
|
|
|
|
if err := protocolHandler.Close(); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
} else {
|
|
|
|
log.Infof("protocol handler closed")
|
|
|
|
}
|
|
|
|
if err := port.Close(); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
} else {
|
|
|
|
log.Infof("serial port closed")
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func runMainLoop(requestChannel *protocol.SignerProtocolRequestChannel, responseChannel *chan *datastructures.SignerResponse) {
|
2020-04-17 19:39:01 +02:00
|
|
|
crlCheck := 0
|
|
|
|
|
|
|
|
log.Debug("starting main loop")
|
|
|
|
|
2020-04-19 22:29:58 +02:00
|
|
|
go func() {
|
|
|
|
for response := range *responseChannel {
|
|
|
|
if err := processing.Process(response); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Trace("processing goroutine terminated")
|
|
|
|
}()
|
2018-10-31 11:17:51 +01:00
|
|
|
|
2020-04-19 22:29:58 +02:00
|
|
|
for {
|
2020-04-17 19:39:01 +02:00
|
|
|
log.Debug("handling GPG database ...")
|
|
|
|
// HandleGPG(&requestChannel)
|
|
|
|
log.Debug("issuing certificates ...")
|
|
|
|
// HandleCertificates(&requestChannel)
|
|
|
|
log.Debug("revoking certificates ...")
|
|
|
|
// RevokeCertificates(&requestChannel)
|
|
|
|
|
|
|
|
crlCheck++
|
|
|
|
if crlCheck%100 == 0 {
|
|
|
|
log.Debug("refresh CRLs ...")
|
|
|
|
// RefreshCRLs(&requestChannel)
|
2018-10-31 11:17:51 +01:00
|
|
|
}
|
|
|
|
|
2020-04-19 22:29:58 +02:00
|
|
|
if requestChannel.IsClosed() {
|
|
|
|
return
|
2018-10-31 11:17:51 +01:00
|
|
|
}
|
2020-04-19 22:29:58 +02:00
|
|
|
log.Debug("send NUL request to keep connection open")
|
|
|
|
requestChannel.C <- datastructures.NewNulRequest()
|
2020-04-17 19:39:01 +02:00
|
|
|
|
|
|
|
log.Debug("sleep for 2.7 seconds")
|
|
|
|
time.Sleep(2700 * time.Millisecond)
|
2018-10-31 11:17:51 +01:00
|
|
|
}
|
2020-04-17 19:38:54 +02:00
|
|
|
}
|