zget 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5a831b06d7c4da66c9b0632b5110bc6031754ac1
4
+ data.tar.gz: 69b1be9ef5e06e7f5651cc7c8d9c5aa01e9ae5c1
5
+ SHA512:
6
+ metadata.gz: 690435177f2fa5d251e575cd71235dcf324abf71e0a970948161847443ed2c9964fbe5b8e6cff64918f0b5afd55a2188a77a5497e49f1e47b9bfe3383d49db77
7
+ data.tar.gz: dcf962e64d9804c051207bf80fd34cc1c805f05370c46238e19ec040b7e2957184afd127a7d819f9165b40db2806fc6c4217ca2cd5c079755cfa59db14a5b0ff
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ before_install: gem install bundler -v 1.14.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in zget.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Patricio Mac Adden
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,111 @@
1
+ # Zget
2
+
3
+ A simple, zeroconf-based, p2p file transfer utility. A port of [zget](https://github.com/nils-werner/zget).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem "zget"
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ ```
22
+ $ gem install zget
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ ### Sharing
28
+
29
+ ```
30
+ $ zput awesome_file.zip
31
+ ```
32
+
33
+ Then the other end can just:
34
+
35
+ ```
36
+ $ zget awesome_file.zip
37
+ ```
38
+
39
+ ### Receiving
40
+
41
+ ```
42
+ $ zget awesome_file.zip
43
+ ```
44
+
45
+ Then the other end can just:
46
+
47
+ ```
48
+ $ zput awesome_file.zip
49
+ ```
50
+
51
+ ### Aliases
52
+
53
+ In case of complicated filenames you can use aliases or have `zget`/`zput`
54
+ generate one for you:
55
+
56
+ ```
57
+ $ zput myReaLlyComp_licat3d_fiLeNam3.zip
58
+ Download this file using `zget myReaLlyComp_licat3d_fiLeNam3.zip` or `zget e208`
59
+ ```
60
+
61
+ Also `zget` can generate an alias so you can initiate the transfer without
62
+ knowing what file will be transferred:
63
+
64
+ ```
65
+ $ zget
66
+ Upload a file using `zput <filename> 8332`
67
+ ```
68
+
69
+ ### Options
70
+
71
+ To see the available options you can just run:
72
+
73
+ ```
74
+ $ zget -h
75
+ ```
76
+
77
+ or
78
+
79
+ ```
80
+ $ zput -h
81
+ ```
82
+
83
+ ## Compatibility
84
+
85
+ Unfortunately this version isn't compatible with python's zget. The main reason
86
+ is because [dnssd](github.com/tenderlove/dnssd) doesn't play well with service
87
+ names with a dot, therefore we can't use the same service name as python's zget.
88
+
89
+ ## Development
90
+
91
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run
92
+ `rake spec` to run the tests. You can also run `bin/console` for an interactive
93
+ prompt that will allow you to experiment.
94
+
95
+ To install this gem onto your local machine, run `bundle exec rake install`.
96
+ To release a new version, update the version number in `version.rb`, and then
97
+ run `bundle exec rake release`, which will create a git tag for the version,
98
+ push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
99
+
100
+ ## TODO
101
+
102
+ * Improve the test suite
103
+
104
+ ## Contributing
105
+
106
+ Bug reports and pull requests are welcome on GitHub at [https://github.com/patriciomacadden/zget](https://github.com/patriciomacadden/zget).
107
+
108
+ ## License
109
+
110
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
111
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "zget"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "optparse"
4
+ require "zget"
5
+
6
+ options = {}
7
+
8
+ parser = OptionParser.new do |opts|
9
+ opts.banner = "Usage: #{File.basename __FILE__} [options] file_or_alias [output]"
10
+
11
+ opts.on "-h", "--help", "Show this message" do
12
+ puts opts.help
13
+ exit
14
+ end
15
+
16
+ opts.on "-v", "--version", "Show version" do
17
+ puts "#{File.basename __FILE__} #{Zget::VERSION}"
18
+ exit
19
+ end
20
+ end
21
+
22
+ parser.parse!
23
+
24
+ if ARGV.size > 2
25
+ puts parser.help
26
+ exit
27
+ end
28
+
29
+ options[:file_or_alias] = ARGV.first
30
+ options[:output] = ARGV.size == 2 ? ARGV.last : nil
31
+
32
+ Zget.get options
33
+
@@ -0,0 +1,51 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "optparse"
4
+ require "zget"
5
+
6
+ options = {}
7
+
8
+ parser = OptionParser.new do |opts|
9
+ opts.banner = "Usage: #{File.basename __FILE__} [options] file [alias]"
10
+
11
+ opts.on "-b", "--bind-address BIND_ADDRESS", "The address to share the file on (default: 0.0.0.0)" do |bind_address|
12
+ options[:bind_address] = bind_address
13
+ end
14
+
15
+ opts.on "-h", "--help", "Show this message" do
16
+ puts opts.help
17
+ exit
18
+ end
19
+
20
+ opts.on "-p", "--port PORT", Integer, "The port to share the file on (default: 6666)" do |port|
21
+ options[:port] = port
22
+ end
23
+
24
+ opts.on "-v", "--version", "Show version" do
25
+ puts "#{File.basename __FILE__} #{Zget::VERSION}"
26
+ exit
27
+ end
28
+ end
29
+
30
+ parser.parse!
31
+
32
+ if ARGV.none? || ARGV.size > 2
33
+ puts parser.help
34
+ exit
35
+ end
36
+
37
+ options[:file] = ARGV.first
38
+ if ARGV.size == 2
39
+ options[:_alias] = ARGV.last
40
+ end
41
+
42
+ begin
43
+ Zget.put options
44
+ rescue IPAddr::InvalidAddressError
45
+ puts "Invalid bind address"
46
+ rescue Zget::InvalidFileError
47
+ puts "Invalid file"
48
+ rescue Zget::InvalidPortError
49
+ puts "Invalid port"
50
+ end
51
+
@@ -0,0 +1,25 @@
1
+ require "digest"
2
+ require "dnssd"
3
+ require "ipaddr"
4
+ require "open-uri"
5
+ require "securerandom"
6
+ require "socket"
7
+ require "webrick"
8
+
9
+ require "zget/command"
10
+ require "zget/command/zget"
11
+ require "zget/command/zput"
12
+ require "zget/errors"
13
+ require "zget/file_handler"
14
+ require "zget/version"
15
+
16
+ module Zget
17
+ def self.get(file_or_alias: nil, output: nil)
18
+ Zget.new(file_or_alias: file_or_alias, output: output).call
19
+ end
20
+
21
+ def self.put(_alias: SecureRandom.hex(2), bind_address: "0.0.0.0", file:, port: 6666)
22
+ Zput.new(_alias: _alias, bind_address: bind_address, file: file, port: port).call
23
+ end
24
+ end
25
+
@@ -0,0 +1,8 @@
1
+ module Zget
2
+ class Command
3
+ def call
4
+ raise NotImplementedError
5
+ end
6
+ end
7
+ end
8
+
@@ -0,0 +1,53 @@
1
+ module Zget
2
+ class Zget < Command
3
+ attr_reader :file_or_alias, :output
4
+
5
+ def initialize(file_or_alias: nil, output: nil)
6
+ @file_or_alias = file_or_alias
7
+ @output = output
8
+ end
9
+
10
+ def call
11
+ if file_or_alias.nil?
12
+ @file_or_alias = SecureRandom.hex 2
13
+ puts "Upload a file using `zput <filename> #{file_or_alias}`"
14
+ else
15
+ puts "Upload a file using `zput #{file_or_alias}` or `zput <filename> #{file_or_alias}`"
16
+ end
17
+
18
+ services = []
19
+ DNSSD.browse! "_http._tcp" do |reply|
20
+ services << reply
21
+
22
+ next if reply.flags.more_coming?
23
+
24
+ service = services.detect { |s| s.name == hash }
25
+
26
+ unless service.nil?
27
+ DNSSD.resolve! service do |r|
28
+ open url(r) do |f|
29
+ filename = output || f.meta["content-disposition"].split(";").last.split("=").last
30
+ File.write filename, f.read
31
+ end
32
+ break
33
+ end
34
+ end
35
+ break
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ def hash
42
+ Digest::SHA1.hexdigest File.basename(file_or_alias)
43
+ end
44
+
45
+ def url(reply)
46
+ info = Socket.getaddrinfo(reply.target, nil, Socket::AF_INET)
47
+ host = info[0][2]
48
+
49
+ "http://#{host}:#{reply.port}"
50
+ end
51
+ end
52
+ end
53
+
@@ -0,0 +1,43 @@
1
+ module Zget
2
+ class Zput < Command
3
+ attr_reader :alias, :bind_address, :file, :port
4
+
5
+ def initialize(_alias: SecureRandom.hex(2), bind_address: "0.0.0.0", file:, port: 6666)
6
+ @bind_address = IPAddr.new bind_address
7
+ @file = file.tap do |f|
8
+ raise InvalidFileError unless File.exist? f
9
+ end
10
+ @alias = _alias
11
+ @port = port.tap do |p|
12
+ raise InvalidPortError unless p.between? 0, 65535
13
+ end
14
+ end
15
+
16
+ def call
17
+ puts "Download this file using `zget #{File.basename file}` or `zget #{@alias}`"
18
+
19
+ hashes.each do |hash|
20
+ DNSSD.register hash, "_http._tcp", nil, port
21
+ end
22
+
23
+ server.start
24
+ end
25
+
26
+ private
27
+
28
+ def hashes
29
+ [
30
+ Digest::SHA1.hexdigest(File.basename(file)),
31
+ Digest::SHA1.hexdigest(@alias)
32
+ ]
33
+ end
34
+
35
+ def server
36
+ @server ||= WEBrick::HTTPServer.new(AccessLog: [], BindAddress: bind_address.to_s, Logger: WEBrick::Log.new("/dev/null"), Port: port).tap do |s|
37
+ s.mount "/", FileHandler, file
38
+ trap("INT") { s.shutdown }
39
+ end
40
+ end
41
+ end
42
+ end
43
+
@@ -0,0 +1,5 @@
1
+ module Zget
2
+ InvalidFileError = Class.new StandardError
3
+ InvalidPortError = Class.new StandardError
4
+ end
5
+
@@ -0,0 +1,11 @@
1
+ module Zget
2
+ # We want this server to shutdown as soon as the file is downloaded
3
+ class FileHandler < WEBrick::HTTPServlet::DefaultFileHandler
4
+ def do_GET(request, response)
5
+ response["Content-Disposition"] = "inline; filename=#{File.basename @local_path}"
6
+ super
7
+ @server.shutdown
8
+ end
9
+ end
10
+ end
11
+
@@ -0,0 +1,3 @@
1
+ module Zget
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,38 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "zget/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "zget"
8
+ spec.version = Zget::VERSION
9
+ spec.authors = ["Patricio Mac Adden"]
10
+ spec.email = ["patriciomacadden@gmail.com"]
11
+
12
+ spec.summary = %q{Write a short summary, because Rubygems requires one.}
13
+ spec.description = %q{Write a longer description or delete this line.}
14
+ spec.homepage = "https://github.com/patriciomacadden/zget"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against " \
23
+ # "public gem pushes."
24
+ # end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.14"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "rspec", "~> 3.0"
36
+
37
+ spec.add_runtime_dependency "dnssd", "~> 3.0.1"
38
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zget
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Patricio Mac Adden
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-04-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: dnssd
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 3.0.1
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 3.0.1
69
+ description: Write a longer description or delete this line.
70
+ email:
71
+ - patriciomacadden@gmail.com
72
+ executables:
73
+ - zget
74
+ - zput
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".gitignore"
79
+ - ".rspec"
80
+ - ".travis.yml"
81
+ - Gemfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/setup
87
+ - exe/zget
88
+ - exe/zput
89
+ - lib/zget.rb
90
+ - lib/zget/command.rb
91
+ - lib/zget/command/zget.rb
92
+ - lib/zget/command/zput.rb
93
+ - lib/zget/errors.rb
94
+ - lib/zget/file_handler.rb
95
+ - lib/zget/version.rb
96
+ - zget.gemspec
97
+ homepage: https://github.com/patriciomacadden/zget
98
+ licenses:
99
+ - MIT
100
+ metadata: {}
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 2.6.8
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: Write a short summary, because Rubygems requires one.
121
+ test_files: []