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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
{-# LANGUAGE OverloadedStrings, RankNTypes, FlexibleContexts #-}
module Main where
import Types
import Hash
import Pty
import Memory
import CmdLine
import Log
import Graphviz
import Replay
import Session
import Control.Concurrent
import Control.Concurrent.Async
import Control.Concurrent.STM
import System.IO
import System.Process
import System.Exit
import qualified Data.ByteString as B
import Data.List
import Data.List.NonEmpty (NonEmpty(..), toList)
import Data.Monoid
import Data.Time.Clock.POSIX
main :: IO ()
main = do
c <- getCmdLine
case mode c of
Test -> test
Graphviz g -> graphviz g
Replay r -> replay r
test :: IO ()
test = do
exitstatus <- go startSession
sessionDone
exitWith exitstatus
where
go startmsg = runWithPty "dash" [] $ \(p, ph) -> do
ichan <- newTChanIO
ochan <- newTChanIO
dthread <- async (developer ichan ochan)
uthread <- async (user startmsg p ichan ochan)
exitstatus <- waitForProcess ph
cancel dthread
cancel uthread
return exitstatus
networkDelay :: IO ()
networkDelay = threadDelay 800000 -- 800 ms ; the latency to geosync orbit
-- networkDelay = threadDelay 150000 -- 150 ms ; transatlantic latency
developer :: TChan (Activity Entered) -> TChan (Activity Seen) -> IO ()
developer ichan ochan = withLogger "debug-me-developer.log" $ \logger -> do
startact <- atomically $ readTChan ochan
logger $ ActivitySeen startact
case startact of
Activity (Proto (Seen (Val b))) Nothing sig -> do
B.hPut stdout b
hFlush stdout
_ -> protocolError $ "Unexpected startup: " ++ show startact
devstate <- newTVarIO $ DeveloperState
{ lastSeen = hash startact
, sentSince = mempty
, enteredSince = mempty
, lastActivity = hash startact
}
_ <- sendTtyInput ichan devstate logger
`concurrently` sendTtyOutput ochan devstate logger
return ()
data DeveloperState = DeveloperState
{ lastSeen :: Hash
, sentSince :: [B.ByteString]
, enteredSince :: [Hash]
, lastActivity :: Hash
}
deriving (Show)
-- | Read things typed by the developer, and forward them to the TChan.
sendTtyInput :: TChan (Activity Entered) -> TVar DeveloperState -> Logger -> IO ()
sendTtyInput ichan devstate logger = go
where
go = do
b <- B.hGetSome stdin 1024
if b == B.empty
then return ()
else send b
send b = do
act <- atomically $ do
ds <- readTVar devstate
let ed = if lastActivity ds == lastSeen ds
then B.concat $ sentSince ds
else case reverse (sentSince ds) of
[] -> mempty
(lb:_) -> lb
let entered = Entered
{ enteredData = Val b
, echoData = Val ed
}
let act = Activity (Proto entered) (Just $ lastActivity ds) dummySignature
writeTChan ichan act
let acth = hash act
let ds' = ds
{ sentSince = sentSince ds ++ [b]
, enteredSince = enteredSince ds ++ [acth]
, lastActivity = acth
}
writeTVar devstate ds'
return act
logger $ ActivityEntered act
go
-- | Read activity from the TChan and display it to the developer.
sendTtyOutput :: TChan (Activity Seen) -> TVar DeveloperState -> Logger -> IO ()
sendTtyOutput ochan devstate logger = go
where
go = do
(v, act) <- atomically $ processOutput ochan devstate
logger $ ActivitySeen act
case v of
ProtocolError e -> protocolError e
TtyOutput b -> do
B.hPut stdout b
hFlush stdout
go
Beep -> do
B.hPut stdout "\a"
hFlush stdout
go
data Output = TtyOutput B.ByteString | Beep | ProtocolError String
processOutput :: TChan (Activity Seen) -> TVar DeveloperState -> STM (Output, Activity Seen)
processOutput ochan devstate = do
act <- readTChan ochan
ds <- readTVar devstate
let (legal, ds') = isLegalSeen act ds
if legal
then case act of
Activity (Proto (Seen (Val b))) _ _ -> do
writeTVar devstate ds'
return (TtyOutput b, act)
Activity (Rejected _) _ _ -> do
writeTVar devstate ds'
return (Beep, act)
else return (ProtocolError $ "Illegal Seen value: " ++ show (act, hash act) ++ "\n" ++ show ds, act)
-- | Check if the Seen activity is legal, and returns an updated
-- DeveloperState.
isLegalSeen :: Activity Seen -> DeveloperState -> (Bool, DeveloperState)
isLegalSeen act@(Activity p (Just hp) sig) ds
-- Does it chain to the last Seen value?
| hp == lastSeen ds = case p of
Rejected _ -> yesrej
Proto (Seen (Val b)) ->
-- Trim sentSince and enteredSince to
-- values after the Seen value.
let ss = sentSince ds
es = enteredSince ds
n = B.length b
(ss', es') = if b `B.isPrefixOf` mconcat ss
then (drop n ss, drop n es)
else (mempty, mempty)
in yes $ DeveloperState
{ lastSeen = acth
, sentSince = ss'
, enteredSince = es'
, lastActivity = acth
}
-- Does it chain to something we've entered since the last Seen
-- value? Eg, user sent A, we replied B C, and the user has
-- now replied to B.
-- If so, we can drop B (and anything before it) from
-- enteredSince and sentSince.
| otherwise = case elemIndex hp (enteredSince ds) of
Nothing -> (False, ds)
Just i -> case p of
Rejected _ -> yesrej
Proto (Seen (Val _)) ->
let ss = sentSince ds
es = enteredSince ds
ss' = drop (i+1) ss
es' = drop (i+1) es
in yes $ DeveloperState
{ lastSeen = acth
, sentSince = ss'
, enteredSince = es'
, lastActivity = acth
}
where
acth = hash act
yes ds' = (True, ds')
-- When they rejected a message we sent, anything we sent
-- subsequently will also be rejected, so forget about it.
yesrej = yes $ ds
{ lastSeen = acth
, lastActivity = acth
}
isLegalSeen (Activity _ Nothing _) ds = (False, ds)
user :: B.ByteString -> Pty -> TChan (Activity Entered) -> TChan (Activity Seen) -> IO ()
user startmsg p ichan ochan = withLogger "debug-me.log" $ \logger -> do
let startact = Activity (Proto (Seen (Val (startmsg <> "\r\n")))) Nothing dummySignature
logger $ ActivitySeen startact
l <- mkActivityLog (ActivitySeen startact) <$> getPOSIXTime
atomically $ writeTChan ochan startact
backlog <- newTVarIO $ Backlog (l :| [])
_ <- sendPtyOutput p ochan backlog logger
`concurrently` sendPtyInput ichan ochan p backlog logger
return ()
-- | Log of recent Activity, with the most recent first.
data Backlog = Backlog (NonEmpty ActivityLog)
deriving (Show)
-- | Forward things written to the Pty out the TChan.
sendPtyOutput :: Pty -> TChan (Activity Seen) -> TVar Backlog -> Logger -> IO ()
sendPtyOutput p ochan backlog logger = go
where
go = do
b <- readPty p
now <- getPOSIXTime
act <- atomically $ do
let seen = Seen (Val b)
sendDeveloper ochan backlog (Proto seen) now
logger $ ActivitySeen act
go
sendDeveloper :: TChan (Activity Seen) -> TVar Backlog -> Proto Seen -> POSIXTime -> STM (Activity Seen)
sendDeveloper ochan backlog pseen now = do
Backlog (bl@(prev :| _)) <- readTVar backlog
let prevhash = loggedHash prev
let act = Activity pseen (Just prevhash) dummySignature
let l = mkActivityLog (ActivitySeen act) now
writeTChan ochan act
writeTVar backlog $ Backlog (l :| toList bl)
return act
-- | Read things to be entered from the TChan, verify if they're legal,
-- and send them to the Pty.
sendPtyInput :: TChan (Activity Entered) -> TChan (Activity Seen) -> Pty -> TVar Backlog -> Logger -> IO ()
sendPtyInput ichan ochan p backlog logger = go
where
go = do
networkDelay
now <- getPOSIXTime
v <- atomically $ do
entered <- readTChan ichan
bl <- readTVar backlog
-- Don't need to retain backlog before the Activity
-- that entered references.
let bl'@(Backlog bll) = reduceBacklog $
truncateBacklog bl entered
if isLegalEntered entered bl'
then do
let l = mkActivityLog (ActivityEntered entered) now
writeTVar backlog (Backlog (l :| toList bll))
return (Right entered)
else do
let reject = Rejected entered
Left <$> sendDeveloper ochan backlog reject now
case v of
Right entered -> do
logger (ActivityEntered entered)
case activity entered of
Proto e -> writePty p (val (enteredData e))
Rejected r -> protocolError $ "User side received a Rejected: " ++ show r
go
Left rejact -> do
logger $ ActivitySeen rejact
go
-- | Truncate the Backlog to remove entries older than the one
-- that the Activity Entered refers to, but only if the referred
-- to Activity is an Activity Seen.
--
-- Once the developer has referred to a given Activity Seen in
-- their Activity Entered, they cannot refer backwards to anything
-- that came before it.
--
-- If the Activity refers to an item not in the backlog, no truncation is
-- done.
truncateBacklog :: Backlog -> Activity Entered -> Backlog
truncateBacklog (Backlog (b :| l)) (Activity _ hp _)
| truncationpoint b = Backlog (b :| [])
| otherwise = Backlog (b :| go [] l)
where
go c [] = reverse c
go c (x:xs)
| truncationpoint x = reverse (x:c)
| otherwise = go (x:c) xs
truncationpoint x@(ActivityLog { loggedActivity = ActivitySeen {}}) = Just (loggedHash x) == hp
truncationpoint _ = False
-- | To avoid DOS attacks that try to fill up the backlog and so use all
-- memory, don't let the backlog contain more than 1000 items, or
-- more than 16 megabytes of total data. (Excluding the most recent
-- item).
reduceBacklog :: Backlog -> Backlog
reduceBacklog (Backlog (b :| l)) = Backlog (b :| go 0 (take 1000 l))
where
go _ [] = []
go n (x:xs)
| n > 16777216 = []
| otherwise = x : go (n + dataSize x) xs
-- | Entered activity is legal when it points to the last Seen activvity,
-- because this guarantees that the person who entered it saw
-- the current state of the system before manipulating it.
--
-- To support typeahead on slow links, some echoData may be provided
-- in the Entered activity. If the Entered activity points
-- to an older Seen activity, then the echoData must match the
-- concatenation of all Seen activities after that one, up to the
-- most recent Seen activity.
isLegalEntered :: Activity Entered -> Backlog -> Bool
isLegalEntered (Activity (Proto entered) hp sig) (Backlog (lastseen :| bl))
| Just (loggedHash lastseen) == hp = True
| B.null (val (echoData entered)) = False -- optimisation
| any (== hp) (map (Just . loggedHash) bl) =
let sincehp = reverse (lastseen : takeWhile (\l -> Just (loggedHash l) /= hp) bl)
in echoData entered == mconcat (map (getseen . loggedActivity) sincehp)
| otherwise = False
where
getseen (ActivitySeen a) = case activity a of
Proto s -> seenData s
_ -> mempty
getseen (ActivityEntered _) = mempty
-- Developer should never send Rejected.
isLegalEntered (Activity (Rejected _) _ _) _ = False
dummySignature :: Signature
dummySignature = Signature mempty
-- | Temporary hack while user and developer share a process.
protocolError :: String -> IO a
protocolError e = do
hPutStrLn stderr e
hFlush stderr
_ <- exitWith (ExitFailure 101)
error e
|