Browse Source

Initial commit

master
commit
7b9df51020
No known key found for this signature in database GPG Key ID: DA34C790D267C164
18 changed files with 2385 additions and 0 deletions
  1. +9
    -0
      .editorconfig
  2. +1
    -0
      .env
  3. +5
    -0
      .gitignore
  4. +1
    -0
      .travis.yml
  5. +21
    -0
      LICENSE
  6. +27
    -0
      README.md
  7. +83
    -0
      public/index.css
  8. +25
    -0
      public/index.html
  9. +2048
    -0
      public/js/intercooler-1.2.2.js
  10. +2
    -0
      public/js/jquery-3.3.1.min.js
  11. +11
    -0
      public/js/transform.js
  12. +34
    -0
      shard.lock
  13. +24
    -0
      shard.yml
  14. BIN
      shashort.sqlite3
  15. +9
    -0
      spec/shashort_spec.cr
  16. +2
    -0
      spec/spec_helper.cr
  17. +1
    -0
      src/.gitignore
  18. +82
    -0
      src/shashort.cr

+ 9
- 0
.editorconfig View File

@ -0,0 +1,9 @@
root = true
[*.cr]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2
trim_trailing_whitespace = true

+ 1
- 0
.env View File

@ -0,0 +1 @@
port = 8000

+ 5
- 0
.gitignore View File

@ -0,0 +1,5 @@
/docs/
/lib/
/bin/
/.shards/
*.dwarf

+ 1
- 0
.travis.yml View File

@ -0,0 +1 @@
language: crystal

+ 21
- 0
LICENSE View File

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2019 deing
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

+ 27
- 0
README.md View File

@ -0,0 +1,27 @@
# shashort
TODO: Write a description here
## Installation
TODO: Write installation instructions here
## Usage
TODO: Write usage instructions here
## Development
TODO: Write development instructions here
## Contributing
1. Fork it (<https://github.com/your-github-user/shashort/fork>)
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request
## Contributors
- [deing](https://github.com/your-github-user) - creator and maintainer

+ 83
- 0
public/index.css View File

@ -0,0 +1,83 @@
#stage {
max-width: 75ch;
padding: .3rem;
color: #444;
margin: 0 auto 0 auto;
font-family: Roboto, sans-serif;
}
h1,h2,h3 {
margin: 0;
font-size: 1.3rem;
}
h1 {
text-align: center;
text-transform: uppercase;
border-bottom: 3px solid #444;
}
h2 {
font-style: italic;
margin: .2rem;
text-align: center;
}
p {
text-align: justify;
}
p.noscript {
text-transform: uppercase;
font-weight: bold;
}
form {
width:100%;
display: flex;
}
form input {
flex-grow: 1;
height: 2rem;
font-size: 1rem;
border: 2px solid #444;
padding: .3rem;
box-sizing: content-box;
}
form input[type="url"] {
border-top-left-radius: .3rem;
border-bottom-left-radius: .3rem;
border-right: none;
}
form input[type="url"]:focus, form input[type="url"]:hover {
box-shadow: 0px 0px 10px #3366bb;
}
form input[type="submit"] {
flex-grow: .3;
color: white;
background-color: #003399;
border-color: #003399;
border-top-right-radius: .3rem;
border-bottom-right-radius: .3rem;
cursor: pointer;
}
form input[type="submit"]:focus, form input[type="submit"]:hover {
box-shadow: 0px 0px 10px #3366bb;
}
#target {
padding: .3rem;
margin-top: 1rem;
}
#target p {
margin: 0;
text-transform: uppercase;
}
#target a {
color: #444;
text-decoration: none;
}
#target a:hover, target a:focus {
text-decoration: underline;
}
#target.success {
border-top: 3px solid lightgreen;
}

+ 25
- 0
public/index.html View File

@ -0,0 +1,25 @@
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>SHAShort</title>
<script src="js/jquery-3.3.1.min.js"></script>
<script src="js/intercooler-1.2.2.js"></script>
<script src="js/transform.js"></script>
<link rel="stylesheet" href="/index.css">
</head>
<body>
<div id="stage">
<h1>The World's First Military Grade Link Shortener</h1>
<h2>because your links deserve the best protection…</h2>
<p>SHAShort is the first and only URL shortening service to only use <em>cryptographically secure</em> hashing functions in the process of shortening, thereby making it <em>computationally infeasible</em> to reverse-engineer the original URL from the shortened link.</p>
<noscript>
<p class="noscript">This page requires JavaScript to work because it is a joke.</p>
</noscript>
<form ic-post-to="/api/web/create" ic-target="#target" ic-replace-target="true" ic-transform-response="return responseToHTML(content)">
<input type="url" id="url" placeholder="https://example.com" name="url"><input type="submit" value="Go!">
</form>
<div id="target"></div>
</div>
</body>
</html>

+ 2048
- 0
public/js/intercooler-1.2.2.js
File diff suppressed because it is too large
View File


+ 2
- 0
public/js/jquery-3.3.1.min.js
File diff suppressed because it is too large
View File


+ 11
- 0
public/js/transform.js View File

@ -0,0 +1,11 @@
function responseToHTML(content) {
content = JSON.parse(content);
switch (content.status) {
case 'error':
return `<div id="target" class="error"><p>Error</p>Service returned status ${content.status}</div>`;
case 'rate-limited':
return `<div id="target" class="error"><p>Error</p>You are being rate limited.</div>`;
case 'success':
return `<div id="target" class="success"><p>✔ Successfully shortened</p><a href="/resolve/${content.hash}">https://ding.15318.de/resolve/${content.hash.substring(0,32)}…</a></div>`;
}
}

+ 34
- 0
shard.lock View File

@ -0,0 +1,34 @@
version: 1.0
shards:
db:
github: crystal-lang/crystal-db
version: 0.5.1
dotenv:
github: gdotdesign/cr-dotenv
version: 0.2.0
exception_page:
github: crystal-loot/exception_page
version: 0.1.2
kemal:
github: kemalcr/kemal
version: 0.25.2
kilt:
github: jeromegn/kilt
version: 0.4.0
radix:
github: luislavena/radix
version: 0.3.9
sha3:
github: OscarBarrett/crystal-sha3
version: 0.3.1
sqlite3:
github: crystal-lang/crystal-sqlite3
version: 0.10.0

+ 24
- 0
shard.yml View File

@ -0,0 +1,24 @@
name: shashort
version: 0.1.0
authors:
- deing <admin@15318.de>
targets:
shashort:
main: src/shashort.cr
dependencies:
kemal:
github: kemalcr/kemal
sqlite3:
github: crystal-lang/crystal-sqlite3
sha3:
github: OscarBarrett/crystal-sha3
version: ~> 0.3
dotenv:
github: gdotdesign/cr-dotenv
crystal: 0.27.2
license: MIT

BIN
shashort.sqlite3 View File


+ 9
- 0
spec/shashort_spec.cr View File

@ -0,0 +1,9 @@
require "./spec_helper"
describe Shashort do
# TODO: Write tests
it "works" do
false.should eq(true)
end
end

+ 2
- 0
spec/spec_helper.cr View File

@ -0,0 +1,2 @@
require "spec"
require "../src/shashort"

+ 1
- 0
src/.gitignore View File

@ -0,0 +1 @@
shashort.sqlite3

+ 82
- 0
src/shashort.cr View File

@ -0,0 +1,82 @@
require "kemal"
require "db"
require "sqlite3"
require "sha3"
require "dotenv"
module SHAShort
extend self
@@last_hash_time = Time.unix(0)
RATELIMIT = Time::Span.new(0, 0, 1)
def ratelimited?
Time.now - @@last_hash_time < RATELIMIT
end
def ratelimit_set_now
@@last_hash_time = Time.now
end
end
post "/api/v0/create" do |env|
handle_create(env.request.body.not_nil!.gets_to_end, env)
end
post "/api/web/create" do |env|
handle_create(env.params.body["url"], env)
end
get "/" do |env|
send_file env, "public/index.html"
end
get "/resolve/:hash" do |env|
hash = env.params.url["hash"]
begin
link = fetch_link(hash)
env.redirect link
rescue e
puts e
env.response.status_code = 404
end
end
def handle_create(url, env)
env.response.content_type = "application/json"
if SHAShort.ratelimited?
env.response.status_code = 400
puts "Rate limited"
return {status: "error"}.to_json
end
if url.size > 2000
env.response.status_code = 413
puts "Payload size #{url.size} too large"
return {status: "error"}.to_json
end
begin
SHAShort.ratelimit_set_now
hash = create_link(url)
return {status: "success", hash: hash}.to_json
rescue e
puts e
env.response.status_code = 500
return {status: "error"}.to_json
end
end
def create_link(url)
raise "Too long, sorry" if url.size >= 2000
raise "Invalid URL #{url}" if URLREGEX.match(url).nil?
hash = Digest::SHA3.hexdigest(url)
DATABASE.exec "insert into urlmap (url, hash) values (?,?)", url, hash
hash
end
def fetch_link(hash)
raise "Invalid Hash Length #{hash.size}" if hash.size != 128
DATABASE.query_one "select url from urlmap where hash = ?", hash, as: String
end
DATABASE = DB.open "sqlite3://./shashort.sqlite3"
URLREGEX = Regex.new("^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]")
Dotenv.load!
Kemal.run ENV["port"].to_i32

Loading…
Cancel
Save