class ConcertoConfigServer

Constants

ADDRESSING_METHODS

… and available layer-3 addressing methods.

CONNECTION_METHODS

push these over to netconfig.rb? Our list of available physical-layer connection methods…

LOCALHOSTS

Hosts we allow to access configuration without authenticating.

Public Instance Methods

active_page?(path='') click to toggle source
# File lib/bandshell/application/app.rb, line 43
def active_page?(path='')
  request.path_info == '/' + path
end
authorized?() click to toggle source

Check authorization credentials. Currently configured to check if the REMOTE_ADDR is local and allow everything if so. This permits someone at local console to configure without the need for a password. Others must have the correct password to be considered authorized.

# File lib/bandshell/application/app.rb, line 107
def authorized?
  password = Bandshell::ConfigStore.read_config(
    'password', 'default'
  )
  if request_is_local?
    # allow all requests from localhost no questions asked
    true
  else
    @auth ||= Rack::Auth::Basic::Request.new(request.env)
    @auth.provided? && @auth.basic? && @auth.credentials && \
      @auth.credentials == ['root', password]
  end
end
concerto_url() click to toggle source

Get our base URL from wherever it may be stored.

# File lib/bandshell/application/app.rb, line 122
def concerto_url
  Bandshell::ConfigStore.read_config('concerto_url', '')
end
do_assign(params, instance) click to toggle source

Set the arguments on an instance of a given configuration class. This uses the safe_assign method that should be present in all configuration classes to determine which values are allowed to be passed via form fields (i.e. which ones are subject to validation)

# File lib/bandshell/application/app.rb, line 333
def do_assign(params, instance)
  safe = instance.safe_assign

  params.each do |param, value|
    if safe.include? param.intern
      instance.send((param + '=').intern, value)
    end
  end
end
extract_class_args(params, target_class) click to toggle source

Extract arguments from a set of form data that are intended to go to a specific network configuration class. These fields have names of the form ‘ClassName/field_name’; this function returns a hash in the form { ‘field_name’ => ‘value } containing the configuration arguments.

# File lib/bandshell/application/app.rb, line 317
def extract_class_args(params, target_class)
  result = { }
  params.each do |key, value|
    klass, arg = key.split('/', 2)
    if klass == target_class
      result[arg] = value
    end
  end

  result
end
my_ip() click to toggle source

Try to figure out what our current IPv4 address is and return it as a string.

# File lib/bandshell/application/app.rb, line 128
def my_ip
  iface = Bandshell.configured_interface
  if iface
    iface.ip
  else
    "Network setup failed or bad configuration"
  end
end
my_port() click to toggle source

Try to figure out what our current port is and return it as a string.

# File lib/bandshell/application/app.rb, line 139
def my_port
  settings.port
end
network_ok() click to toggle source

Check if we have something resembling a network connection. This means we found a usable interface and it has an IPv4 address.

# File lib/bandshell/application/app.rb, line 145
def network_ok
  return true if settings.no_netconfig
  iface = Bandshell.configured_interface
  if iface
    if iface.ip != "0.0.0.0"
      true
    else
      false
    end
  else
    false
  end
end
pick_class(name, list) click to toggle source

Given the name of a class, pick a class out of a list of allowed classes. This is used for parsing the form input for network configuration.

# File lib/bandshell/application/app.rb, line 309
def pick_class(name, list)
  list.find { |klass| klass.basename == name }
end
player_info() click to toggle source
# File lib/bandshell/application/app.rb, line 47
def player_info
  # Note: probably not thread-safe.
  @@player_info ||= Bandshell::PlayerInfo.new
end
protected!() click to toggle source

Enforce authentication on actions. Calling from within an action will check authentication and return 401 if unauthorized.

# File lib/bandshell/application/app.rb, line 89
def protected!
  unless authorized?
    response['WWW-Authenticate'] = \
      %(Basic realm="Concerto Configuration")
    throw(:halt, [401, "Not authorized\n"])
  end
end
request_is_local?() click to toggle source
# File lib/bandshell/application/app.rb, line 97
def request_is_local?
  ip = IPAddress.parse(request.env['REMOTE_ADDR'])
  LOCALHOSTS.include? ip
end
validate_url(url) click to toggle source

Check if we can retrieve a URL and get a 200 status code.

# File lib/bandshell/application/app.rb, line 160
def validate_url(url)
  begin
    # this will fail with Errno::something if server
    # can't be reached
    uri = URI(url)
    Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
      request = Net::HTTP::Get.new uri.request_uri
      response = http.request request
      # also bomb out if we don't get an OK response
      # maybe demanding 200 is too strict here?
      if response.code != "200"
        fail
      end
    end

    # if we get here we have a somewhat valid URL to go to
    true
  rescue
    # our request bombed out for some reason
    false
  end
end
value_from(obj, method) click to toggle source

Get the return value of the method on obj if obj supports the method. Otherwise return the empty string. This is useful in views where arguments may be of diverse types.

# File lib/bandshell/application/app.rb, line 78
def value_from(obj, method)
  if obj.respond_to? method
    obj.send method
  else
    ""
  end
end