145 lines
2.7 KiB
Go
145 lines
2.7 KiB
Go
|
/*
|
||
|
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 (
|
||
|
"fmt"
|
||
|
"log"
|
||
|
"os"
|
||
|
"sync"
|
||
|
"time"
|
||
|
|
||
|
"github.com/justincpresley/go-cobs"
|
||
|
"github.com/shamaton/msgpackgen/msgpack"
|
||
|
|
||
|
"git.cacert.org/cacert-gosigner/pkg/messages"
|
||
|
)
|
||
|
|
||
|
const cobsDelimiter = 0x00
|
||
|
|
||
|
var cobsConfig = cobs.Config{SpecialByte: cobsDelimiter, Delimiter: true, EndingSave: true}
|
||
|
|
||
|
func main() {
|
||
|
const (
|
||
|
bufferSize = 1024 * 1024
|
||
|
readInterval = 50 * time.Millisecond
|
||
|
)
|
||
|
|
||
|
errors := make(chan error)
|
||
|
|
||
|
errorLog := log.New(os.Stderr, "", log.LstdFlags)
|
||
|
|
||
|
wg := sync.WaitGroup{}
|
||
|
wg.Add(1)
|
||
|
|
||
|
done := make(chan struct{})
|
||
|
frame := make(chan []byte)
|
||
|
|
||
|
go func(done chan struct{}) {
|
||
|
buf := make([]byte, bufferSize)
|
||
|
|
||
|
for {
|
||
|
select {
|
||
|
case <-done:
|
||
|
wg.Done()
|
||
|
|
||
|
return
|
||
|
|
||
|
default:
|
||
|
count, err := os.Stdin.Read(buf)
|
||
|
if err != nil {
|
||
|
errors <- err
|
||
|
|
||
|
wg.Done()
|
||
|
|
||
|
return
|
||
|
}
|
||
|
|
||
|
if count == 0 {
|
||
|
time.Sleep(readInterval)
|
||
|
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
data := buf[:count]
|
||
|
|
||
|
err = cobs.Verify(data, cobsConfig)
|
||
|
if err != nil {
|
||
|
errors <- err
|
||
|
|
||
|
wg.Done()
|
||
|
|
||
|
return
|
||
|
}
|
||
|
|
||
|
frame <- cobs.Decode(data, cobsConfig)
|
||
|
}
|
||
|
}
|
||
|
}(done)
|
||
|
|
||
|
err := writeTestCommands(frame, errorLog)
|
||
|
if err != nil {
|
||
|
errorLog.Printf("could not write test commands")
|
||
|
}
|
||
|
|
||
|
err = <-errors
|
||
|
if err != nil {
|
||
|
errorLog.Printf("error: %v", err)
|
||
|
}
|
||
|
|
||
|
wg.Wait()
|
||
|
}
|
||
|
|
||
|
func writeTestCommands(responses chan []byte, errorLog *log.Logger) error {
|
||
|
messages.RegisterGeneratedResolver()
|
||
|
|
||
|
commands := []messages.Command{
|
||
|
{
|
||
|
Code: messages.CmdHealth,
|
||
|
TimeStamp: time.Now().UTC(),
|
||
|
},
|
||
|
}
|
||
|
|
||
|
for _, command := range commands {
|
||
|
commandBytes, err := msgpack.Marshal(command)
|
||
|
if err != nil {
|
||
|
return fmt.Errorf("could not marshal command bytes: %w", err)
|
||
|
}
|
||
|
|
||
|
_, err = os.Stdout.Write(cobs.Encode(commandBytes, cobsConfig))
|
||
|
if err != nil {
|
||
|
return fmt.Errorf("write failed: %w", err)
|
||
|
}
|
||
|
|
||
|
responseBytes := <-responses
|
||
|
|
||
|
var response messages.Response
|
||
|
|
||
|
err = msgpack.Unmarshal(responseBytes, &response)
|
||
|
if err != nil {
|
||
|
return fmt.Errorf("could not unmarshal msgpack data: %w", err)
|
||
|
}
|
||
|
|
||
|
errorLog.Printf("received response: %+v", response)
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|