Last active
June 2, 2017 11:33
-
-
Save colindean/9029731 to your computer and use it in GitHub Desktop.
Simple DNSSD FTPS client/server written as an example of a secure file transfer system of peer-to-peer nature with autodiscovery, ala https://pay.reddit.com/r/ruby/comments/1y13h4/secure_peertopeer_in_ruby/
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
require 'double_bag_ftps' | |
require 'dnssd' | |
browser = DNSSD::Service.new | |
services = {} | |
browser.browse '_ftps._tcp' do |reply| | |
puts reply.fullname | |
DNSSD::Service.new.resolve(reply) do |r| | |
puts "Listing files available on #{r.name} on #{r.target}:#{r.port}" | |
ftp = DoubleBagFTPS.new | |
ftp.ssl_context = DoubleBagFTPS.create_ssl_context( | |
:verify_mode => OpenSSL::SSL::VERIFY_NONE) | |
ftp.passive = true | |
ftp.connect r.target, r.port | |
ftp.login | |
puts ftp.list | |
end | |
end |
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
source 'https://rubygems.org' | |
gem 'dnssd' | |
gem 'double-bag-ftps' | |
gem 'ftpd' |
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
#!/bin/bash | |
#this is untested, just pulled from my history | |
openssl genrsa 4096 > priv | |
chmod 400 priv | |
openssl req -new -x509 -nodes -sha1 -days 3650 -key priv > cert.pem | |
cat priv cert.pem > cert2.pem | |
#note that we don't care at all about identity here, just that we're encrypting what goes over the wire |
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
require 'ftpd' | |
require 'tmpdir' | |
require 'dnssd' | |
require 'logger' | |
module Ftpd | |
class Server | |
def raw_socket | |
@server_socket.to_io | |
end | |
end | |
end | |
class Driver | |
include Ftpd::InsecureCertificate | |
def initialize(temp_dir) | |
@temp_dir = temp_dir | |
end | |
def authenticate(user, password) | |
true | |
end | |
def file_system(user) | |
Ftpd::DiskFileSystem.new(@temp_dir) | |
end | |
end | |
Dir.mktmpdir do |temp_dir| | |
driver = Driver.new(temp_dir) | |
server = Ftpd::FtpServer.new(driver) | |
server.interface = "0.0.0.0" | |
server.port = 8721 | |
server.certfile_path = "cert2.pem" | |
server.tls = :explicit | |
server.log = Logger.new STDOUT | |
server.start | |
DNSSD.announce server.raw_socket, 'Ruby FTPS DNSSD Example', 'ftps' | |
puts "Server listening on port #{server.bound_port}" | |
puts "Files can go in #{temp_dir}" | |
gets | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment