108

Anypaste - Command line file sharing for hackers

 6 years ago
source link: https://anypaste.xyz
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Smart command-line file sharing tool for Linux and Mac

Anypaste: Command-line file sharing for hackers.

Anypaste is a tool for uploading files to public-facing hosting sites such as pastebins, transfer.sh, Imgur, and Gfycat. Anypaste automatically detects the type of file being uploaded and chooses an appropriate site accordingly. Anypaste has a plugin system for adding new hosting sites. Anypaste is perfect for sharing content on Reddit, IRC, Discord, Email, forums, or anywhere else that links can go.

IntroductionBack to Top

Anypaste is a tool for uploading files to public-facing websites. Ideally, you can run anypaste /path/to/file and several things will happen:

  1. Anypaste will use file and other utilities to determine the type of the file.
  2. Anypaste will filter the list of plugins (supported hosting sites) to the ones which report they are compatible with the file.
  3. Anypaste will pass control to the “best” supported plugin, which will upload the file to the site and send the link to your terminal.

Anypaste is written in Bash, and was designed to support Linux and macOS. It should also work on other *nix-y systems, like FreeBSD or the Windows Subsystem for Linux, but some desktop integration features (namely the --copy and --notify command line parameters) will probably have issues.

InstallationBack to Top

Anypaste is just a single, cross-platform executable! That’s because it’s a shell script. You can download it from https://anypaste.xyz/sh, which will just redirect you to the raw file hosted on GitHub.

Quick installation:

sudo mkdir -p /usr/local/bin # only needed on macOS
sudo curl -Lo /usr/local/bin/anypaste https://anypaste.xyz/sh
sudo chmod +x /usr/local/bin/anypaste

Dependencies

Bash, curl, and POSIX core utils are all you need. ffprobe is optional but can provide more accurate compatibility checking for media file types; typically it comes inside of an ffmpeg package.

Command-line optionsBack to Top

All command-line options are documented in anypaste -h as well as here.

  • -p: Specify part of a plugin name to use.
  • -t: Comma-separated list of required tags. For example, -t private,permanent will only match plugins that keep your file indefinitely and don’t publicly list links.
  • -f: Skip compatibility checks (force).
  • -i: Interactive mode. Check it out, super cool!
  • -n: Specify a custom name for the file (displayed only on certain sites).
  • -c: Specify a path to a custom configuration file.
  • -C: Create a default configuration file. Everything will be commented out by default. See Configuration for more info.
  • -l, --list: List all currently installed plugins in human-readable form. Can be combined with other options, if so it will list the plugins that would otherwise be attempted for upload.
  • --list-machine: Same as -l, but for use inside of other scripts. It prints the raw get_info output from each plugin, with a blank line between plugins and at the end.
  • -x, --copy, --notify: See Post-Upload Hooks.
  • -v, --version: Print version.
  • -h, --help: Print help text.

ConfigurationBack to Top

Anypaste’s configuration file is a small shell script which sets environment variables. As such, if you know how to write Bash, you know how to write Anypaste config. Here’s a cheat sheet:

  • Set variable (string): export var_name='I am a string'.
  • Set variable (number): export var_name=647.
  • Set array: export arr_name=('I am a string' 42 'This is the third element').
  • Comment: # don't unset this or everything will break! Not sure why....

The export preceding variable names tells Bash to pass these variables to subprocesses. The export is only necessary when configuring external plugins, i.e plugins that aren’t built-in to Anypaste.

To create your configuration file, run anypaste -C. It will tell you where the config file was put. It will typically be ~/.config/anypaste.conf on Linux and ~/.anypaste.conf on Mac. Everything is commented out by default.

Option Description Default
ap_plugins List of enabled plugins, in order of precedence See config file
ap_hooks List of hooks that are enabled by default Empty
ap_hook_policy If set to “greedy”, hooks will be run after every file uploaded. If set to “lazy”, they will only be run just before Anypaste exits. lazy
ap_unicode Whether to prettify things with unicode characters. Mainly used in anypaste -l. true
ap_color Whether to colorize the output. true
ap_copy_regex The regular expression to determine which line to copy from when --copy is used. . (matches first line)

Post-Upload HooksBack to Top

Hooks run after Anypaste finishes uploading a file. Anypaste has two built-in hooks:

  • Copy: Copies the link to your clipboard. Activated by adding the -x or --copy command-line options. By default it copies the “main” link, i.e. the one that begins with Link:. If you wish to copy a different link, say the “direct” links, you can set the ap_copy_regex config option to the regex pattern which will match the line containing the link you want. For direct links, you can do export ap_copy_regex=Direct.
  • Notify: Sends a desktop notification when an upload completes. Activated with --notify. It uses notify-send on Linux to achieve a cross-desktop-environment experience, but some distros may not ship with it. On Mac, it should “just work” out of the box.

You can change how hooks work when multiple files are uploaded using ap_hook_policy. If it is set to lazy (the default), hooks will only run after the last file in a batch. If set to greedy, they will be run after every file.

Custom hooks can be added to the ap_hooks array in the config file. They have access to the same environment variables as plugins, but additionally can see the output of the last plugin in ap_last_stdout. Custom hooks are able to work in combination with built-in plugins; both will run. Example: Adding the following to your config file will save every link uploaded with Anypaste to ~/Other/anypaste-links.txt:

ap_hook_policy=greedy
ap_hooks=('echo -n "$ap_last_stdout" | grep ^Link: | head -n 1 >> ~/Other/anypaste-links.txt')

PluginsBack to Top

