Module: TempmailSdk::Providers::TenminuteOne
- Defined in:
- lib/tempmail_sdk/providers/tenminute_one.rb
Overview
10minutemail.one 渠道 SSR NUXT_DATA 中的 mailServiceToken(JWT)+ 页面 emailDomains; 收信 GET web.10minutemail.one/api/v1/mailbox/email
Constant Summary collapse
- CHANNEL =
"10minute-one"- SITE_ORIGIN =
"https://10minutemail.one"- API_BASE =
"https://web.10minutemail.one/api/v1"- KNOWN_EMAIL_DOMAINS =
%w[xghff.com oqqaj.com psovv.com dbwot.com ygwpr.com imxwe.com].freeze
- DEFAULT_UA =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"- NUXT_DATA_RE =
/<script[^>]*\bid="__NUXT_DATA__"[^>]*>([\s\S]*?)<\/script>/i- JWT_RE =
/\A[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\z/
Class Method Summary collapse
- .api_headers(tok) ⇒ Object
-
.enc_mailbox_email(email) ⇒ Object
URL 编码邮箱(@ 编码为 %40).
- .generate_email(domain = nil) ⇒ EmailInfo
- .get_emails(email, token) ⇒ Array<Email>
- .item_needs_detail(m) ⇒ Object
-
.jwt_exp_ms(token) ⇒ Object
从 JWT 中提取过期时间戳(毫秒).
-
.parse_mail_service_token(arr) ⇒ Object
从 NUXT 数组中提取 mailServiceToken(JWT).
-
.parse_nuxt_array(html) ⇒ Object
解析 NUXT_DATA JSON 数组.
-
.parse_quoted_json_array(html, field) ⇒ Object
从 HTML 页面中提取字段对应的 JSON 数组(如 emailDomains、blockedUsernames).
- .pick_locale(domain) ⇒ Object
- .pick_mailbox_domain(hint, available) ⇒ Object
- .random_local(n) ⇒ Object
-
.resolve_ref(arr, value, depth = 0) ⇒ Object
解引用 NUXT 数组中的值(递归追踪索引引用).
Class Method Details
.api_headers(tok) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/tempmail_sdk/providers/tenminute_one.rb', line 154 def api_headers(tok) { "Accept" => "*/*", "Accept-Language" => "zh-CN,zh;q=0.9,en;q=0.8", "Authorization" => "Bearer #{tok}", "Cache-Control" => "no-cache", "Content-Type" => "application/json", "DNT" => "1", "Origin" => SITE_ORIGIN, "Pragma" => "no-cache", "Referer" => "#{SITE_ORIGIN}/", "User-Agent" => DEFAULT_UA, "X-Request-ID" => SecureRandom.hex(16), "X-Timestamp" => Time.now.to_i.to_s } end |
.enc_mailbox_email(email) ⇒ Object
URL 编码邮箱(@ 编码为 %40)
27 28 29 30 31 32 33 34 |
# File 'lib/tempmail_sdk/providers/tenminute_one.rb', line 27 def enc_mailbox_email(email) if email.include?("@") local, _, dom = email.partition("@") "#{URI.encode_www_form_component(local)}%40#{URI.encode_www_form_component(dom)}" else URI.encode_www_form_component(email) end end |
.generate_email(domain = nil) ⇒ EmailInfo
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/tempmail_sdk/providers/tenminute_one.rb', line 180 def generate_email(domain = nil) loc = pick_locale(domain) page_url = "#{SITE_ORIGIN}/#{loc}" r = Http.get(page_url, headers: { "User-Agent" => DEFAULT_UA, "Accept" => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Language" => "zh-CN,zh;q=0.9,en;q=0.8", "Cache-Control" => "no-cache", "Pragma" => "no-cache", "DNT" => "1", "Referer" => page_url }) r.raise_for_status html = r.body arr = parse_nuxt_array(html) token = parse_mail_service_token(arr) domains = (parse_quoted_json_array(html, "emailDomains") + KNOWN_EMAIL_DOMAINS).uniq domains = KNOWN_EMAIL_DOMAINS.dup if domains.empty? blocked = parse_quoted_json_array(html, "blockedUsernames").map(&:downcase).to_set dom_hint = domain && domain.to_s.strip.include?(".") ? domain.to_s.strip : nil mail_domain = pick_mailbox_domain(dom_hint, domains) local = nil 32.times do cand = random_local(10) unless blocked.include?(cand.downcase) local = cand break end end raise "10minute-one: could not pick username" unless local address = "#{local}@#{mail_domain}" exp_ms = jwt_exp_ms(token) EmailInfo.new(channel: CHANNEL, email: address, token: token, expires_at: exp_ms) end |
.get_emails(email, token) ⇒ Array<Email>
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/tempmail_sdk/providers/tenminute_one.rb', line 224 def get_emails(email, token) url = "#{API_BASE}/mailbox/#{enc_mailbox_email(email)}" r = Http.get(url, headers: api_headers(token)) r.raise_for_status data = r.json raise "10minute-one: invalid inbox JSON" unless data.is_a?(Array) data.filter_map do |raw| next unless raw.is_a?(Hash) row = raw.dup if item_needs_detail(row) mid = row["id"].to_s detail_url = "#{API_BASE}/mailbox/#{enc_mailbox_email(email)}/#{URI.encode_www_form_component(mid)}" begin d = Http.get(detail_url, headers: api_headers(token)) if d.ok? detail = d.json if detail.is_a?(Hash) detail.each { |k, v| row[k] = v unless row.key?(k) } end end rescue StandardError # 忽略详情失败 end end Normalize.normalize_email(row, email) end end |
.item_needs_detail(m) ⇒ Object
171 172 173 174 175 176 |
# File 'lib/tempmail_sdk/providers/tenminute_one.rb', line 171 def item_needs_detail(m) return false if m["id"].to_s.strip.empty? body = (m["text"] || m["body"] || m["html"] || m["mail_text"] || "").to_s.strip body.empty? end |
.jwt_exp_ms(token) ⇒ Object
从 JWT 中提取过期时间戳(毫秒)
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/tempmail_sdk/providers/tenminute_one.rb', line 137 def jwt_exp_ms(token) parts = token.split(".") return nil if parts.length < 2 b = parts[1] # 补齐 Base64 填充 b += "=" * ((-b.length) % 4) b = b.tr("-_", "+/") payload = JSON.parse(Base64.decode64(b)) exp = payload["exp"] return (exp * 1000).to_i if exp.is_a?(Numeric) nil rescue StandardError nil end |
.parse_mail_service_token(arr) ⇒ Object
从 NUXT 数组中提取 mailServiceToken(JWT)
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/tempmail_sdk/providers/tenminute_one.rb', line 54 def parse_mail_service_token(arr) # 先扫描前 48 项 n = [arr.length, 48].min n.times do |i| el = arr[i] next unless el.is_a?(Hash) && el.key?("mailServiceToken") t = resolve_ref(arr, el["mailServiceToken"]) return t if t.is_a?(String) && t.match?(JWT_RE) end # 全量扫描 arr.each do |el| next unless el.is_a?(Hash) && el.key?("mailServiceToken") t = resolve_ref(arr, el["mailServiceToken"]) return t if t.is_a?(String) && t.match?(JWT_RE) end # 最后尝试找裸 JWT 字符串 arr.each do |el| return el if el.is_a?(String) && el.match?(JWT_RE) end raise "10minute-one: mailServiceToken not found" end |
.parse_nuxt_array(html) ⇒ Object
解析 NUXT_DATA JSON 数组
37 38 39 40 41 42 |
# File 'lib/tempmail_sdk/providers/tenminute_one.rb', line 37 def parse_nuxt_array(html) m = html.match(NUXT_DATA_RE) raise "10minute-one: __NUXT_DATA__ not found" unless m JSON.parse(m[1].strip) end |
.parse_quoted_json_array(html, field) ⇒ Object
从 HTML 页面中提取字段对应的 JSON 数组(如 emailDomains、blockedUsernames)
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 |
# File 'lib/tempmail_sdk/providers/tenminute_one.rb', line 79 def parse_quoted_json_array(html, field) key = "#{field}:\"" start = html.index(key) return [] unless start slice_start = start + key.length return [] if slice_start >= html.length || html[slice_start] != "[" depth = 0 j = slice_start while j < html.length c = html[j] if c == "[" depth += 1 elsif c == "]" depth -= 1 if depth.zero? j += 1 break end end j += 1 end raw = html[slice_start...j] unesc = raw.gsub('\\"', '"') v = JSON.parse(unesc) v.is_a?(Array) ? v : [] rescue JSON::ParserError [] end |
.pick_locale(domain) ⇒ Object
110 111 112 113 114 115 116 |
# File 'lib/tempmail_sdk/providers/tenminute_one.rb', line 110 def pick_locale(domain) s = domain.to_s.strip return "zh" if s.empty? return "zh" if s.include?(".") && !s.include?("/") s end |
.pick_mailbox_domain(hint, available) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/tempmail_sdk/providers/tenminute_one.rb', line 118 def pick_mailbox_domain(hint, available) raise "10minute-one: no email domains" if available.empty? if hint && !hint.empty? h = hint.strip.downcase if h.include?(".") found = available.find { |d| d.downcase == h } return found if found end end available.sample end |
.random_local(n) ⇒ Object
131 132 133 134 |
# File 'lib/tempmail_sdk/providers/tenminute_one.rb', line 131 def random_local(n) chars = "abcdefghijklmnopqrstuvwxyz0123456789" Array.new(n) { chars[rand(chars.length)] }.join end |
.resolve_ref(arr, value, depth = 0) ⇒ Object
解引用 NUXT 数组中的值(递归追踪索引引用)
45 46 47 48 49 50 51 |
# File 'lib/tempmail_sdk/providers/tenminute_one.rb', line 45 def resolve_ref(arr, value, depth = 0) return value if depth > 64 return value if value == true || value == false return value unless value.is_a?(Integer) && value >= 0 && value < arr.length resolve_ref(arr, arr[value], depth + 1) end |