├── .github └── workflows │ └── rust.yml ├── .gitignore ├── Cargo.lock ├── Cargo.toml ├── LICENSE ├── README.md └── src ├── cli.rs ├── gateway.rs └── main.rs /.github/workflows/rust.yml: -------------------------------------------------------------------------------- 1 | name: Rust 2 | 3 | on: 4 | push: 5 | branches: [ "main" ] 6 | pull_request: 7 | branches: [ "main" ] 8 | 9 | env: 10 | CARGO_TERM_COLOR: always 11 | 12 | jobs: 13 | build: 14 | 15 | runs-on: ubuntu-latest 16 | 17 | steps: 18 | - uses: actions/checkout@v3 19 | - name: Check 20 | run: cargo check --verbose # makes it fail faster if it must fail 21 | - name: Build 22 | run: cargo build --verbose 23 | - name: Run tests 24 | run: cargo test --verbose 25 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | nohup.out 3 | dsicord_data/ 4 | *.url 5 | *.dht 6 | *.jsonl 7 | *.json 8 | *.mitmdump 9 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "ahash" 7 | version = "0.7.8" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "891477e0c6a8957309ee5c45a6368af3ae14bb510732d2684ffa19af310920f9" 10 | dependencies = [ 11 | "getrandom", 12 | "once_cell", 13 | "version_check", 14 | ] 15 | 16 | [[package]] 17 | name = "aho-corasick" 18 | version = "1.1.3" 19 | source = "registry+https://github.com/rust-lang/crates.io-index" 20 | checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" 21 | dependencies = [ 22 | "memchr", 23 | ] 24 | 25 | [[package]] 26 | name = "atty" 27 | version = "0.2.14" 28 | source = "registry+https://github.com/rust-lang/crates.io-index" 29 | checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" 30 | dependencies = [ 31 | "hermit-abi", 32 | "libc", 33 | "winapi", 34 | ] 35 | 36 | [[package]] 37 | name = "autocfg" 38 | version = "1.3.0" 39 | source = "registry+https://github.com/rust-lang/crates.io-index" 40 | checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" 41 | 42 | [[package]] 43 | name = "bitflags" 44 | version = "1.3.2" 45 | source = "registry+https://github.com/rust-lang/crates.io-index" 46 | checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" 47 | 48 | [[package]] 49 | name = "cc" 50 | version = "1.0.104" 51 | source = "registry+https://github.com/rust-lang/crates.io-index" 52 | checksum = "74b6a57f98764a267ff415d50a25e6e166f3831a5071af4995296ea97d210490" 53 | 54 | [[package]] 55 | name = "cfg-if" 56 | version = "1.0.0" 57 | source = "registry+https://github.com/rust-lang/crates.io-index" 58 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 59 | 60 | [[package]] 61 | name = "clap" 62 | version = "3.2.8" 63 | source = "registry+https://github.com/rust-lang/crates.io-index" 64 | checksum = "190814073e85d238f31ff738fcb0bf6910cedeb73376c87cd69291028966fd83" 65 | dependencies = [ 66 | "atty", 67 | "bitflags", 68 | "clap_derive", 69 | "clap_lex", 70 | "indexmap", 71 | "once_cell", 72 | "strsim", 73 | "termcolor", 74 | "textwrap", 75 | ] 76 | 77 | [[package]] 78 | name = "clap_derive" 79 | version = "3.2.7" 80 | source = "registry+https://github.com/rust-lang/crates.io-index" 81 | checksum = "759bf187376e1afa7b85b959e6a664a3e7a95203415dba952ad19139e798f902" 82 | dependencies = [ 83 | "heck", 84 | "proc-macro-error", 85 | "proc-macro2", 86 | "quote", 87 | "syn", 88 | ] 89 | 90 | [[package]] 91 | name = "clap_lex" 92 | version = "0.2.4" 93 | source = "registry+https://github.com/rust-lang/crates.io-index" 94 | checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5" 95 | dependencies = [ 96 | "os_str_bytes", 97 | ] 98 | 99 | [[package]] 100 | name = "dht_urls_extractor" 101 | version = "1.3.0" 102 | dependencies = [ 103 | "clap", 104 | "regex", 105 | "rusqlite", 106 | "tinyjson", 107 | ] 108 | 109 | [[package]] 110 | name = "fallible-iterator" 111 | version = "0.2.0" 112 | source = "registry+https://github.com/rust-lang/crates.io-index" 113 | checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7" 114 | 115 | [[package]] 116 | name = "fallible-streaming-iterator" 117 | version = "0.1.9" 118 | source = "registry+https://github.com/rust-lang/crates.io-index" 119 | checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a" 120 | 121 | [[package]] 122 | name = "getrandom" 123 | version = "0.2.15" 124 | source = "registry+https://github.com/rust-lang/crates.io-index" 125 | checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" 126 | dependencies = [ 127 | "cfg-if", 128 | "libc", 129 | "wasi", 130 | ] 131 | 132 | [[package]] 133 | name = "hashbrown" 134 | version = "0.11.2" 135 | source = "registry+https://github.com/rust-lang/crates.io-index" 136 | checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" 137 | dependencies = [ 138 | "ahash", 139 | ] 140 | 141 | [[package]] 142 | name = "hashbrown" 143 | version = "0.12.3" 144 | source = "registry+https://github.com/rust-lang/crates.io-index" 145 | checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" 146 | 147 | [[package]] 148 | name = "hashlink" 149 | version = "0.7.0" 150 | source = "registry+https://github.com/rust-lang/crates.io-index" 151 | checksum = "7249a3129cbc1ffccd74857f81464a323a152173cdb134e0fd81bc803b29facf" 152 | dependencies = [ 153 | "hashbrown 0.11.2", 154 | ] 155 | 156 | [[package]] 157 | name = "heck" 158 | version = "0.4.1" 159 | source = "registry+https://github.com/rust-lang/crates.io-index" 160 | checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" 161 | 162 | [[package]] 163 | name = "hermit-abi" 164 | version = "0.1.19" 165 | source = "registry+https://github.com/rust-lang/crates.io-index" 166 | checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" 167 | dependencies = [ 168 | "libc", 169 | ] 170 | 171 | [[package]] 172 | name = "indexmap" 173 | version = "1.9.3" 174 | source = "registry+https://github.com/rust-lang/crates.io-index" 175 | checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" 176 | dependencies = [ 177 | "autocfg", 178 | "hashbrown 0.12.3", 179 | ] 180 | 181 | [[package]] 182 | name = "libc" 183 | version = "0.2.155" 184 | source = "registry+https://github.com/rust-lang/crates.io-index" 185 | checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" 186 | 187 | [[package]] 188 | name = "libsqlite3-sys" 189 | version = "0.23.2" 190 | source = "registry+https://github.com/rust-lang/crates.io-index" 191 | checksum = "d2cafc7c74096c336d9d27145f7ebd4f4b6f95ba16aa5a282387267e6925cb58" 192 | dependencies = [ 193 | "cc", 194 | "pkg-config", 195 | "vcpkg", 196 | ] 197 | 198 | [[package]] 199 | name = "memchr" 200 | version = "2.7.4" 201 | source = "registry+https://github.com/rust-lang/crates.io-index" 202 | checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" 203 | 204 | [[package]] 205 | name = "once_cell" 206 | version = "1.19.0" 207 | source = "registry+https://github.com/rust-lang/crates.io-index" 208 | checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" 209 | 210 | [[package]] 211 | name = "os_str_bytes" 212 | version = "6.6.1" 213 | source = "registry+https://github.com/rust-lang/crates.io-index" 214 | checksum = "e2355d85b9a3786f481747ced0e0ff2ba35213a1f9bd406ed906554d7af805a1" 215 | 216 | [[package]] 217 | name = "pkg-config" 218 | version = "0.3.30" 219 | source = "registry+https://github.com/rust-lang/crates.io-index" 220 | checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec" 221 | 222 | [[package]] 223 | name = "proc-macro-error" 224 | version = "1.0.4" 225 | source = "registry+https://github.com/rust-lang/crates.io-index" 226 | checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" 227 | dependencies = [ 228 | "proc-macro-error-attr", 229 | "proc-macro2", 230 | "quote", 231 | "syn", 232 | "version_check", 233 | ] 234 | 235 | [[package]] 236 | name = "proc-macro-error-attr" 237 | version = "1.0.4" 238 | source = "registry+https://github.com/rust-lang/crates.io-index" 239 | checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" 240 | dependencies = [ 241 | "proc-macro2", 242 | "quote", 243 | "version_check", 244 | ] 245 | 246 | [[package]] 247 | name = "proc-macro2" 248 | version = "1.0.86" 249 | source = "registry+https://github.com/rust-lang/crates.io-index" 250 | checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" 251 | dependencies = [ 252 | "unicode-ident", 253 | ] 254 | 255 | [[package]] 256 | name = "quote" 257 | version = "1.0.36" 258 | source = "registry+https://github.com/rust-lang/crates.io-index" 259 | checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" 260 | dependencies = [ 261 | "proc-macro2", 262 | ] 263 | 264 | [[package]] 265 | name = "regex" 266 | version = "1.10.5" 267 | source = "registry+https://github.com/rust-lang/crates.io-index" 268 | checksum = "b91213439dad192326a0d7c6ee3955910425f441d7038e0d6933b0aec5c4517f" 269 | dependencies = [ 270 | "aho-corasick", 271 | "memchr", 272 | "regex-automata", 273 | "regex-syntax", 274 | ] 275 | 276 | [[package]] 277 | name = "regex-automata" 278 | version = "0.4.7" 279 | source = "registry+https://github.com/rust-lang/crates.io-index" 280 | checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df" 281 | dependencies = [ 282 | "aho-corasick", 283 | "memchr", 284 | "regex-syntax", 285 | ] 286 | 287 | [[package]] 288 | name = "regex-syntax" 289 | version = "0.8.4" 290 | source = "registry+https://github.com/rust-lang/crates.io-index" 291 | checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" 292 | 293 | [[package]] 294 | name = "rusqlite" 295 | version = "0.26.3" 296 | source = "registry+https://github.com/rust-lang/crates.io-index" 297 | checksum = "4ba4d3462c8b2e4d7f4fcfcf2b296dc6b65404fbbc7b63daa37fd485c149daf7" 298 | dependencies = [ 299 | "bitflags", 300 | "fallible-iterator", 301 | "fallible-streaming-iterator", 302 | "hashlink", 303 | "libsqlite3-sys", 304 | "memchr", 305 | "smallvec", 306 | ] 307 | 308 | [[package]] 309 | name = "smallvec" 310 | version = "1.13.2" 311 | source = "registry+https://github.com/rust-lang/crates.io-index" 312 | checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" 313 | 314 | [[package]] 315 | name = "strsim" 316 | version = "0.10.0" 317 | source = "registry+https://github.com/rust-lang/crates.io-index" 318 | checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" 319 | 320 | [[package]] 321 | name = "syn" 322 | version = "1.0.109" 323 | source = "registry+https://github.com/rust-lang/crates.io-index" 324 | checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" 325 | dependencies = [ 326 | "proc-macro2", 327 | "quote", 328 | "unicode-ident", 329 | ] 330 | 331 | [[package]] 332 | name = "termcolor" 333 | version = "1.4.1" 334 | source = "registry+https://github.com/rust-lang/crates.io-index" 335 | checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" 336 | dependencies = [ 337 | "winapi-util", 338 | ] 339 | 340 | [[package]] 341 | name = "textwrap" 342 | version = "0.15.2" 343 | source = "registry+https://github.com/rust-lang/crates.io-index" 344 | checksum = "b7b3e525a49ec206798b40326a44121291b530c963cfb01018f63e135bac543d" 345 | 346 | [[package]] 347 | name = "tinyjson" 348 | version = "2.5.1" 349 | source = "registry+https://github.com/rust-lang/crates.io-index" 350 | checksum = "9ab95735ea2c8fd51154d01e39cf13912a78071c2d89abc49a7ef102a7dd725a" 351 | 352 | [[package]] 353 | name = "unicode-ident" 354 | version = "1.0.12" 355 | source = "registry+https://github.com/rust-lang/crates.io-index" 356 | checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" 357 | 358 | [[package]] 359 | name = "vcpkg" 360 | version = "0.2.15" 361 | source = "registry+https://github.com/rust-lang/crates.io-index" 362 | checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" 363 | 364 | [[package]] 365 | name = "version_check" 366 | version = "0.9.4" 367 | source = "registry+https://github.com/rust-lang/crates.io-index" 368 | checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" 369 | 370 | [[package]] 371 | name = "wasi" 372 | version = "0.11.0+wasi-snapshot-preview1" 373 | source = "registry+https://github.com/rust-lang/crates.io-index" 374 | checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" 375 | 376 | [[package]] 377 | name = "winapi" 378 | version = "0.3.9" 379 | source = "registry+https://github.com/rust-lang/crates.io-index" 380 | checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" 381 | dependencies = [ 382 | "winapi-i686-pc-windows-gnu", 383 | "winapi-x86_64-pc-windows-gnu", 384 | ] 385 | 386 | [[package]] 387 | name = "winapi-i686-pc-windows-gnu" 388 | version = "0.4.0" 389 | source = "registry+https://github.com/rust-lang/crates.io-index" 390 | checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" 391 | 392 | [[package]] 393 | name = "winapi-util" 394 | version = "0.1.8" 395 | source = "registry+https://github.com/rust-lang/crates.io-index" 396 | checksum = "4d4cc384e1e73b93bafa6fb4f1df8c41695c8a91cf9c4c64358067d15a7b6c6b" 397 | dependencies = [ 398 | "windows-sys", 399 | ] 400 | 401 | [[package]] 402 | name = "winapi-x86_64-pc-windows-gnu" 403 | version = "0.4.0" 404 | source = "registry+https://github.com/rust-lang/crates.io-index" 405 | checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" 406 | 407 | [[package]] 408 | name = "windows-sys" 409 | version = "0.52.0" 410 | source = "registry+https://github.com/rust-lang/crates.io-index" 411 | checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" 412 | dependencies = [ 413 | "windows-targets", 414 | ] 415 | 416 | [[package]] 417 | name = "windows-targets" 418 | version = "0.52.5" 419 | source = "registry+https://github.com/rust-lang/crates.io-index" 420 | checksum = "6f0713a46559409d202e70e28227288446bf7841d3211583a4b53e3f6d96e7eb" 421 | dependencies = [ 422 | "windows_aarch64_gnullvm", 423 | "windows_aarch64_msvc", 424 | "windows_i686_gnu", 425 | "windows_i686_gnullvm", 426 | "windows_i686_msvc", 427 | "windows_x86_64_gnu", 428 | "windows_x86_64_gnullvm", 429 | "windows_x86_64_msvc", 430 | ] 431 | 432 | [[package]] 433 | name = "windows_aarch64_gnullvm" 434 | version = "0.52.5" 435 | source = "registry+https://github.com/rust-lang/crates.io-index" 436 | checksum = "7088eed71e8b8dda258ecc8bac5fb1153c5cffaf2578fc8ff5d61e23578d3263" 437 | 438 | [[package]] 439 | name = "windows_aarch64_msvc" 440 | version = "0.52.5" 441 | source = "registry+https://github.com/rust-lang/crates.io-index" 442 | checksum = "9985fd1504e250c615ca5f281c3f7a6da76213ebd5ccc9561496568a2752afb6" 443 | 444 | [[package]] 445 | name = "windows_i686_gnu" 446 | version = "0.52.5" 447 | source = "registry+https://github.com/rust-lang/crates.io-index" 448 | checksum = "88ba073cf16d5372720ec942a8ccbf61626074c6d4dd2e745299726ce8b89670" 449 | 450 | [[package]] 451 | name = "windows_i686_gnullvm" 452 | version = "0.52.5" 453 | source = "registry+https://github.com/rust-lang/crates.io-index" 454 | checksum = "87f4261229030a858f36b459e748ae97545d6f1ec60e5e0d6a3d32e0dc232ee9" 455 | 456 | [[package]] 457 | name = "windows_i686_msvc" 458 | version = "0.52.5" 459 | source = "registry+https://github.com/rust-lang/crates.io-index" 460 | checksum = "db3c2bf3d13d5b658be73463284eaf12830ac9a26a90c717b7f771dfe97487bf" 461 | 462 | [[package]] 463 | name = "windows_x86_64_gnu" 464 | version = "0.52.5" 465 | source = "registry+https://github.com/rust-lang/crates.io-index" 466 | checksum = "4e4246f76bdeff09eb48875a0fd3e2af6aada79d409d33011886d3e1581517d9" 467 | 468 | [[package]] 469 | name = "windows_x86_64_gnullvm" 470 | version = "0.52.5" 471 | source = "registry+https://github.com/rust-lang/crates.io-index" 472 | checksum = "852298e482cd67c356ddd9570386e2862b5673c85bd5f88df9ab6802b334c596" 473 | 474 | [[package]] 475 | name = "windows_x86_64_msvc" 476 | version = "0.52.5" 477 | source = "registry+https://github.com/rust-lang/crates.io-index" 478 | checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" 479 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "dht_urls_extractor" 3 | version = "1.3.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | clap = {version = "=3.2.8", features = ["derive"]} 10 | regex = "1.5.4" 11 | rusqlite = { version = "0.26.3", features = ["bundled"] } 12 | tinyjson = "2.3.0" 13 | 14 | [profile.release] 15 | opt-level = 3 16 | lto = true 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # discord-urls-extractor 2 | 3 | ### Maintainence Level: Sad :( 4 | 5 | I haven't made Discord dumps in quite some time, so I haven't needed to update this. PRs are still welcome! 6 | 7 | ### Information 8 | 9 | Created for injesting URLs from DHT scrapes (and now discard2 ones, too!) (oh wait: now DiscordChatExporter ones, three!) into the Archiveteam URLs project. But you can use the URL lists for whatever you please! (Except DDoSing. Please don't do that.) 10 | 11 | Might not extract all URLs correctly. [#8](https://github.com/TheTechRobo/discordhistorytracker-urls-extractor/pull/8) improved on this, though. 12 | 13 | :warning: Every time you run the script, it will overwrite `urls.url` with the URLs from the current script. A good idea is to use a separate directory for each scrape (Cargo's `--manifest-path` can help with that) or back up the urls.url file before you run the script. 14 | 15 | Tip: `ignores.url` is a list of URLs that should NOT be extracted. This allows you to run the script on different scrapes without having duplicates. It's also helpful if you have previously used other tools to scrape URLs - just add the URLs you scraped to ignores.url and they won't be scraped again! It's not perfect, but it should work 99% of the time. `ignores.url` is read into memory, new URLs are added to it, and then `ignores.url` is overwritten with the loaded values. As such, don't modify `ignores.url` while the script is running - your changes will have no effect, and when the script finishes they will be overwritten! 16 | 17 | (Also, I think this goes without saying, but don't run the app multiple times at the same time in the same folder. That's a recipe for ~~turnabout~~ disaster.) 18 | 19 | ## Usage with Discord History Tracker (DESKTOP APP ONLY) 20 | :warning: Note that the DHT extractor is pretty much unmaintained since I no longer use it. I'll fix bugs, but ~~it doesn't support embeds~~ or any new DHT features. It only supports extracting attachment urls (which can now be done via DHT!) and finding URLs in messages using a regex (and getting avatar urls). ~~It does not look through embeds~~ or any other feature. 21 | 22 | Once you've got your .dht file, run: 23 | 24 | cargo r dht 25 | 26 | Of course, if the file path has spaces, pad it with quotes (`"`) if your shell requires it. 27 | 28 | Example: `cargo r /home/thetechrobo/Discordbackups/dsicord_data/SteamgridDB/SteamGridDB.dht dht` 29 | 30 | Obviously replace the file path with the actual one. Unless, of course, you have the exact same path as me - in which case, twinsies! 31 | 32 | ## Usage with discard2 33 | 34 | :warning: These steps have changed! (The old steps will still work, but the new ones are simpler and get more URLs.) 35 | 36 | 1. Use the `raw-jsonl` reader from discard2. Save its output into a file. Do not name that file `urls.url` or `ignores.url`. 37 | 3. Run `cargo r messages.jsonl discard2`, assuming you named the file in Step 2 `messages.jsonl`. 38 | 4. Use a program to get rid of any duplicate URLs. (There shouldn't be any, but I'm not perfect.) On \*nix you can use `sort -u` or `uniq`. 39 | 40 | To get even more data (server emojis, role icons, and more), `--parse-websockets`. Note that you then have to specify the `--guild-id` (server ID; can be found in the state.json file or by right clicking on the server in Discord's UI and hitting "Copy ID") because I'm wayyy too lazy to try to autodetect what server the crawl is in. 41 | 42 | ## Usage with DiscordChatExporter 43 | 44 | :warning: DiscordChatExporter's extractor will use a ton of memory for large channels. This is due to both limitations in the file format, and limitations in the JSON library I'm using. 45 | 46 | :warning: If you go this route, you CANNOT run DiscordChatExporter with the `--media` option ! Doing so will replace the URLs in the json with the path to the local file, which will cause the URL list to have paths to local files instead of HTTP resources. So don't, I don't know, do a day-long crawl of a huge server until you realise that the attachment urls are all screwed up. (Ask me how I know.) 47 | 48 | DiscordChatExporter is now supported! You can only run one channel at a time, though, and you must use the JSON output format. CSV may be supported in the future. Usage is: 49 | 50 | ```bash 51 | cargo run /path/to/channel.json dce 52 | ``` 53 | 54 | To run an entire folder of JSONs, you could run a script. For example, here's the script I use (tested on zsh, probably won't work on windows, might work on bash): 55 | 56 | ```zsh 57 | for i in *.json ; do echo $i ; "$i" dce && { cat urls.url >> urls.url_finished; rm urls.url; continue }; echo FAILED; break ; done 58 | ``` 59 | 60 | ## Usage with plain text 61 | If you have some plain text files, you can use them directly. That will find all URLs saved in the file, or at least most of them. I think. 62 | 63 | cargo r plaintext 64 | 65 | ## Licence 66 | 67 | Licenced under the Apache 2.0 licence. Copyright (C) TheTechRobo, 2021-2022. 68 | 69 | > Copyright 2021-2023 TheTechRobo 70 | > 71 | > Licensed under the Apache License, Version 2.0 (the "License"); 72 | > you may not use this file except in compliance with the License. 73 | > You may obtain a copy of the License at 74 | > 75 | > http://www.apache.org/licenses/LICENSE-2.0 76 | > 77 | > Unless required by applicable law or agreed to in writing, software 78 | > distributed under the License is distributed on an "AS IS" BASIS, 79 | > WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 80 | > See the License for the specific language governing permissions and 81 | > limitations under the License. 82 | -------------------------------------------------------------------------------- /src/cli.rs: -------------------------------------------------------------------------------- 1 | use clap::Parser; 2 | 3 | use std::path::PathBuf; 4 | 5 | const AUTHOR: &str = "TheTechRobo"; 6 | const VERSION: &str = "v.1.2"; 7 | const ABOUT: &str = "Extracts URLs from Discord scrapes"; 8 | 9 | #[derive(Parser, Debug)] 10 | #[clap(author=AUTHOR, version=VERSION, about=ABOUT)] 11 | pub struct Args { 12 | /// Specifies server ID. Required by --parse-websockets 13 | #[clap(long="guild-id")] 14 | pub guild_id: Option, 15 | 16 | /// Go through websockets to get server icon, server emojis, and more 17 | #[clap(long="parse-websockets")] 18 | pub use_websockets: bool, 19 | 20 | #[clap(value_name="FILE")] 21 | pub file: PathBuf, 22 | 23 | #[clap(value_name="TYPE", value_parser=["dht", "discard2", "plaintext", "dce"])] 24 | pub file_type: String 25 | } 26 | -------------------------------------------------------------------------------- /src/gateway.rs: -------------------------------------------------------------------------------- 1 | use tinyjson::JsonValue; 2 | 3 | use std::collections::HashMap; 4 | 5 | pub fn gateway_parse(json: JsonValue, server_id: String) -> Vec { 6 | let mut finish = Vec::new(); 7 | let json_hashmap: HashMap = json.clone().try_into().unwrap(); 8 | if !json_hashmap.contains_key("t") || !json_hashmap.contains_key("s") { 9 | return finish; 10 | } 11 | if json["t"].is_null() { 12 | return finish; 13 | } 14 | let t: String = json["t"].clone().try_into().unwrap(); 15 | let s: bool = json["s"].is_null(); 16 | if (t != "READY") || s { // makes sure this is the correct websocket 17 | return finish; 18 | } 19 | let actual_data: HashMap = json["d"].clone().try_into().unwrap(); 20 | let v: f64 = actual_data["v"].clone().try_into().unwrap(); 21 | if v != 9.0 { 22 | panic!("Unsupported API version `{v}'"); 23 | } 24 | let guilds: Vec = actual_data["guilds"].clone().try_into().unwrap(); 25 | for raw_guild in guilds { 26 | let guild: HashMap = raw_guild.try_into().unwrap(); 27 | let guild_id: String = guild["id"].clone().try_into().unwrap(); 28 | if guild_id != server_id { 29 | eprintln!("Found unrelated guild {guild_id}"); 30 | continue; 31 | } 32 | if !guild["icon"].is_null() { 33 | let icon: String = guild["icon"].clone().try_into().unwrap(); 34 | finish.push(format!("https://cdn.discordapp.com/icons/{}/{}.webp?size=4096&quality=lossless", guild_id, icon)); 35 | finish.push(format!("https://cdn.discordapp.com/icons/{}/{}.gif?size=4096&quality=lossless", guild_id, icon)); 36 | } 37 | let roles: Vec = guild["roles"].clone().try_into().unwrap(); 38 | for raw_role in roles { 39 | let role: HashMap = raw_role.try_into().unwrap(); 40 | if role.contains_key("icon") && !role["icon"].is_null() { 41 | let role_id: String =role["id"].clone().try_into().unwrap(); 42 | let role_icon: String =role["icon"].clone().try_into().unwrap(); 43 | finish.push(format!("https://cdn.discordapp.com/role-icons/{}/{}.webp?size=4096&quality=lossless", role_id, role_icon)); 44 | } 45 | } 46 | let emojis: Vec =guild["emojis"].clone().try_into().unwrap(); 47 | for raw_emoji in emojis { 48 | let emoji:HashMap = raw_emoji.try_into().unwrap(); 49 | // todo: extract optional user object from here? 50 | let id: String = emoji["id"].clone().try_into().unwrap(); 51 | let mut animated: bool = false; 52 | if emoji.contains_key("animated") { 53 | animated = emoji["animated"].clone().try_into().unwrap(); 54 | } 55 | if animated { 56 | finish.push(format!("https://cdn.discordapp.com/emojis/{}.gif?size=4096&quality=lossless", id)); 57 | } 58 | else { 59 | finish.push(format!("https://cdn.discordapp.com/emojis/{}.webp?size=4096&quality=lossless", id)); 60 | } 61 | } 62 | } 63 | finish 64 | } 65 | -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | use regex::Regex; 2 | use rusqlite::Connection; 3 | use std::collections::HashMap; 4 | use std::fs; 5 | use std::fs::File; 6 | use std::io::Write; 7 | use std::io::{BufRead, BufReader, Error}; 8 | 9 | use clap::Parser; 10 | 11 | use tinyjson::JsonValue; 12 | 13 | mod gateway; 14 | mod cli; 15 | 16 | #[derive(Debug)] 17 | struct S { 18 | data: String, // i actually don't remember why I had to do it this way 19 | } 20 | 21 | #[derive(Debug)] 22 | struct User { 23 | id: u64, 24 | avatar_id: Option, 25 | } 26 | 27 | struct RetVal { // so we can return both urls and ignores from a function 28 | urls: Vec, 29 | ignores: Vec, 30 | } 31 | 32 | fn read_data() -> Vec { // should really be named `read_ignores()' 33 | eprintln!("Reading ignores. Please wait..."); 34 | let status = fs::read_to_string("ignores.url"); 35 | let data = match status { 36 | Ok(s) => s, 37 | Err(_) => { 38 | eprintln!("failed to read ignores.url, proceeding with default"); 39 | "".to_string() 40 | } 41 | }; 42 | let urls: Vec<&str> = data.split('\n').collect(); 43 | let mut ignores = Vec::new(); 44 | for url in urls { 45 | if !url.is_empty() { 46 | ignores.push(url.to_string()); 47 | } 48 | } 49 | ignores 50 | } 51 | 52 | fn write_data(ignores: Vec, urls: Vec) { 53 | eprintln!("Now writing data."); 54 | let file = fs::OpenOptions::new() 55 | .write(true) 56 | .create(true) 57 | .open("ignores.url"); 58 | #[allow(unused_assignments)] 59 | let mut filefailed = false; // this brings up a warning but I'm not sure how to fix it 60 | let mut error: Error = Error::new(std::io::ErrorKind::Other, "bye"); 61 | let mut file = match file { 62 | Ok(fil) => { 63 | filefailed = false; 64 | fil 65 | } 66 | Err(e) => { 67 | error = e; 68 | filefailed = true; 69 | fs::OpenOptions::new() 70 | .write(true) 71 | .open("/dev/null") 72 | .unwrap() 73 | } 74 | }; 75 | if !filefailed { 76 | for ignore in ignores { 77 | writeln!(file, "{}", ignore).expect("failed to write file"); 78 | } 79 | } else { 80 | eprintln!("Failed to write ignores: {}", error); 81 | } 82 | let mut file = fs::OpenOptions::new() 83 | .create(true) 84 | .write(true) 85 | .open("urls.url") 86 | .expect("couldn't open urls.url"); 87 | for url in urls { 88 | writeln!(file, "{}", url).expect("failed to write URLs"); 89 | } 90 | //https://www.codegrepper.com/code-examples/rust/rust+how+to+append+to+a+file 91 | } 92 | 93 | fn dce(filename: &str, ignores: Vec, mut urls: Vec, regex: Regex, arguments: cli::Args) -> RetVal { 94 | if arguments.use_websockets { 95 | panic!("--parse-websockets can only be used with discard2 JSONL"); 96 | } 97 | let jj: JsonValue = { 98 | // doing this as a block makes sure we drop this string afterwards 99 | // the compiler might optimise that but I'm not sure 100 | eprintln!("Reading JSON... This may take up an obscene amount of RAM."); 101 | let contents = fs::read_to_string(filename).expect("Couldnt read file"); 102 | contents.parse().unwrap() 103 | }; 104 | eprintln!("Now extracting URLs. This may take awhile!"); 105 | let icon_url: String = jj["guild"]["iconUrl"].clone().try_into().unwrap(); 106 | if !ignores.contains(&icon_url) { 107 | urls.push(icon_url); 108 | } 109 | let messages: Vec<_> = jj["messages"].clone().try_into().unwrap(); 110 | for message in messages { 111 | let avatar_url: String = message["author"]["avatarUrl"].clone().try_into().unwrap(); 112 | if !ignores.contains(&avatar_url) { 113 | urls.push(avatar_url.clone()); 114 | } 115 | let content: String = message["content"].clone().try_into().unwrap(); 116 | for mat in regex.find_iter(&content) { 117 | let i = mat.as_str(); 118 | if !ignores.contains(&i.to_string()) { 119 | urls.push(i.to_string()); 120 | } 121 | } 122 | let attachments: Vec<_> = message["attachments"].clone().try_into().unwrap(); 123 | for attachment in attachments { 124 | let url: String = attachment["url"].clone().try_into().unwrap(); 125 | if !ignores.contains(&url) { 126 | urls.push(url); 127 | } 128 | } 129 | let reactions: Vec<_> = message["reactions"].clone().try_into().unwrap(); 130 | for reaction in reactions { 131 | let emoji: HashMap<_,_> = reaction["emoji"].clone().try_into().unwrap(); 132 | let url: String = emoji["imageUrl"].clone().try_into().unwrap(); 133 | if !ignores.contains(&url) { 134 | urls.push(url); 135 | } 136 | } 137 | for embed_url in get_embed_urls(message, regex.clone()) { 138 | if !ignores.contains(&embed_url) { 139 | urls.push(embed_url); 140 | } 141 | } 142 | } 143 | RetVal { urls: urls, ignores: ignores } 144 | } 145 | 146 | fn sql(filename: &str, ignores: Vec, mut urls: Vec, regex: Regex, arguments: cli::Args) -> RetVal { 147 | if arguments.use_websockets { 148 | panic!("--parse-websockets can only be used with discard2 JSONL"); 149 | } 150 | eprintln!("Connecting to SQL database..."); 151 | let conn = Connection::open(filename).unwrap(); 152 | eprintln!("Attachments..."); 153 | let mut stmt = conn.prepare("SELECT * FROM attachments").unwrap(); 154 | let person_iter = stmt 155 | .query_map([], |row| { 156 | Ok(S { 157 | data: row.get(4).unwrap(), //attachment URL is on 5th column of each row. 158 | }) 159 | }) 160 | .unwrap(); 161 | for attachment_url in person_iter { 162 | let att = attachment_url.unwrap().data; 163 | if ignores.contains(&att) { 164 | continue; 165 | } 166 | urls.push(att); 167 | } 168 | eprintln!("Avatars..."); 169 | let mut stmt = conn.prepare("SELECT * FROM users").unwrap(); 170 | let person_iter = stmt 171 | .query_map([], |row| { 172 | Ok(User { 173 | id: row.get(0).unwrap(), // The user ID 174 | avatar_id: row.get(2).unwrap(), 175 | }) 176 | }) 177 | .unwrap(); 178 | for avatar_url in person_iter { 179 | let avatar_url = avatar_url.unwrap(); 180 | if let Some(avatar_id) = avatar_url.avatar_id.clone() { 181 | let att = format!("https://cdn.discordapp.com/avatars/{}/{}.webp?size=4096", avatar_url.id, avatar_id); 182 | if ignores.contains(&att) { 183 | continue; 184 | } 185 | urls.push(att); 186 | } 187 | } 188 | eprintln!( 189 | "Finished avatars. Now extracting messages...\nThis may take a while. Go get a coffee." 190 | ); 191 | let mut stmt = conn.prepare("SELECT * FROM messages").unwrap(); 192 | let person_iter = stmt 193 | .query_map([], |row| { 194 | Ok(S { 195 | data: row.get(3).unwrap(), // message data is on 4th column of each row. 196 | }) 197 | }) 198 | .unwrap(); 199 | for message in person_iter { 200 | let m = message.unwrap().data; 201 | for mat in regex.find_iter(&m) { 202 | let i = mat.as_str(); 203 | if !ignores.contains(&i.to_string()) { 204 | urls.push(i.to_string()); 205 | } 206 | } 207 | } 208 | eprintln!("Finally, extracting embeds..."); 209 | let mut stmt = conn.prepare("SELECT * FROM embeds").unwrap(); 210 | let person_iter = stmt 211 | .query_map([], |row| { 212 | Ok(S { 213 | data: row.get(1).unwrap(), 214 | }) 215 | }) 216 | .unwrap(); 217 | for embed in person_iter { 218 | let embed = embed.unwrap().data.parse().expect("bad JSON"); 219 | let mut hm = HashMap::new(); 220 | hm.insert("embeds".to_string(), JsonValue::Array(vec![embed])); 221 | let embeds = JsonValue::Object(hm); 222 | for url in get_embed_urls(embeds, regex.clone()) { 223 | if !ignores.contains(&url) { 224 | urls.push(url); 225 | } 226 | } 227 | } 228 | RetVal { urls, ignores } 229 | } 230 | 231 | // Some of the following code logic is taken from 232 | // https://github.com/Sanqui/discard2/blob/master/src/reader/reader.ts 233 | fn messages_from_json(json: JsonValue) -> JsonValue { 234 | let regex = Regex::new(r#"^/api/v9/channels/\d+/messages"#).unwrap(); // the api endpoint currently used by discord 235 | let request_type: String = json["type"].clone().try_into().unwrap(); 236 | if request_type != "http" { 237 | return JsonValue::Array(Vec::new()); 238 | } 239 | let status_code: f64 = json["response"]["status_code"].clone().try_into().unwrap(); 240 | let endpoint: String = json["request"]["path"].clone().try_into().unwrap(); 241 | let method: String = json["request"]["method"].clone().try_into().unwrap(); 242 | if request_type == "http" 243 | && method == "GET" 244 | && status_code == 200.0 245 | && regex.is_match(&endpoint) { 246 | return json["response"]["data"].clone(); 247 | } 248 | JsonValue::Array(Vec::new()) 249 | } 250 | fn get_embed_urls(json: JsonValue, regex: Regex) -> Vec { 251 | let embeds: Vec = json["embeds"].clone().try_into().unwrap(); 252 | let mut this_code_sucks_lol = Vec::new(); 253 | for embed in embeds { 254 | let embed: HashMap = embed.clone().try_into().unwrap(); 255 | let mut description: String = "".to_string(); 256 | if embed.contains_key("description") { // We really need to wrap these into a function 257 | description = embed["description"].clone().try_into().unwrap(); 258 | } 259 | if embed.contains_key("title") { 260 | let title: String = embed["title"].clone().try_into().unwrap(); 261 | description = format!("{}\n{}", title, description); // to reduce code duplication (since we already operate on the description) 262 | } 263 | if embed.contains_key("footer") { 264 | let footer: HashMap = embed["footer"].clone().try_into().unwrap(); 265 | let footer_text: String = embed["footer"]["text"].clone().try_into().unwrap(); 266 | description = format!("{}\n{}", description, footer_text); 267 | if footer.contains_key("icon_url") { 268 | let icon_url: String = embed["footer"]["icon_url"].clone().try_into().unwrap(); 269 | this_code_sucks_lol.push(icon_url); 270 | } 271 | } 272 | if embed.contains_key("thumbnail") { 273 | let thumbnail: String = embed["thumbnail"]["url"].clone().try_into().unwrap(); 274 | this_code_sucks_lol.push(thumbnail); 275 | } 276 | if embed.contains_key("video") { 277 | if embed.contains_key("url") { 278 | let url: String = embed["video"]["url"].clone().try_into().unwrap(); 279 | this_code_sucks_lol.push(url); 280 | } else if embed.contains_key("proxy_url") { 281 | // better than nothing 282 | let url: String = embed["video"]["proxy_url"].clone().try_into().unwrap(); 283 | this_code_sucks_lol.push(url); 284 | } else { 285 | eprintln!("EMBED WARNING: Video does not have a url or proxy_url.") 286 | } 287 | } 288 | if embed.contains_key("author") { 289 | let author: HashMap = embed["author"].clone().try_into().unwrap(); 290 | if author.contains_key("url") && !author["url"].is_null() { 291 | let url: String = author["url"].clone().try_into().unwrap(); 292 | this_code_sucks_lol.push(url); 293 | } 294 | if author.contains_key("icon_url") { 295 | let url: String = author["icon_url"].clone().try_into().unwrap(); 296 | this_code_sucks_lol.push(url); 297 | } 298 | if author.contains_key("proxy_icon_url") { 299 | let url: String = author["proxy_icon_url"].clone().try_into().unwrap(); 300 | this_code_sucks_lol.push(url); 301 | } 302 | } 303 | if embed.contains_key("provider") { 304 | let provider: HashMap = 305 | embed["provider"].clone().try_into().unwrap(); 306 | if provider.contains_key("url") { 307 | let url: String = provider["url"].clone().try_into().unwrap(); 308 | this_code_sucks_lol.push(url); 309 | } 310 | } 311 | // not sure searching for urls in the fields is worth it since it's so small 312 | let m = description; 313 | for mat in regex.find_iter(&m) { 314 | let i = mat.as_str(); 315 | this_code_sucks_lol.push(i.to_string()); 316 | } 317 | if embed.contains_key("image") { 318 | let image: String = embed["image"]["url"].clone().try_into().unwrap(); 319 | this_code_sucks_lol.push(image); 320 | } 321 | if embed.contains_key("url") && !embed["url"].is_null() { 322 | let url: String = embed["url"].clone().try_into().unwrap(); 323 | this_code_sucks_lol.push(url.to_string()); 324 | } 325 | } 326 | this_code_sucks_lol 327 | // todo reduce duplication 328 | } 329 | 330 | fn get_component_urls(json: JsonValue) -> Vec { 331 | let mut urls = Vec::new(); 332 | let components: Vec = json["components"].clone().try_into().unwrap(); 333 | for raw_component_lis in components { 334 | let component_lis: Vec = raw_component_lis["components"].clone().try_into().unwrap(); 335 | for component_ra in component_lis { 336 | let component: HashMap = component_ra.clone().try_into().unwrap(); 337 | //println!("{:?}", component); 338 | if component.contains_key("url") { 339 | let url: String = component["url"].clone().try_into().unwrap(); 340 | urls.push(url); 341 | } 342 | } 343 | } 344 | urls 345 | } 346 | 347 | fn discard2_jsonl( 348 | filename: &str, 349 | mut ignores: Vec, 350 | mut urls: Vec, 351 | regex: Regex, 352 | arguments: cli::Args 353 | ) -> RetVal { 354 | let guild_id: String = match arguments.guild_id { 355 | Some(id) => id, 356 | None => "a".to_string() // one that will never be used 357 | }; 358 | if guild_id == "a" && arguments.use_websockets { 359 | panic!("You have to provide the server ID to use --parse-websockets. See --help for more information."); 360 | } 361 | let file = File::open(filename).expect("Failed to open file"); 362 | let reader = BufReader::new(file); 363 | eprintln!("Searching through messages, this may take some time..."); 364 | for line in reader.lines() { 365 | let json_line: JsonValue = line.unwrap().parse().expect("Failed to parse JSONL"); 366 | let request_type: String = json_line["type"].clone().try_into().unwrap(); 367 | if request_type == "ws" && arguments.use_websockets { 368 | let direction: String = json_line["direction"].clone().try_into().unwrap(); 369 | if direction == "recv" { 370 | for url in gateway::gateway_parse(json_line["data"].clone(), guild_id.clone()) { 371 | if !ignores.contains(&url) { 372 | urls.push(url.clone()); 373 | ignores.push(url); 374 | } 375 | } 376 | } 377 | } 378 | let messages: Vec = messages_from_json(json_line).try_into().unwrap(); 379 | for message in messages { // try to match with the url regex 380 | let m: String = message["content"].clone().try_into().unwrap(); 381 | for mat in regex.find_iter(&m) { 382 | let i = mat.as_str(); 383 | if !ignores.contains(&i.to_string()) { 384 | ignores.push(i.to_string()); 385 | urls.push(i.to_string()); 386 | } 387 | } 388 | // Avatar 389 | if !message["author"]["avatar"].is_null() { 390 | let user_id: String = message["author"]["id"].clone().try_into().unwrap(); 391 | let avatar_id: String = message["author"]["avatar"].clone().try_into().unwrap(); 392 | let avatar_link = format!( 393 | "https://cdn.discordapp.com/avatars/{}/{}.webp?size=4096", // get the highest quality avatar available 394 | user_id, avatar_id 395 | ); 396 | if !ignores.contains(&avatar_link.to_string()) { 397 | urls.push(avatar_link.clone()); 398 | ignores.push(avatar_link); 399 | } 400 | } 401 | // Attachments 402 | let attachments: Vec = message["attachments"].clone().try_into().unwrap(); 403 | for attachment in attachments { 404 | let i: String = attachment["url"].clone().try_into().unwrap(); 405 | if !ignores.contains(&i.to_string()) { 406 | urls.push(i.clone()); 407 | ignores.push(i); 408 | } 409 | } 410 | // Embeds (this is a big one!) 411 | for embed_url in get_embed_urls(message.clone(), regex.clone()) { 412 | if !ignores.contains(&embed_url.to_string()) { 413 | urls.push(embed_url.clone()); 414 | ignores.push(embed_url); 415 | } 416 | } 417 | for component_url in get_component_urls(message.clone()) { 418 | if !ignores.contains(&component_url.to_string()) { 419 | urls.push(component_url.clone()); 420 | ignores.push(component_url); 421 | } 422 | } 423 | //println!("{:?}", message); 424 | } 425 | } 426 | RetVal { urls, ignores } 427 | } 428 | fn plain_text(filename: &str, ignores: Vec, mut urls: Vec, regex: Regex, arguments: cli::Args) -> RetVal { 429 | if arguments.use_websockets { 430 | panic!("--parse-websockets can only be used with discard2 JSONL"); 431 | } 432 | let file = File::open(filename).expect("Failed to open file"); 433 | let reader = BufReader::new(file); 434 | eprintln!("Searching through lines of plain-text file..."); 435 | for line in reader.lines() { 436 | let m = line.unwrap(); 437 | for mat in regex.find_iter(&m) { 438 | let i = mat.as_str(); 439 | if !ignores.contains(&i.to_string()) { 440 | urls.push(i.to_string()); 441 | } 442 | } 443 | } 444 | RetVal { urls, ignores } 445 | } 446 | 447 | fn main() { 448 | if std::path::Path::new("urls.url").exists() { 449 | panic!("cowardly refusing to overwrite urls.url"); 450 | } 451 | let regex = Regex::new(r#"(https?://[^\s<]+[^?~*|<>.,:;"'`)\]\s])"#).unwrap(); 452 | let arguments = cli::Args::parse(); 453 | let mut ignores = read_data(); 454 | let mut urls = vec![]; 455 | let filename: &str = &arguments.file.clone().into_os_string().into_string().unwrap(); 456 | let s: RetVal = match arguments.file_type.as_str() { 457 | "dht" => sql(filename, ignores.clone(), urls, regex, arguments), 458 | "plaintext" => plain_text(filename, ignores.clone(), urls, regex, arguments), 459 | "discard2" => discard2_jsonl(filename, ignores.clone(), urls, regex, arguments), 460 | "dce" => dce(filename, ignores.clone(), urls, regex, arguments), 461 | _ => panic!("...?") 462 | }; 463 | urls = s.urls; 464 | ignores = s.ignores; 465 | for url_to_ignore in &urls { 466 | ignores.push(url_to_ignore.clone()); 467 | } 468 | write_data(ignores, urls); 469 | } 470 | --------------------------------------------------------------------------------