Skip to content

Instantly share code, notes, and snippets.

@almokhtarbr
Forked from johnthethird/customdomain.rb
Created January 4, 2023 19:57
Show Gist options
  • Select an option

  • Save almokhtarbr/2aee4b3295f6361d58cd16f18ddb5eff to your computer and use it in GitHub Desktop.

Select an option

Save almokhtarbr/2aee4b3295f6361d58cd16f18ddb5eff to your computer and use it in GitHub Desktop.

Revisions

  1. @speedmax speedmax revised this gist Aug 12, 2009. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion customdomain.rb
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,9 @@
    require File.dirname(__FILE__) + '/../../vendor/gems/net-dns-0.4/lib/net/dns/resolver'
    require 'net/dns/resolver'

    # Custom Domain
    #
    # Require net-dns gem
    #
    # A Rack middleware to to resolve the custom domain to original subdomain
    # for your multi telent application.
    #
    @@ -9,6 +12,7 @@
    #
    # www.example.org => example.myapp.com
    #
    # Credit: Inspired by http://codetunes.com/2009/04/17/dynamic-cookie-domains-with-racks-middleware/
    class CustomDomain
    @@cache = {}

  2. @speedmax speedmax revised this gist Aug 12, 2009. 1 changed file with 8 additions and 4 deletions.
    12 changes: 8 additions & 4 deletions customdomain.rb
    Original file line number Diff line number Diff line change
    @@ -25,15 +25,14 @@ def call(env)
    domain = cname_lookup(host)
    env['HTTP_X_FORWARDED_HOST'] = [host, domain].join(', ')

    RAILS_DEFAULT_LOGGER.info("CustomDomain: mapped #{host} => #{domain}")
    logger.info("CustomDomain: mapped #{host} => #{domain}")
    end

    @app.call(env)
    end

    def custom_domain?(host)
    domain = @default_domain.sub(/^\./, '')
    host !~ Regexp.new("#{domain}$", Regexp::IGNORECASE)
    host !~ /#{@default_domain.sub(/^\./, '')}/i
    end

    def cname_lookup(host)
    @@ -45,4 +44,9 @@ def cname_lookup(host)

    @@cache[host]
    end

    private
    def logger
    RAILS_DEFAULT_LOGGER || Logger.new(STDOUT)
    end
    end
  3. @speedmax speedmax revised this gist Aug 12, 2009. 1 changed file with 1 addition and 5 deletions.
    6 changes: 1 addition & 5 deletions customdomain.rb
    Original file line number Diff line number Diff line change
    @@ -23,11 +23,7 @@ def call(env)
    # modify Environment variable HTTP_HOST if custom domain is found
    if custom_domain?(host)
    domain = cname_lookup(host)

    env["HTTP_HOST"] = port ? "#{domain}:#{port}" : domain

    # Rails overwrite request.host with this particular header
    # env["HTTP_X_FORWARDED_HOST"] = host
    env['HTTP_X_FORWARDED_HOST'] = [host, domain].join(', ')

    RAILS_DEFAULT_LOGGER.info("CustomDomain: mapped #{host} => #{domain}")
    end
  4. @speedmax speedmax revised this gist Aug 12, 2009. 1 changed file with 12 additions and 9 deletions.
    21 changes: 12 additions & 9 deletions customdomain.rb
    Original file line number Diff line number Diff line change
    @@ -23,12 +23,16 @@ def call(env)
    # modify Environment variable HTTP_HOST if custom domain is found
    if custom_domain?(host)
    domain = cname_lookup(host)

    env["HTTP_HOST"] = port ? "#{domain}:#{port}" : domain
    env["HTTP_X_FORWARDED_HOST"] = host

    # Rails overwrite request.host with this particular header
    # env["HTTP_X_FORWARDED_HOST"] = host

    RAILS_DEFAULT_LOGGER.info("CustomDomain: mapped #{host} => #{domain}")
    end

    env
    # @app.call(env)
    @app.call(env)
    end

    def custom_domain?(host)
    @@ -37,13 +41,12 @@ def custom_domain?(host)
    end

    def cname_lookup(host)
    result = nil
    return host if @@cache[host]

    Net::DNS::Resolver.new.query(host).each_cname do |cname|
    result = cname if cname.include?(@default_domain)
    unless @@cache[host]
    Net::DNS::Resolver.new.query(host).each_cname do |cname|
    @@cache[host] = cname if cname.include?(@default_domain)
    end
    end

    @@cache[host] = result
    @@cache[host]
    end
    end
  5. @speedmax speedmax revised this gist Aug 12, 2009. 1 changed file with 5 additions and 6 deletions.
    11 changes: 5 additions & 6 deletions customdomain.rb
    Original file line number Diff line number Diff line change
    @@ -8,9 +8,7 @@
    # overwrite HTTP_HOST if needed
    #
    # www.example.org => example.myapp.com
    #
    # Base on : http://codetunes.com/2009/04/17/dynamic-cookie-domains-with-racks-middleware/
    #
    #
    class CustomDomain
    @@cache = {}

    @@ -29,7 +27,8 @@ def call(env)
    env["HTTP_X_FORWARDED_HOST"] = host
    end

    @app.call(env)
    env
    # @app.call(env)
    end

    def custom_domain?(host)
    @@ -42,9 +41,9 @@ def cname_lookup(host)
    return host if @@cache[host]

    Net::DNS::Resolver.new.query(host).each_cname do |cname|
    result = $0 if cname.include?(@default_domain)
    result = cname if cname.include?(@default_domain)
    end

    @@cache[host] = result
    end
    end
  6. @speedmax speedmax revised this gist Aug 12, 2009. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions customdomain.rb
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,9 @@
    # overwrite HTTP_HOST if needed
    #
    # www.example.org => example.myapp.com
    #
    #
    # Base on : http://codetunes.com/2009/04/17/dynamic-cookie-domains-with-racks-middleware/
    #
    class CustomDomain
    @@cache = {}

    @@ -24,7 +26,7 @@ def call(env)
    if custom_domain?(host)
    domain = cname_lookup(host)
    env["HTTP_HOST"] = port ? "#{domain}:#{port}" : domain
    env["rack.custom_domain"] = host
    env["HTTP_X_FORWARDED_HOST"] = host
    end

    @app.call(env)
  7. @speedmax speedmax created this gist Aug 11, 2009.
    48 changes: 48 additions & 0 deletions customdomain.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    require File.dirname(__FILE__) + '/../../vendor/gems/net-dns-0.4/lib/net/dns/resolver'

    # Custom Domain
    # A Rack middleware to to resolve the custom domain to original subdomain
    # for your multi telent application.
    #
    # It's all transperant to your application, it performs cname lookup and
    # overwrite HTTP_HOST if needed
    #
    # www.example.org => example.myapp.com
    #
    class CustomDomain
    @@cache = {}

    def initialize(app, default_domain)
    @app = app
    @default_domain = default_domain
    end

    def call(env)
    host, port = env["HTTP_HOST"].split(':')

    # modify Environment variable HTTP_HOST if custom domain is found
    if custom_domain?(host)
    domain = cname_lookup(host)
    env["HTTP_HOST"] = port ? "#{domain}:#{port}" : domain
    env["rack.custom_domain"] = host
    end

    @app.call(env)
    end

    def custom_domain?(host)
    domain = @default_domain.sub(/^\./, '')
    host !~ Regexp.new("#{domain}$", Regexp::IGNORECASE)
    end

    def cname_lookup(host)
    result = nil
    return host if @@cache[host]

    Net::DNS::Resolver.new.query(host).each_cname do |cname|
    result = $0 if cname.include?(@default_domain)
    end

    @@cache[host] = result
    end
    end