diff options
| author | Linnea Gräf <nea@nea.moe> | 2025-08-13 12:36:12 +0200 |
|---|---|---|
| committer | Linnea Gräf <nea@nea.moe> | 2025-08-13 12:36:25 +0200 |
| commit | f3b845fd0b1dfdcf5acf813c732d0e2ec8d778fc (patch) | |
| tree | 13348823a0dac94fbb8ebc48e3e9409dd0e954fa | |
| parent | 36532ea59f762db424d0b6526b43278c72b8991e (diff) | |
| download | discordavatarproxy-f3b845fd0b1dfdcf5acf813c732d0e2ec8d778fc.tar.gz discordavatarproxy-f3b845fd0b1dfdcf5acf813c732d0e2ec8d778fc.tar.bz2 discordavatarproxy-f3b845fd0b1dfdcf5acf813c732d0e2ec8d778fc.zip | |
use rustls instead of native openssl
| -rw-r--r-- | Cargo.lock | 385 | ||||
| -rw-r--r-- | Cargo.toml | 4 | ||||
| -rw-r--r-- | Dockerfile | 2 | ||||
| -rw-r--r-- | src/main.rs | 375 |
4 files changed, 399 insertions, 367 deletions
@@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "addr2line" @@ -51,12 +51,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] -name = "bitflags" -version = "2.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4682ae6287fcf752ecaabbfcc7b6f9b72aa33933dc23a554d853aea8eea8635" - -[[package]] name = "bytes" version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -64,11 +58,11 @@ checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223" [[package]] name = "cc" -version = "1.0.83" +version = "1.2.32" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0" +checksum = "2352e5597e9c544d5e6d9c95190d5d27738ade584fa8db0a16e130e5c2b5296e" dependencies = [ - "libc", + "shlex", ] [[package]] @@ -94,54 +88,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa" [[package]] -name = "errno" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "136526188508e25c6fef639d7927dfb3e0e3084488bf202267829cf7fc23dbdd" -dependencies = [ - "errno-dragonfly", - "libc", - "windows-sys", -] - -[[package]] -name = "errno-dragonfly" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf" -dependencies = [ - "cc", - "libc", -] - -[[package]] -name = "fastrand" -version = "2.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6999dc1837253364c2ebb0704ba97994bd874e8f195d665c50b7548f6ea92764" - -[[package]] name = "fnv" version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" [[package]] -name = "foreign-types" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" -dependencies = [ - "foreign-types-shared", -] - -[[package]] -name = "foreign-types-shared" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" - -[[package]] name = "futures-channel" version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -181,6 +133,17 @@ dependencies = [ ] [[package]] +name = "getrandom" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "335ff9f135e4384c8150d6f27c6daed433577f86b4750418338c01a1a2528592" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] name = "gimli" version = "0.28.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -276,16 +239,20 @@ dependencies = [ ] [[package]] -name = "hyper-tls" -version = "0.5.0" +name = "hyper-rustls" +version = "0.25.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d6183ddfa99b85da61a140bea0efc93fdf56ceaa041b37d553518030827f9905" +checksum = "399c78f9338483cb7e630c8474b07268983c6bd5acee012e4211f9f7bb21b070" dependencies = [ - "bytes", + "futures-util", + "http", "hyper", - "native-tls", + "log", + "rustls", + "rustls-native-certs", + "rustls-pki-types", "tokio", - "tokio-native-tls", + "tokio-rustls", ] [[package]] @@ -305,22 +272,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" [[package]] -name = "lazy_static" -version = "1.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" - -[[package]] name = "libc" -version = "0.2.148" +version = "0.2.175" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9cdc71e17332e86d2e1d38c1f99edcb6288ee11b815fb1a4b049eaa2114d369b" - -[[package]] -name = "linux-raw-sys" -version = "0.4.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a9bad9f94746442c783ca431b22403b519cd7fbeed0533fdd6328b2f2212128" +checksum = "6a82ae493e598baaea5209805c49bbf2ea7de956d50d7da0da1164f9c6d28543" [[package]] name = "lock_api" @@ -361,25 +316,7 @@ checksum = "927a765cd3fc26206e66b296465fa9d3e5ab003e651c1b3c060e7956d96b19d2" dependencies = [ "libc", "wasi", - "windows-sys", -] - -[[package]] -name = "native-tls" -version = "0.2.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07226173c32f2926027b63cce4bcd8076c3552846cbe7925f3aaffeac0a3b92e" -dependencies = [ - "lazy_static", - "libc", - "log", - "openssl", - "openssl-probe", - "openssl-sys", - "schannel", - "security-framework", - "security-framework-sys", - "tempfile", + "windows-sys 0.48.0", ] [[package]] @@ -408,50 +345,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" [[package]] -name = "openssl" -version = "0.10.57" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bac25ee399abb46215765b1cb35bc0212377e58a061560d8b29b024fd0430e7c" -dependencies = [ - "bitflags 2.4.0", - "cfg-if", - "foreign-types", - "libc", - "once_cell", - "openssl-macros", - "openssl-sys", -] - -[[package]] -name = "openssl-macros" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] name = "openssl-probe" version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" [[package]] -name = "openssl-sys" -version = "0.9.93" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db4d56a4c0478783083cfafcc42493dd4a981d41669da64b4572a2a089b51b1d" -dependencies = [ - "cc", - "libc", - "pkg-config", - "vcpkg", -] - -[[package]] name = "parking_lot" version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -471,7 +370,7 @@ dependencies = [ "libc", "redox_syscall", "smallvec", - "windows-targets", + "windows-targets 0.48.5", ] [[package]] @@ -480,7 +379,7 @@ version = "0.1.0" dependencies = [ "anyhow", "hyper", - "hyper-tls", + "hyper-rustls", "serde", "serde_json", "tokio", @@ -499,12 +398,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" [[package]] -name = "pkg-config" -version = "0.3.27" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964" - -[[package]] name = "proc-macro2" version = "1.0.67" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -528,7 +421,21 @@ version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "567664f262709473930a4bf9e51bf2ebf3348f2e748ccc50dea20646858f8f29" dependencies = [ - "bitflags 1.3.2", + "bitflags", +] + +[[package]] +name = "ring" +version = "0.17.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4689e6c2294d81e88dc6261c768b63bc4fcdb852be6d1352498b114f61383b7" +dependencies = [ + "cc", + "cfg-if", + "getrandom", + "libc", + "untrusted", + "windows-sys 0.52.0", ] [[package]] @@ -538,16 +445,59 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" [[package]] -name = "rustix" -version = "0.38.13" +name = "rustls" +version = "0.22.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7db8590df6dfcd144d22afd1b83b36c21a18d7cbc1dc4bb5295a8712e9eb662" +checksum = "bf4ef73721ac7bcd79b2b315da7779d8fc09718c6b3d2d1b2d94850eb8c18432" dependencies = [ - "bitflags 2.4.0", - "errno", - "libc", - "linux-raw-sys", - "windows-sys", + "log", + "ring", + "rustls-pki-types", + "rustls-webpki", + "subtle", + "zeroize", +] + +[[package]] +name = "rustls-native-certs" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5bfb394eeed242e909609f56089eecfe5fda225042e8b171791b9c95f5931e5" +dependencies = [ + "openssl-probe", + "rustls-pemfile", + "rustls-pki-types", + "schannel", + "security-framework", +] + +[[package]] +name = "rustls-pemfile" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dce314e5fee3f39953d46bb63bb8a46d40c2f8fb7cc5a3b6cab2bde9721d6e50" +dependencies = [ + "rustls-pki-types", +] + +[[package]] +name = "rustls-pki-types" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "229a4a4c221013e7e1f1a043678c5cc39fe5171437c88fb47151a21e6f5b5c79" +dependencies = [ + "zeroize", +] + +[[package]] +name = "rustls-webpki" +version = "0.102.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64ca1bc8749bd4cf37b5ce386cc146580777b4e8572c7b97baf22c83f444bee9" +dependencies = [ + "ring", + "rustls-pki-types", + "untrusted", ] [[package]] @@ -562,7 +512,7 @@ version = "0.1.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c3733bf4cf7ea0880754e19cb5a462007c4a8c1914bff372ccc95b464f1df88" dependencies = [ - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -577,7 +527,7 @@ version = "2.9.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "05b64fb303737d99b81884b2c63433e9ae28abebe5eb5045dcdd175dc2ecf4de" dependencies = [ - "bitflags 1.3.2", + "bitflags", "core-foundation", "core-foundation-sys", "libc", @@ -626,6 +576,12 @@ dependencies = [ ] [[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + +[[package]] name = "signal-hook-registry" version = "1.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -666,10 +622,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4031e820eb552adee9295814c0ced9e5cf38ddf1e8b7d566d6de8e2538ea989e" dependencies = [ "libc", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] +name = "subtle" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" + +[[package]] name = "syn" version = "2.0.37" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -681,19 +643,6 @@ dependencies = [ ] [[package]] -name = "tempfile" -version = "3.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb94d2f3cc536af71caac6b6fcebf65860b347e7ce0cc9ebe8f70d3e521054ef" -dependencies = [ - "cfg-if", - "fastrand", - "redox_syscall", - "rustix", - "windows-sys", -] - -[[package]] name = "tokio" version = "1.32.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -709,7 +658,7 @@ dependencies = [ "signal-hook-registry", "socket2 0.5.4", "tokio-macros", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -724,12 +673,13 @@ dependencies = [ ] [[package]] -name = "tokio-native-tls" -version = "0.3.1" +name = "tokio-rustls" +version = "0.25.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbae76ab933c85776efabc971569dd6119c580d8f5d448769dec1764bf796ef2" +checksum = "775e0c0f0adb3a2f22a00c4745d728b479985fc15ee7ca6a2608388c5569860f" dependencies = [ - "native-tls", + "rustls", + "rustls-pki-types", "tokio", ] @@ -786,10 +736,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" [[package]] -name = "vcpkg" -version = "0.2.15" +name = "untrusted" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" +checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" [[package]] name = "want" @@ -834,7 +784,16 @@ version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" dependencies = [ - "windows-targets", + "windows-targets 0.48.5", +] + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets 0.52.6", ] [[package]] @@ -843,13 +802,29 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", + "windows_i686_gnullvm", + "windows_i686_msvc 0.52.6", + "windows_x86_64_gnu 0.52.6", + "windows_x86_64_gnullvm 0.52.6", + "windows_x86_64_msvc 0.52.6", ] [[package]] @@ -859,37 +834,91 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" [[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] name = "windows_aarch64_msvc" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" [[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] name = "windows_i686_gnu" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" [[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] name = "windows_i686_msvc" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" [[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] name = "windows_x86_64_gnu" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" [[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] name = "windows_x86_64_gnullvm" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" [[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] name = "windows_x86_64_msvc" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "zeroize" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde" @@ -7,8 +7,8 @@ edition = "2021" [dependencies] -[dependencies.hyper-tls] -version = "*" +[dependencies.hyper-rustls] +version = "0.25.0" [dependencies.hyper] version = "*" @@ -1,6 +1,5 @@ FROM clux/muslrust:1.89.0-stable AS chef USER root -RUN apk add openssl RUN cargo install cargo-chef WORKDIR /app @@ -18,7 +17,6 @@ RUN rm rust-toolchain.toml RUN cargo build --release --target x86_64-unknown-linux-musl --bin pfp FROM docker.io/alpine:3 AS runtime -RUN apk add openssl WORKDIR /app COPY --from=builder /app/target/x86_64-unknown-linux-musl/release/pfp /usr/local/bin/ CMD ["/usr/local/bin/pfp"] diff --git a/src/main.rs b/src/main.rs index e80c67d..7921416 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,237 +5,242 @@ use std::sync::Arc; use hyper::client::{Client, HttpConnector}; use hyper::service::{make_service_fn, service_fn}; use hyper::{Body, Method, Request, Response, Server}; -use hyper_tls::HttpsConnector; +use hyper_rustls::{HttpsConnector, HttpsConnectorBuilder}; use serde::{Deserialize, Serialize}; macro_rules! unwrap_resp { - ($x:expr) => { - match $x { - Ok(x) => x, - Err(x) => return Ok(x), - } - }; + ($x:expr) => { + match $x { + Ok(x) => x, + Err(x) => return Ok(x), + } + }; } #[derive(Deserialize, Debug)] struct DiscordUserFormat { - accent_color: Option<i64>, - username: String, - discriminator: String, - id: String, - public_flags: i64, - #[serde(default)] - bot: bool, - banner: Option<String>, - avatar: Option<String>, - #[serde(default)] - global_name: Option<String>, + accent_color: Option<i64>, + username: String, + discriminator: String, + id: String, + public_flags: i64, + #[serde(default)] + bot: bool, + banner: Option<String>, + avatar: Option<String>, + #[serde(default)] + global_name: Option<String>, } async fn get_user_data( - client: &Client<HttpsConnector<HttpConnector>>, - token: &str, - user_id: u64, + client: &Client<HttpsConnector<HttpConnector>>, + token: &str, + user_id: u64, ) -> anyhow::Result<DiscordUserFormat> { - let request = Request::builder() - .method(Method::GET) - .uri(format!("https://discord.com/api/v10/users/{}", user_id)) - .header("accept", "application/json") - .header("authorization", format!("Bot {}", token)) - .body(Body::empty())?; - let mut x = client.request(request).await?; - let body = hyper::body::to_bytes(x.body_mut()).await?; - let json_data = String::from_utf8(Vec::from(body))?; - println!("{}", json_data); - let json: DiscordUserFormat = serde_json::from_str(&json_data)?; - Ok(json) + let request = Request::builder() + .method(Method::GET) + .uri(format!("https://discord.com/api/v10/users/{}", user_id)) + .header("accept", "application/json") + .header("authorization", format!("Bot {}", token)) + .body(Body::empty())?; + let mut x = client.request(request).await?; + let body = hyper::body::to_bytes(x.body_mut()).await?; + let json_data = String::from_utf8(Vec::from(body))?; + println!("{}", json_data); + let json: DiscordUserFormat = serde_json::from_str(&json_data)?; + Ok(json) } fn get_avatar_url(json: &DiscordUserFormat, allow_animated: bool) -> anyhow::Result<String> { - println!( - "Served request for {}: {}#{}", - json.id, json.username, json.discriminator - ); - let avatar_url = match &json.avatar { - None => default_avatar_url(&json.id, &json.discriminator)?, - Some(avatar_hash) => { - if avatar_hash.starts_with("a_") && allow_animated { - format!( - "https://cdn.discordapp.com/avatars/{}/{}.gif", - json.id, avatar_hash - ) - } else { - format!( - "https://cdn.discordapp.com/avatars/{}/{}.png", - json.id, avatar_hash - ) - } - } - }; - Ok(avatar_url) + println!( + "Served request for {}: {}#{}", + json.id, json.username, json.discriminator + ); + let avatar_url = match &json.avatar { + None => default_avatar_url(&json.id, &json.discriminator)?, + Some(avatar_hash) => { + if avatar_hash.starts_with("a_") && allow_animated { + format!( + "https://cdn.discordapp.com/avatars/{}/{}.gif", + json.id, avatar_hash + ) + } else { + format!( + "https://cdn.discordapp.com/avatars/{}/{}.png", + json.id, avatar_hash + ) + } + } + }; + Ok(avatar_url) } fn make_err(err: u16, text: &str) -> anyhow::Result<Response<Body>> { - return Ok(Response::builder() - .status(err) - .body(format!("{} {}", err, text).into())?); + return Ok(Response::builder() + .status(err) + .body(format!("{} {}", err, text).into())?); } async fn resp(arc: Arc<Ctx>, req: Request<Body>) -> anyhow::Result<Response<Body>> { - let x = req.uri().path(); - if x == "/" { - return Ok(Response::builder() - .status(302) - .header("Location", "https://git.nea.moe/nea/discordavatarproxy") - .body(Body::empty())?); - } - let request = match x.strip_prefix("/avatar/") { - None => return make_err(404, "Not found"), - Some(request) => request, - }; - if let Some(userid) = request.strip_suffix(".gif") { - return respond_with_image(arc, userid, true).await; - } - if let Some(userid) = request.strip_suffix(".png") { - return respond_with_image(arc, userid, false).await; - } - if let Some(userid) = request.strip_suffix(".json") { - return respond_with_json(arc, userid).await; - } - return make_err(404, "Invalid format"); + let x = req.uri().path(); + if x == "/" { + return Ok(Response::builder() + .status(302) + .header("Location", "https://git.nea.moe/nea/discordavatarproxy") + .body(Body::empty())?); + } + let request = match x.strip_prefix("/avatar/") { + None => return make_err(404, "Not found"), + Some(request) => request, + }; + if let Some(userid) = request.strip_suffix(".gif") { + return respond_with_image(arc, userid, true).await; + } + if let Some(userid) = request.strip_suffix(".png") { + return respond_with_image(arc, userid, false).await; + } + if let Some(userid) = request.strip_suffix(".json") { + return respond_with_json(arc, userid).await; + } + return make_err(404, "Invalid format"); } fn default_avatar_url(user_id: &str, discrim: &str) -> anyhow::Result<String> { - let d = discrim.parse::<u16>()?; - let id = user_id.parse::<u64>()?; - let bare = if d == 0 { - ((id >> 22) % 6) as u16 - } else { - d % 5 - }; - Ok(format!( - "https://cdn.discordapp.com/embed/avatars/{}.png", - bare - )) + let d = discrim.parse::<u16>()?; + let id = user_id.parse::<u64>()?; + let bare = if d == 0 { + ((id >> 22) % 6) as u16 + } else { + d % 5 + }; + Ok(format!( + "https://cdn.discordapp.com/embed/avatars/{}.png", + bare + )) } #[derive(Serialize, Debug)] struct ResponseUserFormat { - username: String, - discriminator: String, - avatar: String, - avatar_animated: String, - banner: Option<String>, - display_name: Option<String>, + username: String, + discriminator: String, + avatar: String, + avatar_animated: String, + banner: Option<String>, + display_name: Option<String>, } async fn respond_with_json(arc: Arc<Ctx>, userid: &str) -> anyhow::Result<Response<Body>> { - let json = unwrap_resp!(get_discord_data_for(&arc, userid).await?); - let avatar_url_animated = get_avatar_url(&json, true)?; - let avatar_url = get_avatar_url(&json, false)?; - let response = ResponseUserFormat { - username: json.username, - discriminator: json.discriminator, - avatar: avatar_url, - avatar_animated: avatar_url_animated, - display_name: json.global_name, - banner: json.banner.map(|hash| { - format!( - "https://cdn.discordapp.com/banners/{}/{}.png", - json.id, hash - ) - }), - }; - Ok(Response::builder() - .status(200) - .header("content-type", "application/json") - .body(serde_json::to_string(&response)?.into())?) + let json = unwrap_resp!(get_discord_data_for(&arc, userid).await?); + let avatar_url_animated = get_avatar_url(&json, true)?; + let avatar_url = get_avatar_url(&json, false)?; + let response = ResponseUserFormat { + username: json.username, + discriminator: json.discriminator, + avatar: avatar_url, + avatar_animated: avatar_url_animated, + display_name: json.global_name, + banner: json.banner.map(|hash| { + format!( + "https://cdn.discordapp.com/banners/{}/{}.png", + json.id, hash + ) + }), + }; + Ok(Response::builder() + .status(200) + .header("content-type", "application/json") + .body(serde_json::to_string(&response)?.into())?) } async fn get_discord_data_for( - arc: &Arc<Ctx>, - userid: &str, + arc: &Arc<Ctx>, + userid: &str, ) -> anyhow::Result<anyhow::Result<DiscordUserFormat, Response<Body>>> { - let num_id = match userid.parse::<u64>() { - Err(_) => return make_err(404, "Not found").map(Err), - Ok(num) => num, - }; - Ok(Ok( - match get_user_data(&arc.client, &arc.token, num_id).await { - Err(e) => { - eprintln!("Got error from discord: {:?}", e); - return make_err(502, "Discord failed to respond").map(Err); - } - Ok(user_data) => user_data, - }, - )) + let num_id = match userid.parse::<u64>() { + Err(_) => return make_err(404, "Not found").map(Err), + Ok(num) => num, + }; + Ok(Ok( + match get_user_data(&arc.client, &arc.token, num_id).await { + Err(e) => { + eprintln!("Got error from discord: {:?}", e); + return make_err(502, "Discord failed to respond").map(Err); + } + Ok(user_data) => user_data, + }, + )) } async fn respond_with_image( - arc: Arc<Ctx>, - userid: &str, - allow_animated: bool, + arc: Arc<Ctx>, + userid: &str, + allow_animated: bool, ) -> anyhow::Result<Response<Body>> { - let json = unwrap_resp!(get_discord_data_for(&arc, userid).await?); - let avatar_url = match get_avatar_url(&json, allow_animated) { - Err(_) => return make_err(502, "Discord failed to respond"), - Ok(avatar_url) => avatar_url, - }; - let resp = match arc.client.get(avatar_url.parse()?).await { - Err(_) => { - return make_err( - 502, - &format!("Discord failed to supply avatar for url: {}", avatar_url), - ) - } - Ok(avatar_data) => avatar_data, - }; - Ok(Response::builder() - .status(200) - .header( - "content-type", - if avatar_url.ends_with(".gif") { - "image/gif" - } else { - "image/png" - }, - ) - .body(resp.into_body())?) + let json = unwrap_resp!(get_discord_data_for(&arc, userid).await?); + let avatar_url = match get_avatar_url(&json, allow_animated) { + Err(_) => return make_err(502, "Discord failed to respond"), + Ok(avatar_url) => avatar_url, + }; + let resp = match arc.client.get(avatar_url.parse()?).await { + Err(_) => { + return make_err( + 502, + &format!("Discord failed to supply avatar for url: {}", avatar_url), + ) + } + Ok(avatar_data) => avatar_data, + }; + Ok(Response::builder() + .status(200) + .header( + "content-type", + if avatar_url.ends_with(".gif") { + "image/gif" + } else { + "image/png" + }, + ) + .body(resp.into_body())?) } struct Ctx { - client: Client<HttpsConnector<HttpConnector>>, - token: String, + client: Client<HttpsConnector<HttpConnector>>, + token: String, } async fn wrap_error(arc: Arc<Ctx>, req: Request<Body>) -> anyhow::Result<Response<Body>> { - return match resp(arc, req).await { - Err(e) => { - eprintln!("{:?}", e); - Ok(Response::builder() - .status(500) - .body("500 Internal Error".into())?) - } - Ok(o) => Ok(o), - }; + return match resp(arc, req).await { + Err(e) => { + eprintln!("{:?}", e); + Ok(Response::builder() + .status(500) + .body("500 Internal Error".into())?) + } + Ok(o) => Ok(o), + }; } #[tokio::main] async fn main() -> anyhow::Result<()> { - let token = env::var("TOKEN")?; - let portstr = env::var("PORT")?; - let port = portstr.parse::<u16>()?; - println!("Running with token: {}", token); - let https = HttpsConnector::new(); - let client = Client::builder().build::<_, Body>(https); - let arc = Arc::new(Ctx { client, token }); - let addr = SocketAddr::from(([127, 0, 0, 1], port)); - let service = make_service_fn(|_conn| { - let carc = Arc::clone(&arc); - async move { Ok::<_, anyhow::Error>(service_fn(move |req| wrap_error(Arc::clone(&carc), req))) } - }); - - let server = Server::bind(&addr).serve(service); - server.await?; - Ok(()) + let token = env::var("TOKEN")?; + let portstr = env::var("PORT")?; + let port = portstr.parse::<u16>()?; + println!("Running with token: {}", token); + let https = HttpsConnectorBuilder::new() + .with_native_roots() + .unwrap() + .https_only() + .enable_http1() + .build(); + let client = Client::builder().build::<_, Body>(https); + let arc = Arc::new(Ctx { client, token }); + let addr = SocketAddr::from(([127, 0, 0, 1], port)); + let service = make_service_fn(|_conn| { + let carc = Arc::clone(&arc); + async move { Ok::<_, anyhow::Error>(service_fn(move |req| wrap_error(Arc::clone(&carc), req))) } + }); + + let server = Server::bind(&addr).serve(service); + server.await?; + Ok(()) } |