Anypaste plugins are each single executable files (except for the built-in ones, which are bundled inside the main Anypaste executable). The recommended way to “install” a plugin is to download it from its author’s website (hopefully a GitHub release), mark it as executable (chmod +x plugin-file), then put it in ~/.anypaste-plugins (you may need to create this folder). Alternatively, you could put it anywhere in your $PATH. After that, you must enable the plugin by adding it to the ap_plugins array in your config file. Remember that this array is in order of precedence – if you put it at the end, it will almost never be used!

Built-In Plugins

Name File Types Extra Notes Anypaste version
Sendvid Videos 1.0
Streamable Videos Requires authentication. 1.0
Gfycat Videos (Gifs) Short videos only. 1.0
Tinyimg Images 10MB upload limit. 1.0
Imgur Images Uses a hardcoded API key. 1.1
Pixhost Images 10MB upload limit. 1.1.4
ix.io Text 1.0
Pastie Text 1.1.4
P.defau.lt Text 1.1.4
Paste2 Text 1.1.4
Hastebin Text Frequent downtimes. 1.0
Docdroid Documents Requires authentication. 1.0
Gofile Generic Unlimited size, official API. 1.1.4
Bayfiles Generic 20GB limit, Official API. 1.1.4
Filemail Generic 50GB upload limit. 1.1.3
Transfer.sh Generic 10GB upload limit. 1.1
Keep.sh Generic 500MB upload limit. 1.1.3
File.io Generic Files deleted after first download. 1.0

Third party plugins

Want to see your plugin on this list? Open an issue!

Name Description Plugin Homepage
ThinImg A simple plugin created mainly to serve as an example about how to make third-party plugins. Note that thinimg.com has gone down since this plugin was created, so it is useful only for pedagogical purposes. See Making Plugins for more information. GitHub

Making PluginsBack to Top

An Anypaste plugin is just an executable file (Bash will be used for all examples, but you can use any language you wish). This executable file is called with a single argument, which instructs the plugin on what to do. Additional data is not passed on the command line, but rather supplied through environment variables which the plugin can read at will. Here’s a list of “commands” (the command line arguments) that can be passed to your plugin, and what your plugin should do in each situation:

check_eligibility

The plugin should determine whether the file being uploaded is compatible with this plugin. Typically, this involves checking that $ap_mime is a supported MIME type and that $ap_size is not over the site’s size limit. The plugin should exit with an exit code of 0 if it’s compatible, or non-zero if it is incompatible. This step should not involve any sort of network communications.

upload

The plugin should perform the actual upload. Typically this involves using curl to upload the file at $ap_path, using whatever API the site provides. It might also use $ap_human_name to set the name of the file on the site being uploaded to, if the site supports that. Any warnings, errors, or progress bars should be outputted to stderr. If the upload completes successfully, the plugin should output a blank line to stdout, followed by the links, followed by another blank line. Each “link” should consist of some sort of label, followed by :, then followed by the actual link. Here’s an example output:


Link: https://imgur.com/abcdef
Direct: https://i.imgur.com/abcdef.png
Edit: https://imgur.com/edit/abcdef/secret
Delete: https://imgur.com/delete/abcdef/secret

Your plugin should try to use “standard” names before the : whenever possible. This makes Anypaste’s behavior more consistent, and makes it easier to use inside of other scripts. If there’s something special about a link that a user should know, it’s better to output it to stderr before the links rather than stdout with the links. When in doubt, look at the built-in plugins and try to act as similarly to them as possible.

Exit with a code of 0 if the upload was successful, or non-zero otherwise.

get_info

The plugin should output “static” information about itself (metadata). Most of the environment variables will not be available during this step. Your plugin should output its information in an ini-like format. Each “section” should start with [section-name], then any lines after that until the next section are the contents. Blank lines are ignored. For example:

[description]
This is a cool plugin for uploading to imgur.com
[tags]
permanent
editable
deletable
[config]
required|imgur_api_public|Public API key
requied|imgur_api_secret|Private API secret

List of possible sections:

  • name: A human-readable name of the plugin. Often just a capitalized version of the machine-readable name.
  • description: A human-readable description of what this plugin does.
  • tags: The plugin’s tags, line-separated.
  • config: Has lines in the format requiredness|name|description. Each is an additional environment variable/configuration option that the user may supply via anypaste.conf. requiredness should be either optional, in which case it is purely cosmetic for display in anypaste -l, recommended, which will trigger a warning whenever the plugin is run without that option, and required, which will consider the plugin incompatible if that option is missing. name is the name of the environment variable for this option. description is cosmetic and human-readable.

Aside: Config options

Config options are just environment variables which are set in the config file. You don’t have to do anything special to get access to them. Keep in mind that they are in a “global” scope, so name them in a way that they don’t interfere with other config options (e.g, don’t name an option “api_key”; instead, use “imgur_api_key”)

Example plugins

The official example plugin for ThinImg is a great learning resource. However, ThinImg is very simple, and we don’t make use of some of the features plugins can have. If you want to see more fleshed-out plugins, look in the first several hundred lines of the main Anypaste source code – that’s where all the built-in plugins are.

Environment VariablesBack to Top

Plugins and hooks have access to a number of environment variables. This is the primary way to get information about the currently uploading file.

Variable Description
ap_path The absolute path to the uploading file on-disk.
ap_human_name The name specified using -n or inferred from the file name.
ap_file_info Output of file "$ap_path", contains lots of useful file metadata.
ap_mime Mime type of the file (from file --mime-type --brief "$ap_path").
ap_ffprobe Output of ffprobe -show_streams -show_format "$ap_path". Null if ffprobe is not installed. Often provides more accurate and reliable info that file can for audio and video.
ap_size Size of the file, in bytes (from wc -c).
ap_plugin The name of the plugin as it appears in ap_plugins.
ap_version Current Anypaste version.
ap_mac true if running on Mac, false otherwise.

There are many other variables set by Anypaste, but they are meant for internal use only and may change without warning.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK