Code Monkey home page Code Monkey logo

pastebin-worker's Introduction

Pastebin-worker

This is a pastebin that can be deployed on Cloudflare workers. Try it on shz.al.

Philosophy: effortless deployment, friendly CLI usage, rich functionality.

Features:

  1. Share your paste with as short as 4 characters
  2. Customize the paste URL
  3. Update and delete your paste as you want
  4. Expire your paste after a period of time
  5. Syntax highlighting powered by PrismJS
  6. Display markdown file as HTML
  7. Used as a URL shortener
  8. Customize returned mimetype

Usage

  1. You can post, update, delete your paste directly on the website (such as shz.al).

  2. It also provides a convenient HTTP API to use. See API reference for details. You can easily call API via command line (using curl or similar tools).

  3. pb is a bash script to make it easier to use on command line.

Limitations

  1. If deployed on Cloudflare Worker free-tier plan, the service allows at most 100,000 reads and 1000 writes, 1000 deletes per day.
  2. Due to the size limit of Cloudflare KV storage, the size of each paste is bounded under 25 MB.

Deploy

You are free to deploy the pastebin on your own domain if you host your domain on Cloudflare.

  1. Install node and yarn.

  2. Create a KV namespace on Cloudflare workers dashboard, remember its ID.

  3. Clone the repository and enter the directory. Login to your Cloudflare account with wrangler login.

  4. Modify entries in wrangler.toml. Its comments will tell you how.

  5. Deploy and enjoy!

$ yarn install
$ yarn deploy

Auth

If you want a private deployment (only you can upload paste, but everyone can read the paste), add the following entry to your wrangler.toml.

[vars.BASIC_AUTH]
user1 = "passwd1"
user2 = "passwd2"

Now every access to POST request, and every access to static pages, requires an HTTP basic auth with the user-password pair listed above. For example:

$ curl example-pb.com
HTTP basic auth is required

$ curl -Fc=@/path/to/file example-pb.com
HTTP basic auth is required

$ curl -u admin1:wrong-passwd -Fc=@/path/to/file example-pb.com
Error 401: incorrect passwd for basic auth

$ curl -u admin1:this-is-passwd-1 -Fc=@/path/to/file example-pb.com
{
  "url": "https://example-pb.com/YCDX",
  "suggestUrl": null,
  "admin": "https://example-pb.com/YCDX:Sij23HwbMjeZwKznY3K5trG8",
  "isPrivate": false
}

Administration

Delete a paste:

$ yarn delete-paste <name-of-paste>

List pastes:

$ yarn wrangler kv:key list --binding PB > kv_list.json

Development

Run a local simulator:

$ yarn dev

Run tests:

$ yarn test

Run tests with coverage report:

$ yarn coverage

pastebin-worker's People

Contributors

6david9 avatar klihz avatar ksco avatar misaka13514 avatar sainnhe avatar sharzyl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

pastebin-worker's Issues

Empty response when visting paste's admin page for self-hoster

I recently deploy my own version of pastebin-worker.

When I tried to vising a paste's admin page like https://pb.example.com/<ID>:<SECRET>, the browser displays nothing. Looks like the response is empty.

So I checked the source code, it looks like the handleGet function will fetch an entry named index from the KV DB and return it as an text/html response.

// return the editor for admin URL
if (passwd.length > 0) {
const item = await env.PB.get('index')
return new Response(item, {
headers: { "content-type": "text/html;charset=UTF-8" }
})
}

However there seems to be nothing about "configuring an index KV entry" in README's "Deploy" section.

Suggestion: Show file extension on upload result

Hello!

I noticed when you upload a file, you are able to upload formats other than plain text, for example an image, and view them back. I also noticed that the correct content-type can be returned by adding an extension to the URL. For example https://shz.al/Si3d.jpg

It would be nice (If possible) if the API/frontend would detect the file extension of the file I'm uploading, and suggest that file extension for the resulting URL, to increase discoverability of this functionality and make the service function for uploading and sharing files as well (Is that out of scope? I assumed not considering the rest of the stuff for it is here)

