Skip to content

Commit

Permalink
Merge pull request #49 from mamantoha/local_ip_address
Browse files Browse the repository at this point in the history
use local IP address by default
  • Loading branch information
mamantoha authored Sep 20, 2024
2 parents 85c8d6b + ee722a8 commit da2c13d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 2 deletions.
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

0 comments on commit da2c13d

Please sign in to comment.