Created
January 30, 2025 07:07
-
-
Save sevenc-nanashi/8f69fa906e017043a27d35c271d6a05d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require "yaml" | |
require "json" | |
ALL_PATH = File.expand_path("~/.cfd-hosts") | |
if File.exist?(ALL_PATH) | |
hosts = JSON.parse(File.read(ALL_PATH), symbolize_names: true) | |
else | |
hosts = [] | |
end | |
subcommand = ARGV.shift | |
case subcommand | |
when "add" | |
port = ARGV.shift | |
name = ARGV.shift | |
if port.nil? || name.nil? | |
puts "Usage: cfd add <port> <name>" | |
exit 1 | |
end | |
hosts << { port: port, name: name } | |
File.write(ALL_PATH, JSON.dump(hosts)) | |
puts "Added #{name}" | |
when "list" | |
puts hosts | |
exit 0 | |
when "remove" | |
host = ARGV.shift | |
hosts.delete(host) | |
File.write(ALL_PATH, JSON.dump(hosts)) | |
puts "Removed #{host}" | |
when "refresh" | |
else | |
puts "Usage: cfd [add|list|remove|refresh]" | |
exit 1 | |
end | |
config = { | |
"tunnel" => "...", | |
"credentials-file" => "...", | |
"ingress" => [ | |
*( | |
hosts.map do |host| | |
{ | |
"hostname" => "#{host[:name]}.sevenc7c.eu.org", | |
"service" => "http://localhost:#{host[:port]}" | |
} | |
end | |
), | |
{ "service" => "http_status:404" } | |
] | |
} | |
File.write(File.expand_path("~/.cloudflared/config.yml"), YAML.dump(config)) | |
system "systemctl --user restart cloudflared.service" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment