💾 Archived View for gemini.ctrl-c.club › ~de_alchmst › data › guestbook.rb.txt captured on 2024-05-26 at 16:34:39.

View Raw

More Information

-=-=-=-=-=-=-

#! /usr/bin/ruby

require "socket"
require "cgi"

# setup server
server = TCPServer.new 3700 # listen on non-standard port
puts "server started"

# listen for client input
loop {
  Thread.start(server.accept) { |client|
    # PROCESS DATA #
    time = Time.now.strftime("%Y-%B-%d %H:%M:%S")
    input = client.gets.match /post=(\S+)/

    # skip if empty
    msg = ""
    if input
      msg = CGI.unescape(input[1]).strip
    end

    unless msg.empty?
      # replace all malicious parts of post
      g_msg = "  " + msg.gsub("\n", "\n  ")
      h_msg = msg.gsub("&", "&amp;").gsub("<", "&lt;").gsub(">", "&gt;")

      # gemini #
      f = File.open "/home/de_alchmst/public_gemini/guestbook/index.gmi", "r"
      contents = f.read
      f.close

      parts = contents.split "\n``` entries"
      contents = parts[0] + "\n``` entries\n" + "---------#{time}---------\n"+\
                 g_msg + "\n" + parts[1]

      f = File.open "/home/de_alchmst/public_gemini/guestbook/index.gmi", "w"
      f.write contents
      f.close

      # web #
      f = File.open "/home/de_alchmst/public_html/guestbook/index.html", "r"
      contents = f.read
      f.close

      parts = contents.split "<!-- HERE -->"
      contents = parts[0] + "<!-- HERE -->\n" + \
                 "<pre class=\"gb-entry\"><span class=\"gb-time\">" + \
                 "#{time}</span><br>" + h_msg + "</pre>" + \
                 parts[1]
                  

      f = File.open "/home/de_alchmst/public_html/guestbook/index.html", "w"
      f.write contents
      f.close
    end

    # send redirect back to regular page
    client.puts <<~EOF
HTTP/1.1 302 Found
Location: https://ctrl-c.club/~de_alchmst/guestbook
Content-Type: text/html
Content-Length: 230
Date: Sat, 18 May 2024 10:00:00 GMT
Server: Apache/2.4.41 (Ubuntu)

<!DOCTYPE html>
<html>
<head>
  <title>Sample Page</title>
  <meta http-equiv="refresh"
  content="0;url=https://ctrl-c.club/~de_alchmst/guestbook"> 
</head>
<body>
<a href="https://ctrl-c.club/~de_alchmst/guestbook">
if you are not redirected automatically, click here</a>
</body>
</html>
EOF

    # close connection
    client.close
  }
}