Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use local IP address by default #49

Merged
merged 5 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions shard.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 11 additions & 0 deletions spec/zipstream/config_spec.cr
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions src/zipstream.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ require "option_parser"
require "crystar"
require "zip64"
require "qr-code"
require "ip_address_list"

require "./zipstream/**"

Expand Down
13 changes: 11 additions & 2 deletions src/zipstream/config.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,7 +21,6 @@ module Zipstream
property? no_banner : Bool = false

def initialize
@host = "0.0.0.0"
@port = 8090
@format = "zip"
@output = "download"
Expand All @@ -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
Expand All @@ -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