Refactor signer code into command and package
This commit is contained in:
parent
3a2578ae55
commit
38566f35ef
13 changed files with 476 additions and 276 deletions
|
@ -2,7 +2,6 @@ package main
|
|||
|
||||
import (
|
||||
"flag"
|
||||
"io"
|
||||
"os"
|
||||
"os/signal"
|
||||
"sync"
|
||||
|
@ -45,13 +44,12 @@ func main() {
|
|||
requestChannel := protocol.NewSignerProtocolRequestChannel()
|
||||
responseChannel := make(chan *datastructures.SignerResponse, 1)
|
||||
|
||||
readWriteCloser := (io.ReadWriteCloser)(port)
|
||||
clientProtocolConfig := protocol.NewSignerProtocolConfig()
|
||||
if clientConfig.BufferSize != 0 {
|
||||
clientProtocolConfig.BufferSize = int(clientConfig.BufferSize)
|
||||
}
|
||||
protocolHandler := protocol.NewProtocolHandler(
|
||||
requestChannel, &responseChannel, &readWriteCloser, clientProtocolConfig,
|
||||
requestChannel, &responseChannel, port, clientProtocolConfig,
|
||||
)
|
||||
|
||||
cancelChannel := make(chan os.Signal, 1)
|
||||
|
|
|
@ -2,7 +2,10 @@ package processing
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.cacert.org/cacert-gosigner/datastructures"
|
||||
"git.cacert.org/cacert-gosigner/shared"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
|
@ -11,7 +14,7 @@ func Process(response *datastructures.SignerResponse) (err error) {
|
|||
logrus.Tracef("process response %+v", response)
|
||||
|
||||
switch response.Action {
|
||||
case datastructures.ActionNul:
|
||||
case shared.ActionNul:
|
||||
logrus.Trace("received response for NUL request")
|
||||
return
|
||||
default:
|
||||
|
|
|
@ -3,12 +3,14 @@ package protocol
|
|||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"git.cacert.org/cacert-gosigner/datastructures"
|
||||
"git.cacert.org/cacert-gosigner/shared"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"io"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"git.cacert.org/cacert-gosigner/datastructures"
|
||||
"git.cacert.org/cacert-gosigner/shared"
|
||||
)
|
||||
|
||||
type SignerProtocolHandler interface {
|
||||
|
@ -52,7 +54,7 @@ func (rc *SignerProtocolRequestChannel) IsClosed() bool {
|
|||
type protocolHandler struct {
|
||||
requestChannel *SignerProtocolRequestChannel
|
||||
responseChannel *chan *datastructures.SignerResponse
|
||||
serialConnection *io.ReadWriteCloser
|
||||
serialConnection io.ReadWriteCloser
|
||||
config *signerProtocolConfig
|
||||
}
|
||||
|
||||
|
@ -76,7 +78,12 @@ func (ph *protocolHandler) Close() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func NewProtocolHandler(requests *SignerProtocolRequestChannel, response *chan *datastructures.SignerResponse, serialConnection *io.ReadWriteCloser, config *signerProtocolConfig) SignerProtocolHandler {
|
||||
func NewProtocolHandler(
|
||||
requests *SignerProtocolRequestChannel,
|
||||
response *chan *datastructures.SignerResponse,
|
||||
serialConnection io.ReadWriteCloser,
|
||||
config *signerProtocolConfig,
|
||||
) SignerProtocolHandler {
|
||||
return &protocolHandler{
|
||||
requestChannel: requests,
|
||||
responseChannel: response,
|
||||
|
@ -123,13 +130,13 @@ func (ph *protocolHandler) HandleSignerProtocol() error {
|
|||
|
||||
func (ph *protocolHandler) sendHandshake() (err error) {
|
||||
var bytesWritten, bytesRead int
|
||||
if bytesWritten, err = (*ph.serialConnection).Write([]byte{shared.HandshakeByte}); err != nil {
|
||||
if bytesWritten, err = ph.serialConnection.Write([]byte{shared.HandshakeByte}); err != nil {
|
||||
return
|
||||
} else {
|
||||
log.Tracef("wrote %d bytes of handshake info", bytesWritten)
|
||||
}
|
||||
data := make([]byte, 1)
|
||||
if bytesRead, err = (*ph.serialConnection).Read(data); err != nil {
|
||||
if bytesRead, err = ph.serialConnection.Read(data); err != nil {
|
||||
return
|
||||
}
|
||||
log.Tracef("%d bytes read", bytesRead)
|
||||
|
@ -142,13 +149,13 @@ func (ph *protocolHandler) sendHandshake() (err error) {
|
|||
|
||||
func (ph *protocolHandler) sendRequest(requestBytes *[]byte) error {
|
||||
for {
|
||||
if length, err := (*ph.serialConnection).Write(*requestBytes); err != nil {
|
||||
if length, err := ph.serialConnection.Write(*requestBytes); err != nil {
|
||||
return err
|
||||
} else {
|
||||
log.Tracef("wrote %d request bytes", length)
|
||||
}
|
||||
|
||||
if length, err := (*ph.serialConnection).Write([]byte{
|
||||
if length, err := ph.serialConnection.Write([]byte{
|
||||
datastructures.CalculateXorCheckSum([][]byte{*requestBytes}),
|
||||
}); err != nil {
|
||||
return err
|
||||
|
@ -156,7 +163,7 @@ func (ph *protocolHandler) sendRequest(requestBytes *[]byte) error {
|
|||
log.Tracef("wrote %d checksum bytes", length)
|
||||
}
|
||||
|
||||
if length, err := (*ph.serialConnection).Write([]byte(shared.MagicTrailer)); err != nil {
|
||||
if length, err := ph.serialConnection.Write([]byte(shared.MagicTrailer)); err != nil {
|
||||
return err
|
||||
} else {
|
||||
log.Tracef("wrote %d trailer bytes", length)
|
||||
|
@ -184,7 +191,7 @@ func (ph *protocolHandler) waitForResponseHandshake() (err error) {
|
|||
log.Warnf("received invalid handshake byte 0x%x", data[0])
|
||||
return UnExpectedHandshakeByte{data[0]}
|
||||
}
|
||||
if err = shared.SendByte(ph.serialConnection, shared.AckByte); err != nil {
|
||||
if err = shared.SendBytes(ph.serialConnection, []byte{shared.AckByte}); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -229,7 +236,7 @@ func (ph *protocolHandler) readResponse() (*[]byte, *[]byte, byte, error) {
|
|||
if calculatedChecksum != checkSum {
|
||||
return nil, nil, 0, fmt.Errorf("calculated checksum mismatch 0x%x vs 0x%x", calculatedChecksum, checkSum)
|
||||
}
|
||||
if err := shared.SendByte(ph.serialConnection, shared.AckByte); err != nil {
|
||||
if err := shared.SendBytes(ph.serialConnection, []byte{shared.AckByte}); err != nil {
|
||||
return nil, nil, 0, err
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue