summaryrefslogtreecommitdiffstats
path: root/katzenpost/cmd/yamn-submit/main.go
blob: cde35f91269137f59d59a8dc5537fe700b0db076 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main

import (
	"encoding/json"
	"errors"
	"flag"
	"fmt"
	"io"
	"os"
	"time"

	"git.virebent.art/virebent/yamnweb/katzenpost/dispatcher"
	"github.com/katzenpost/hpqc/hash"
	clientconfig "github.com/katzenpost/katzenpost/client/config"
	"github.com/katzenpost/katzenpost/client/thin"
)

const capability = "yamn-dispatch-v1"

func run() error {
	var configPath string
	var settle time.Duration
	flag.StringVar(&configPath, "config", "thinclient.toml", "thin-client configuration")
	flag.DurationVar(&settle, "settle", 2*time.Second, "time allowed for kpclientd to queue frames")
	flag.Parse()

	input, err := io.ReadAll(io.LimitReader(os.Stdin, dispatcher.MaxEnvelopeBytes*2))
	if err != nil {
		return fmt.Errorf("read envelope: %w", err)
	}
	var envelope dispatcher.Envelope
	if err := json.Unmarshal(input, &envelope); err != nil {
		return errors.New("invalid envelope input")
	}
	encoded, err := dispatcher.EncodeEnvelope(envelope)
	if err != nil {
		return err
	}
	frames, err := dispatcher.SplitEnvelope(encoded)
	if err != nil {
		return err
	}

	config, err := thin.LoadFile(configPath)
	if err != nil {
		return fmt.Errorf("load thin-client config: %w", err)
	}
	client := thin.NewThinClient(config, &clientconfig.Logging{Level: "ERROR", Disable: true})
	defer client.Close()
	if err := client.Dial(); err != nil {
		return fmt.Errorf("connect to kpclientd: %w", err)
	}
	service, err := client.GetService(capability)
	if err != nil {
		return fmt.Errorf("find dispatcher service: %w", err)
	}
	destination := hash.Sum256(service.MixDescriptor.IdentityKey)
	for _, frame := range frames {
		if err := client.SendMessageWithoutReply(frame, &destination, service.RecipientQueueID); err != nil {
			return fmt.Errorf("send frame: %w", err)
		}
	}
	time.Sleep(settle)
	fmt.Fprintln(os.Stdout, "accepted")
	return nil
}

func main() {
	if err := run(); err != nil {
		fmt.Fprintf(os.Stderr, "submission failed: %v\n", err)
		os.Exit(1)
	}
}