I do have my own instance that I would be using, just used yours for the example URL for simplicity!

build failing

⬢[codebam@toolbox pastebin-worker]$ yarn
yarn install v1.22.18
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
Done in 5.99s.
⬢[codebam@toolbox pastebin-worker]$ yarn build
yarn run v1.22.18
$ webpack --mode=production
node:internal/crypto/hash:67
  this[kHandle] = new _Hash(algorithm, xofLen);
                  ^

Error: error:0308010C:digital envelope routines::unsupported
    at new Hash (node:internal/crypto/hash:67:19)
    at Object.createHash (node:crypto:133:10)
    at BulkUpdateDecorator.hashFactory (/var/home/codebam/git/pastebin-worker/node_modules/webpack/lib/util/createHash.js:145:18)
    at BulkUpdateDecorator.update (/var/home/codebam/git/pastebin-worker/node_modules/webpack/lib/util/createHash.js:46:50)
    at RawSource.updateHash (/var/home/codebam/git/pastebin-worker/node_modules/webpack-sources/lib/RawSource.js:70:8)
    at NormalModule._initBuildHash (/var/home/codebam/git/pastebin-worker/node_modules/webpack/lib/NormalModule.js:880:17)
    at handleParseResult (/var/home/codebam/git/pastebin-worker/node_modules/webpack/lib/NormalModule.js:946:10)
    at /var/home/codebam/git/pastebin-worker/node_modules/webpack/lib/NormalModule.js:1038:4
    at processResult (/var/home/codebam/git/pastebin-worker/node_modules/webpack/lib/NormalModule.js:755:11)
    at /var/home/codebam/git/pastebin-worker/node_modules/webpack/lib/NormalModule.js:819:5 {
  opensslErrorStack: [ 'error:03000086:digital envelope routines::initialization error' ],
  library: 'digital envelope routines',
  reason: 'unsupported',
  code: 'ERR_OSSL_EVP_UNSUPPORTED'
}

Node.js v18.2.0
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Worker exceeded resource limits when file size greater than about 1.2MB

The approximate size I encountered was about 1140 KB.
When I paste a file of 1130 KB, no problem occurred.

log
$wc -c more1m_a
1167360 more1m_a
$curl -Fc=@more1m_a -Fn=more1m-a-1140 -Fe=60 --insecure https://shz.al
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js ie6 oldie" lang="en-US"> <![endif]-->
<!--[if IE 7]>    <html class="no-js ie7 oldie" lang="en-US"> <![endif]-->
<!--[if IE 8]>    <html class="no-js ie8 oldie" lang="en-US"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en-US"> <!--<![endif]-->
<head>
<title>Worker exceeded resource limits | shz.al | Cloudflare</title></title>
<meta charset="UTF-8" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
<meta name="robots" content="noindex, nofollow" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<link rel="stylesheet" id="cf_styles-css" href="/cdn-cgi/styles/cf.errors.css" type="text/css" media="screen,projection" />
<!--[if lt IE 9]><link rel="stylesheet" id='cf_styles-ie-css' href="/cdn-cgi/styles/cf.errors.ie.css" type="text/css" media="screen,projection" /><![endif]-->
<style type="text/css">body{margin:0;padding:0}</style>


<!--[if gte IE 10]><!-->
<script>
  if (!navigator.cookieEnabled) {
    window.addEventListener('DOMContentLoaded', function () {
      var cookieEl = document.getElementById('cookie-alert');
      cookieEl.style.display = 'block';
    })
  }
</script>
<!--<![endif]-->


