homebrew-core/Formula/inlets.rb

115 lines
3.7 KiB
Ruby

class Inlets < Formula
desc "Expose your local endpoints to the Internet"
homepage "https://github.com/inlets/inlets"
url "https://github.com/inlets/inlets.git",
tag: "2.7.3",
revision: "2934c3c247ea28996ebb50889403befced94b29a"
license "MIT"
livecheck do
url "https://github.com/inlets/inlets/releases/latest"
regex(%r{href=.*?/tag/v?(\d+(?:\.\d+)+)["' >]}i)
end
bottle do
cellar :any_skip_relocation
sha256 "8706b38a917c0acfa8343479db33c206a78132d01992834a39816400d65949f9" => :catalina
sha256 "20ee65ae593585ac381997df0862a2267383a216a14440e75f775373df9117fb" => :mojave
sha256 "43a486286ab55be362fd2c2a5c3a5764b00fd671fc93077d82b70b2719a62e5a" => :high_sierra
end
depends_on "go" => :build
uses_from_macos "ruby" => :test
def install
ENV["GOPATH"] = buildpath
(buildpath/"src/github.com/inlets/inlets").install buildpath.children
cd "src/github.com/inlets/inlets" do
commit = Utils.safe_popen_read("git", "rev-parse", "HEAD").chomp
system "go", "build", "-ldflags",
"-s -w -X main.GitCommit=#{commit} -X main.Version=#{version}",
"-a",
"-installsuffix", "cgo", "-o", bin/"inlets"
prefix.install_metafiles
end
end
def cleanup(name, pid)
puts "Tearing down #{name} on PID #{pid}"
Process.kill("TERM", pid)
Process.wait(pid)
end
test do
upstream_port = free_port
remote_port = free_port
MOCK_RESPONSE = "INLETS OK".freeze
SECRET_TOKEN = "itsasecret-sssshhhhh".freeze
(testpath/"mock_upstream_server.rb").write <<~EOS
require 'socket'
server = TCPServer.new('localhost', #{upstream_port})
loop do
socket = server.accept
request = socket.gets
STDERR.puts request
response = "OK\\n"
shutdown = false
if request.include? "inlets-test"
response = "#{MOCK_RESPONSE}\\n"
shutdown = true
end
socket.print "HTTP/1.1 200 OK\\r\\n" +
"Host: localhost:#{upstream_port}\\r\\n" +
"Content-Type: text/plain\\r\\n" +
"Content-Length: \#\{response.bytesize\}\\r\\n" +
"Connection: close\\r\\n"
socket.print "\\r\\n"
socket.print response
socket.close
if shutdown
puts "Exiting test server"
exit 0
end
end
EOS
mock_upstream_server_pid = fork do
exec "ruby mock_upstream_server.rb"
end
begin
stable_resource = stable.instance_variable_get(:@resource)
commit = stable_resource.instance_variable_get(:@specs)[:revision]
# Basic --version test
inlets_version = shell_output("#{bin}/inlets version")
assert_match /\s#{commit}$/, inlets_version
assert_match /\s#{version}$/, inlets_version
# Client/Server e2e test
# This test involves establishing a client-server inlets tunnel on the
# remote_port, running a mock server on the upstream_port and then
# testing that we can hit the mock server upstream_port via the tunnel remote_port
sleep 3 # Waiting for mock server
server_pid = fork do
exec "#{bin}/inlets server --port #{remote_port} --token #{SECRET_TOKEN}"
end
client_pid = fork do
# Starting inlets client
exec "#{bin}/inlets client --remote localhost:#{remote_port} " \
"--upstream localhost:#{upstream_port} --token #{SECRET_TOKEN}"
end
sleep 3 # Waiting for inlets websocket tunnel
assert_match MOCK_RESPONSE, shell_output("curl -s http://localhost:#{remote_port}/inlets-test")
ensure
cleanup("Mock Server", mock_upstream_server_pid)
cleanup("Inlets Server", server_pid)
cleanup("Inlets Client", client_pid)
end
end
end