Module: TempmailSdk::Providers::XkxMe
- Defined in:
- lib/tempmail_sdk/providers/xkx_me.rb
Overview
xkx.me 临时邮箱渠道(CSRF + session cookie) GET / 获取 CSRF token + session cookie POST /mailbox/create/random 创建邮箱(不跟随重定向,从 Location 提取邮箱) GET /mailbox/email/messages 获取邮件列表
Constant Summary collapse
- CHANNEL =
"xkx-me"- BASE_URL =
"https://xkx.me"- CSRF_RE =
/csrf-token"\s+content="([^"]+)"/- HEADERS =
{ "User-Agent" => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/149.0.0.0 Safari/537.36" }.freeze
Class Method Summary collapse
- .extract_cookie_header(resp) ⇒ Object
- .generate_email ⇒ EmailInfo
- .get_emails(token, email) ⇒ Array<Email>
Class Method Details
.extract_cookie_header(resp) ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/tempmail_sdk/providers/xkx_me.rb', line 23 def (resp) = {} resp..each do |line| pair = line.split(";").first.to_s.strip k, v = pair.split("=", 2) [k] = v if k && v end .sort.map { |k, v| "#{k}=#{v}" }.join("; ") end |
.generate_email ⇒ EmailInfo
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/tempmail_sdk/providers/xkx_me.rb', line 34 def generate_email # 获取 CSRF token 和 session cookie resp = Http.get(BASE_URL, headers: HEADERS, timeout: 15) resp.raise_for_status m = resp.body.match(CSRF_RE) raise "xkx-me: 无法获取 CSRF token" unless m csrf = m[1] = (resp) # POST 创建邮箱(不跟随重定向) post_headers = HEADERS.merge( "Content-Type" => "application/x-www-form-urlencoded", "Referer" => "#{BASE_URL}/" ) post_headers["Cookie"] = unless .empty? body = "_token=#{URI.encode_www_form_component(csrf)}" resp2 = Http.post("#{BASE_URL}/mailbox/create/random", headers: post_headers, body: body, timeout: 15) # 从 Location 头提取邮箱地址(302 重定向) location = resp2.header("Location") || "" email_match = location.match(/mailbox\/([^\/"'<>\s]+@xkx\.me)/) raise "xkx-me: 无法从响应中提取邮箱地址" unless email_match email = email_match[1] EmailInfo.new(channel: CHANNEL, email: email, token: ) end |
.get_emails(token, email) ⇒ Array<Email>
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/tempmail_sdk/providers/xkx_me.rb', line 69 def get_emails(token, email) address = (email || "").strip raise "xkx-me: 缺少邮箱地址" if address.empty? = (token || "").strip headers = HEADERS.merge( "Accept" => "application/json", "X-Requested-With" => "XMLHttpRequest" ) headers["Cookie"] = unless .empty? encoded_email = URI.encode_www_form_component(address).gsub("%40", "@") resp = Http.get("#{BASE_URL}/mailbox/#{encoded_email}/messages", headers: headers, timeout: 15) return [] if resp.status_code == 404 resp.raise_for_status data = resp.json return [] unless data = if data.is_a?(Array) data elsif data.is_a?(Hash) if data.key?("messages") data["messages"] elsif data["message"].is_a?(Hash) [data["message"]] else [] end else [] end return [] unless .is_a?(Array) .filter_map do |msg| next unless msg.is_a?(Hash) raw = { "id" => (msg["id"] || "").to_s, "from" => (msg["from"] || "").to_s, "to" => address, "subject" => (msg["subject"] || "").to_s, "date" => (msg["date"] || "").to_s, "html" => (msg["html"] || msg["body"] || "").to_s, "text" => (msg["text"] || "").to_s, "is_read" => false, "attachments" => [] } Normalize.normalize_email(raw, address) end end |