diff --git a/shard.lock b/shard.lock index 9bf16b0..718c38e 100644 --- a/shard.lock +++ b/shard.lock @@ -4,6 +4,10 @@ shards: git: https://github.com/naqvis/crystar.git version: 0.4.0+git.commit.3975502cbe89bca528e1af4b86fbfc0f0bfacce1 + ip_address_list: + git: https://github.com/mamantoha/ip_address_list.git + version: 0.1.0 + qr-code: git: https://github.com/spider-gazelle/qr-code.git version: 1.0.3 diff --git a/shard.yml b/shard.yml index b5741cf..e376e9a 100644 --- a/shard.yml +++ b/shard.yml @@ -13,6 +13,8 @@ dependencies: version: ">= 0.2.0" qr-code: github: spider-gazelle/qr-code + ip_address_list: + github: mamantoha/ip_address_list targets: zipstream: diff --git a/spec/zipstream/config_spec.cr b/spec/zipstream/config_spec.cr new file mode 100644 index 0000000..509847f --- /dev/null +++ b/spec/zipstream/config_spec.cr @@ -0,0 +1,11 @@ +require "../spec_helper" + +describe Zipstream::Config do + config = Zipstream::Config.new + + context "host" do + it "should has host as local IP address" do + config.host.should match(/\d+\.\d+\.\d+\.\d+/) + end + end +end diff --git a/src/zipstream.cr b/src/zipstream.cr index 6d68c18..04639a8 100644 --- a/src/zipstream.cr +++ b/src/zipstream.cr @@ -5,6 +5,7 @@ require "option_parser" require "crystar" require "zip64" require "qr-code" +require "ip_address_list" require "./zipstream/**" diff --git a/src/zipstream/config.cr b/src/zipstream/config.cr index e7801a7..b4b2a33 100644 --- a/src/zipstream/config.cr +++ b/src/zipstream/config.cr @@ -2,7 +2,7 @@ module Zipstream class Config INSTANCE = Config.new - property host + setter host : String? = nil property port property? log : Bool = false property? web : Bool = false @@ -21,7 +21,6 @@ module Zipstream property? no_banner : Bool = false def initialize - @host = "0.0.0.0" @port = 8090 @format = "zip" @output = "download" @@ -30,6 +29,10 @@ module Zipstream @env = "development" end + def host + @host || local_ip_address.try(&.address) || "0.0.0.0" + end + def filename [output, format].join(".") end @@ -53,5 +56,11 @@ module Zipstream str << url_path end end + + def local_ip_address : Socket::IPAddress? + Socket.ip_address_list.find do |ip_address| + ip_address.family == Socket::Family::INET && ip_address.private? && !ip_address.address.starts_with?("127") + end + end end end