diff options
Diffstat (limited to 'src/Network/BitTorrent')
-rw-r--r-- | src/Network/BitTorrent/Core/Fingerprint.hs | 283 | ||||
-rw-r--r-- | src/Network/BitTorrent/Core/PeerId.hs | 2 |
2 files changed, 284 insertions, 1 deletions
diff --git a/src/Network/BitTorrent/Core/Fingerprint.hs b/src/Network/BitTorrent/Core/Fingerprint.hs new file mode 100644 index 00000000..e2fbb777 --- /dev/null +++ b/src/Network/BitTorrent/Core/Fingerprint.hs | |||
@@ -0,0 +1,283 @@ | |||
1 | -- | | ||
2 | -- Copyright : (c) Sam Truzjan 2013 | ||
3 | -- License : BSD3 | ||
4 | -- Maintainer : pxqr.sta@gmail.com | ||
5 | -- Stability : experimental | ||
6 | -- Portability : portable | ||
7 | -- | ||
8 | -- 'ClientInfo' is used to identify the client implementation and | ||
9 | -- version which also contained in 'Peer'. For exsample first 6 | ||
10 | -- bytes of peer id of this this library are @-HS0100-@ while for | ||
11 | -- mainline we have @M4-3-6--@. We could extract this info and | ||
12 | -- print in human-friendly form: this is useful for debugging and | ||
13 | -- logging. | ||
14 | -- | ||
15 | -- For more information see: | ||
16 | -- <http://bittorrent.org/beps/bep_0020.html> | ||
17 | -- | ||
18 | -- | ||
19 | -- NOTE: Do /not/ use this information to control client | ||
20 | -- capabilities (such as supported enchancements), this should be | ||
21 | -- done using 'Network.BitTorrent.Extension'! | ||
22 | -- | ||
23 | {-# OPTIONS -fno-warn-orphans #-} | ||
24 | module Network.BitTorrent.Core.Fingerprint | ||
25 | ( ClientImpl (..) | ||
26 | , ClientInfo (..) | ||
27 | , libClientInfo | ||
28 | ) where | ||
29 | |||
30 | import Data.Default | ||
31 | import Data.List as L | ||
32 | import Data.List.Split as L | ||
33 | import Data.String | ||
34 | import Data.Version | ||
35 | import Text.PrettyPrint hiding ((<>)) | ||
36 | import Text.PrettyPrint.Class | ||
37 | import Text.Read (readMaybe) | ||
38 | -- import Paths_bittorrent (version) | ||
39 | |||
40 | -- TODO FIXME | ||
41 | version :: Version | ||
42 | version = Version [0, 0, 0, 3] [] | ||
43 | |||
44 | -- | List of registered client versions + 'IlibHSbittorrent' (this | ||
45 | -- package) + 'IUnknown' (for not recognized software). All names are | ||
46 | -- prefixed by \"I\" because some of them starts from lowercase letter | ||
47 | -- but that is not a valid Haskell constructor name. | ||
48 | -- | ||
49 | data ClientImpl = | ||
50 | IUnknown | ||
51 | |||
52 | | IMainline | ||
53 | |||
54 | | IABC | ||
55 | | IOspreyPermaseed | ||
56 | | IBTQueue | ||
57 | | ITribler | ||
58 | | IShadow | ||
59 | | IBitTornado | ||
60 | |||
61 | -- UPnP(!) Bit Torrent !??? | ||
62 | -- 'U' - UPnP NAT Bit Torrent | ||
63 | | IBitLord | ||
64 | | IOpera | ||
65 | | IMLdonkey | ||
66 | |||
67 | | IAres | ||
68 | | IArctic | ||
69 | | IAvicora | ||
70 | | IBitPump | ||
71 | | IAzureus | ||
72 | | IBitBuddy | ||
73 | | IBitComet | ||
74 | | IBitflu | ||
75 | | IBTG | ||
76 | | IBitRocket | ||
77 | | IBTSlave | ||
78 | | IBittorrentX | ||
79 | | IEnhancedCTorrent | ||
80 | | ICTorrent | ||
81 | | IDelugeTorrent | ||
82 | | IPropagateDataClient | ||
83 | | IEBit | ||
84 | | IElectricSheep | ||
85 | | IFoxTorrent | ||
86 | | IGSTorrent | ||
87 | | IHalite | ||
88 | | IlibHSbittorrent | ||
89 | | IHydranode | ||
90 | | IKGet | ||
91 | | IKTorrent | ||
92 | | ILH_ABC | ||
93 | | ILphant | ||
94 | | ILibtorrent | ||
95 | | ILibTorrent | ||
96 | | ILimeWire | ||
97 | | IMonoTorrent | ||
98 | | IMooPolice | ||
99 | | IMiro | ||
100 | | IMoonlightTorrent | ||
101 | | INetTransport | ||
102 | | IPando | ||
103 | | IqBittorrent | ||
104 | | IQQDownload | ||
105 | | IQt4TorrentExample | ||
106 | | IRetriever | ||
107 | | IShareaza | ||
108 | | ISwiftbit | ||
109 | | ISwarmScope | ||
110 | | ISymTorrent | ||
111 | | Isharktorrent | ||
112 | | ITorrentDotNET | ||
113 | | ITransmission | ||
114 | | ITorrentstorm | ||
115 | | ITuoTu | ||
116 | | IuLeecher | ||
117 | | IuTorrent | ||
118 | | IVagaa | ||
119 | | IBitLet | ||
120 | | IFireTorrent | ||
121 | | IXunlei | ||
122 | | IXanTorrent | ||
123 | | IXtorrent | ||
124 | | IZipTorrent | ||
125 | deriving (Show, Eq, Ord, Enum, Bounded) | ||
126 | |||
127 | -- | Used to represent a not recognized implementation | ||
128 | instance Default ClientImpl where | ||
129 | def = IUnknown | ||
130 | {-# INLINE def #-} | ||
131 | |||
132 | -- | Example: @\"BitLet\" == 'IBitLet'@ | ||
133 | instance IsString ClientImpl where | ||
134 | fromString str | ||
135 | | Just impl <- L.lookup str alist = impl | ||
136 | | otherwise = error $ "fromString: not recognized " ++ str | ||
137 | where | ||
138 | alist = L.map mk [minBound..maxBound] | ||
139 | mk x = (L.tail $ show x, x) | ||
140 | |||
141 | -- | Example: @pretty 'IBitLet' == \"IBitLet\"@ | ||
142 | instance Pretty ClientImpl where | ||
143 | pretty = text . L.tail . show | ||
144 | |||
145 | -- | Just the '0' version. | ||
146 | instance Default Version where | ||
147 | def = Version [0] [] | ||
148 | {-# INLINE def #-} | ||
149 | |||
150 | -- | For dot delimited version strings. | ||
151 | -- Example: @fromString \"0.1.0.2\" == Version [0, 1, 0, 2]@ | ||
152 | -- | ||
153 | instance IsString Version where | ||
154 | fromString str | ||
155 | | Just nums <- chunkNums str = Version nums [] | ||
156 | | otherwise = error $ "fromString: invalid version string " ++ str | ||
157 | where | ||
158 | chunkNums = sequence . L.map readMaybe . L.linesBy ('.' ==) | ||
159 | |||
160 | instance Pretty Version where | ||
161 | pretty = text . showVersion | ||
162 | |||
163 | -- | The all sensible infomation that can be obtained from a peer | ||
164 | -- identifier or torrent /createdBy/ field. | ||
165 | data ClientInfo = ClientInfo { | ||
166 | ciImpl :: ClientImpl | ||
167 | , ciVersion :: Version | ||
168 | } deriving (Show, Eq, Ord) | ||
169 | |||
170 | -- | Unrecognized client implementation. | ||
171 | instance Default ClientInfo where | ||
172 | def = ClientInfo def def | ||
173 | {-# INLINE def #-} | ||
174 | |||
175 | -- | Example: @\"BitComet-1.2\" == ClientInfo IBitComet (Version [1, 2] [])@ | ||
176 | instance IsString ClientInfo where | ||
177 | fromString str | ||
178 | | _ : ver <- _ver = ClientInfo (fromString impl) (fromString ver) | ||
179 | | otherwise = error $ "fromString: invalid client info string" ++ str | ||
180 | where | ||
181 | (impl, _ver) = L.span ((/=) '-') str | ||
182 | |||
183 | instance Pretty ClientInfo where | ||
184 | pretty ClientInfo {..} = pretty ciImpl <+> "version" <+> pretty ciVersion | ||
185 | |||
186 | -- | Client info of this (the bittorrent library) package. Normally, | ||
187 | -- applications should introduce its own idenitifiers, otherwise they | ||
188 | -- can use 'libClientInfo' value. | ||
189 | -- | ||
190 | libClientInfo :: ClientInfo | ||
191 | libClientInfo = ClientInfo IlibHSbittorrent version | ||
192 | |||
193 | {----------------------------------------------------------------------- | ||
194 | -- For torrent file | ||
195 | -----------------------------------------------------------------------} | ||
196 | -- TODO collect information about createdBy torrent field | ||
197 | {- | ||
198 | renderImpl :: ClientImpl -> Text | ||
199 | renderImpl = T.pack . L.tail . show | ||
200 | |||
201 | renderVersion :: Version -> Text | ||
202 | renderVersion = undefined | ||
203 | |||
204 | renderClientInfo :: ClientInfo -> Text | ||
205 | renderClientInfo ClientInfo {..} = renderImpl ciImpl <> "/" <> renderVersion ciVersion | ||
206 | |||
207 | parseClientInfo :: Text -> ClientImpl | ||
208 | parseClientInfo t = undefined | ||
209 | -} | ||
210 | {- | ||
211 | -- code used for generation; remove it later on | ||
212 | |||
213 | mkEnumTyDef :: NM -> String | ||
214 | mkEnumTyDef = unlines . map (" | I" ++) . nub . map snd | ||
215 | |||
216 | mkPars :: NM -> String | ||
217 | mkPars = unlines . map (\(code, impl) -> " f \"" ++ code ++ "\" = " ++ "I" ++ impl) | ||
218 | |||
219 | type NM = [(String, String)] | ||
220 | nameMap :: NM | ||
221 | nameMap = | ||
222 | [ ("AG", "Ares") | ||
223 | , ("A~", "Ares") | ||
224 | , ("AR", "Arctic") | ||
225 | , ("AV", "Avicora") | ||
226 | , ("AX", "BitPump") | ||
227 | , ("AZ", "Azureus") | ||
228 | , ("BB", "BitBuddy") | ||
229 | , ("BC", "BitComet") | ||
230 | , ("BF", "Bitflu") | ||
231 | , ("BG", "BTG") | ||
232 | , ("BR", "BitRocket") | ||
233 | , ("BS", "BTSlave") | ||
234 | , ("BX", "BittorrentX") | ||
235 | , ("CD", "EnhancedCTorrent") | ||
236 | , ("CT", "CTorrent") | ||
237 | , ("DE", "DelugeTorrent") | ||
238 | , ("DP", "PropagateDataClient") | ||
239 | , ("EB", "EBit") | ||
240 | , ("ES", "ElectricSheep") | ||
241 | , ("FT", "FoxTorrent") | ||
242 | , ("GS", "GSTorrent") | ||
243 | , ("HL", "Halite") | ||
244 | , ("HS", "libHSnetwork_bittorrent") | ||
245 | , ("HN", "Hydranode") | ||
246 | , ("KG", "KGet") | ||
247 | , ("KT", "KTorrent") | ||
248 | , ("LH", "LH_ABC") | ||
249 | , ("LP", "Lphant") | ||
250 | , ("LT", "Libtorrent") | ||
251 | , ("lt", "LibTorrent") | ||
252 | , ("LW", "LimeWire") | ||
253 | , ("MO", "MonoTorrent") | ||
254 | , ("MP", "MooPolice") | ||
255 | , ("MR", "Miro") | ||
256 | , ("MT", "MoonlightTorrent") | ||
257 | , ("NX", "NetTransport") | ||
258 | , ("PD", "Pando") | ||
259 | , ("qB", "qBittorrent") | ||
260 | , ("QD", "QQDownload") | ||
261 | , ("QT", "Qt4TorrentExample") | ||
262 | , ("RT", "Retriever") | ||
263 | , ("S~", "Shareaza") | ||
264 | , ("SB", "Swiftbit") | ||
265 | , ("SS", "SwarmScope") | ||
266 | , ("ST", "SymTorrent") | ||
267 | , ("st", "sharktorrent") | ||
268 | , ("SZ", "Shareaza") | ||
269 | , ("TN", "TorrentDotNET") | ||
270 | , ("TR", "Transmission") | ||
271 | , ("TS", "Torrentstorm") | ||
272 | , ("TT", "TuoTu") | ||
273 | , ("UL", "uLeecher") | ||
274 | , ("UT", "uTorrent") | ||
275 | , ("VG", "Vagaa") | ||
276 | , ("WT", "BitLet") | ||
277 | , ("WY", "FireTorrent") | ||
278 | , ("XL", "Xunlei") | ||
279 | , ("XT", "XanTorrent") | ||
280 | , ("XX", "Xtorrent") | ||
281 | , ("ZT", "ZipTorrent") | ||
282 | ] | ||
283 | -} | ||
diff --git a/src/Network/BitTorrent/Core/PeerId.hs b/src/Network/BitTorrent/Core/PeerId.hs index 1b4409a8..8deb854a 100644 --- a/src/Network/BitTorrent/Core/PeerId.hs +++ b/src/Network/BitTorrent/Core/PeerId.hs | |||
@@ -62,7 +62,7 @@ import Text.PrettyPrint hiding ((<>)) | |||
62 | import Text.PrettyPrint.Class | 62 | import Text.PrettyPrint.Class |
63 | import Text.Read (readMaybe) | 63 | import Text.Read (readMaybe) |
64 | 64 | ||
65 | import Data.Torrent.Client | 65 | import Network.BitTorrent.Core.Fingerprint |
66 | 66 | ||
67 | -- TODO use unpacked Word160 form (length is known statically) | 67 | -- TODO use unpacked Word160 form (length is known statically) |
68 | 68 | ||