cacert-gosigner/client/main.go

131 lines
3 KiB
Go
Raw Normal View History

2018-10-31 11:17:51 +01:00
package main
import (
"flag"
"io"
"os"
"os/signal"
"sync"
"syscall"
"time"
log "github.com/sirupsen/logrus"
"go.bug.st/serial"
"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() {
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()
var clientConfig *ClientConfig
var serialConfig *serial.Mode
var err error
if clientConfig, err = readConfig(configFile); err != nil {
log.Panic(err)
}
serialConfig = fillSerialMode(clientConfig)
if clientConfig.Debug {
log.SetLevel(log.TraceLevel)
}
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)
}
log.Debug("serial port connected")
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,
)
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
}
wg.Done()
}()
go func() {
runMainLoop(requestChannel, &responseChannel)
wg.Done()
2018-10-31 11:17:51 +01: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) {
crlCheck := 0
log.Debug("starting main loop")
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
for {
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
}
if requestChannel.IsClosed() {
return
2018-10-31 11:17:51 +01:00
}
log.Debug("send NUL request to keep connection open")
requestChannel.C <- datastructures.NewNulRequest()
log.Debug("sleep for 2.7 seconds")
time.Sleep(2700 * time.Millisecond)
2018-10-31 11:17:51 +01:00
}
}