-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.hs
More file actions
91 lines (80 loc) · 2.73 KB
/
cli.hs
File metadata and controls
91 lines (80 loc) · 2.73 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
import qualified Data.ByteString.Lazy as BL
import qualified Data.ByteString as B
import Data.Binary.Put
import Data.Binary.Get
import Data.Binary
import Data.Int
import Data.Time.Clock.POSIX
import System.Locale
import Data.Ratio
import System.Entropy
import Data.Digest.CRC32
import Network.Simple.TCP hiding (recv)
import Network.Socket.ByteString.Lazy
import TLData
data TcpFullPack = TcpFullPack
{ len :: !Word32
, seqNum :: !Word32
, body :: ResPQ
, crc :: !Int32
} deriving (Show)
data ResPQ = ResPQ
{ auth_key_id :: !Word64
, message_id :: !Word64
, message_length :: !Word32
, constructor :: !Word32
, nonce :: B.ByteString
, server_nonce :: B.ByteString
, pq :: Integer
, fingerprints :: [Integer]
} deriving (Show)
req_pq :: B.ByteString -> Put
req_pq nonce = do
putInt32le 1615239032
putByteString $ nonce
wrap :: Integer -> B.ByteString -> Put
wrap exactUnixTime bytes = do
putInt64le 0
putInt64le $ fromIntegral $ exactUnixTime
putInt32le $ fromIntegral $ B.length bytes
putByteString $ bytes
unwrap :: Get TcpFullPack
unwrap = do
len <- getWord32le
seqNum <- getWord32le
body <- getLazyByteString $ fromIntegral $ len - 12
crc <- getInt32le
return $! TcpFullPack len seqNum (runGet unwrapResPq body) crc
unwrapResPq :: Get ResPQ
unwrapResPq = do
auth_key_id <- getWord64le
message_id <- getWord64le
message_length <- getWord32le
constructor <- getWord32le
nonce <- getByteString $ fromIntegral $ 16
server_nonce <- getByteString $ fromIntegral $ 16
pq <- getVarInteger
fingerprints <- getVectorLong
return $! ResPQ auth_key_id message_id message_length constructor nonce server_nonce pq fingerprints
calcCrc32 :: B.ByteString -> Put
calcCrc32 bytes = do
putByteString $ bytes
putWord32le $ crc32 bytes
tcp_pack_full :: Integer -> B.ByteString -> Put
tcp_pack_full seqNum bytes = calcCrc32 $ BL.toStrict . runPut $ do
putInt32le $ fromIntegral $ 12 + B.length bytes
putInt32le $ fromIntegral $ seqNum
putByteString $ bytes
main :: IO ()
-- main = do
-- time <- getPOSIXTime
-- key <- getEntropy 16
-- BL.putStr $ runPut $ tcp_pack_full 0 $ (BL.toStrict . runPut $ wrap (numerator . toRational . (* 4096000000) $ time) (BL.toStrict . runPut $ req_pq key))
main = do
connect "149.154.167.40" "443" $ \(connectionSocket, remoteAddr) -> do
time <- getPOSIXTime
key <- getEntropy 16
let req = runPut $ tcp_pack_full 0 $ (BL.toStrict . runPut $ wrap (numerator . toRational . (* 4096000000) $ time) (BL.toStrict . runPut $ req_pq key))
sendLazy connectionSocket req
resp <- recv connectionSocket 100000
putStrLn $ show $ runGet unwrap resp