我试图在config.hosts中白名单我的Ngrok隧道主机名,但是每次启动ngrok时它都会改变。有没有办法获得我的Ngrok隧道的公共网址,这样我就不用使用hosts.clear了
发布于 2021-12-15 09:46:38
只需像这样配置config/environments/development.rb:
Rails.application.configure do
...
config.hosts << '.ngrok.io'
end这将允许来自ngrok.io的任何子域的请求。
发布于 2020-02-18 01:31:20
Ngrok有一个内置的JSON,可以在localhost:4040/api/tunnels上找到。
# lib/ngrok_client.rb
module NgrokClient
def self.tunnels
response = Net::HTTP.get(URI('http://localhost:4040/api/tunnels'))
begin
JSON.parse(response).dig("tunnels")
rescue JSON::ParserError => e
Rails.logger.error %q{
Failed to parse reponse from Ngroks internal API. Are you sure its running?
}
end
end
def self.public_urls
tunnels.select do |h|
# TODO - don't hardcode the host name
h.dig("config", "addr") == "http://localhost:3000"
end.map { |h| URI(h["public_url"]).hostname }.uniq
end
end# config/environments/development.rb
Rails.application.configure do
# ...
require 'ngrok_client'
config.hosts += NgrokClient.public_urls
end还有一个隧道 gem,可以用来从中间件层启动ngrok,作为一个不那么麻烦的解决方案。
发布于 2022-05-10 15:30:54
https://stackoverflow.com/a/70361487/14001685没有为我工作(Rails 6),但regex为我工作。
config.hosts << %r{.+\.ngrok.io$}https://stackoverflow.com/questions/60272560
复制相似问题