diff --git a/.github/workflows/deploy-feature.yml b/.github/workflows/deploy-feature.yml new file mode 100644 index 0000000..9761210 --- /dev/null +++ b/.github/workflows/deploy-feature.yml @@ -0,0 +1,20 @@ +name: Deploy-feature + +on: + push: + branches: [ feature/* ] + +jobs: + deploy: + runs-on: self-hosted + + steps: + - name: Checkout code + uses: actions/checkout@v6 + with: + fetch-depth: 0 + + - name: Push to Dokku + run: | + git remote add dokku dokku@localhost:${{ secrets.DOKKU_APP }} 2>/dev/null || true + git push dokku feature/long-url:main --force diff --git a/app/assets/stylesheets/app.css b/app/assets/stylesheets/app.css index c7894fa..12854a6 100644 --- a/app/assets/stylesheets/app.css +++ b/app/assets/stylesheets/app.css @@ -370,6 +370,31 @@ body { gap: 0.6rem; margin-bottom: 1.25rem; } +.links-filter { + margin-left: auto; + display: flex; + gap: 0.3rem; +} +.filter-btn { + background: none; + border: 1px solid var(--border); + border-radius: 999px; + padding: 0.25rem 0.75rem; + font-size: 0.8rem; + color: var(--muted); + cursor: pointer; + transition: background 0.15s, color 0.15s, border-color 0.15s; +} +.filter-btn:hover { + border-color: var(--blue); + color: var(--blue); +} +.filter-btn.active { + background: var(--blue); + border-color: var(--blue); + color: #fff; + font-weight: 500; +} .links-title { font-size: 1.6rem; @@ -423,6 +448,11 @@ body { color: var(--blue); text-decoration: none; letter-spacing: -0.01em; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; + min-width: 0; + flex: 1; } .link-short:hover { text-decoration: underline; } @@ -867,3 +897,93 @@ body { .link-date { display: none; } #version-banner { white-space: normal; width: calc(100% - 2rem); } } + +/* ── Tools card (Lengthen / Expand tabs) ────────────────────────────── */ +.tools-card { + background: var(--card); + border: 1px solid var(--border); + border-radius: var(--r); + max-width: 720px; + margin: 0 auto 1.5rem; + box-shadow: 0 1px 6px rgba(0,0,0,.06); + overflow: hidden; +} +.tools-tabs { + display: flex; + border-bottom: 1px solid var(--border); +} +.tools-tab { + flex: 1; + background: none; + border: none; + padding: 0.7rem 1rem; + font-size: 0.88rem; + font-weight: 500; + color: var(--muted); + cursor: pointer; + transition: color 0.15s, background 0.15s; +} +.tools-tab:hover { color: var(--text); background: var(--hover, rgba(0,0,0,.03)); } +.tools-tab.active { color: var(--blue); border-bottom: 2px solid var(--blue); margin-bottom: -1px; } +.tools-panel { padding: 1.25rem 2rem 1.5rem; } + +/* ── Expand URL section ──────────────────────────────────────────────── */ +.expand-card { + background: var(--card); + border: 1px solid var(--border); + border-radius: var(--r); + padding: 1.5rem 2rem; + max-width: 720px; + margin: 0 auto 1.5rem; + box-shadow: 0 1px 6px rgba(0,0,0,.06); +} +.expand-title { + font-size: 1rem; + font-weight: 600; + margin-bottom: 0.25rem; +} +.expand-hint { + font-size: 0.82rem; + color: var(--muted); + margin-bottom: 0.75rem; +} +.expand-row { + display: flex; + border: 1px solid var(--border); + border-radius: var(--r); + overflow: hidden; + background: var(--card); +} +.expand-row:focus-within { + border-color: var(--blue); + box-shadow: 0 0 0 3px rgba(59,130,246,.15); +} +.expand-row .form-input { + border: none; + box-shadow: none; + border-radius: 0; + flex: 1; +} +#expand-result { + display: flex; + align-items: center; + gap: 0.6rem; + margin-top: 0.75rem; + flex-wrap: wrap; +} +.expand-result-label { + font-size: 0.82rem; + color: var(--muted); + white-space: nowrap; +} +.expand-result-link { + font-size: 0.88rem; + color: var(--blue); + word-break: break-all; + flex: 1; + overflow: hidden; + display: -webkit-box; + -webkit-line-clamp: 3; + -webkit-box-orient: vertical; +} +.expand-result-link:hover { text-decoration: underline; } diff --git a/app/controllers/urls_controller.rb b/app/controllers/urls_controller.rb index 97c70e7..c5ced1d 100644 --- a/app/controllers/urls_controller.rb +++ b/app/controllers/urls_controller.rb @@ -1,6 +1,9 @@ +require "net/http" + class UrlsController < ApplicationController before_action :authenticate_user!, only: [:destroy, :analytics] before_action :set_url, only: [:destroy, :analytics, :qr_code] + before_action :throttle_expand!, only: [:expand] def index @url = Url.new @@ -46,6 +49,41 @@ def analytics @recent_clicks = @url.click_events.order(created_at: :desc).limit(50) end + def lengthen + raw = params[:url].to_s.strip + raw = "https://#{raw}" unless raw.match?(/\Ahttps?:\/\//i) + + @url = Url.new(original_url: raw) + @url.user = current_user if user_signed_in? + + # Override auto-generated short_code with a very long one + @url.short_code = generate_long_code + + if @url.save + long_url = short_url_for(@url) + render json: { long_url: long_url } + else + render json: { error: @url.errors.full_messages.first }, status: :unprocessable_entity + end + end + + def expand + raw = params[:url].to_s.strip + raw = "https://#{raw}" unless raw.match?(/\Ahttps?:\/\//i) + + begin + uri = URI.parse(raw) + raise ArgumentError unless uri.is_a?(URI::HTTP) || uri.is_a?(URI::HTTPS) + rescue URI::InvalidURIError, ArgumentError + return render json: { error: "Invalid URL" }, status: :unprocessable_entity + end + + final_url = follow_redirects(uri) + render json: { original: raw, expanded: final_url } + rescue => e + render json: { error: "Could not expand URL: #{e.message}" }, status: :unprocessable_entity + end + def qr_code qr = RQRCode::QRCode.new(short_url_for(@url)) svg = qr.as_svg( @@ -75,6 +113,56 @@ def set_url end end + PRIVATE_IP_PATTERNS = [ + /\A127\./, + /\A10\./, + /\A172\.(1[6-9]|2[0-9]|3[01])\./, + /\A192\.168\./, + /\A::1\z/, + /\Alocalhost\z/i + ].freeze + + MAX_REDIRECTS = 10 + + def follow_redirects(uri, hops = 0) + raise "Too many redirects" if hops >= MAX_REDIRECTS + + host = uri.host.to_s + raise "Requests to private addresses are not allowed" if PRIVATE_IP_PATTERNS.any? { |p| p.match?(host) } + + use_ssl = uri.scheme == "https" + response = Net::HTTP.start(uri.host, uri.port, use_ssl: use_ssl, open_timeout: 5, read_timeout: 5) do |http| + http.head(uri.request_uri) + end + + case response + when Net::HTTPRedirection + location = response["location"] + next_uri = URI.parse(location) + next_uri = uri.merge(next_uri) if next_uri.relative? + follow_redirects(next_uri, hops + 1) + else + uri.to_s + end + end + + def generate_long_code + loop do + code = SecureRandom.alphanumeric(rand(200..300)) + return code unless Url.exists?(short_code: code) + end + end + + EXPAND_RATE_LIMIT = 10 # requests per window + EXPAND_RATE_WINDOW = 60 # seconds + + def throttle_expand! + key = "expand:#{request.remote_ip}" + count = Rails.cache.increment(key, 1, expires_in: EXPAND_RATE_WINDOW) rescue nil + return unless count.is_a?(Integer) && count > EXPAND_RATE_LIMIT + render json: { error: "Too many requests. Please slow down." }, status: :too_many_requests + end + def permitted_url_params allowed = [:original_url] allowed << :short_code if user_signed_in? diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 579aa35..2a31e0c 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -3,7 +3,7 @@
Free URL shortener. Shorten links and track clicks.
+ + <%# Guest result — shows after shortening without an account %> <% if flash[:guest_short_url].present? %>