2022-08-03 12:38:36 +00:00
|
|
|
/*
|
|
|
|
Copyright 2022 CAcert Inc.
|
|
|
|
SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// client simulator
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-11-20 17:59:37 +00:00
|
|
|
"bytes"
|
2022-08-04 08:16:41 +00:00
|
|
|
"context"
|
2022-08-03 12:38:36 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/justincpresley/go-cobs"
|
|
|
|
"github.com/shamaton/msgpackgen/msgpack"
|
2022-11-20 09:07:02 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2022-11-20 08:13:11 +00:00
|
|
|
|
2022-11-20 17:59:37 +00:00
|
|
|
"git.cacert.org/cacert-gosigner/pkg/protocol"
|
|
|
|
|
2022-11-20 08:13:11 +00:00
|
|
|
"git.cacert.org/cacert-gosigner/pkg/messages"
|
2022-08-03 12:38:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const cobsDelimiter = 0x00
|
|
|
|
|
|
|
|
var cobsConfig = cobs.Config{SpecialByte: cobsDelimiter, Delimiter: true, EndingSave: true}
|
|
|
|
|
|
|
|
func main() {
|
2022-11-20 09:07:02 +00:00
|
|
|
logger := logrus.New()
|
2022-11-20 17:59:37 +00:00
|
|
|
logger.SetOutput(os.Stderr)
|
2022-11-20 09:07:02 +00:00
|
|
|
logger.SetLevel(logrus.InfoLevel)
|
2022-08-04 08:16:41 +00:00
|
|
|
|
|
|
|
sim := &clientSimulator{
|
2022-11-20 09:07:02 +00:00
|
|
|
logger: logger,
|
2022-08-04 08:16:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err := sim.Run()
|
|
|
|
if err != nil {
|
2022-11-20 17:59:37 +00:00
|
|
|
logger.WithError(err).Error("simulator returned an error")
|
2022-08-04 08:16:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type clientSimulator struct {
|
2022-11-20 09:07:02 +00:00
|
|
|
logger *logrus.Logger
|
2022-11-20 17:59:37 +00:00
|
|
|
commands chan *protocol.Command
|
|
|
|
responses chan [][]byte
|
2022-08-04 08:16:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *clientSimulator) writeTestCommands(ctx context.Context) error {
|
|
|
|
messages.RegisterGeneratedResolver()
|
|
|
|
|
|
|
|
const healthInterval = 10 * time.Second
|
|
|
|
|
2022-11-20 17:59:37 +00:00
|
|
|
time.Sleep(healthInterval)
|
|
|
|
|
|
|
|
c.commands <- &protocol.Command{
|
|
|
|
Announce: messages.BuildCommandAnnounce(messages.CmdFetchCRL),
|
|
|
|
Command: &messages.FetchCRLCommand{IssuerID: "sub-ecc_person_2022"},
|
|
|
|
}
|
|
|
|
|
2022-08-04 08:16:41 +00:00
|
|
|
timer := time.NewTimer(healthInterval)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
_ = timer.Stop()
|
|
|
|
|
2022-11-20 17:59:37 +00:00
|
|
|
c.logger.Info("stopping health check loop")
|
|
|
|
|
2022-08-04 08:16:41 +00:00
|
|
|
return nil
|
|
|
|
case <-timer.C:
|
2022-11-20 17:59:37 +00:00
|
|
|
c.commands <- &protocol.Command{
|
|
|
|
Announce: messages.BuildCommandAnnounce(messages.CmdHealth),
|
|
|
|
Command: &messages.HealthCommand{},
|
2022-08-04 08:16:41 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-20 17:59:37 +00:00
|
|
|
|
|
|
|
timer.Reset(healthInterval)
|
2022-08-04 08:16:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *clientSimulator) handleInput(ctx context.Context) error {
|
2022-08-03 12:38:36 +00:00
|
|
|
const (
|
|
|
|
bufferSize = 1024 * 1024
|
|
|
|
readInterval = 50 * time.Millisecond
|
|
|
|
)
|
|
|
|
|
2022-08-04 08:16:41 +00:00
|
|
|
buf := make([]byte, bufferSize)
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-20 17:59:37 +00:00
|
|
|
type protocolState int8
|
|
|
|
|
|
|
|
const (
|
|
|
|
stAnn protocolState = iota
|
|
|
|
stResp
|
|
|
|
)
|
|
|
|
|
|
|
|
state := stAnn
|
|
|
|
|
|
|
|
var announce []byte
|
|
|
|
|
|
|
|
reading:
|
2022-08-04 08:16:41 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
count, err := os.Stdin.Read(buf)
|
|
|
|
if err != nil {
|
2022-11-20 08:13:11 +00:00
|
|
|
return fmt.Errorf("reading input failed: %w", err)
|
2022-08-04 08:16:41 +00:00
|
|
|
}
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-08-04 08:16:41 +00:00
|
|
|
if count == 0 {
|
|
|
|
time.Sleep(readInterval)
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-08-04 08:16:41 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-08-04 08:16:41 +00:00
|
|
|
data := buf[:count]
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-20 17:59:37 +00:00
|
|
|
for _, frame := range bytes.SplitAfter(data, []byte{cobsConfig.SpecialByte}) {
|
|
|
|
if len(frame) == 0 {
|
|
|
|
continue reading
|
|
|
|
}
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-20 17:59:37 +00:00
|
|
|
err = cobs.Verify(frame, cobsConfig)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("frame verification failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if state == stAnn {
|
|
|
|
announce = cobs.Decode(frame, cobsConfig)
|
|
|
|
|
|
|
|
state = stResp
|
|
|
|
} else {
|
|
|
|
c.responses <- [][]byte{announce, cobs.Decode(frame, cobsConfig)}
|
|
|
|
|
|
|
|
state = stAnn
|
|
|
|
}
|
|
|
|
}
|
2022-08-04 08:16:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-08-04 08:16:41 +00:00
|
|
|
func (c *clientSimulator) handleCommands(ctx context.Context) error {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case command := <-c.commands:
|
2022-11-20 17:59:37 +00:00
|
|
|
if err := writeCommandAnnouncement(command); err != nil {
|
|
|
|
return err
|
2022-08-04 08:16:41 +00:00
|
|
|
}
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-20 17:59:37 +00:00
|
|
|
if err := writeCommand(command); err != nil {
|
|
|
|
return err
|
2022-08-04 08:16:41 +00:00
|
|
|
}
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-20 17:59:37 +00:00
|
|
|
respData := <-c.responses
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-20 17:59:37 +00:00
|
|
|
c.logger.WithField("respdata", respData).Trace("read response data")
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-20 17:59:37 +00:00
|
|
|
response := &protocol.Response{}
|
|
|
|
|
|
|
|
if err := msgpack.Unmarshal(respData[0], &response.Announce); err != nil {
|
|
|
|
return fmt.Errorf("could not unmarshal response announcement: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.handleResponse(response, respData[1]); err != nil {
|
|
|
|
return err
|
2022-08-04 08:16:41 +00:00
|
|
|
}
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-08-04 08:16:41 +00:00
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-20 17:59:37 +00:00
|
|
|
func (c *clientSimulator) handleResponse(response *protocol.Response, respBytes []byte) error {
|
|
|
|
switch response.Announce.Code {
|
|
|
|
case messages.RespHealth:
|
|
|
|
data := messages.HealthResponse{}
|
|
|
|
|
|
|
|
if err := msgpack.Unmarshal(respBytes, &data); err != nil {
|
|
|
|
return fmt.Errorf("could not unmarshal health data: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.logger.WithField(
|
|
|
|
"announce",
|
|
|
|
response.Announce,
|
|
|
|
).WithField(
|
|
|
|
"data",
|
|
|
|
&data,
|
|
|
|
).Infof("received response")
|
|
|
|
case messages.RespFetchCRL:
|
|
|
|
data := messages.FetchCRLResponse{}
|
|
|
|
|
|
|
|
if err := msgpack.Unmarshal(respBytes, &data); err != nil {
|
|
|
|
return fmt.Errorf("could not unmarshal fetch CRL data: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.logger.WithField(
|
|
|
|
"announce",
|
|
|
|
response.Announce,
|
|
|
|
).WithField(
|
|
|
|
"data",
|
|
|
|
&data,
|
|
|
|
).Infof("received response")
|
|
|
|
case messages.RespError:
|
|
|
|
data := messages.ErrorResponse{}
|
|
|
|
|
|
|
|
if err := msgpack.Unmarshal(respBytes, &data); err != nil {
|
|
|
|
return fmt.Errorf("could not unmarshal error data: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.logger.WithField(
|
|
|
|
"announce",
|
|
|
|
response.Announce,
|
|
|
|
).WithField(
|
|
|
|
"data",
|
|
|
|
&data,
|
|
|
|
).Infof("received response")
|
|
|
|
default:
|
|
|
|
if err := msgpack.Unmarshal(respBytes, &response.Response); err != nil {
|
|
|
|
return fmt.Errorf("could not unmarshal response: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.logger.WithField("response", response).Infof("received response")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeCommandAnnouncement(command *protocol.Command) error {
|
|
|
|
cmdAnnounceBytes, err := msgpack.Marshal(&command.Announce)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not marshal command announcement bytes: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = os.Stdout.Write(cobs.Encode(cmdAnnounceBytes, cobsConfig)); err != nil {
|
|
|
|
return fmt.Errorf("command announcement write failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeCommand(command *protocol.Command) error {
|
|
|
|
cmdBytes, err := msgpack.Marshal(&command.Command)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not marshal command bytes: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = os.Stdout.Write(cobs.Encode(cmdBytes, cobsConfig)); err != nil {
|
|
|
|
return fmt.Errorf("command write failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-04 08:16:41 +00:00
|
|
|
func (c *clientSimulator) Run() error {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-20 17:59:37 +00:00
|
|
|
c.commands = make(chan *protocol.Command)
|
|
|
|
c.responses = make(chan [][]byte)
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-08-04 08:16:41 +00:00
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(2)
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-20 17:59:37 +00:00
|
|
|
go func() {
|
|
|
|
if err := c.handleInput(ctx); err != nil {
|
|
|
|
c.logger.WithError(err).Error("input handling failed")
|
|
|
|
}
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-08-04 08:16:41 +00:00
|
|
|
cancel()
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-08-04 08:16:41 +00:00
|
|
|
wg.Done()
|
2022-11-20 17:59:37 +00:00
|
|
|
}()
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-20 17:59:37 +00:00
|
|
|
go func() {
|
|
|
|
if err := c.handleCommands(ctx); err != nil {
|
|
|
|
c.logger.WithError(err).Error("command handling failed")
|
|
|
|
}
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-08-04 08:16:41 +00:00
|
|
|
cancel()
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-08-04 08:16:41 +00:00
|
|
|
wg.Done()
|
2022-11-20 17:59:37 +00:00
|
|
|
}()
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-08-04 08:16:41 +00:00
|
|
|
var result error
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-08-04 08:16:41 +00:00
|
|
|
if err := c.writeTestCommands(ctx); err != nil {
|
2022-11-20 17:59:37 +00:00
|
|
|
c.logger.WithError(err).Error("test commands failed")
|
2022-08-04 08:16:41 +00:00
|
|
|
}
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-08-04 08:16:41 +00:00
|
|
|
cancel()
|
|
|
|
wg.Wait()
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-08-04 08:16:41 +00:00
|
|
|
return result
|
2022-08-03 12:38:36 +00:00
|
|
|
}
|