|
|
@ -20,16 +20,16 @@ limitations under the License.
|
|
|
|
package main
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"git.cacert.org/cacert-gosigner/pkg/messages"
|
|
|
|
"github.com/justincpresley/go-cobs"
|
|
|
|
"github.com/justincpresley/go-cobs"
|
|
|
|
"github.com/shamaton/msgpackgen/msgpack"
|
|
|
|
"github.com/shamaton/msgpackgen/msgpack"
|
|
|
|
|
|
|
|
|
|
|
|
"git.cacert.org/cacert-gosigner/pkg/messages"
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
const cobsDelimiter = 0x00
|
|
|
|
const cobsDelimiter = 0x00
|
|
|
@ -37,108 +37,157 @@ const cobsDelimiter = 0x00
|
|
|
|
var cobsConfig = cobs.Config{SpecialByte: cobsDelimiter, Delimiter: true, EndingSave: true}
|
|
|
|
var cobsConfig = cobs.Config{SpecialByte: cobsDelimiter, Delimiter: true, EndingSave: true}
|
|
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
func main() {
|
|
|
|
|
|
|
|
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 {
|
|
|
|
const (
|
|
|
|
const (
|
|
|
|
bufferSize = 1024 * 1024
|
|
|
|
bufferSize = 1024 * 1024
|
|
|
|
readInterval = 50 * time.Millisecond
|
|
|
|
readInterval = 50 * time.Millisecond
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
errors := make(chan error)
|
|
|
|
buf := make([]byte, bufferSize)
|
|
|
|
|
|
|
|
|
|
|
|
errorLog := log.New(os.Stderr, "", log.LstdFlags)
|
|
|
|
for {
|
|
|
|
|
|
|
|
select {
|
|
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
count, err := os.Stdin.Read(buf)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
if count == 0 {
|
|
|
|
wg.Add(1)
|
|
|
|
time.Sleep(readInterval)
|
|
|
|
|
|
|
|
|
|
|
|
done := make(chan struct{})
|
|
|
|
continue
|
|
|
|
frame := make(chan []byte)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
go func(done chan struct{}) {
|
|
|
|
data := buf[:count]
|
|
|
|
buf := make([]byte, bufferSize)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
err = cobs.Verify(data, cobsConfig)
|
|
|
|
select {
|
|
|
|
if err != nil {
|
|
|
|
case <-done:
|
|
|
|
return err
|
|
|
|
wg.Done()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
c.responses <- cobs.Decode(data, cobsConfig)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
func (c *clientSimulator) handleCommands(ctx context.Context) error {
|
|
|
|
count, err := os.Stdin.Read(buf)
|
|
|
|
for {
|
|
|
|
if err != nil {
|
|
|
|
select {
|
|
|
|
errors <- err
|
|
|
|
case command := <-c.commands:
|
|
|
|
|
|
|
|
commandBytes, err := msgpack.Marshal(command)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return fmt.Errorf("could not marshal command bytes: %w", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wg.Done()
|
|
|
|
_, err = os.Stdout.Write(cobs.Encode(commandBytes, cobsConfig))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return fmt.Errorf("write failed: %w", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
responseBytes := <-c.responses
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if count == 0 {
|
|
|
|
var response messages.Response
|
|
|
|
time.Sleep(readInterval)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
continue
|
|
|
|
err = msgpack.Unmarshal(responseBytes, &response)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return fmt.Errorf("could not unmarshal msgpack data: %w", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
data := buf[:count]
|
|
|
|
c.errorLog.Printf("received response: %+v", response)
|
|
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
err = cobs.Verify(data, cobsConfig)
|
|
|
|
func (c *clientSimulator) Run() error {
|
|
|
|
if err != nil {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
errors <- err
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
wg.Done()
|
|
|
|
c.commands = make(chan messages.Command)
|
|
|
|
|
|
|
|
c.responses = make(chan []byte)
|
|
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
}
|
|
|
|
wg.Add(2)
|
|
|
|
|
|
|
|
|
|
|
|
frame <- cobs.Decode(data, cobsConfig)
|
|
|
|
var inputError, commandError error
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}(done)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err := writeTestCommands(frame, errorLog)
|
|
|
|
go func(inputErr error) {
|
|
|
|
if err != nil {
|
|
|
|
inputError = c.handleInput(ctx)
|
|
|
|
errorLog.Printf("could not write test commands")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err = <-errors
|
|
|
|
cancel()
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
errorLog.Printf("error: %v", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
wg.Done()
|
|
|
|
}
|
|
|
|
}(inputError)
|
|
|
|
|
|
|
|
|
|
|
|
func writeTestCommands(responses chan []byte, errorLog *log.Logger) error {
|
|
|
|
go func(commandErr error) {
|
|
|
|
messages.RegisterGeneratedResolver()
|
|
|
|
commandErr = c.handleCommands(ctx)
|
|
|
|
|
|
|
|
|
|
|
|
commands := []messages.Command{
|
|
|
|
cancel()
|
|
|
|
{
|
|
|
|
|
|
|
|
Code: messages.CmdHealth,
|
|
|
|
|
|
|
|
TimeStamp: time.Now().UTC(),
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for _, command := range commands {
|
|
|
|
wg.Done()
|
|
|
|
commandBytes, err := msgpack.Marshal(command)
|
|
|
|
}(commandError)
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return fmt.Errorf("could not marshal command bytes: %w", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_, err = os.Stdout.Write(cobs.Encode(commandBytes, cobsConfig))
|
|
|
|
var result error
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return fmt.Errorf("write failed: %w", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
responseBytes := <-responses
|
|
|
|
if err := c.writeTestCommands(ctx); err != nil {
|
|
|
|
|
|
|
|
c.errorLog.Printf("test commands failed: %v", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var response messages.Response
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
|
|
|
|
err = msgpack.Unmarshal(responseBytes, &response)
|
|
|
|
if inputError != nil {
|
|
|
|
if err != nil {
|
|
|
|
c.errorLog.Printf("reading input failed: %v", inputError)
|
|
|
|
return fmt.Errorf("could not unmarshal msgpack data: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
errorLog.Printf("received response: %+v", response)
|
|
|
|
if commandError != nil {
|
|
|
|
|
|
|
|
c.errorLog.Printf("sending commands failed: %v", commandError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
}
|
|
|
|