-
Notifications
You must be signed in to change notification settings - Fork 24
/
cache.go
102 lines (89 loc) · 2.26 KB
/
cache.go
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright (c) 2016 Company 0, LLC.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"encoding/hex"
"fmt"
"path"
"github.com/companyzero/zkc/rpc"
"github.com/companyzero/zkc/session"
"github.com/companyzero/zkc/zkidentity"
)
// think about establishing whitelist or just blind deliver
func (z *ZKS) handleCache(writer chan *RPCWrapper, kx *session.KX, msg rpc.Message, cache rpc.Cache) error {
// sanity
if msg.Command != rpc.TaggedCmdCache {
return fmt.Errorf("invalid cache command")
}
// deliver message
var (
from [zkidentity.IdentitySize]byte
ok bool
)
from, ok = kx.TheirIdentity().([32]byte)
if !ok {
return fmt.Errorf("invalid identity type")
}
filename, err := z.account.Deliver(cache.To, from, cache.Payload, false)
if err != nil {
return fmt.Errorf("delivery failed: %v", err)
}
// ack
writer <- &RPCWrapper{
Message: rpc.Message{
Command: rpc.TaggedCmdAcknowledge,
Tag: msg.Tag,
},
Payload: rpc.Empty{},
}
// dont eval if not in debug mode
if z.settings.Debug {
z.Dbg(idApp, "handleCache: %v -> %v: %v",
hex.EncodeToString(cache.To[:]),
hex.EncodeToString(from[:]),
path.Base(filename))
}
return nil
}
func (z *ZKS) handleProxy(writer chan *RPCWrapper, kx *session.KX, msg rpc.Message, proxy rpc.Proxy) error {
reply := RPCWrapper{
Message: rpc.Message{
Command: rpc.TaggedCmdProxyReply,
Tag: msg.Tag,
},
}
payload := rpc.ProxyReply{
To: proxy.To,
}
// sanity
if msg.Command != rpc.TaggedCmdProxy {
return fmt.Errorf("invalid proxy command")
}
// deliver message
var (
from [zkidentity.IdentitySize]byte
ok bool
)
from, ok = kx.TheirIdentity().([32]byte)
if !ok {
return fmt.Errorf("invalid identity type")
}
filename, err := z.account.Deliver(proxy.To, from, proxy.Payload, true)
if err != nil {
payload.Error = fmt.Sprintf("proxy delivery failed to: %x",
proxy.To)
z.Dbg(idApp, "proxy delivery failed %x -> %x: %v",
from, proxy.To, err)
}
// dont eval if not in debug mode
if z.settings.Debug {
z.Dbg(idApp, "handleProxy: %v -> %v: %v",
hex.EncodeToString(proxy.To[:]),
hex.EncodeToString(from[:]),
path.Base(filename))
}
reply.Payload = payload
writer <- &reply
return nil
}