-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProtocol.hs
More file actions
164 lines (139 loc) · 4.81 KB
/
Copy pathProtocol.hs
File metadata and controls
164 lines (139 loc) · 4.81 KB
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
{-# LANGUAGE OverloadedStrings, DeriveDataTypeable #-}
{-# OPTIONS_GHC -fno-warn-unused-do-bind #-}
module Network.HSTorChat.Protocol where
import Control.Applicative
import Control.Concurrent
import qualified Control.Exception as E
import Control.Monad
import Data.Attoparsec.Text
import qualified Data.Char as C
import qualified Data.Map as M
import qualified Data.Text as T
import Data.Typeable
import Data.Word
import Graphics.QML
import Prelude hiding (take)
import qualified Prelude as P
import Network
import Network.Socket
import Network.Socks5
import System.IO
import System.Random
type Onion = T.Text
type Cookie = T.Text
data TorChat = TorChat
{ _myonion :: Onion
, _mystatus :: BuddyStatus
, _buddies :: MVar (M.Map Onion (ObjRef Buddy))
, _pending :: MVar [PendingConnection]
} deriving Typeable
data Buddy = Buddy
{ _onion :: Onion -- ^ Buddy onion address.
, _inConn :: Handle
, _outConn :: Handle
, _cookie :: Cookie -- ^ Cookie sent to buddy.
, _status :: BuddyStatus -- ^ Buddy status
, _msgs :: MVar [ObjRef ChatMsg]
} deriving (Typeable)
data BuddyStatus = Offline
| Handshake
| Available
| Away
| Xa -- ^ Extended Away
deriving (Eq, Read, Show)
data PendingConnection = PendingConnection
{ _pcookie :: Cookie
, _ponion :: Onion
, _pouthandle :: Handle
} deriving Show
data ProtocolMsg = Ping Onion Cookie
| Pong T.Text
| Client T.Text
| Version T.Text
| Status BuddyStatus
| ProfileName
| ProfileText
| AvatarAlpha
| ProfileAvatar
| AddMe
| RemoveMe
| Message T.Text
| Filename
| Filedata
| FiledataOk
| FiledataError
| FileStopSending
| FileStopReceiving
deriving Show
data ChatMsg = ChatMsg
{ text :: T.Text
, buddy :: T.Text
, fromme :: Bool
} deriving (Show, Typeable)
gencookie :: StdGen -> Cookie
gencookie g = T.pack . concatMap show $ P.take 3 (randoms g :: [Word64])
torSocksPort :: PortNumber
torSocksPort = 22209
-- Hidden service port.
hstorchatHSPort :: PortNumber
hstorchatHSPort = 11009
-- Port inside Socks5 tunnel.
hstorchatLocalPort :: PortNumber
hstorchatLocalPort = 22009
hstorchatHost :: String
hstorchatHost = "127.0.0.1"
hstorchatOutConn :: Onion -> IO (Maybe Handle)
hstorchatOutConn onion = do
handle <- E.try $ socksConnectWith hstcConf (T.unpack onion) $ PortNumber hstorchatHSPort
case handle of
Left e -> print (e :: SocksError) >> return Nothing
Right o -> do oHdl <- socketToHandle o ReadWriteMode
hSetBuffering oHdl LineBuffering
return $ Just oHdl
where hstcConf = defaultSocksConf hstorchatHost torSocksPort
-- | Format a message to send over a Socket.
formatMsg :: ProtocolMsg -> String
formatMsg AddMe = "add_me"
formatMsg (Message m) = "message " ++ T.unpack m
formatMsg m = map C.toLower . filter (/= '"') . show $ m
-- | Return a BuddyList given the M.Map
buddylist :: M.Map Onion (ObjRef Buddy) -> [ObjRef Buddy]
buddylist bs = snd . unzip $ M.toList bs
parseResponse :: Parser ProtocolMsg
parseResponse = choice [ parsePingPong
, parseVersion
, parseClient
, parseStatus
, parseAddMe
, parseMessage
]
parsePingPong :: Parser ProtocolMsg
parsePingPong = try parsePing <|> try parsePong
parsePing :: Parser ProtocolMsg
parsePing = do
string "ping"
skipSpace
-- parse onion address.
bdy <- take 16
skipSpace
-- parse secret cookie.
cky <- takeText
return $ Ping bdy cky
parsePong :: Parser ProtocolMsg
parsePong = liftM Pong $ string "pong" >> skipSpace >> takeText
parseVersion :: Parser ProtocolMsg
parseVersion = liftM Version $ string "version" >> skipSpace >> takeText
parseClient :: Parser ProtocolMsg
parseClient = liftM Client $ string "client" >> skipSpace >> takeText
parseStatus :: Parser ProtocolMsg
parseStatus = do
string "status" >> skipSpace
st <- takeText
return $ Status (read $ capitalized (T.unpack st) :: BuddyStatus)
where
capitalized [] = []
capitalized (x:xs) = C.toUpper x : xs
parseAddMe :: Parser ProtocolMsg
parseAddMe = string "add_me" >> skipSpace >> return AddMe
parseMessage :: Parser ProtocolMsg
parseMessage = liftM Message $ string "message" >> skipSpace >> takeText