Class: Few::RemoteHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/few.rb

Overview

}}}

Instance Method Summary collapse

Constructor Details

#initialize(o = {}) ⇒ RemoteHelper

{{{



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/few.rb', line 57

def initialize(o = {})
  require 'net/http'
  require 'openssl'
  require 'uri'
  require 'cgi'
  @opt = {
    :private_key => nil, :public_key => nil, :remote_path => 'http://sorah.cosmio.net/few_server.rb'
  }.merge(o)
  @priv_key = @opt[:private_key] ?
    OpenSSL::PKey::RSA.new(@opt[:private_key]) : nil
  @publ_key = @opt[:public_key ] ?
    OpenSSL::PKey::RSA.new(@opt[:public_key ]) : nil
  @remote_path = @opt[:remote_path] ?
    URI.parse(@opt[:remote_path]) : nil
end

Instance Method Details

#crypt(str) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/few.rb', line 89

def crypt(str)
  r = OpenSSL::Cipher::AES.new("256-CBC")
  p = (1..32).map{(rand(95)+33).chr}.join
  r.encrypt
  r.pkcs5_keyivgen(p)
  c =  r.update(str)
  c << r.final
  begin
    [Base64.encode64(c),Base64.encode64(@publ_key.public_encrypt(p))]
  rescue NoMethodError
    return false
  end
end

#decrypt(str, key) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/few.rb', line 103

def decrypt(str,key)
  r = OpenSSL::Cipher::AES.new("256-CBC")
  r.decrypt
  begin
    k = @priv_key.private_decrypt(Base64.decode64(key))
    r.pkcs5_keyivgen(k)
    s =  r.update(Base64.decode64(str))
    s << r.final
    return s
  rescue NoMethodError
    return false
  end
end

#generate_key_pairObject



73
74
75
76
77
78
79
80
# File 'lib/few.rb', line 73

def generate_key_pair
  rsa                = OpenSSL::PKey::RSA.generate(2048)
  @opt[:private_key] = rsa.export
  @opt[:public_key ] = rsa.public_key.to_s
  @priv_key          = rsa
  @publ_key          = OpenSSL::PKey::RSA.new(rsa.public_key)
  self
end

#private_keyObject



82
# File 'lib/few.rb', line 82

def private_key;     @opt[:private_key]; end

#private_key=(x) ⇒ Object



85
# File 'lib/few.rb', line 85

def private_key=(x); @opt[:private_key]; @priv_key    = OpenSSL::PKey::RSA.new(@opt[:private_key]); x; end

#public_keyObject



83
# File 'lib/few.rb', line 83

def public_key;      @opt[:public_key ]; end

#public_key=(x) ⇒ Object



86
# File 'lib/few.rb', line 86

def public_key=(x);  @opt[:public_key ]; @publ_key    = OpenSSL::PKey::RSA.new(@opt[:public_key ]); x; end

#recvObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/few.rb', line 134

def recv
  return unless @opt[:remote_path]
  Net::HTTP.start(@remote_path.host, @remote_path.port) do |h|
    r = h.get(
      @remote_path.path + '?public_key=' + CGI.escape(@opt[:public_key]))
    begin; b = r.body.split(/\r?\n/); rescue; return nil; end
    s = b.shift
    return nil if s.nil?
    if s.chomp == 'have'
      kb = b.join("\n").split(/\n\n--- \('\.v\.'\) < hi ---\n/)
      decrypt(*kb)
    else
      return nil
    end
  end
end

#remote_pathObject



84
# File 'lib/few.rb', line 84

def remote_path;     @opt[:remote_path]; end

#remote_path=(x) ⇒ Object



87
# File 'lib/few.rb', line 87

def remote_path=(x); @opt[:remote_path]; @remote_path = URI.parse(@opt[:remote_path             ]); x; end

#send(str) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/few.rb', line 117

def send(str)
  return unless @opt[:remote_path]
  c = crypt(str)
  begin
    Net::HTTP.start(@remote_path.host, @remote_path.port) do |h|
      r = h.post(
        @remote_path.path,
        'public_key=' + CGI.escape(@opt[:public_key]) +
        '&body=' + CGI.escape(c[0]) + '&aes_key=' + CGI.escape(c[1]))
    end
  rescue Net::ProtocolError
    return r
  else
    return true
  end
end