Skip to content

Commit 54aec90

Browse files
committed
Fix parse ip138 return 502
1 parent 211776c commit 54aec90

File tree

1 file changed

+33
-16
lines changed

1 file changed

+33
-16
lines changed

src/myip.cr

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ require "lexbor"
22
require "./myip/*"
33
require "http/client"
44
require "json"
5+
require "http/headers"
56

67
class Myip
78
getter chan = Channel(Tuple(String, String)).new
@@ -12,51 +13,67 @@ class Myip
1213
response = HTTP::Client.get(url)
1314
result = JSON.parse(response.body)
1415
io = IO::Memory.new
15-
PrettyPrint.format(result, io, 79)
16+
PrettyPrint.format(result, io, width: 79)
1617
io.rewind
17-
chan.send({"ip.sb/geoip:您访问外网地址信息:\n", io.gets_to_end})
18+
chan.send({"----- Result from: #{url}:您访问外网地址信息:-----\n", io.gets_to_end})
1819
rescue ex : ArgumentError | Socket::Error
19-
chan.send({"ip.sb/geoip:", ex.message.not_nil!})
20+
chan.send({"----- Error from: #{url}:-----\n", ex.message.not_nil!})
2021
end
2122
end
2223

2324
def get_ip_from_ip138
2425
spawn do
25-
url = "http://www.ip138.com"
26-
doc = from_url(url, follow: true)
26+
url = "https://www.ip138.com"
27+
doc, code = from_url(url, follow: true)
2728
ip138_url = doc.css("iframe").first.attribute_by("src")
28-
url = "http:#{ip138_url}"
29-
doc = from_url(url)
29+
headers = HTTP::Headers{"Origin" => "https://ip.skk.moe"}
3030

31-
chan.send({"ip138.com:", doc.css("body p").first.tag_text.strip})
31+
doc, code = from_url("https:#{ip138_url}", headers: headers)
32+
33+
if code == 502
34+
myip = doc.css("body p span.F").first.tag_text[/IP:\s*([0-9.]+)/, 1]
35+
url = "https://www.ip138.com/iplookup.php?ip=#{myip}"
36+
doc, code = from_url(url, headers: headers)
37+
38+
output = String.build do |io|
39+
doc.css("div.table-box>table>tbody tr").each { |x| io << x.tag_text }
40+
end
41+
42+
chan.send({"----- Result from: #{url}:-----\n", output.squeeze('\n')})
43+
else
44+
chan.send({"----- Result from: #{url}:-----\n", doc.css("body p").first.tag_text.strip})
45+
end
3246
rescue ex : ArgumentError | Socket::Error
33-
chan.send({"ip138.com:", ex.message.not_nil!})
47+
chan.send({"----- Error from: #{url}:-----\n", ex.message.not_nil!})
3448
end
3549
end
3650

3751
def process
3852
2.times do
3953
select
4054
when value = chan.receive
41-
title, ip = value
42-
STDERR.puts "#{title}#{ip}"
55+
title, ipinfo = value
56+
STDERR.puts "#{title}#{ipinfo}"
4357
when timeout 5.seconds
4458
STDERR.puts "Timeout, check your network connection!"
4559
exit
4660
end
4761
end
4862
end
4963

50-
private def from_url(url : String, follow : Bool = false) : Lexbor::Parser
51-
response = HTTP::Client.get url
64+
private def from_url(url : String, *, follow : Bool = false, headers = HTTP::Headers.new) : Tuple(Lexbor::Parser, Int32)
65+
response = HTTP::Client.get url, headers: headers
5266
if response.status_code == 200
53-
Lexbor::Parser.new(response.body)
67+
{Lexbor::Parser.new(response.body), 200}
5468
elsif follow && response.status_code == 301
55-
from_url response.headers["Location"], follow: true
69+
from_url response.headers["Location"], follow: true, headers: headers
70+
elsif response.status_code == 502
71+
{Lexbor::Parser.new(response.body), 502}
5672
else
5773
raise ArgumentError.new "Host #{url} returned #{response.status_code}"
5874
end
59-
rescue Socket::Error
75+
rescue e : Socket::Error
76+
e.inspect_with_backtrace(STDERR)
6077
raise Socket::Error.new "Visit #{url} failed, please check your internet connection."
6178
end
6279
end

0 commit comments

Comments
 (0)