diff options
| -rw-r--r-- | lib/insomniaircd_conf.rb | 49 | ||||
| -rw-r--r-- | lib/insomniaircd_conf/secure.rb | 35 | 
2 files changed, 84 insertions, 0 deletions
| diff --git a/lib/insomniaircd_conf.rb b/lib/insomniaircd_conf.rb new file mode 100644 index 0000000..e8f15b3 --- /dev/null +++ b/lib/insomniaircd_conf.rb @@ -0,0 +1,49 @@ +require 'erb' +require 'ostruct' +require 'resolv' +require 'yaml' +require 'insomniaircd_conf/secure' + +class InsomniaIRCDConf +  def initialize(hostname, secret_key) +    @secure = InsomniaIRCDConf::Secure.new(secret_key) + +    @hostname = hostname +    @network = OpenStruct.new(YAML.load_file("network.yaml")) + +    raise ArgumentError, "No such server #{hostname}" unless @network.servers.has_key? hostname +    @server = OpenStruct.new(@network.servers[hostname]) + +    @opers = [] +    @network.opers.each do |oper| +      oper["password"] = @secure.decrypt(oper["password"]) +      @opers << oper +    end +  end + +  def hostname +    @hostname +  end + +  def network +    @network +  end + +  def server +    @server +  end + +  def opers +    @opers +  end + +  def secret(key) +    @secure.decrypt @network.secrets[key.to_s] +  end + +  def render_template(input) +    template = ERB.new(input) +    template.result(binding) +  end +end + diff --git a/lib/insomniaircd_conf/secure.rb b/lib/insomniaircd_conf/secure.rb new file mode 100644 index 0000000..91ab9e7 --- /dev/null +++ b/lib/insomniaircd_conf/secure.rb @@ -0,0 +1,35 @@ +require 'base64' + +class Secure + +  def initialize(key) +    @key = Base64.decode64(key) +  end + +  def self.random_key +    cipher = OpenSSL::Cipher::AES256.new(:CBC) +    Base64.encode64(cipher.random_key).gsub("\n", "") +  end + +  def encrypt(secret) +    cipher = OpenSSL::Cipher::AES256.new(:CBC) +    cipher.encrypt +    cipher.key = @key +    iv = cipher.random_iv +    encrypted = cipher.update(secret) + cipher.final + +    Base64.encode64(iv + encrypted).gsub("\n", "") +  end + +  def decrypt(input) +    data = Base64.decode64(input) +    cipher = OpenSSL::Cipher::AES256.new(:CBC) +    cipher.decrypt +    cipher.key = @key +    cipher.iv = data[0..15] + +    cipher.update(data[16..-1]) + cipher.final +  end + +end + | 
