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-08-04 08:16:41 +00:00
|
|
|
"context"
|
2022-08-03 12:38:36 +00:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2022-08-04 08:16:41 +00:00
|
|
|
"git.cacert.org/cacert-gosigner/pkg/messages"
|
2022-08-03 12:38:36 +00:00
|
|
|
"github.com/justincpresley/go-cobs"
|
|
|
|
"github.com/shamaton/msgpackgen/msgpack"
|
|
|
|
)
|
|
|
|
|
|
|
|
const cobsDelimiter = 0x00
|
|
|
|
|
|
|
|
var cobsConfig = cobs.Config{SpecialByte: cobsDelimiter, Delimiter: true, EndingSave: true}
|
|
|
|
|
|
|
|
func main() {
|
2022-08-04 08:16:41 +00:00
|
|
|
errorLog := log.New(os.Stderr, "", log.LstdFlags)
|
|
|
|
|
|
|
|
sim := &clientSimulator{
|
|
|
|
errorLog: errorLog,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := sim.Run()
|
|
|
|
if err != nil {
|
|
|
|
errorLog.Printf("simulator returned an error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type clientSimulator struct {
|
|
|
|
errorLog *log.Logger
|
|
|
|
commands chan messages.Command
|
|
|
|
responses chan []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *clientSimulator) writeTestCommands(ctx context.Context) error {
|
|
|
|
messages.RegisterGeneratedResolver()
|
|
|
|
|
|
|
|
const healthInterval = 10 * time.Second
|
|
|
|
|
|
|
|
timer := time.NewTimer(healthInterval)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
_ = timer.Stop()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
case <-timer.C:
|
|
|
|
c.commands <- messages.Command{
|
|
|
|
Code: messages.CmdHealth,
|
|
|
|
TimeStamp: time.Now().UTC(),
|
|
|
|
}
|
|
|
|
|
|
|
|
timer.Reset(healthInterval)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-08-04 08:16:41 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
count, err := os.Stdin.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
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-08-04 08:16:41 +00:00
|
|
|
err = cobs.Verify(data, cobsConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-08-04 08:16:41 +00:00
|
|
|
c.responses <- cobs.Decode(data, cobsConfig)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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:
|
|
|
|
commandBytes, err := msgpack.Marshal(command)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not marshal command bytes: %w", err)
|
|
|
|
}
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-08-04 08:16:41 +00:00
|
|
|
_, err = os.Stdout.Write(cobs.Encode(commandBytes, cobsConfig))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("write failed: %w", err)
|
|
|
|
}
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-08-04 08:16:41 +00:00
|
|
|
responseBytes := <-c.responses
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-08-04 08:16:41 +00:00
|
|
|
var response messages.Response
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-08-04 08:16:41 +00:00
|
|
|
err = msgpack.Unmarshal(responseBytes, &response)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not unmarshal msgpack data: %w", err)
|
|
|
|
}
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-08-04 08:16:41 +00:00
|
|
|
c.errorLog.Printf("received response: %+v", response)
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-03 12:38:36 +00:00
|
|
|
|
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-08-04 08:16:41 +00:00
|
|
|
c.commands = make(chan messages.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-08-04 08:16:41 +00:00
|
|
|
var inputError, commandError error
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-08-04 08:16:41 +00:00
|
|
|
go func(inputErr error) {
|
|
|
|
inputError = c.handleInput(ctx)
|
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()
|
|
|
|
}(inputError)
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-08-04 08:16:41 +00:00
|
|
|
go func(commandErr error) {
|
|
|
|
commandErr = c.handleCommands(ctx)
|
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()
|
|
|
|
}(commandError)
|
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 {
|
|
|
|
c.errorLog.Printf("test commands failed: %v", err)
|
|
|
|
}
|
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
|
|
|
if inputError != nil {
|
|
|
|
c.errorLog.Printf("reading input failed: %v", inputError)
|
|
|
|
}
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-08-04 08:16:41 +00:00
|
|
|
if commandError != nil {
|
|
|
|
c.errorLog.Printf("sending commands failed: %v", commandError)
|
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
|
|
|
}
|