</head>
<body>
  <div id="cf-wrapper">
    <div class="cf-alert cf-alert-error cf-cookie-error" id="cookie-alert" data-translate="enable_cookies">Please enable cookies.</div>
    <div id="cf-error-details" class="cf-error-details-wrapper">
      <div class="cf-wrapper cf-header cf-error-overview">
        <h1>
          <span class="cf-error-type" data-translate="error">Error</span>
          <span class="cf-error-code">1102</span>
          <small class="heading-ray-id">Ray ID: blablabla &bull; 2021-08-08 15:17:59 UTC</small>
        </h1>
        <h2 class="cf-subheadline" data-translate="error_desc">Worker exceeded resource limits</h2>
      </div><!-- /.header -->

      <section></section><!-- spacer -->

      <div class="cf-section cf-wrapper">
        <div class="cf-columns two">
          <div class="cf-column">
            <h2 data-translate="what_happened">What happened?</h2>
            <p>You've requested a page on a website (shz.al) that is on the <a data-orig-proto="https" data-orig-ref="www.cloudflare.com/5xx-error-landing/" target="_blank">Cloudflare</a> network. An unknown error occurred while rendering the page.</p>
          </div>

          
          <div class="cf-column">
            <h2 data-translate="what_can_i_do">What can I do?</h2>
            <p><strong>If you are the owner of this website:</strong><br />you should <a data-orig-proto="https" data-orig-ref="www.cloudflare.com/login?utm_source=error_100x" target="_blank">login to Cloudflare</a> and check the error logs for shz.al.</p>
          </div>
          
        </div>
      </div><!-- /.section -->

      <div class="cf-error-footer cf-wrapper w-240 lg:w-full py-10 sm:py-4 sm:px-8 mx-auto text-center sm:text-left border-solid border-0 border-t border-gray-300">
  <p class="text-13">
    <span class="cf-footer-item sm:block sm:mb-1">Cloudflare Ray ID: <strong class="font-semibold">bla</strong></span>
    <span class="cf-footer-separator sm:hidden">&bull;</span>
    <span class="cf-footer-item sm:block sm:mb-1"><span>Your IP</span>: himitsudesu</span>
    <span class="cf-footer-separator sm:hidden">&bull;</span>
    <span class="cf-footer-item sm:block sm:mb-1"><span>Performance &amp; security by</span> <a rel="noopener noreferrer" href="https://www.cloudflare.com/5xx-error-landing" id="brand_link" target="_blank">Cloudflare</a></span>
    
  </p>
</div><!-- /.error-footer -->


    </div><!-- /#cf-error-details -->
  </div><!-- /#cf-wrapper -->

  <script type="text/javascript">
  window._cf_translation = {};
  
  
</script>

</body>
</html>

Is there any detailed deployment tutorial?

I tried Fork and changed the corresponding data. The webpage could not find the server and the custom domain was invalid. Is there any detailed tutorial? I searched for an article but failed.

Specify the paste title

We should provide a method to specify the title of a paste. The title is displayed in highlighted HTML, and in the rendered markdown. The frontend should also provide the corresponding input menu.

markdown page returns error 500

With the following text, the markdown page returns Error 500: Cannot read properties of undefined (reading 'write')

# TEST

Hello `world`
- 1. hello
- 2. world

https://ksco.cool/a/KD4X

I haven't investigated the cause yet, but it might be an upstream bug.

Rewrite the test

Current test is written by shell, no one want to read or write it. It require a overhaul, and be intergrated into CI.

Use R2 backend for large files

Due to the limitation of Cloudflare Workers, the size of each paste is limited under 25MB. Maybe we can store large files in Cloudflare R2 storage, to allow us to store larger files.

Possible issues:

  • How to show the progress during the upload

Formdata parsed incorrectly

I uploaded the following paste via the frontend page:

print('Error 2\n', omg, '\n', theta, '\n', expse3mat, '\n', explogexpse3mat)
print('diff = ', np.max(np.abs(expse3mat - explogexpse3mat)))
break

When I fetched the paste, It becomes

print('Error 2\n', omg, '\n', theta, '\n', expse3mat, '\n', explogexpse3mat)
print('diff = ', np.max(np.abs(expse3mat - explogexpse3mat)))
brea

But if I upload the same text via curl, the fetched paste is exactly what I want. I suppose this is due to a buggy implementation of formdata parsing.

Posiblity of deletion of paste by anyone

During recent test of this pastebin, i discovered that its possible for almost anyone to delete your paste without any authentication. Is it made this way for some special reason ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.