diff options
Diffstat (limited to 'src/Network')
-rw-r--r-- | src/Network/BitTorrent/PeerWire/ClientInfo.hs | 270 |
1 files changed, 270 insertions, 0 deletions
diff --git a/src/Network/BitTorrent/PeerWire/ClientInfo.hs b/src/Network/BitTorrent/PeerWire/ClientInfo.hs new file mode 100644 index 00000000..f1055343 --- /dev/null +++ b/src/Network/BitTorrent/PeerWire/ClientInfo.hs | |||
@@ -0,0 +1,270 @@ | |||
1 | -- | | ||
2 | -- Copyright : (c) Sam T. 2013 | ||
3 | -- License : MIT | ||
4 | -- Maintainer : pxqr.sta@gmail.com | ||
5 | -- Stability : experimental | ||
6 | -- Portability : portable | ||
7 | -- | ||
8 | -- This module detect client information such as version and | ||
9 | -- implementation that can be later printed in human frienly | ||
10 | -- form. Useful for debugging and logging. | ||
11 | -- | ||
12 | -- See http://bittorrent.org/beps/bep_0020.html for more information. | ||
13 | -- | ||
14 | {-# LANGUAGE OverloadedStrings #-} | ||
15 | module Network.BitTorrent.PeerWire.ClientInfo | ||
16 | ( ClientInfo(..), ClientVersion, ClientImpl | ||
17 | , clientInfo | ||
18 | , ppClientInfo, ppClientVersion, ppClientImpl | ||
19 | |||
20 | -- , mkEnumTyDef, mkPars, nameMap | ||
21 | ) where | ||
22 | |||
23 | import Control.Applicative | ||
24 | --import Data.List | ||
25 | import Data.ByteString (ByteString) | ||
26 | import qualified Data.ByteString.Char8 as BC | ||
27 | import Data.Serialize.Get | ||
28 | |||
29 | import Network.BitTorrent.PeerID | ||
30 | |||
31 | |||
32 | data ClientImpl = | ||
33 | IUnknown | ||
34 | | IAres | ||
35 | | IArctic | ||
36 | | IAvicora | ||
37 | | IBitPump | ||
38 | | IAzureus | ||
39 | | IBitBuddy | ||
40 | | IBitComet | ||
41 | | IBitflu | ||
42 | | IBTG | ||
43 | | IBitRocket | ||
44 | | IBTSlave | ||
45 | | IBittorrentX | ||
46 | | IEnhancedCTorrent | ||
47 | | ICTorrent | ||
48 | | IDelugeTorrent | ||
49 | | IPropagateDataClient | ||
50 | | IEBit | ||
51 | | IElectricSheep | ||
52 | | IFoxTorrent | ||
53 | | IGSTorrent | ||
54 | | IHalite | ||
55 | | IlibHSnetwork_bittorrent | ||
56 | | IHydranode | ||
57 | | IKGet | ||
58 | | IKTorrent | ||
59 | | ILH_ABC | ||
60 | | ILphant | ||
61 | | ILibtorrent | ||
62 | | ILibTorrent | ||
63 | | ILimeWire | ||
64 | | IMonoTorrent | ||
65 | | IMooPolice | ||
66 | | IMiro | ||
67 | | IMoonlightTorrent | ||
68 | | INetTransport | ||
69 | | IPando | ||
70 | | IqBittorrent | ||
71 | | IQQDownload | ||
72 | | IQt4TorrentExample | ||
73 | | IRetriever | ||
74 | | IShareaza | ||
75 | | ISwiftbit | ||
76 | | ISwarmScope | ||
77 | | ISymTorrent | ||
78 | | Isharktorrent | ||
79 | | ITorrentDotNET | ||
80 | | ITransmission | ||
81 | | ITorrentstorm | ||
82 | | ITuoTu | ||
83 | | IuLeecher | ||
84 | | IuTorrent | ||
85 | | IVagaa | ||
86 | | IBitLet | ||
87 | | IFireTorrent | ||
88 | | IXunlei | ||
89 | | IXanTorrent | ||
90 | | IXtorrent | ||
91 | | IZipTorrent | ||
92 | deriving Show | ||
93 | |||
94 | parseImpl :: ByteString -> ClientImpl | ||
95 | parseImpl = f . BC.unpack | ||
96 | where | ||
97 | f "AG" = IAres | ||
98 | f "A~" = IAres | ||
99 | f "AR" = IArctic | ||
100 | f "AV" = IAvicora | ||
101 | f "AX" = IBitPump | ||
102 | f "AZ" = IAzureus | ||
103 | f "BB" = IBitBuddy | ||
104 | f "BC" = IBitComet | ||
105 | f "BF" = IBitflu | ||
106 | f "BG" = IBTG | ||
107 | f "BR" = IBitRocket | ||
108 | f "BS" = IBTSlave | ||
109 | f "BX" = IBittorrentX | ||
110 | f "CD" = IEnhancedCTorrent | ||
111 | f "CT" = ICTorrent | ||
112 | f "DE" = IDelugeTorrent | ||
113 | f "DP" = IPropagateDataClient | ||
114 | f "EB" = IEBit | ||
115 | f "ES" = IElectricSheep | ||
116 | f "FT" = IFoxTorrent | ||
117 | f "GS" = IGSTorrent | ||
118 | f "HL" = IHalite | ||
119 | f "HS" = IlibHSnetwork_bittorrent | ||
120 | f "HN" = IHydranode | ||
121 | f "KG" = IKGet | ||
122 | f "KT" = IKTorrent | ||
123 | f "LH" = ILH_ABC | ||
124 | f "LP" = ILphant | ||
125 | f "LT" = ILibtorrent | ||
126 | f "lt" = ILibTorrent | ||
127 | f "LW" = ILimeWire | ||
128 | f "MO" = IMonoTorrent | ||
129 | f "MP" = IMooPolice | ||
130 | f "MR" = IMiro | ||
131 | f "MT" = IMoonlightTorrent | ||
132 | f "NX" = INetTransport | ||
133 | f "PD" = IPando | ||
134 | f "qB" = IqBittorrent | ||
135 | f "QD" = IQQDownload | ||
136 | f "QT" = IQt4TorrentExample | ||
137 | f "RT" = IRetriever | ||
138 | f "S~" = IShareaza | ||
139 | f "SB" = ISwiftbit | ||
140 | f "SS" = ISwarmScope | ||
141 | f "ST" = ISymTorrent | ||
142 | f "st" = Isharktorrent | ||
143 | f "SZ" = IShareaza | ||
144 | f "TN" = ITorrentDotNET | ||
145 | f "TR" = ITransmission | ||
146 | f "TS" = ITorrentstorm | ||
147 | f "TT" = ITuoTu | ||
148 | f "UL" = IuLeecher | ||
149 | f "UT" = IuTorrent | ||
150 | f "VG" = IVagaa | ||
151 | f "WT" = IBitLet | ||
152 | f "WY" = IFireTorrent | ||
153 | f "XL" = IXunlei | ||
154 | f "XT" = IXanTorrent | ||
155 | f "XX" = IXtorrent | ||
156 | f "ZT" = IZipTorrent | ||
157 | f _ = IUnknown | ||
158 | |||
159 | ppClientImpl :: ClientImpl -> String | ||
160 | ppClientImpl = tail . show | ||
161 | |||
162 | unknownImpl :: ClientImpl | ||
163 | unknownImpl = IUnknown | ||
164 | |||
165 | |||
166 | |||
167 | type ClientVersion = ByteString | ||
168 | |||
169 | ppClientVersion :: ClientVersion -> String | ||
170 | ppClientVersion = BC.unpack | ||
171 | |||
172 | unknownVersion :: ClientVersion | ||
173 | unknownVersion = "0000" | ||
174 | |||
175 | |||
176 | |||
177 | data ClientInfo = ClientInfo { | ||
178 | ciImpl :: ClientImpl | ||
179 | , ciVersion :: ClientVersion | ||
180 | } deriving Show | ||
181 | |||
182 | ppClientInfo :: ClientInfo -> String | ||
183 | ppClientInfo ci = ppClientImpl (ciImpl ci) ++ " version " | ||
184 | ++ ppClientVersion (ciVersion ci) | ||
185 | |||
186 | unknownClient :: ClientInfo | ||
187 | unknownClient = ClientInfo unknownImpl unknownVersion | ||
188 | |||
189 | clientInfo :: PeerID -> ClientInfo | ||
190 | clientInfo pid = either (const unknownClient) id $ runGet getCI (getPeerID pid) | ||
191 | where -- TODO other styles | ||
192 | getCI = do | ||
193 | getWord8 | ||
194 | ClientInfo <$> (parseImpl <$> getByteString 2) <*> getByteString 4 | ||
195 | |||
196 | |||
197 | {- | ||
198 | -- code used for generation; remove it later on | ||
199 | |||
200 | mkEnumTyDef :: NM -> String | ||
201 | mkEnumTyDef = unlines . map (" | I" ++) . nub . map snd | ||
202 | |||
203 | mkPars :: NM -> String | ||
204 | mkPars = unlines . map (\(code, impl) -> " f \"" ++ code ++ "\" = " ++ "I" ++ impl) | ||
205 | |||
206 | type NM = [(String, String)] | ||
207 | nameMap :: NM | ||
208 | nameMap = | ||
209 | [ ("AG", "Ares") | ||
210 | , ("A~", "Ares") | ||
211 | , ("AR", "Arctic") | ||
212 | , ("AV", "Avicora") | ||
213 | , ("AX", "BitPump") | ||
214 | , ("AZ", "Azureus") | ||
215 | , ("BB", "BitBuddy") | ||
216 | , ("BC", "BitComet") | ||
217 | , ("BF", "Bitflu") | ||
218 | , ("BG", "BTG") | ||
219 | , ("BR", "BitRocket") | ||
220 | , ("BS", "BTSlave") | ||
221 | , ("BX", "BittorrentX") | ||
222 | , ("CD", "EnhancedCTorrent") | ||
223 | , ("CT", "CTorrent") | ||
224 | , ("DE", "DelugeTorrent") | ||
225 | , ("DP", "PropagateDataClient") | ||
226 | , ("EB", "EBit") | ||
227 | , ("ES", "ElectricSheep") | ||
228 | , ("FT", "FoxTorrent") | ||
229 | , ("GS", "GSTorrent") | ||
230 | , ("HL", "Halite") | ||
231 | , ("HS", "libHSnetwork_bittorrent") | ||
232 | , ("HN", "Hydranode") | ||
233 | , ("KG", "KGet") | ||
234 | , ("KT", "KTorrent") | ||
235 | , ("LH", "LH_ABC") | ||
236 | , ("LP", "Lphant") | ||
237 | , ("LT", "Libtorrent") | ||
238 | , ("lt", "LibTorrent") | ||
239 | , ("LW", "LimeWire") | ||
240 | , ("MO", "MonoTorrent") | ||
241 | , ("MP", "MooPolice") | ||
242 | , ("MR", "Miro") | ||
243 | , ("MT", "MoonlightTorrent") | ||
244 | , ("NX", "NetTransport") | ||
245 | , ("PD", "Pando") | ||
246 | , ("qB", "qBittorrent") | ||
247 | , ("QD", "QQDownload") | ||
248 | , ("QT", "Qt4TorrentExample") | ||
249 | , ("RT", "Retriever") | ||
250 | , ("S~", "Shareaza") | ||
251 | , ("SB", "Swiftbit") | ||
252 | , ("SS", "SwarmScope") | ||
253 | , ("ST", "SymTorrent") | ||
254 | , ("st", "sharktorrent") | ||
255 | , ("SZ", "Shareaza") | ||
256 | , ("TN", "TorrentDotNET") | ||
257 | , ("TR", "Transmission") | ||
258 | , ("TS", "Torrentstorm") | ||
259 | , ("TT", "TuoTu") | ||
260 | , ("UL", "uLeecher") | ||
261 | , ("UT", "uTorrent") | ||
262 | , ("VG", "Vagaa") | ||
263 | , ("WT", "BitLet") | ||
264 | , ("WY", "FireTorrent") | ||
265 | , ("XL", "Xunlei") | ||
266 | , ("XT", "XanTorrent") | ||
267 | , ("XX", "Xtorrent") | ||
268 | , ("ZT", "ZipTorrent") | ||
269 | ] | ||
270 | -} \ No newline at end of file | ||