Compare commits

...

19 Commits

Author SHA1 Message Date
Andrea Spacca
0953383523 remove tor, remove bitcoing, fix contact us 2021-12-15 09:34:04 +01:00
Andrea Spacca
6f49951bc0 master to main, what's left 2021-12-07 20:24:58 +01:00
Andrea Spacca
d2a0e77814 fix in force-https redirect (#441) 2021-12-07 19:41:42 +01:00
Andrea Spacca
014b95ff07 fix typo in flake.nix 2021-12-07 13:39:53 +01:00
干志雄
0eec27586d Add uploading and copy download command (#412)
* Add uploading and copy download command
2021-11-13 09:16:53 +01:00
kugiyasan
c7164856d2 issue #420 added MaxDate.IsZero() check (#427)
* issue #420 return 400 response when Max-Days is too big

* issue #420 moved the Max-Days check before saving the metadata

* issue #420 added a logging message when Max-Days is invalid

* issue #420 added MaxDate.IsZero() check

Co-authored-by: kugiyasan <kugiyasan@users.noreply.github.com>
2021-10-30 10:04:43 +02:00
Andrea Spacca
96723b2685 fix go1.16, add go1.17 2021-10-26 19:34:40 +02:00
Andrea Spacca
4a56bad05f bump bluemonday 2021-10-26 19:12:34 +02:00
Andrea Spacca
fa74be02d2 Revert "issue #420 return 400 response when Max-Days is too big (#422)" (#426)
This reverts commit 2959fc2992.
2021-10-26 19:03:51 +02:00
kugiyasan
2959fc2992 issue #420 return 400 response when Max-Days is too big (#422)
* issue #420 return 400 response when Max-Days is too big

Co-authored-by: kugiyasan <kugiyasan@users.noreply.github.com>
2021-10-25 18:33:18 +02:00
Yannik Sander
694b3ce246 Implement Nix Flake (#424)
* Implement flake
2021-10-24 09:42:38 +02:00
Masaya Watanabe
92948fab23 fix missed errors (#417)
Co-authored-by: Andrea Spacca <andrea.spacca@gmail.com>
2021-10-16 09:57:59 +02:00
mattn
e5455d9713 Fix path (#416)
* chan is leaked

* Remove path.Clean

Co-authored-by: Andrea Spacca <andrea.spacca@gmail.com>
2021-10-16 09:51:09 +02:00
Soumil Arora
6ab75b30e5 Edited code of condunct for more information and corrected a grammatical error (#421) 2021-10-16 09:36:02 +02:00
Christian Leo-Pernold
6b8eff8322 Update README.md (#415)
* Update README.md

Add minor grammar improvements.

* Update README.md

Fix grammar.
2021-10-08 10:55:56 +02:00
Stefan Benten
2da62eb235 Merge pull request #414 from modem7/patch-1 2021-10-03 12:51:02 +02:00
modem7
156daa5a24 Update README.md
Edited capitalization of some sections to better follow the rest of the readme.
2021-10-03 02:56:17 +01:00
Andrea Spacca
e97fdcb293 remove build on master 2021-08-20 08:07:12 +02:00
Andrea Spacca
9fe5f9a5c9 Golint (#404)
* golint
* remove gitter badge
2021-08-19 22:45:30 +02:00
22 changed files with 695 additions and 215 deletions

View File

@@ -4,9 +4,9 @@ on:
schedule: schedule:
- cron: '0 0 * * *' # everyday at midnight UTC - cron: '0 0 * * *' # everyday at midnight UTC
pull_request: pull_request:
branches: master branches: main
push: push:
branches: master branches: main
tags: tags:
- v* - v*

View File

@@ -4,14 +4,6 @@ on:
workflow_dispatch: workflow_dispatch:
release: release:
types: [published] types: [published]
push:
branches:
- master
paths:
- "**/*.go"
- "go.mod"
- "go.sum"
- ".github/workflows/*.yml"
jobs: jobs:
build: build:
strategy: strategy:
@@ -115,7 +107,7 @@ jobs:
- name: Set up Go - name: Set up Go
uses: actions/setup-go@v2 uses: actions/setup-go@v2
with: with:
go-version: ^1.16 go-version: ^1.17
- name: Get project dependencies - name: Get project dependencies
run: go mod download run: go mod download

View File

@@ -17,6 +17,7 @@ jobs:
- 1.14.x - 1.14.x
- 1.15.x - 1.15.x
- 1.16.x - 1.16.x
- 1.17.x
name: Test with ${{ matrix.go_version }} name: Test with ${{ matrix.go_version }}
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2

View File

@@ -13,6 +13,7 @@ Examples of unacceptable behavior by participants include:
* Public or private harassment * Public or private harassment
* Publishing other's private information, such as physical or electronic addresses, without explicit permission * Publishing other's private information, such as physical or electronic addresses, without explicit permission
* Other unethical or unprofessional conduct * Other unethical or unprofessional conduct
* Use of harsh language
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. By adopting this Code of Conduct, project maintainers commit themselves to fairly and consistently applying these principles to every aspect of managing this project. Project maintainers who do not follow or enforce the Code of Conduct may be permanently removed from the project team. Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. By adopting this Code of Conduct, project maintainers commit themselves to fairly and consistently applying these principles to every aspect of managing this project. Project maintainers who do not follow or enforce the Code of Conduct may be permanently removed from the project team.
@@ -20,5 +21,5 @@ This code of conduct applies both within project spaces and in public spaces whe
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers. Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
This Code of Conduct is adapted from the [Contributor Covenant](https://www.contributor-covenant.org), version 1.2.0, available at https://www.contributor-covenant.org/version/1/2/0/code-of-conduct.html This Code of Conduct is adapted from the [Contributor Covenant] (https://www.contributor-covenant.org), version 1.2.0, available at https://www.contributor-covenant.org/version/1/2/0/code-of-conduct.html

View File

@@ -1,5 +1,5 @@
# Default to Go 1.16 # Default to Go 1.17
ARG GO_VERSION=1.16 ARG GO_VERSION=1.17
FROM golang:${GO_VERSION}-alpine as build FROM golang:${GO_VERSION}-alpine as build
# Necessary to run 'go get' and to compile the linked binary # Necessary to run 'go get' and to compile the linked binary

View File

@@ -1,4 +1,4 @@
# transfer.sh [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/dutchcoders/transfer.sh?utm_source=badge&utm_medium=badge&utm_campaign=&utm_campaign=pr-badge&utm_content=badge) [![Go Report Card](https://goreportcard.com/badge/github.com/dutchcoders/transfer.sh)](https://goreportcard.com/report/github.com/dutchcoders/transfer.sh) [![Docker pulls](https://img.shields.io/docker/pulls/dutchcoders/transfer.sh.svg)](https://hub.docker.com/r/dutchcoders/transfer.sh/) [![Build Status](https://github.com/dutchcoders/transfer.sh/actions/workflows/test.yml/badge.svg?branch=master)](https://github.com/dutchcoders/transfer.sh/actions/workflows/test.yml?query=branch%3Amaster) # transfer.sh [![Go Report Card](https://goreportcard.com/badge/github.com/dutchcoders/transfer.sh)](https://goreportcard.com/report/github.com/dutchcoders/transfer.sh) [![Docker pulls](https://img.shields.io/docker/pulls/dutchcoders/transfer.sh.svg)](https://hub.docker.com/r/dutchcoders/transfer.sh/) [![Build Status](https://github.com/dutchcoders/transfer.sh/actions/workflows/test.yml/badge.svg?branch=main)](https://github.com/dutchcoders/transfer.sh/actions/workflows/test.yml?query=branch%3Amain)
Easy and fast file sharing from the command-line. This code contains the server with everything you need to create your own instance. Easy and fast file sharing from the command-line. This code contains the server with everything you need to create your own instance.
@@ -15,17 +15,17 @@ The service at transfersh.com is of unknown origin and reported as cloud malware
$ curl --upload-file ./hello.txt https://transfer.sh/hello.txt $ curl --upload-file ./hello.txt https://transfer.sh/hello.txt
``` ```
### Encrypt & upload: ### Encrypt & Upload:
```bash ```bash
$ cat /tmp/hello.txt|gpg -ac -o-|curl -X PUT --upload-file "-" https://transfer.sh/test.txt $ cat /tmp/hello.txt|gpg -ac -o-|curl -X PUT --upload-file "-" https://transfer.sh/test.txt
```` ````
### Download & decrypt: ### Download & Decrypt:
```bash ```bash
$ curl https://transfer.sh/1lDau/test.txt|gpg -o- > /tmp/hello.txt $ curl https://transfer.sh/1lDau/test.txt|gpg -o- > /tmp/hello.txt
``` ```
### Upload to virustotal: ### Upload to Virustotal:
```bash ```bash
$ curl -X PUT --upload-file nhgbhhj https://transfer.sh/test.txt/virustotal $ curl -X PUT --upload-file nhgbhhj https://transfer.sh/test.txt/virustotal
``` ```
@@ -51,7 +51,7 @@ $ curl --upload-file ./hello.txt https://transfer.sh/hello.txt -H "Max-Days: 1"
### X-Url-Delete ### X-Url-Delete
The URL used to request the deletion of a file. Returned as a response header. The URL used to request the deletion of a file and returned as a response header.
```bash ```bash
curl -sD - --upload-file ./hello https://transfer.sh/hello.txt | grep 'X-Url-Delete' curl -sD - --upload-file ./hello https://transfer.sh/hello.txt | grep 'X-Url-Delete'
X-Url-Delete: https://transfer.sh/hello.txt/BAYh0/hello.txt/PDw0NHPcqU X-Url-Delete: https://transfer.sh/hello.txt/BAYh0/hello.txt/PDw0NHPcqU
@@ -90,6 +90,7 @@ temp-path | path to temp folder | system temp | TEMP_PATH |
web-path | path to static web files (for development or custom front end) | | WEB_PATH | web-path | path to static web files (for development or custom front end) | | WEB_PATH |
proxy-path | path prefix when service is run behind a proxy | | PROXY_PATH | proxy-path | path prefix when service is run behind a proxy | | PROXY_PATH |
proxy-port | port of the proxy when the service is run behind a proxy | | PROXY_PORT | proxy-port | port of the proxy when the service is run behind a proxy | | PROXY_PORT |
email-contact | email contact for the front end | | EMAIL_CONTACT |
ga-key | google analytics key for the front end | | GA_KEY | ga-key | google analytics key for the front end | | GA_KEY |
provider | which storage provider to use | (s3, storj, gdrive or local) | provider | which storage provider to use | (s3, storj, gdrive or local) |
uservoice-key | user voice key for the front end | | USERVOICE_KEY | uservoice-key | user voice key for the front end | | USERVOICE_KEY |
@@ -161,23 +162,23 @@ To use a custom non-AWS S3 provider, you need to specify the endpoint as defined
## Storj Network Provider ## Storj Network Provider
To use the Storj Network as storage provider you need to specify the following flags: To use the Storj Network as a storage provider you need to specify the following flags:
- provider `--provider storj` - provider `--provider storj`
- storj-access _(either via flag or environment variable STORJ_ACCESS)_ - storj-access _(either via flag or environment variable STORJ_ACCESS)_
- storj-bucket _(either via flag or environment variable STORJ_BUCKET)_ - storj-bucket _(either via flag or environment variable STORJ_BUCKET)_
### Creating Bucket and Scope ### Creating Bucket and Scope
In preparation you need to create an access grant (or copy it from the uplink configuration) and a bucket. You need to create an access grant (or copy it from the uplink configuration) and a bucket in preparation.
To get started, login to your account and go to the Access Grant Menu and start the Wizard on the upper right. To get started, log in to your account and go to the Access Grant Menu and start the Wizard on the upper right.
Enter your access grant name of choice, hit *Next* and restrict it as necessary/preferred. Enter your access grant name of choice, hit *Next* and restrict it as necessary/preferred.
Aftwards continue either in CLI or within the Browser. You'll be asked for a Passphrase used as Encryption Key. Afterwards continue either in CLI or within the Browser. Next, you'll be asked for a Passphrase used as Encryption Key.
**Make sure to save it in a safe place, without it you will lose the ability to decrypt your files!** **Make sure to save it in a safe place. Without it, you will lose the ability to decrypt your files!**
Afterwards you can copy the access grant and then start the startup of the transfer.sh endpoint. Afterwards, you can copy the access grant and then start the startup of the transfer.sh endpoint.
For enhanced security its recommended to provide both the access grant and the bucket name as ENV Variables. It is recommended to provide both the access grant and the bucket name as ENV Variables for enhanced security.
Example: Example:
``` ```
@@ -196,8 +197,7 @@ For the usage with Google drive, you need to specify the following options:
### Creating Gdrive Client Json ### Creating Gdrive Client Json
You need to create a Oauth Client id from console.cloud.google.com You need to create an OAuth Client id from console.cloud.google.com, download the file, and place it into a safe directory.
download the file and place into a safe directory
### Usage example ### Usage example
@@ -221,7 +221,7 @@ Contributions are welcome.
**Stefan Benten** **Stefan Benten**
## Copyright and license ## Copyright and License
Code and documentation copyright 2011-2018 Remco Verhoef. Code and documentation copyright 2011-2018 Remco Verhoef.
Code and documentation copyright 2018-2020 Andrea Spacca. Code and documentation copyright 2018-2020 Andrea Spacca.

View File

@@ -12,6 +12,7 @@ import (
"google.golang.org/api/googleapi" "google.golang.org/api/googleapi"
) )
// Version is inject at build time
var Version = "0.0.0" var Version = "0.0.0"
var helpTemplate = `NAME: var helpTemplate = `NAME:
{{.Name}} - {{.Usage}} {{.Name}} - {{.Usage}}
@@ -97,6 +98,12 @@ var globalFlags = []cli.Flag{
Value: "", Value: "",
EnvVar: "PROXY_PORT", EnvVar: "PROXY_PORT",
}, },
cli.StringFlag{
Name: "email-contact",
Usage: "email address to link in Contact Us (front end)",
Value: "",
EnvVar: "EMAIL_CONTACT",
},
cli.StringFlag{ cli.StringFlag{
Name: "ga-key", Name: "ga-key",
Usage: "key for google analytics (front end)", Usage: "key for google analytics (front end)",
@@ -282,14 +289,16 @@ var globalFlags = []cli.Flag{
}, },
} }
// Cmd wraps cli.app
type Cmd struct { type Cmd struct {
*cli.App *cli.App
} }
func VersionAction(c *cli.Context) { func versionAction(c *cli.Context) {
fmt.Println(color.YellowString(fmt.Sprintf("transfer.sh %s: Easy file sharing from the command line", Version))) fmt.Println(color.YellowString(fmt.Sprintf("transfer.sh %s: Easy file sharing from the command line", Version)))
} }
// New is the factory for transfer.sh
func New() *Cmd { func New() *Cmd {
logger := log.New(os.Stdout, "[transfer.sh]", log.LstdFlags) logger := log.New(os.Stdout, "[transfer.sh]", log.LstdFlags)
@@ -304,7 +313,7 @@ func New() *Cmd {
app.Commands = []cli.Command{ app.Commands = []cli.Command{
{ {
Name: "version", Name: "version",
Action: VersionAction, Action: versionAction,
}, },
} }
@@ -345,6 +354,10 @@ func New() *Cmd {
options = append(options, server.ProxyPort(v)) options = append(options, server.ProxyPort(v))
} }
if v := c.String("email-contact"); v != "" {
options = append(options, server.EmailContact(v))
}
if v := c.String("ga-key"); v != "" { if v := c.String("ga-key"); v != "" {
options = append(options, server.GoogleAnalytics(v)) options = append(options, server.GoogleAnalytics(v))
} }
@@ -403,13 +416,13 @@ func New() *Cmd {
} }
if c.Bool("force-https") { if c.Bool("force-https") {
options = append(options, server.ForceHTTPs()) options = append(options, server.ForceHTTPS())
} }
if httpAuthUser := c.String("http-auth-user"); httpAuthUser == "" { if httpAuthUser := c.String("http-auth-user"); httpAuthUser == "" {
} else if httpAuthPass := c.String("http-auth-pass"); httpAuthPass == "" { } else if httpAuthPass := c.String("http-auth-pass"); httpAuthPass == "" {
} else { } else {
options = append(options, server.HttpAuthCredentials(httpAuthUser, httpAuthPass)) options = append(options, server.HTTPAuthCredentials(httpAuthUser, httpAuthPass))
} }
applyIPFilter := false applyIPFilter := false
@@ -445,13 +458,13 @@ func New() *Cmd {
case "gdrive": case "gdrive":
chunkSize := c.Int("gdrive-chunk-size") chunkSize := c.Int("gdrive-chunk-size")
if clientJsonFilepath := c.String("gdrive-client-json-filepath"); clientJsonFilepath == "" { if clientJSONFilepath := c.String("gdrive-client-json-filepath"); clientJSONFilepath == "" {
panic("client-json-filepath not set.") panic("client-json-filepath not set.")
} else if localConfigPath := c.String("gdrive-local-config-path"); localConfigPath == "" { } else if localConfigPath := c.String("gdrive-local-config-path"); localConfigPath == "" {
panic("local-config-path not set.") panic("local-config-path not set.")
} else if basedir := c.String("basedir"); basedir == "" { } else if basedir := c.String("basedir"); basedir == "" {
panic("basedir not set.") panic("basedir not set.")
} else if storage, err := server.NewGDriveStorage(clientJsonFilepath, localConfigPath, basedir, chunkSize, logger); err != nil { } else if storage, err := server.NewGDriveStorage(clientJSONFilepath, localConfigPath, basedir, chunkSize, logger); err != nil {
panic(err) panic(err)
} else { } else {
options = append(options, server.UseStorage(storage)) options = append(options, server.UseStorage(storage))

View File

@@ -5,6 +5,7 @@
* [Archiving and backups](#archiving-and-backups) * [Archiving and backups](#archiving-and-backups)
* [Encrypting and decrypting](#encrypting-and-decrypting) * [Encrypting and decrypting](#encrypting-and-decrypting)
* [Scanning for viruses](#scanning-for-viruses) * [Scanning for viruses](#scanning-for-viruses)
* [Uploading and copy download command](#uploading-and-copy-download-command)
## Aliases ## Aliases
<a name="aliases"/> <a name="aliases"/>
@@ -173,4 +174,90 @@ $ curl -X PUT --upload-file ./eicar.com https://transfer.sh/eicar.com/scan
### Upload malware to VirusTotal, get a permalink in return ### Upload malware to VirusTotal, get a permalink in return
```bash ```bash
$ curl -X PUT --upload-file nhgbhhj https://transfer.sh/test.txt/virustotal $ curl -X PUT --upload-file nhgbhhj https://transfer.sh/test.txt/virustotal
``` ```
## Uploading and copy download command
Download commands can be automatically copied to the clipboard after files are uploaded using transfer.sh.
It was designed for Linux or macOS.
### 1. Install xclip or xsel for Linux, macOS skips this step
- install xclip see https://command-not-found.com/xclip
- install xsel see https://command-not-found.com/xsel
Install later, add pbcopy and pbpaste to .bashrc or .zshrc or its equivalent.
- If use xclip, paste the following lines:
```sh
alias pbcopy='xclip -selection clipboard'
alias pbpaste='xclip -selection clipboard -o'
```
- If use xsel, paste the following lines:
```sh
alias pbcopy='xsel --clipboard --input'
alias pbpaste='xsel --clipboard --output'
```
### 2. Add Uploading and copy download command shell function
1. Open .bashrc or .zshrc or its equivalent.
2. Add the following shell script:
```sh
transfer() {
curl --progress-bar --upload-file "$1" https://transfer.sh/$(basename "$1") | pbcopy;
echo "1) Download link:"
echo "$(pbpaste)"
echo "\n2) Linux or macOS download command:"
linux_macos_download_command="wget $(pbpaste)"
echo $linux_macos_download_command
echo "\n3) Windows download command:"
windows_download_command="Invoke-WebRequest -Uri "$(pbpaste)" -OutFile $(basename $1)"
echo $windows_download_command
case $2 in
l|m) echo $linux_macos_download_command | pbcopy
;;
w) echo $windows_download_command | pbcopy
;;
esac
}
```
### 3. Test
The transfer command has two parameters:
1. The first parameter is the path to upload the file.
2. The second parameter indicates which system's download command is copied. optional:
- This parameter is empty to copy the download link.
- `l` or `m` copy the Linux or macOS command that downloaded the file.
- `w` copy the Windows command that downloaded the file.
For example, The command to download the file on Windows will be copied:
```sh
$ transfer ~/temp/a.log w
######################################################################## 100.0%
1) Download link:
https://transfer.sh/y0qr2c/a.log
2) Linux or macOS download command:
wget https://transfer.sh/y0qr2c/a.log
3) Windows download command:
Invoke-WebRequest -Uri https://transfer.sh/y0qr2c/a.log -OutFile a.log
```

41
flake.lock generated Normal file
View File

@@ -0,0 +1,41 @@
{
"nodes": {
"flake-utils": {
"locked": {
"lastModified": 1631561581,
"narHash": "sha256-3VQMV5zvxaVLvqqUrNz3iJelLw30mIVSfZmAaauM3dA=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "7e5bf3925f6fbdfaf50a2a7ca0be2879c4261d19",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1632470817,
"narHash": "sha256-tGyOesdpqQEVqlmVeElsC98OJ2GDy+LNaCThSby/GQM=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "39e8ec2db68b863543bd377e44fbe02f8d05864e",
"type": "github"
},
"original": {
"id": "nixpkgs",
"type": "indirect"
}
},
"root": {
"inputs": {
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}

212
flake.nix Normal file
View File

@@ -0,0 +1,212 @@
{
description = "Transfer.sh";
inputs.flake-utils.url = "github:numtide/flake-utils";
outputs = { self, nixpkgs, flake-utils }:
let
transfer-sh = pkgs: pkgs.buildGoModule {
src = self;
name = "transfer.sh";
vendorSha256 = "sha256-bgQUMiC33yVorcKOWhegT1/YU+fvxsz2pkeRvjf3R7g=";
};
in
flake-utils.lib.eachDefaultSystem (
system:
let
pkgs = nixpkgs.legacyPackages.${system};
in
rec {
packages = flake-utils.lib.flattenTree {
transfer-sh = transfer-sh pkgs;
};
defaultPackage = packages.transfer-sh;
apps.transfer-sh = flake-utils.lib.mkApp { drv = packages.transfer-sh; };
defaultApp = apps.transfer-sh;
}
) // rec {
nixosModules = {
transfer-sh = { config, lib, pkgs, ... }: with lib; let
RUNTIME_DIR = "/var/lib/transfer.sh";
cfg = config.services.transfer-sh;
general_options = {
enable = mkEnableOption "Transfer.sh service";
listener = mkOption { default = 80; type = types.int; description = "port to use for http (:80)"; };
profile-listener = mkOption { default = 6060; type = types.int; description = "port to use for profiler (:6060)"; };
force-https = mkOption { type = types.nullOr types.bool; description = "redirect to https"; };
tls-listener = mkOption { default = 443; type = types.int; description = "port to use for https (:443)"; };
tls-listener-only = mkOption { type = types.nullOr types.bool; description = "flag to enable tls listener only"; };
tls-cert-file = mkOption { type = types.nullOr types.str; description = "path to tls certificate"; };
tls-private-key = mkOption { type = types.nullOr types.str; description = "path to tls private key "; };
http-auth-user = mkOption { type = types.nullOr types.str; description = "user for basic http auth on upload"; };
http-auth-pass = mkOption { type = types.nullOr types.str; description = "pass for basic http auth on upload"; };
ip-whitelist = mkOption { type = types.nullOr types.str; description = "comma separated list of ips allowed to connect to the service"; };
ip-blacklist = mkOption { type = types.nullOr types.str; description = "comma separated list of ips not allowed to connect to the service"; };
temp-path = mkOption { type = types.nullOr types.str; description = "path to temp folder"; };
web-path = mkOption { type = types.nullOr types.str; description = "path to static web files (for development or custom front end)"; };
proxy-path = mkOption { type = types.nullOr types.str; description = "path prefix when service is run behind a proxy"; };
proxy-port = mkOption { type = types.nullOr types.str; description = "port of the proxy when the service is run behind a proxy"; };
ga-key = mkOption { type = types.nullOr types.str; description = "google analytics key for the front end"; };
email-contact = mkOption { type = types.nullOr types.str; description = "email contact for the front end"; };
uservoice-key = mkOption { type = types.nullOr types.str; description = "user voice key for the front end"; };
lets-encrypt-hosts = mkOption { type = types.nullOr (types.listOf types.str); description = "hosts to use for lets encrypt certificates"; };
log = mkOption { type = types.nullOr types.str; description = "path to log file"; };
cors-domains = mkOption { type = types.nullOr (types.listOf types.str); description = "comma separated list of domains for CORS, setting it enable CORS "; };
clamav-host = mkOption { type = types.nullOr types.str; description = "host for clamav feature"; };
rate-limit = mkOption { type = types.nullOr types.int; description = "request per minute"; };
max-upload-size = mkOption { type = types.nullOr types.int; description = "max upload size in kilobytes "; };
purge-days = mkOption { type = types.nullOr types.int; description = "number of days after the uploads are purged automatically "; };
random-token-length = mkOption { type = types.nullOr types.int; description = "length of the random token for the upload path (double the size for delete path)"; };
};
provider_options = {
aws = {
enable = mkEnableOption "Enable AWS backend";
aws-access-key = mkOption { type = types.str; description = "aws access key"; };
aws-secret-key = mkOption { type = types.str; description = "aws secret key"; };
bucket = mkOption { type = types.str; description = "aws bucket "; };
s3-endpoint = mkOption {
type = types.nullOr types.str;
description = ''
Custom S3 endpoint.
If you specify the s3-region, you don't need to set the endpoint URL since the correct endpoint will used automatically.
'';
};
s3-region = mkOption { type = types.str; description = "region of the s3 bucket eu-west-"; };
s3-no-multipart = mkOption { type = types.nullOr types.bool; description = "disables s3 multipart upload "; };
s3-path-style = mkOption { type = types.nullOr types.str; description = "Forces path style URLs, required for Minio. "; };
};
storj = {
enable = mkEnableOption "Enable storj backend";
storj-access = mkOption { type = types.str; description = "Access for the project"; };
storj-bucket = mkOption { type = types.str; description = "Bucket to use within the project"; };
};
gdrive = {
enable = mkEnableOption "Enable gdrive backend";
gdrive-client-json = mkOption { type = types.str; description = "oauth client json config for gdrive provider"; };
gdrive-chunk-size = mkOption { default = 8; type = types.nullOr types.int; description = "chunk size for gdrive upload in megabytes, must be lower than available memory (8 MB)"; };
basedir = mkOption { type = types.str; description = "path storage for gdrive provider"; default = "${cfg.stateDir}/store"; };
purge-interval = mkOption { type = types.nullOr types.int; description = "interval in hours to run the automatic purge for (not applicable to S3 and Storj)"; };
};
local = {
enable = mkEnableOption "Enable local backend";
basedir = mkOption { type = types.str; description = "path storage for local provider"; default = "${cfg.stateDir}/store"; };
purge-interval = mkOption { type = types.nullOr types.int; description = "interval in hours to run the automatic purge for (not applicable to S3 and Storj)"; };
};
};
in
{
options.services.transfer-sh = fold recursiveUpdate {} [
general_options
{
provider = provider_options;
user = mkOption {
type = types.str;
description = "User to run the service under";
default = "transfer.sh";
};
group = mkOption {
type = types.str;
description = "Group to run the service under";
default = "transfer.sh";
};
stateDir = mkOption {
type = types.path;
description = "Variable state directory";
default = RUNTIME_DIR;
};
}
];
config = let
mkFlags = cfg: options:
let
mkBoolFlag = option: if cfg.${option} then [ "--${option}" ] else [];
mkFlag = option:
if isBool cfg.${option}
then mkBoolFlag option
else [ "--${option}" "${cfg.${option}}" ];
in
lists.flatten (map (mkFlag) (filter (option: cfg.${option} != null && option != "enable") options));
aws-config = (mkFlags cfg.provider.aws (attrNames provider_options)) ++ [ "--provider" "aws" ];
gdrive-config = mkFlags cfg.provider.gdrive (attrNames provider_options.gdrive) ++ [ "--provider" "gdrive" ];
storj-config = mkFlags cfg.provider.storj (attrNames provider_options.storj) ++ [ "--provider" "storj" ];
local-config = mkFlags cfg.provider.local (attrNames provider_options.local) ++ [ "--provider" "local" ];
general-config = concatStringsSep " " (mkFlags cfg (attrNames general_options));
provider-config = concatStringsSep " " (
if cfg.provider.aws.enable && !cfg.provider.storj.enable && !cfg.provider.gdrive.enable && !cfg.provider.local.enable then aws-config
else if !cfg.provider.aws.enable && cfg.provider.storj.enable && !cfg.provider.gdrive.enable && !cfg.provider.local.enable then storj-config
else if !cfg.provider.aws.enable && !cfg.provider.storj.enable && cfg.provider.gdrive.enable && !cfg.provider.local.enable then gdrive-config
else if !cfg.provider.aws.enable && !cfg.provider.storj.enable && !cfg.provider.gdrive.enable && cfg.provider.local.enable then local-config
else throw "transfer.sh requires exactly one provider (aws, storj, gdrive, local)"
);
in
lib.mkIf cfg.enable
{
systemd.tmpfiles.rules = [
"d ${cfg.stateDir} 0750 ${cfg.user} ${cfg.group} - -"
] ++ optional cfg.provider.gdrive.enable cfg.provider.gdrive.basedir
++ optional cfg.provider.local.enable cfg.provider.local.basedir;
systemd.services.transfer-sh = {
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
serviceConfig = {
User = cfg.user;
Group = cfg.group;
ExecStart = "${transfer-sh pkgs}/bin/transfer.sh ${general-config} ${provider-config} ";
};
};
networking.firewall.allowedTCPPorts = [ cfg.listener cfg.profile-listener cfg.tls-listener ];
};
};
default = { self, pkgs, ... }: {
imports = [ nixosModules.transfer-sh ];
# Network configuration.
# useDHCP is generally considered to better be turned off in favor
# of <adapter>.useDHCP
networking.useDHCP = false;
networking.firewall.allowedTCPPorts = [];
# Enable the inventaire server.
services.transfer-sh = {
enable = true;
provider.local = {
enable = true;
};
};
nixpkgs.config.allowUnfree = true;
};
};
nixosConfigurations."container" = nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
modules = [
nixosModules.default
({ ... }: { boot.isContainer = true; })
];
};
};
}

7
go.mod
View File

@@ -8,10 +8,11 @@ require (
github.com/VojtechVitek/ratelimit v0.0.0-20160722140851-dc172bc0f6d2 github.com/VojtechVitek/ratelimit v0.0.0-20160722140851-dc172bc0f6d2
github.com/aws/aws-sdk-go v1.37.14 github.com/aws/aws-sdk-go v1.37.14
github.com/calebcase/tmpfile v1.0.2 // indirect github.com/calebcase/tmpfile v1.0.2 // indirect
github.com/chris-ramon/douceur v0.2.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/dutchcoders/go-clamd v0.0.0-20170520113014-b970184f4d9e github.com/dutchcoders/go-clamd v0.0.0-20170520113014-b970184f4d9e
github.com/dutchcoders/go-virustotal v0.0.0-20140923143438-24cc8e6fa329 github.com/dutchcoders/go-virustotal v0.0.0-20140923143438-24cc8e6fa329
github.com/dutchcoders/transfer.sh-web v0.0.0-20210723094506-f0946ebceb7a github.com/dutchcoders/transfer.sh-web v0.0.0-20211215083008-31e11925a9d3
github.com/elazarl/go-bindata-assetfs v1.0.1 github.com/elazarl/go-bindata-assetfs v1.0.1
github.com/fatih/color v1.10.0 github.com/fatih/color v1.10.0
github.com/garyburd/redigo v1.6.2 // indirect github.com/garyburd/redigo v1.6.2 // indirect
@@ -19,7 +20,7 @@ require (
github.com/gorilla/handlers v1.5.1 github.com/gorilla/handlers v1.5.1
github.com/gorilla/mux v1.8.0 github.com/gorilla/mux v1.8.0
github.com/gorilla/securecookie v1.1.1 // indirect github.com/gorilla/securecookie v1.1.1 // indirect
github.com/microcosm-cc/bluemonday v1.0.5 github.com/microcosm-cc/bluemonday v1.0.16
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d // indirect github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d // indirect
github.com/russross/blackfriday/v2 v2.1.0 github.com/russross/blackfriday/v2 v2.1.0
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
@@ -27,7 +28,7 @@ require (
github.com/urfave/cli v1.22.5 github.com/urfave/cli v1.22.5
go.opencensus.io v0.22.6 // indirect go.opencensus.io v0.22.6 // indirect
golang.org/x/crypto v0.0.0-20210415154028-4f45737414dc golang.org/x/crypto v0.0.0-20210415154028-4f45737414dc
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 golang.org/x/net v0.0.0-20210614182718-04defd469f4e
golang.org/x/oauth2 v0.0.0-20210218202405-ba52d332ba99 golang.org/x/oauth2 v0.0.0-20210218202405-ba52d332ba99
google.golang.org/api v0.40.0 google.golang.org/api v0.40.0
google.golang.org/genproto v0.0.0-20210218151259-fe80b386bf06 // indirect google.golang.org/genproto v0.0.0-20210218151259-fe80b386bf06 // indirect

12
go.sum
View File

@@ -86,6 +86,10 @@ github.com/dutchcoders/go-virustotal v0.0.0-20140923143438-24cc8e6fa329 h1:ERqCk
github.com/dutchcoders/go-virustotal v0.0.0-20140923143438-24cc8e6fa329/go.mod h1:G5qOfE5bQZ5scycLpB7fYWgN4y3xdfXo+pYWM8z2epY= github.com/dutchcoders/go-virustotal v0.0.0-20140923143438-24cc8e6fa329/go.mod h1:G5qOfE5bQZ5scycLpB7fYWgN4y3xdfXo+pYWM8z2epY=
github.com/dutchcoders/transfer.sh-web v0.0.0-20210723094506-f0946ebceb7a h1:+N7J1NK7gxKZ+X4syY1HqafUudJiR8voJGcXWkxLgAw= github.com/dutchcoders/transfer.sh-web v0.0.0-20210723094506-f0946ebceb7a h1:+N7J1NK7gxKZ+X4syY1HqafUudJiR8voJGcXWkxLgAw=
github.com/dutchcoders/transfer.sh-web v0.0.0-20210723094506-f0946ebceb7a/go.mod h1:F6Q37CxDh2MHr5KXkcZmNB3tdkK7v+bgE+OpBY+9ilI= github.com/dutchcoders/transfer.sh-web v0.0.0-20210723094506-f0946ebceb7a/go.mod h1:F6Q37CxDh2MHr5KXkcZmNB3tdkK7v+bgE+OpBY+9ilI=
github.com/dutchcoders/transfer.sh-web v0.0.0-20210819203540-bbdd40be1311 h1:/Rwuhcp8ZLUauWajAgMyy6AiVbobvD52I+/OnzThK0A=
github.com/dutchcoders/transfer.sh-web v0.0.0-20210819203540-bbdd40be1311/go.mod h1:F6Q37CxDh2MHr5KXkcZmNB3tdkK7v+bgE+OpBY+9ilI=
github.com/dutchcoders/transfer.sh-web v0.0.0-20211215083008-31e11925a9d3 h1:HyfU90/8y9S5IkHTQgIfzs4dT3iagbJUJLncCsSwZCI=
github.com/dutchcoders/transfer.sh-web v0.0.0-20211215083008-31e11925a9d3/go.mod h1:F6Q37CxDh2MHr5KXkcZmNB3tdkK7v+bgE+OpBY+9ilI=
github.com/elazarl/go-bindata-assetfs v1.0.1 h1:m0kkaHRKEu7tUIUFVwhGGGYClXvyl4RE03qmvRTNfbw= github.com/elazarl/go-bindata-assetfs v1.0.1 h1:m0kkaHRKEu7tUIUFVwhGGGYClXvyl4RE03qmvRTNfbw=
github.com/elazarl/go-bindata-assetfs v1.0.1/go.mod h1:v+YaWX3bdea5J/mo8dSETolEo7R71Vk1u8bnjau5yw4= github.com/elazarl/go-bindata-assetfs v1.0.1/go.mod h1:v+YaWX3bdea5J/mo8dSETolEo7R71Vk1u8bnjau5yw4=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
@@ -217,6 +221,8 @@ github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHX
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/microcosm-cc/bluemonday v1.0.5 h1:cF59UCKMmmUgqN1baLvqU/B1ZsMori+duLVTLpgiG3w= github.com/microcosm-cc/bluemonday v1.0.5 h1:cF59UCKMmmUgqN1baLvqU/B1ZsMori+duLVTLpgiG3w=
github.com/microcosm-cc/bluemonday v1.0.5/go.mod h1:8iwZnFn2CDDNZ0r6UXhF4xawGvzaqzCRa1n3/lO3W2w= github.com/microcosm-cc/bluemonday v1.0.5/go.mod h1:8iwZnFn2CDDNZ0r6UXhF4xawGvzaqzCRa1n3/lO3W2w=
github.com/microcosm-cc/bluemonday v1.0.16 h1:kHmAq2t7WPWLjiGvzKa5o3HzSfahUKiOq7fAPUiMNIc=
github.com/microcosm-cc/bluemonday v1.0.16/go.mod h1:Z0r70sCuXHig8YpBzCc5eGHAap2K7e/u082ZUpDRRqM=
github.com/mitchellh/mapstructure v0.0.0-20170523030023-d0303fe80992/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/mitchellh/mapstructure v0.0.0-20170523030023-d0303fe80992/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d h1:VhgPp6v9qf9Agr/56bj7Y/xa04UccTW04VP0Qed4vnQ= github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d h1:VhgPp6v9qf9Agr/56bj7Y/xa04UccTW04VP0Qed4vnQ=
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d/go.mod h1:YUTz3bUH2ZwIWBy3CJBeOBEugqcmXREj14T+iG/4k4U= github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d/go.mod h1:YUTz3bUH2ZwIWBy3CJBeOBEugqcmXREj14T+iG/4k4U=
@@ -354,6 +360,8 @@ golang.org/x/net v0.0.0-20201209123823-ac852fbbde11/go.mod h1:m0MpNAwzfU5UDzcl9v
golang.org/x/net v0.0.0-20210119194325-5f4716e94777/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210119194325-5f4716e94777/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 h1:qWPm9rbaAMKs8Bq/9LRpbMqxWRVUAQwMI9fVrssnTfw= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 h1:qWPm9rbaAMKs8Bq/9LRpbMqxWRVUAQwMI9fVrssnTfw=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210614182718-04defd469f4e h1:XpT3nA5TvE525Ne3hInMh6+GETgn27Zfm9dxsThnX2Q=
golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/oauth2 v0.0.0-20170912212905-13449ad91cb2/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20170912212905-13449ad91cb2/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
@@ -417,6 +425,8 @@ golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210420072515-93ed5bcd2bfe h1:WdX7u8s3yOigWAhHEaDl8r9G+4XwFQEQFtBMYyN+kXQ= golang.org/x/sys v0.0.0-20210420072515-93ed5bcd2bfe h1:WdX7u8s3yOigWAhHEaDl8r9G+4XwFQEQFtBMYyN+kXQ=
golang.org/x/sys v0.0.0-20210420072515-93ed5bcd2bfe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210420072515-93ed5bcd2bfe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da h1:b3NXsE2LusjYGGjL5bxEVZZORm/YEFFrWFjR8eFrw/c=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
@@ -426,6 +436,8 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.5 h1:i6eZZ+zk0SOf0xgBpEpPD18qWcJda6q1sxt3S0kzyUQ= golang.org/x/text v0.3.5 h1:i6eZZ+zk0SOf0xgBpEpPD18qWcJda6q1sxt3S0kzyUQ=
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/time v0.0.0-20170424234030-8be79e1e0910/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20170424234030-8be79e1e0910/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=

View File

@@ -57,6 +57,7 @@ func (s *Server) scanHandler(w http.ResponseWriter, r *http.Request) {
c := clamd.NewClamd(s.ClamAVDaemonHost) c := clamd.NewClamd(s.ClamAVDaemonHost)
abort := make(chan bool) abort := make(chan bool)
defer close(abort)
response, err := c.ScanStream(reader, abort) response, err := c.ScanStream(reader, abort)
if err != nil { if err != nil {
s.logger.Printf("%s", err.Error()) s.logger.Printf("%s", err.Error())
@@ -70,6 +71,4 @@ func (s *Server) scanHandler(w http.ResponseWriter, r *http.Request) {
case <-time.After(time.Second * 60): case <-time.After(time.Second * 60):
abort <- true abort <- true
} }
close(abort)
} }

View File

@@ -37,7 +37,6 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
blackfriday "github.com/russross/blackfriday/v2"
"html" "html"
html_template "html/template" html_template "html/template"
"io" "io"
@@ -54,9 +53,12 @@ import (
text_template "text/template" text_template "text/template"
"time" "time"
blackfriday "github.com/russross/blackfriday/v2"
"net" "net"
"encoding/base64" "encoding/base64"
web "github.com/dutchcoders/transfer.sh-web" web "github.com/dutchcoders/transfer.sh-web"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/microcosm-cc/bluemonday" "github.com/microcosm-cc/bluemonday"
@@ -123,7 +125,7 @@ func (s *Server) previewHandler(w http.ResponseWriter, r *http.Request) {
token := vars["token"] token := vars["token"]
filename := vars["filename"] filename := vars["filename"]
metadata, err := s.CheckMetadata(token, filename, false) metadata, err := s.checkMetadata(token, filename, false)
if err != nil { if err != nil {
s.logger.Printf("Error metadata: %s", err.Error()) s.logger.Printf("Error metadata: %s", err.Error())
@@ -198,9 +200,9 @@ func (s *Server) previewHandler(w http.ResponseWriter, r *http.Request) {
ContentType string ContentType string
Content html_template.HTML Content html_template.HTML
Filename string Filename string
Url string URL string
UrlGet string URLGet string
UrlRandomToken string URLRandomToken string
Hostname string Hostname string
WebAddress string WebAddress string
ContentLength uint64 ContentLength uint64
@@ -251,6 +253,7 @@ func (s *Server) viewHandler(w http.ResponseWriter, r *http.Request) {
data := struct { data := struct {
Hostname string Hostname string
WebAddress string WebAddress string
EmailContact string
GAKey string GAKey string
UserVoiceKey string UserVoiceKey string
PurgeTime string PurgeTime string
@@ -260,12 +263,13 @@ func (s *Server) viewHandler(w http.ResponseWriter, r *http.Request) {
}{ }{
hostname, hostname,
webAddress, webAddress,
s.emailContact,
s.gaKey, s.gaKey,
s.userVoiceKey, s.userVoiceKey,
purgeTime, purgeTime,
maxUploadSize, maxUploadSize,
Token(s.randomTokenLength), token(s.randomTokenLength),
Token(s.randomTokenLength), token(s.randomTokenLength),
} }
if acceptsHTML(r.Header) { if acceptsHTML(r.Header) {
@@ -286,7 +290,7 @@ func (s *Server) notFoundHandler(w http.ResponseWriter, r *http.Request) {
} }
func sanitize(fileName string) string { func sanitize(fileName string) string {
return path.Clean(path.Base(fileName)) return path.Base(fileName)
} }
func (s *Server) postHandler(w http.ResponseWriter, r *http.Request) { func (s *Server) postHandler(w http.ResponseWriter, r *http.Request) {
@@ -296,7 +300,7 @@ func (s *Server) postHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
token := Token(s.randomTokenLength) token := token(s.randomTokenLength)
w.Header().Set("Content-Type", "text/plain") w.Header().Set("Content-Type", "text/plain")
@@ -342,6 +346,11 @@ func (s *Server) postHandler(w http.ResponseWriter, r *http.Request) {
} }
reader, err = os.Open(file.Name()) reader, err = os.Open(file.Name())
if err != nil {
s.logger.Printf("%s", err.Error())
http.Error(w, err.Error(), 500)
return
}
} else { } else {
reader = bytes.NewReader(b.Bytes()) reader = bytes.NewReader(b.Bytes())
} }
@@ -354,7 +363,7 @@ func (s *Server) postHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
metadata := MetadataForRequest(contentType, s.randomTokenLength, r) metadata := metadataForRequest(contentType, s.randomTokenLength, r)
buffer := &bytes.Buffer{} buffer := &bytes.Buffer{}
if err := json.NewEncoder(buffer).Encode(metadata); err != nil { if err := json.NewEncoder(buffer).Encode(metadata); err != nil {
@@ -403,7 +412,7 @@ func (s *Server) cleanTmpFile(f *os.File) {
} }
} }
type Metadata struct { type metadata struct {
// ContentType is the original uploading content type // ContentType is the original uploading content type
ContentType string ContentType string
// Secret as knowledge to delete file // Secret as knowledge to delete file
@@ -418,13 +427,13 @@ type Metadata struct {
DeletionToken string DeletionToken string
} }
func MetadataForRequest(contentType string, randomTokenLength int, r *http.Request) Metadata { func metadataForRequest(contentType string, randomTokenLength int, r *http.Request) metadata {
metadata := Metadata{ metadata := metadata{
ContentType: strings.ToLower(contentType), ContentType: strings.ToLower(contentType),
MaxDate: time.Time{}, MaxDate: time.Time{},
Downloads: 0, Downloads: 0,
MaxDownloads: -1, MaxDownloads: -1,
DeletionToken: Token(randomTokenLength) + Token(randomTokenLength), DeletionToken: token(randomTokenLength) + token(randomTokenLength),
} }
if v := r.Header.Get("Max-Downloads"); v == "" { if v := r.Header.Get("Max-Downloads"); v == "" {
@@ -491,6 +500,11 @@ func (s *Server) putHandler(w http.ResponseWriter, r *http.Request) {
} }
reader, err = os.Open(file.Name()) reader, err = os.Open(file.Name())
if err != nil {
s.logger.Printf("%s", err.Error())
http.Error(w, err.Error(), 500)
return
}
} else { } else {
reader = bytes.NewReader(b.Bytes()) reader = bytes.NewReader(b.Bytes())
} }
@@ -512,15 +526,19 @@ func (s *Server) putHandler(w http.ResponseWriter, r *http.Request) {
contentType := mime.TypeByExtension(filepath.Ext(vars["filename"])) contentType := mime.TypeByExtension(filepath.Ext(vars["filename"]))
token := Token(s.randomTokenLength) token := token(s.randomTokenLength)
metadata := MetadataForRequest(contentType, s.randomTokenLength, r) metadata := metadataForRequest(contentType, s.randomTokenLength, r)
buffer := &bytes.Buffer{} buffer := &bytes.Buffer{}
if err := json.NewEncoder(buffer).Encode(metadata); err != nil { if err := json.NewEncoder(buffer).Encode(metadata); err != nil {
s.logger.Printf("%s", err.Error()) s.logger.Printf("%s", err.Error())
http.Error(w, errors.New("Could not encode metadata").Error(), 500) http.Error(w, errors.New("Could not encode metadata").Error(), 500)
return return
} else if !metadata.MaxDate.IsZero() && time.Now().After(metadata.MaxDate) {
s.logger.Print("Invalid MaxDate")
http.Error(w, errors.New("Invalid MaxDate, make sure Max-Days is smaller than 290 years").Error(), 400)
return
} else if err := s.storage.Put(token, fmt.Sprintf("%s.metadata", filename), buffer, "text/json", uint64(buffer.Len())); err != nil { } else if err := s.storage.Put(token, fmt.Sprintf("%s.metadata", filename), buffer, "text/json", uint64(buffer.Len())); err != nil {
s.logger.Printf("%s", err.Error()) s.logger.Printf("%s", err.Error())
http.Error(w, errors.New("Could not save metadata").Error(), 500) http.Error(w, errors.New("Could not save metadata").Error(), 500)
@@ -614,32 +632,31 @@ func getURL(r *http.Request, proxyPort string) *url.URL {
u.Scheme = "http" u.Scheme = "http"
} }
if u.Host == "" { host, port, err := net.SplitHostPort(r.Host)
host, port, err := net.SplitHostPort(r.Host) if err != nil {
if err != nil { host = r.Host
host = r.Host port = ""
port = "" }
} if len(proxyPort) != 0 {
if len(proxyPort) != 0 { port = proxyPort
port = proxyPort }
}
if len(port) == 0 { if len(port) == 0 {
u.Host = host
} else {
if port == "80" && u.Scheme == "http" {
u.Host = host
} else if port == "443" && u.Scheme == "https" {
u.Host = host u.Host = host
} else { } else {
if port == "80" && u.Scheme == "http" { u.Host = net.JoinHostPort(host, port)
u.Host = host
} else if port == "443" && u.Scheme == "https" {
u.Host = host
} else {
u.Host = net.JoinHostPort(host, port)
}
} }
} }
return u return u
} }
func (metadata Metadata) remainingLimitHeaderValues() (remainingDownloads, remainingDays string) { func (metadata metadata) remainingLimitHeaderValues() (remainingDownloads, remainingDays string) {
if metadata.MaxDate.IsZero() { if metadata.MaxDate.IsZero() {
remainingDays = "n/a" remainingDays = "n/a"
} else { } else {
@@ -656,7 +673,7 @@ func (metadata Metadata) remainingLimitHeaderValues() (remainingDownloads, remai
return remainingDownloads, remainingDays return remainingDownloads, remainingDays
} }
func (s *Server) Lock(token, filename string) { func (s *Server) lock(token, filename string) {
key := path.Join(token, filename) key := path.Join(token, filename)
lock, _ := s.locks.LoadOrStore(key, &sync.Mutex{}) lock, _ := s.locks.LoadOrStore(key, &sync.Mutex{})
@@ -666,7 +683,7 @@ func (s *Server) Lock(token, filename string) {
return return
} }
func (s *Server) Unlock(token, filename string) { func (s *Server) unlock(token, filename string) {
key := path.Join(token, filename) key := path.Join(token, filename)
lock, _ := s.locks.LoadOrStore(key, &sync.Mutex{}) lock, _ := s.locks.LoadOrStore(key, &sync.Mutex{})
@@ -674,11 +691,11 @@ func (s *Server) Unlock(token, filename string) {
lock.(*sync.Mutex).Unlock() lock.(*sync.Mutex).Unlock()
} }
func (s *Server) CheckMetadata(token, filename string, increaseDownload bool) (Metadata, error) { func (s *Server) checkMetadata(token, filename string, increaseDownload bool) (metadata, error) {
s.Lock(token, filename) s.lock(token, filename)
defer s.Unlock(token, filename) defer s.unlock(token, filename)
var metadata Metadata var metadata metadata
r, _, err := s.storage.Get(token, fmt.Sprintf("%s.metadata", filename)) r, _, err := s.storage.Get(token, fmt.Sprintf("%s.metadata", filename))
if err != nil { if err != nil {
@@ -690,9 +707,9 @@ func (s *Server) CheckMetadata(token, filename string, increaseDownload bool) (M
if err := json.NewDecoder(r).Decode(&metadata); err != nil { if err := json.NewDecoder(r).Decode(&metadata); err != nil {
return metadata, err return metadata, err
} else if metadata.MaxDownloads != -1 && metadata.Downloads >= metadata.MaxDownloads { } else if metadata.MaxDownloads != -1 && metadata.Downloads >= metadata.MaxDownloads {
return metadata, errors.New("MaxDownloads expired.") return metadata, errors.New("maxDownloads expired")
} else if !metadata.MaxDate.IsZero() && time.Now().After(metadata.MaxDate) { } else if !metadata.MaxDate.IsZero() && time.Now().After(metadata.MaxDate) {
return metadata, errors.New("MaxDate expired.") return metadata, errors.New("maxDate expired")
} else if metadata.MaxDownloads != -1 && increaseDownload { } else if metadata.MaxDownloads != -1 && increaseDownload {
// todo(nl5887): mutex? // todo(nl5887): mutex?
@@ -710,15 +727,15 @@ func (s *Server) CheckMetadata(token, filename string, increaseDownload bool) (M
return metadata, nil return metadata, nil
} }
func (s *Server) CheckDeletionToken(deletionToken, token, filename string) error { func (s *Server) checkDeletionToken(deletionToken, token, filename string) error {
s.Lock(token, filename) s.lock(token, filename)
defer s.Unlock(token, filename) defer s.unlock(token, filename)
var metadata Metadata var metadata metadata
r, _, err := s.storage.Get(token, fmt.Sprintf("%s.metadata", filename)) r, _, err := s.storage.Get(token, fmt.Sprintf("%s.metadata", filename))
if s.storage.IsNotExist(err) { if s.storage.IsNotExist(err) {
return errors.New("Metadata doesn't exist") return errors.New("metadata doesn't exist")
} else if err != nil { } else if err != nil {
return err return err
} }
@@ -728,7 +745,7 @@ func (s *Server) CheckDeletionToken(deletionToken, token, filename string) error
if err := json.NewDecoder(r).Decode(&metadata); err != nil { if err := json.NewDecoder(r).Decode(&metadata); err != nil {
return err return err
} else if metadata.DeletionToken != deletionToken { } else if metadata.DeletionToken != deletionToken {
return errors.New("Deletion token doesn't match.") return errors.New("deletion token doesn't match")
} }
return nil return nil
@@ -754,7 +771,7 @@ func (s *Server) deleteHandler(w http.ResponseWriter, r *http.Request) {
filename := vars["filename"] filename := vars["filename"]
deletionToken := vars["deletionToken"] deletionToken := vars["deletionToken"]
if err := s.CheckDeletionToken(deletionToken, token, filename); err != nil { if err := s.checkDeletionToken(deletionToken, token, filename); err != nil {
s.logger.Printf("Error metadata: %s", err.Error()) s.logger.Printf("Error metadata: %s", err.Error())
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return return
@@ -790,7 +807,7 @@ func (s *Server) zipHandler(w http.ResponseWriter, r *http.Request) {
token := strings.Split(key, "/")[0] token := strings.Split(key, "/")[0]
filename := sanitize(strings.Split(key, "/")[1]) filename := sanitize(strings.Split(key, "/")[1])
if _, err := s.CheckMetadata(token, filename, true); err != nil { if _, err := s.checkMetadata(token, filename, true); err != nil {
s.logger.Printf("Error metadata: %s", err.Error()) s.logger.Printf("Error metadata: %s", err.Error())
continue continue
} }
@@ -801,11 +818,11 @@ func (s *Server) zipHandler(w http.ResponseWriter, r *http.Request) {
if s.storage.IsNotExist(err) { if s.storage.IsNotExist(err) {
http.Error(w, "File not found", 404) http.Error(w, "File not found", 404)
return return
} else {
s.logger.Printf("%s", err.Error())
http.Error(w, "Could not retrieve file.", 500)
return
} }
s.logger.Printf("%s", err.Error())
http.Error(w, "Could not retrieve file.", 500)
return
} }
defer reader.Close() defer reader.Close()
@@ -862,7 +879,7 @@ func (s *Server) tarGzHandler(w http.ResponseWriter, r *http.Request) {
token := strings.Split(key, "/")[0] token := strings.Split(key, "/")[0]
filename := sanitize(strings.Split(key, "/")[1]) filename := sanitize(strings.Split(key, "/")[1])
if _, err := s.CheckMetadata(token, filename, true); err != nil { if _, err := s.checkMetadata(token, filename, true); err != nil {
s.logger.Printf("Error metadata: %s", err.Error()) s.logger.Printf("Error metadata: %s", err.Error())
continue continue
} }
@@ -872,11 +889,11 @@ func (s *Server) tarGzHandler(w http.ResponseWriter, r *http.Request) {
if s.storage.IsNotExist(err) { if s.storage.IsNotExist(err) {
http.Error(w, "File not found", 404) http.Error(w, "File not found", 404)
return return
} else {
s.logger.Printf("%s", err.Error())
http.Error(w, "Could not retrieve file.", 500)
return
} }
s.logger.Printf("%s", err.Error())
http.Error(w, "Could not retrieve file.", 500)
return
} }
defer reader.Close() defer reader.Close()
@@ -921,7 +938,7 @@ func (s *Server) tarHandler(w http.ResponseWriter, r *http.Request) {
token := strings.Split(key, "/")[0] token := strings.Split(key, "/")[0]
filename := strings.Split(key, "/")[1] filename := strings.Split(key, "/")[1]
if _, err := s.CheckMetadata(token, filename, true); err != nil { if _, err := s.checkMetadata(token, filename, true); err != nil {
s.logger.Printf("Error metadata: %s", err.Error()) s.logger.Printf("Error metadata: %s", err.Error())
continue continue
} }
@@ -931,11 +948,11 @@ func (s *Server) tarHandler(w http.ResponseWriter, r *http.Request) {
if s.storage.IsNotExist(err) { if s.storage.IsNotExist(err) {
http.Error(w, "File not found", 404) http.Error(w, "File not found", 404)
return return
} else {
s.logger.Printf("%s", err.Error())
http.Error(w, "Could not retrieve file.", 500)
return
} }
s.logger.Printf("%s", err.Error())
http.Error(w, "Could not retrieve file.", 500)
return
} }
defer reader.Close() defer reader.Close()
@@ -966,7 +983,7 @@ func (s *Server) headHandler(w http.ResponseWriter, r *http.Request) {
token := vars["token"] token := vars["token"]
filename := vars["filename"] filename := vars["filename"]
metadata, err := s.CheckMetadata(token, filename, false) metadata, err := s.checkMetadata(token, filename, false)
if err != nil { if err != nil {
s.logger.Printf("Error metadata: %s", err.Error()) s.logger.Printf("Error metadata: %s", err.Error())
@@ -1001,7 +1018,7 @@ func (s *Server) getHandler(w http.ResponseWriter, r *http.Request) {
token := vars["token"] token := vars["token"]
filename := vars["filename"] filename := vars["filename"]
metadata, err := s.CheckMetadata(token, filename, true) metadata, err := s.checkMetadata(token, filename, true)
if err != nil { if err != nil {
s.logger.Printf("Error metadata: %s", err.Error()) s.logger.Printf("Error metadata: %s", err.Error())
@@ -1073,19 +1090,32 @@ func (s *Server) getHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
// RedirectHandler handles redirect
func (s *Server) RedirectHandler(h http.Handler) http.HandlerFunc { func (s *Server) RedirectHandler(h http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
if !s.forceHTTPs { if !s.forceHTTPS {
// we don't want to enforce https // we don't want to enforce https
} else if r.URL.Path == "/health.html" { } else if r.URL.Path == "/health.html" {
// health check url won't redirect // health check url won't redirect
} else if strings.HasSuffix(ipAddrFromRemoteAddr(r.Host), ".onion") { } else if strings.HasSuffix(ipAddrFromRemoteAddr(r.Host), ".onion") {
// .onion addresses cannot get a valid certificate, so don't redirect // .onion addresses cannot get a valid certificate, so don't redirect
} else if r.Header.Get("X-Forwarded-Proto") == "https" { } else if r.Header.Get("X-Forwarded-Proto") == "https" {
} else if r.URL.Scheme == "https" { } else if r.TLS != nil {
} else { } else {
u := getURL(r, s.proxyPort) u := getURL(r, s.proxyPort)
u.Scheme = "https" u.Scheme = "https"
if len(s.proxyPort) == 0 && len(s.TLSListenerString) > 0 {
_, port, err := net.SplitHostPort(s.TLSListenerString)
if err != nil || port == "443" {
port = ""
}
if len(port) > 0 {
u.Host = net.JoinHostPort(u.Hostname(), port)
} else {
u.Host = u.Hostname()
}
}
http.Redirect(w, r, u.String(), http.StatusPermanentRedirect) http.Redirect(w, r, u.String(), http.StatusPermanentRedirect)
return return
@@ -1095,17 +1125,17 @@ func (s *Server) RedirectHandler(h http.Handler) http.HandlerFunc {
} }
} }
// Create a log handler for every request it receives. // LoveHandler Create a log handler for every request it receives.
func LoveHandler(h http.Handler) http.HandlerFunc { func LoveHandler(h http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("x-made-with", "<3 by DutchCoders") w.Header().Set("x-made-with", "<3 by DutchCoders")
w.Header().Set("x-served-by", "Proudly served by DutchCoders") w.Header().Set("x-served-by", "Proudly served by DutchCoders")
w.Header().Set("Server", "Transfer.sh HTTP Server 1.0") w.Header().Set("server", "Transfer.sh HTTP Server")
h.ServeHTTP(w, r) h.ServeHTTP(w, r)
} }
} }
func IPFilterHandler(h http.Handler, ipFilterOptions *IPFilterOptions) http.HandlerFunc { func ipFilterHandler(h http.Handler, ipFilterOptions *IPFilterOptions) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
if ipFilterOptions == nil { if ipFilterOptions == nil {
h.ServeHTTP(w, r) h.ServeHTTP(w, r)
@@ -1116,7 +1146,7 @@ func IPFilterHandler(h http.Handler, ipFilterOptions *IPFilterOptions) http.Hand
} }
} }
func (s *Server) BasicAuthHandler(h http.Handler) http.HandlerFunc { func (s *Server) basicAuthHandler(h http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
if s.AuthUser == "" || s.AuthPass == "" { if s.AuthUser == "" || s.AuthPass == "" {
h.ServeHTTP(w, r) h.ServeHTTP(w, r)

View File

@@ -13,16 +13,16 @@ import (
func Test(t *testing.T) { TestingT(t) } func Test(t *testing.T) { TestingT(t) }
var ( var (
_ = Suite(&SuiteRedirectWithForceHTTPs{}) _ = Suite(&suiteRedirectWithForceHTTPS{})
_ = Suite(&SuiteRedirectWithoutForceHTTPs{}) _ = Suite(&suiteRedirectWithoutForceHTTPS{})
) )
type SuiteRedirectWithForceHTTPs struct { type suiteRedirectWithForceHTTPS struct {
handler http.HandlerFunc handler http.HandlerFunc
} }
func (s *SuiteRedirectWithForceHTTPs) SetUpTest(c *C) { func (s *suiteRedirectWithForceHTTPS) SetUpTest(c *C) {
srvr, err := New(ForceHTTPs()) srvr, err := New(ForceHTTPS())
c.Assert(err, IsNil) c.Assert(err, IsNil)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@@ -32,7 +32,7 @@ func (s *SuiteRedirectWithForceHTTPs) SetUpTest(c *C) {
s.handler = srvr.RedirectHandler(handler) s.handler = srvr.RedirectHandler(handler)
} }
func (s *SuiteRedirectWithForceHTTPs) TestHTTPs(c *C) { func (s *suiteRedirectWithForceHTTPS) TestHTTPs(c *C) {
req := httptest.NewRequest("GET", "https://test/test", nil) req := httptest.NewRequest("GET", "https://test/test", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
@@ -42,7 +42,7 @@ func (s *SuiteRedirectWithForceHTTPs) TestHTTPs(c *C) {
c.Assert(resp.StatusCode, Equals, http.StatusOK) c.Assert(resp.StatusCode, Equals, http.StatusOK)
} }
func (s *SuiteRedirectWithForceHTTPs) TestOnion(c *C) { func (s *suiteRedirectWithForceHTTPS) TestOnion(c *C) {
req := httptest.NewRequest("GET", "http://test.onion/test", nil) req := httptest.NewRequest("GET", "http://test.onion/test", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
@@ -52,7 +52,7 @@ func (s *SuiteRedirectWithForceHTTPs) TestOnion(c *C) {
c.Assert(resp.StatusCode, Equals, http.StatusOK) c.Assert(resp.StatusCode, Equals, http.StatusOK)
} }
func (s *SuiteRedirectWithForceHTTPs) TestXForwardedFor(c *C) { func (s *suiteRedirectWithForceHTTPS) TestXForwardedFor(c *C) {
req := httptest.NewRequest("GET", "http://127.0.0.1/test", nil) req := httptest.NewRequest("GET", "http://127.0.0.1/test", nil)
req.Header.Set("X-Forwarded-Proto", "https") req.Header.Set("X-Forwarded-Proto", "https")
@@ -63,7 +63,7 @@ func (s *SuiteRedirectWithForceHTTPs) TestXForwardedFor(c *C) {
c.Assert(resp.StatusCode, Equals, http.StatusOK) c.Assert(resp.StatusCode, Equals, http.StatusOK)
} }
func (s *SuiteRedirectWithForceHTTPs) TestHTTP(c *C) { func (s *suiteRedirectWithForceHTTPS) TestHTTP(c *C) {
req := httptest.NewRequest("GET", "http://127.0.0.1/test", nil) req := httptest.NewRequest("GET", "http://127.0.0.1/test", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
@@ -74,11 +74,11 @@ func (s *SuiteRedirectWithForceHTTPs) TestHTTP(c *C) {
c.Assert(resp.Header.Get("Location"), Equals, "https://127.0.0.1/test") c.Assert(resp.Header.Get("Location"), Equals, "https://127.0.0.1/test")
} }
type SuiteRedirectWithoutForceHTTPs struct { type suiteRedirectWithoutForceHTTPS struct {
handler http.HandlerFunc handler http.HandlerFunc
} }
func (s *SuiteRedirectWithoutForceHTTPs) SetUpTest(c *C) { func (s *suiteRedirectWithoutForceHTTPS) SetUpTest(c *C) {
srvr, err := New() srvr, err := New()
c.Assert(err, IsNil) c.Assert(err, IsNil)
@@ -89,7 +89,7 @@ func (s *SuiteRedirectWithoutForceHTTPs) SetUpTest(c *C) {
s.handler = srvr.RedirectHandler(handler) s.handler = srvr.RedirectHandler(handler)
} }
func (s *SuiteRedirectWithoutForceHTTPs) TestHTTP(c *C) { func (s *suiteRedirectWithoutForceHTTPS) TestHTTP(c *C) {
req := httptest.NewRequest("GET", "http://127.0.0.1/test", nil) req := httptest.NewRequest("GET", "http://127.0.0.1/test", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
@@ -99,7 +99,7 @@ func (s *SuiteRedirectWithoutForceHTTPs) TestHTTP(c *C) {
c.Assert(resp.StatusCode, Equals, http.StatusOK) c.Assert(resp.StatusCode, Equals, http.StatusOK)
} }
func (s *SuiteRedirectWithoutForceHTTPs) TestHTTPs(c *C) { func (s *suiteRedirectWithoutForceHTTPS) TestHTTPs(c *C) {
req := httptest.NewRequest("GET", "https://127.0.0.1/test", nil) req := httptest.NewRequest("GET", "https://127.0.0.1/test", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()

View File

@@ -21,7 +21,7 @@ import (
"github.com/tomasen/realip" "github.com/tomasen/realip"
) )
//IPFilterOptions for IPFilter. Allowed takes precendence over Blocked. //IPFilterOptions for ipFilter. Allowed takes precedence over Blocked.
//IPs can be IPv4 or IPv6 and can optionally contain subnet //IPs can be IPv4 or IPv6 and can optionally contain subnet
//masks (/24). Note however, determining if a given IP is //masks (/24). Note however, determining if a given IP is
//included in a subnet requires a linear scan so is less performant //included in a subnet requires a linear scan so is less performant
@@ -43,7 +43,8 @@ type IPFilterOptions struct {
} }
} }
type IPFilter struct { // ipFilter
type ipFilter struct {
opts IPFilterOptions opts IPFilterOptions
//mut protects the below //mut protects the below
//rw since writes are rare //rw since writes are rare
@@ -59,13 +60,12 @@ type subnet struct {
allowed bool allowed bool
} }
//New constructs IPFilter instance. func newIPFilter(opts IPFilterOptions) *ipFilter {
func NewIPFilter(opts IPFilterOptions) *IPFilter {
if opts.Logger == nil { if opts.Logger == nil {
flags := log.LstdFlags flags := log.LstdFlags
opts.Logger = log.New(os.Stdout, "", flags) opts.Logger = log.New(os.Stdout, "", flags)
} }
f := &IPFilter{ f := &ipFilter{
opts: opts, opts: opts,
ips: map[string]bool{}, ips: map[string]bool{},
defaultAllowed: !opts.BlockByDefault, defaultAllowed: !opts.BlockByDefault,
@@ -79,15 +79,15 @@ func NewIPFilter(opts IPFilterOptions) *IPFilter {
return f return f
} }
func (f *IPFilter) AllowIP(ip string) bool { func (f *ipFilter) AllowIP(ip string) bool {
return f.ToggleIP(ip, true) return f.ToggleIP(ip, true)
} }
func (f *IPFilter) BlockIP(ip string) bool { func (f *ipFilter) BlockIP(ip string) bool {
return f.ToggleIP(ip, false) return f.ToggleIP(ip, false)
} }
func (f *IPFilter) ToggleIP(str string, allowed bool) bool { func (f *ipFilter) ToggleIP(str string, allowed bool) bool {
//check if has subnet //check if has subnet
if ip, net, err := net.ParseCIDR(str); err == nil { if ip, net, err := net.ParseCIDR(str); err == nil {
// containing only one ip? // containing only one ip?
@@ -128,19 +128,19 @@ func (f *IPFilter) ToggleIP(str string, allowed bool) bool {
} }
//ToggleDefault alters the default setting //ToggleDefault alters the default setting
func (f *IPFilter) ToggleDefault(allowed bool) { func (f *ipFilter) ToggleDefault(allowed bool) {
f.mut.Lock() f.mut.Lock()
f.defaultAllowed = allowed f.defaultAllowed = allowed
f.mut.Unlock() f.mut.Unlock()
} }
//Allowed returns if a given IP can pass through the filter //Allowed returns if a given IP can pass through the filter
func (f *IPFilter) Allowed(ipstr string) bool { func (f *ipFilter) Allowed(ipstr string) bool {
return f.NetAllowed(net.ParseIP(ipstr)) return f.NetAllowed(net.ParseIP(ipstr))
} }
//NetAllowed returns if a given net.IP can pass through the filter //NetAllowed returns if a given net.IP can pass through the filter
func (f *IPFilter) NetAllowed(ip net.IP) bool { func (f *ipFilter) NetAllowed(ip net.IP) bool {
//invalid ip //invalid ip
if ip == nil { if ip == nil {
return false return false
@@ -173,35 +173,35 @@ func (f *IPFilter) NetAllowed(ip net.IP) bool {
} }
//Blocked returns if a given IP can NOT pass through the filter //Blocked returns if a given IP can NOT pass through the filter
func (f *IPFilter) Blocked(ip string) bool { func (f *ipFilter) Blocked(ip string) bool {
return !f.Allowed(ip) return !f.Allowed(ip)
} }
//NetBlocked returns if a given net.IP can NOT pass through the filter //NetBlocked returns if a given net.IP can NOT pass through the filter
func (f *IPFilter) NetBlocked(ip net.IP) bool { func (f *ipFilter) NetBlocked(ip net.IP) bool {
return !f.NetAllowed(ip) return !f.NetAllowed(ip)
} }
//WrapIPFilter the provided handler with simple IP blocking middleware //WrapIPFilter the provided handler with simple IP blocking middleware
//using this IP filter and its configuration //using this IP filter and its configuration
func (f *IPFilter) Wrap(next http.Handler) http.Handler { func (f *ipFilter) Wrap(next http.Handler) http.Handler {
return &ipFilterMiddleware{IPFilter: f, next: next} return &ipFilterMiddleware{ipFilter: f, next: next}
} }
//WrapIPFilter is equivalent to NewIPFilter(opts) then Wrap(next) //WrapIPFilter is equivalent to newIPFilter(opts) then Wrap(next)
func WrapIPFilter(next http.Handler, opts IPFilterOptions) http.Handler { func WrapIPFilter(next http.Handler, opts IPFilterOptions) http.Handler {
return NewIPFilter(opts).Wrap(next) return newIPFilter(opts).Wrap(next)
} }
type ipFilterMiddleware struct { type ipFilterMiddleware struct {
*IPFilter *ipFilter
next http.Handler next http.Handler
} }
func (m *ipFilterMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (m *ipFilterMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
remoteIP := realip.FromRequest(r) remoteIP := realip.FromRequest(r)
if !m.IPFilter.Allowed(remoteIP) { if !m.ipFilter.Allowed(remoteIP) {
//show simple forbidden text //show simple forbidden text
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden) http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
return return

View File

@@ -48,6 +48,7 @@ import (
"github.com/VojtechVitek/ratelimit/memory" "github.com/VojtechVitek/ratelimit/memory"
"github.com/gorilla/mux" "github.com/gorilla/mux"
// import pprof
_ "net/http/pprof" _ "net/http/pprof"
"crypto/tls" "crypto/tls"
@@ -59,28 +60,30 @@ import (
"path/filepath" "path/filepath"
) )
const SERVER_INFO = "transfer.sh"
// parse request with maximum memory of _24Kilobits // parse request with maximum memory of _24Kilobits
const _24K = (1 << 3) * 24 const _24K = (1 << 3) * 24
// parse request with maximum memory of _5Megabytes // parse request with maximum memory of _5Megabytes
const _5M = (1 << 20) * 5 const _5M = (1 << 20) * 5
// OptionFn is the option function type
type OptionFn func(*Server) type OptionFn func(*Server)
// ClamavHost sets clamav host
func ClamavHost(s string) OptionFn { func ClamavHost(s string) OptionFn {
return func(srvr *Server) { return func(srvr *Server) {
srvr.ClamAVDaemonHost = s srvr.ClamAVDaemonHost = s
} }
} }
// VirustotalKey sets virus total key
func VirustotalKey(s string) OptionFn { func VirustotalKey(s string) OptionFn {
return func(srvr *Server) { return func(srvr *Server) {
srvr.VirusTotalKey = s srvr.VirusTotalKey = s
} }
} }
// Listener set listener
func Listener(s string) OptionFn { func Listener(s string) OptionFn {
return func(srvr *Server) { return func(srvr *Server) {
srvr.ListenerString = s srvr.ListenerString = s
@@ -88,6 +91,7 @@ func Listener(s string) OptionFn {
} }
// CorsDomains sets CORS domains
func CorsDomains(s string) OptionFn { func CorsDomains(s string) OptionFn {
return func(srvr *Server) { return func(srvr *Server) {
srvr.CorsDomains = s srvr.CorsDomains = s
@@ -95,18 +99,28 @@ func CorsDomains(s string) OptionFn {
} }
// EmailContact sets email contact
func EmailContact(emailContact string) OptionFn {
return func(srvr *Server) {
srvr.emailContact = emailContact
}
}
// GoogleAnalytics sets GA key
func GoogleAnalytics(gaKey string) OptionFn { func GoogleAnalytics(gaKey string) OptionFn {
return func(srvr *Server) { return func(srvr *Server) {
srvr.gaKey = gaKey srvr.gaKey = gaKey
} }
} }
// UserVoice sets UV key
func UserVoice(userVoiceKey string) OptionFn { func UserVoice(userVoiceKey string) OptionFn {
return func(srvr *Server) { return func(srvr *Server) {
srvr.userVoiceKey = userVoiceKey srvr.userVoiceKey = userVoiceKey
} }
} }
// TLSListener sets TLS listener and option
func TLSListener(s string, t bool) OptionFn { func TLSListener(s string, t bool) OptionFn {
return func(srvr *Server) { return func(srvr *Server) {
srvr.TLSListenerString = s srvr.TLSListenerString = s
@@ -115,12 +129,14 @@ func TLSListener(s string, t bool) OptionFn {
} }
// ProfileListener sets profile listener
func ProfileListener(s string) OptionFn { func ProfileListener(s string) OptionFn {
return func(srvr *Server) { return func(srvr *Server) {
srvr.ProfileListenerString = s srvr.ProfileListenerString = s
} }
} }
// WebPath sets web path
func WebPath(s string) OptionFn { func WebPath(s string) OptionFn {
return func(srvr *Server) { return func(srvr *Server) {
if s[len(s)-1:] != "/" { if s[len(s)-1:] != "/" {
@@ -131,6 +147,7 @@ func WebPath(s string) OptionFn {
} }
} }
// ProxyPath sets proxy path
func ProxyPath(s string) OptionFn { func ProxyPath(s string) OptionFn {
return func(srvr *Server) { return func(srvr *Server) {
if s[len(s)-1:] != "/" { if s[len(s)-1:] != "/" {
@@ -141,12 +158,14 @@ func ProxyPath(s string) OptionFn {
} }
} }
// ProxyPort sets proxy port
func ProxyPort(s string) OptionFn { func ProxyPort(s string) OptionFn {
return func(srvr *Server) { return func(srvr *Server) {
srvr.proxyPort = s srvr.proxyPort = s
} }
} }
// TempPath sets temp path
func TempPath(s string) OptionFn { func TempPath(s string) OptionFn {
return func(srvr *Server) { return func(srvr *Server) {
if s[len(s)-1:] != "/" { if s[len(s)-1:] != "/" {
@@ -157,6 +176,7 @@ func TempPath(s string) OptionFn {
} }
} }
// LogFile sets log file
func LogFile(logger *log.Logger, s string) OptionFn { func LogFile(logger *log.Logger, s string) OptionFn {
return func(srvr *Server) { return func(srvr *Server) {
f, err := os.OpenFile(s, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) f, err := os.OpenFile(s, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
@@ -169,30 +189,36 @@ func LogFile(logger *log.Logger, s string) OptionFn {
} }
} }
// Logger sets logger
func Logger(logger *log.Logger) OptionFn { func Logger(logger *log.Logger) OptionFn {
return func(srvr *Server) { return func(srvr *Server) {
srvr.logger = logger srvr.logger = logger
} }
} }
// MaxUploadSize sets max upload size
func MaxUploadSize(kbytes int64) OptionFn { func MaxUploadSize(kbytes int64) OptionFn {
return func(srvr *Server) { return func(srvr *Server) {
srvr.maxUploadSize = kbytes * 1024 srvr.maxUploadSize = kbytes * 1024
} }
} }
// RateLimit set rate limit
func RateLimit(requests int) OptionFn { func RateLimit(requests int) OptionFn {
return func(srvr *Server) { return func(srvr *Server) {
srvr.rateLimitRequests = requests srvr.rateLimitRequests = requests
} }
} }
// RandomTokenLength sets random token length
func RandomTokenLength(length int) OptionFn { func RandomTokenLength(length int) OptionFn {
return func(srvr *Server) { return func(srvr *Server) {
srvr.randomTokenLength = length srvr.randomTokenLength = length
} }
} }
// Purge sets purge days and option
func Purge(days, interval int) OptionFn { func Purge(days, interval int) OptionFn {
return func(srvr *Server) { return func(srvr *Server) {
srvr.purgeDays = time.Duration(days) * time.Hour * 24 srvr.purgeDays = time.Duration(days) * time.Hour * 24
@@ -200,24 +226,28 @@ func Purge(days, interval int) OptionFn {
} }
} }
func ForceHTTPs() OptionFn { // ForceHTTPS sets forcing https
func ForceHTTPS() OptionFn {
return func(srvr *Server) { return func(srvr *Server) {
srvr.forceHTTPs = true srvr.forceHTTPS = true
} }
} }
// EnableProfiler sets enable profiler
func EnableProfiler() OptionFn { func EnableProfiler() OptionFn {
return func(srvr *Server) { return func(srvr *Server) {
srvr.profilerEnabled = true srvr.profilerEnabled = true
} }
} }
// UseStorage set storage to use
func UseStorage(s Storage) OptionFn { func UseStorage(s Storage) OptionFn {
return func(srvr *Server) { return func(srvr *Server) {
srvr.storage = s srvr.storage = s
} }
} }
// UseLetsEncrypt set letsencrypt usage
func UseLetsEncrypt(hosts []string) OptionFn { func UseLetsEncrypt(hosts []string) OptionFn {
return func(srvr *Server) { return func(srvr *Server) {
cacheDir := "./cache/" cacheDir := "./cache/"
@@ -246,6 +276,7 @@ func UseLetsEncrypt(hosts []string) OptionFn {
} }
} }
// TLSConfig sets TLS config
func TLSConfig(cert, pk string) OptionFn { func TLSConfig(cert, pk string) OptionFn {
certificate, err := tls.LoadX509KeyPair(cert, pk) certificate, err := tls.LoadX509KeyPair(cert, pk)
return func(srvr *Server) { return func(srvr *Server) {
@@ -257,13 +288,15 @@ func TLSConfig(cert, pk string) OptionFn {
} }
} }
func HttpAuthCredentials(user string, pass string) OptionFn { // HTTPAuthCredentials sets basic http auth credentials
func HTTPAuthCredentials(user string, pass string) OptionFn {
return func(srvr *Server) { return func(srvr *Server) {
srvr.AuthUser = user srvr.AuthUser = user
srvr.AuthPass = pass srvr.AuthPass = pass
} }
} }
// FilterOptions sets ip filtering
func FilterOptions(options IPFilterOptions) OptionFn { func FilterOptions(options IPFilterOptions) OptionFn {
for i, allowedIP := range options.AllowedIPs { for i, allowedIP := range options.AllowedIPs {
options.AllowedIPs[i] = strings.TrimSpace(allowedIP) options.AllowedIPs[i] = strings.TrimSpace(allowedIP)
@@ -278,6 +311,7 @@ func FilterOptions(options IPFilterOptions) OptionFn {
} }
} }
// Server is the main application
type Server struct { type Server struct {
AuthUser string AuthUser string
AuthPass string AuthPass string
@@ -298,7 +332,7 @@ type Server struct {
storage Storage storage Storage
forceHTTPs bool forceHTTPS bool
randomTokenLength int randomTokenLength int
@@ -312,6 +346,7 @@ type Server struct {
webPath string webPath string
proxyPath string proxyPath string
proxyPort string proxyPort string
emailContact string
gaKey string gaKey string
userVoiceKey string userVoiceKey string
@@ -327,6 +362,7 @@ type Server struct {
LetsEncryptCache string LetsEncryptCache string
} }
// New is the factory fot Server
func New(options ...OptionFn) (*Server, error) { func New(options ...OptionFn) (*Server, error) {
s := &Server{ s := &Server{
locks: sync.Map{}, locks: sync.Map{},
@@ -347,6 +383,7 @@ func init() {
rand.Seed(int64(binary.LittleEndian.Uint64(seedBytes[:]))) rand.Seed(int64(binary.LittleEndian.Uint64(seedBytes[:])))
} }
// Run starts Server
func (s *Server) Run() { func (s *Server) Run() {
listening := false listening := false
@@ -402,7 +439,7 @@ func (s *Server) Run() {
r.HandleFunc("/favicon.ico", staticHandler.ServeHTTP).Methods("GET") r.HandleFunc("/favicon.ico", staticHandler.ServeHTTP).Methods("GET")
r.HandleFunc("/robots.txt", staticHandler.ServeHTTP).Methods("GET") r.HandleFunc("/robots.txt", staticHandler.ServeHTTP).Methods("GET")
r.HandleFunc("/{filename:(?:favicon\\.ico|robots\\.txt|health\\.html)}", s.BasicAuthHandler(http.HandlerFunc(s.putHandler))).Methods("PUT") r.HandleFunc("/{filename:(?:favicon\\.ico|robots\\.txt|health\\.html)}", s.basicAuthHandler(http.HandlerFunc(s.putHandler))).Methods("PUT")
r.HandleFunc("/health.html", healthHandler).Methods("GET") r.HandleFunc("/health.html", healthHandler).Methods("GET")
r.HandleFunc("/", s.viewHandler).Methods("GET") r.HandleFunc("/", s.viewHandler).Methods("GET")
@@ -446,10 +483,10 @@ func (s *Server) Run() {
r.HandleFunc("/{filename}/virustotal", s.virusTotalHandler).Methods("PUT") r.HandleFunc("/{filename}/virustotal", s.virusTotalHandler).Methods("PUT")
r.HandleFunc("/{filename}/scan", s.scanHandler).Methods("PUT") r.HandleFunc("/{filename}/scan", s.scanHandler).Methods("PUT")
r.HandleFunc("/put/{filename}", s.BasicAuthHandler(http.HandlerFunc(s.putHandler))).Methods("PUT") r.HandleFunc("/put/{filename}", s.basicAuthHandler(http.HandlerFunc(s.putHandler))).Methods("PUT")
r.HandleFunc("/upload/{filename}", s.BasicAuthHandler(http.HandlerFunc(s.putHandler))).Methods("PUT") r.HandleFunc("/upload/{filename}", s.basicAuthHandler(http.HandlerFunc(s.putHandler))).Methods("PUT")
r.HandleFunc("/{filename}", s.BasicAuthHandler(http.HandlerFunc(s.putHandler))).Methods("PUT") r.HandleFunc("/{filename}", s.basicAuthHandler(http.HandlerFunc(s.putHandler))).Methods("PUT")
r.HandleFunc("/", s.BasicAuthHandler(http.HandlerFunc(s.postHandler))).Methods("POST") r.HandleFunc("/", s.basicAuthHandler(http.HandlerFunc(s.postHandler))).Methods("POST")
// r.HandleFunc("/{page}", viewHandler).Methods("GET") // r.HandleFunc("/{page}", viewHandler).Methods("GET")
r.HandleFunc("/{token}/{filename}/{deletionToken}", s.deleteHandler).Methods("DELETE") r.HandleFunc("/{token}/{filename}/{deletionToken}", s.deleteHandler).Methods("DELETE")
@@ -474,7 +511,7 @@ func (s *Server) Run() {
} }
h := handlers.PanicHandler( h := handlers.PanicHandler(
IPFilterHandler( ipFilterHandler(
handlers.LogHandler( handlers.LogHandler(
LoveHandler( LoveHandler(
s.RedirectHandler(cors(r))), s.RedirectHandler(cors(r))),

View File

@@ -27,31 +27,43 @@ import (
"storj.io/uplink" "storj.io/uplink"
) )
// Storage is the interface for storage operation
type Storage interface { type Storage interface {
// Get retrieves a file from storage
Get(token string, filename string) (reader io.ReadCloser, contentLength uint64, err error) Get(token string, filename string) (reader io.ReadCloser, contentLength uint64, err error)
// Head retrieves content length of a file from storage
Head(token string, filename string) (contentLength uint64, err error) Head(token string, filename string) (contentLength uint64, err error)
// Put saves a file on storage
Put(token string, filename string, reader io.Reader, contentType string, contentLength uint64) error Put(token string, filename string, reader io.Reader, contentType string, contentLength uint64) error
// Delete removes a file from storage
Delete(token string, filename string) error Delete(token string, filename string) error
// IsNotExist indicates if a file doesn't exist on storage
IsNotExist(err error) bool IsNotExist(err error) bool
// Purge cleans up the storage
Purge(days time.Duration) error Purge(days time.Duration) error
// Type returns the storage type
Type() string Type() string
} }
// LocalStorage is a local storage
type LocalStorage struct { type LocalStorage struct {
Storage Storage
basedir string basedir string
logger *log.Logger logger *log.Logger
} }
// NewLocalStorage is the factory for LocalStorage
func NewLocalStorage(basedir string, logger *log.Logger) (*LocalStorage, error) { func NewLocalStorage(basedir string, logger *log.Logger) (*LocalStorage, error) {
return &LocalStorage{basedir: basedir, logger: logger}, nil return &LocalStorage{basedir: basedir, logger: logger}, nil
} }
// Type returns the storage type
func (s *LocalStorage) Type() string { func (s *LocalStorage) Type() string {
return "local" return "local"
} }
// Head retrieves content length of a file from storage
func (s *LocalStorage) Head(token string, filename string) (contentLength uint64, err error) { func (s *LocalStorage) Head(token string, filename string) (contentLength uint64, err error) {
path := filepath.Join(s.basedir, token, filename) path := filepath.Join(s.basedir, token, filename)
@@ -65,6 +77,7 @@ func (s *LocalStorage) Head(token string, filename string) (contentLength uint64
return return
} }
// Get retrieves a file from storage
func (s *LocalStorage) Get(token string, filename string) (reader io.ReadCloser, contentLength uint64, err error) { func (s *LocalStorage) Get(token string, filename string) (reader io.ReadCloser, contentLength uint64, err error) {
path := filepath.Join(s.basedir, token, filename) path := filepath.Join(s.basedir, token, filename)
@@ -83,6 +96,7 @@ func (s *LocalStorage) Get(token string, filename string) (reader io.ReadCloser,
return return
} }
// Delete removes a file from storage
func (s *LocalStorage) Delete(token string, filename string) (err error) { func (s *LocalStorage) Delete(token string, filename string) (err error) {
metadata := filepath.Join(s.basedir, token, fmt.Sprintf("%s.metadata", filename)) metadata := filepath.Join(s.basedir, token, fmt.Sprintf("%s.metadata", filename))
os.Remove(metadata) os.Remove(metadata)
@@ -92,6 +106,7 @@ func (s *LocalStorage) Delete(token string, filename string) (err error) {
return return
} }
// Purge cleans up the storage
func (s *LocalStorage) Purge(days time.Duration) (err error) { func (s *LocalStorage) Purge(days time.Duration) (err error) {
err = filepath.Walk(s.basedir, err = filepath.Walk(s.basedir,
func(path string, info os.FileInfo, err error) error { func(path string, info os.FileInfo, err error) error {
@@ -113,6 +128,7 @@ func (s *LocalStorage) Purge(days time.Duration) (err error) {
return return
} }
// IsNotExist indicates if a file doesn't exist on storage
func (s *LocalStorage) IsNotExist(err error) bool { func (s *LocalStorage) IsNotExist(err error) bool {
if err == nil { if err == nil {
return false return false
@@ -121,6 +137,7 @@ func (s *LocalStorage) IsNotExist(err error) bool {
return os.IsNotExist(err) return os.IsNotExist(err)
} }
// Put saves a file on storage
func (s *LocalStorage) Put(token string, filename string, reader io.Reader, contentType string, contentLength uint64) error { func (s *LocalStorage) Put(token string, filename string, reader io.Reader, contentType string, contentLength uint64) error {
var f io.WriteCloser var f io.WriteCloser
var err error var err error
@@ -144,6 +161,7 @@ func (s *LocalStorage) Put(token string, filename string, reader io.Reader, cont
return nil return nil
} }
// S3Storage is a storage backed by AWS S3
type S3Storage struct { type S3Storage struct {
Storage Storage
bucket string bucket string
@@ -154,6 +172,7 @@ type S3Storage struct {
noMultipart bool noMultipart bool
} }
// NewS3Storage is the factory for S3Storage
func NewS3Storage(accessKey, secretKey, bucketName string, purgeDays int, region, endpoint string, disableMultipart bool, forcePathStyle bool, logger *log.Logger) (*S3Storage, error) { func NewS3Storage(accessKey, secretKey, bucketName string, purgeDays int, region, endpoint string, disableMultipart bool, forcePathStyle bool, logger *log.Logger) (*S3Storage, error) {
sess := getAwsSession(accessKey, secretKey, region, endpoint, forcePathStyle) sess := getAwsSession(accessKey, secretKey, region, endpoint, forcePathStyle)
@@ -167,10 +186,12 @@ func NewS3Storage(accessKey, secretKey, bucketName string, purgeDays int, region
}, nil }, nil
} }
// Type returns the storage type
func (s *S3Storage) Type() string { func (s *S3Storage) Type() string {
return "s3" return "s3"
} }
// Head retrieves content length of a file from storage
func (s *S3Storage) Head(token string, filename string) (contentLength uint64, err error) { func (s *S3Storage) Head(token string, filename string) (contentLength uint64, err error) {
key := fmt.Sprintf("%s/%s", token, filename) key := fmt.Sprintf("%s/%s", token, filename)
@@ -192,11 +213,13 @@ func (s *S3Storage) Head(token string, filename string) (contentLength uint64, e
return return
} }
// Purge cleans up the storage
func (s *S3Storage) Purge(days time.Duration) (err error) { func (s *S3Storage) Purge(days time.Duration) (err error) {
// NOOP expiration is set at upload time // NOOP expiration is set at upload time
return nil return nil
} }
// IsNotExist indicates if a file doesn't exist on storage
func (s *S3Storage) IsNotExist(err error) bool { func (s *S3Storage) IsNotExist(err error) bool {
if err == nil { if err == nil {
return false return false
@@ -212,6 +235,7 @@ func (s *S3Storage) IsNotExist(err error) bool {
return false return false
} }
// Get retrieves a file from storage
func (s *S3Storage) Get(token string, filename string) (reader io.ReadCloser, contentLength uint64, err error) { func (s *S3Storage) Get(token string, filename string) (reader io.ReadCloser, contentLength uint64, err error) {
key := fmt.Sprintf("%s/%s", token, filename) key := fmt.Sprintf("%s/%s", token, filename)
@@ -233,6 +257,7 @@ func (s *S3Storage) Get(token string, filename string) (reader io.ReadCloser, co
return return
} }
// Delete removes a file from storage
func (s *S3Storage) Delete(token string, filename string) (err error) { func (s *S3Storage) Delete(token string, filename string) (err error) {
metadata := fmt.Sprintf("%s/%s.metadata", token, filename) metadata := fmt.Sprintf("%s/%s.metadata", token, filename)
deleteRequest := &s3.DeleteObjectInput{ deleteRequest := &s3.DeleteObjectInput{
@@ -256,6 +281,7 @@ func (s *S3Storage) Delete(token string, filename string) (err error) {
return return
} }
// Put saves a file on storage
func (s *S3Storage) Put(token string, filename string, reader io.Reader, contentType string, contentLength uint64) (err error) { func (s *S3Storage) Put(token string, filename string, reader io.Reader, contentType string, contentLength uint64) (err error) {
key := fmt.Sprintf("%s/%s", token, filename) key := fmt.Sprintf("%s/%s", token, filename)
@@ -288,17 +314,19 @@ func (s *S3Storage) Put(token string, filename string, reader io.Reader, content
return return
} }
// GDrive is a storage backed by GDrive
type GDrive struct { type GDrive struct {
service *drive.Service service *drive.Service
rootId string rootID string
basedir string basedir string
localConfigPath string localConfigPath string
chunkSize int chunkSize int
logger *log.Logger logger *log.Logger
} }
func NewGDriveStorage(clientJsonFilepath string, localConfigPath string, basedir string, chunkSize int, logger *log.Logger) (*GDrive, error) { // NewGDriveStorage is the factory for GDrive
b, err := ioutil.ReadFile(clientJsonFilepath) func NewGDriveStorage(clientJSONFilepath string, localConfigPath string, basedir string, chunkSize int, logger *log.Logger) (*GDrive, error) {
b, err := ioutil.ReadFile(clientJSONFilepath)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -315,7 +343,7 @@ func NewGDriveStorage(clientJsonFilepath string, localConfigPath string, basedir
} }
chunkSize = chunkSize * 1024 * 1024 chunkSize = chunkSize * 1024 * 1024
storage := &GDrive{service: srv, basedir: basedir, rootId: "", localConfigPath: localConfigPath, chunkSize: chunkSize, logger: logger} storage := &GDrive{service: srv, basedir: basedir, rootID: "", localConfigPath: localConfigPath, chunkSize: chunkSize, logger: logger}
err = storage.setupRoot() err = storage.setupRoot()
if err != nil { if err != nil {
return nil, err return nil, err
@@ -324,26 +352,26 @@ func NewGDriveStorage(clientJsonFilepath string, localConfigPath string, basedir
return storage, nil return storage, nil
} }
const GDriveRootConfigFile = "root_id.conf" const gdriveRootConfigFile = "root_id.conf"
const GDriveTokenJsonFile = "token.json" const gdriveTokenJSONFile = "token.json"
const GDriveDirectoryMimeType = "application/vnd.google-apps.folder" const gdriveDirectoryMimeType = "application/vnd.google-apps.folder"
func (s *GDrive) setupRoot() error { func (s *GDrive) setupRoot() error {
rootFileConfig := filepath.Join(s.localConfigPath, GDriveRootConfigFile) rootFileConfig := filepath.Join(s.localConfigPath, gdriveRootConfigFile)
rootId, err := ioutil.ReadFile(rootFileConfig) rootID, err := ioutil.ReadFile(rootFileConfig)
if err != nil && !os.IsNotExist(err) { if err != nil && !os.IsNotExist(err) {
return err return err
} }
if string(rootId) != "" { if string(rootID) != "" {
s.rootId = string(rootId) s.rootID = string(rootID)
return nil return nil
} }
dir := &drive.File{ dir := &drive.File{
Name: s.basedir, Name: s.basedir,
MimeType: GDriveDirectoryMimeType, MimeType: gdriveDirectoryMimeType,
} }
di, err := s.service.Files.Create(dir).Fields("id").Do() di, err := s.service.Files.Create(dir).Fields("id").Do()
@@ -351,8 +379,8 @@ func (s *GDrive) setupRoot() error {
return err return err
} }
s.rootId = di.Id s.rootID = di.Id
err = ioutil.WriteFile(rootFileConfig, []byte(s.rootId), os.FileMode(0600)) err = ioutil.WriteFile(rootFileConfig, []byte(s.rootID), os.FileMode(0600))
if err != nil { if err != nil {
return err return err
} }
@@ -368,13 +396,13 @@ func (s *GDrive) list(nextPageToken string, q string) (*drive.FileList, error) {
return s.service.Files.List().Fields("nextPageToken, files(id, name, mimeType)").Q(q).PageToken(nextPageToken).Do() return s.service.Files.List().Fields("nextPageToken, files(id, name, mimeType)").Q(q).PageToken(nextPageToken).Do()
} }
func (s *GDrive) findId(filename string, token string) (string, error) { func (s *GDrive) findID(filename string, token string) (string, error) {
filename = strings.Replace(filename, `'`, `\'`, -1) filename = strings.Replace(filename, `'`, `\'`, -1)
filename = strings.Replace(filename, `"`, `\"`, -1) filename = strings.Replace(filename, `"`, `\"`, -1)
fileId, tokenId, nextPageToken := "", "", "" fileID, tokenID, nextPageToken := "", "", ""
q := fmt.Sprintf("'%s' in parents and name='%s' and mimeType='%s' and trashed=false", s.rootId, token, GDriveDirectoryMimeType) q := fmt.Sprintf("'%s' in parents and name='%s' and mimeType='%s' and trashed=false", s.rootID, token, gdriveDirectoryMimeType)
l, err := s.list(nextPageToken, q) l, err := s.list(nextPageToken, q)
if err != nil { if err != nil {
return "", err return "", err
@@ -382,7 +410,7 @@ func (s *GDrive) findId(filename string, token string) (string, error) {
for 0 < len(l.Files) { for 0 < len(l.Files) {
for _, fi := range l.Files { for _, fi := range l.Files {
tokenId = fi.Id tokenID = fi.Id
break break
} }
@@ -391,15 +419,18 @@ func (s *GDrive) findId(filename string, token string) (string, error) {
} }
l, err = s.list(l.NextPageToken, q) l, err = s.list(l.NextPageToken, q)
if err != nil {
return "", err
}
} }
if filename == "" { if filename == "" {
return tokenId, nil return tokenID, nil
} else if tokenId == "" { } else if tokenID == "" {
return "", fmt.Errorf("Cannot find file %s/%s", token, filename) return "", fmt.Errorf("Cannot find file %s/%s", token, filename)
} }
q = fmt.Sprintf("'%s' in parents and name='%s' and mimeType!='%s' and trashed=false", tokenId, filename, GDriveDirectoryMimeType) q = fmt.Sprintf("'%s' in parents and name='%s' and mimeType!='%s' and trashed=false", tokenID, filename, gdriveDirectoryMimeType)
l, err = s.list(nextPageToken, q) l, err = s.list(nextPageToken, q)
if err != nil { if err != nil {
return "", err return "", err
@@ -408,7 +439,7 @@ func (s *GDrive) findId(filename string, token string) (string, error) {
for 0 < len(l.Files) { for 0 < len(l.Files) {
for _, fi := range l.Files { for _, fi := range l.Files {
fileId = fi.Id fileID = fi.Id
break break
} }
@@ -417,28 +448,33 @@ func (s *GDrive) findId(filename string, token string) (string, error) {
} }
l, err = s.list(l.NextPageToken, q) l, err = s.list(l.NextPageToken, q)
if err != nil {
return "", err
}
} }
if fileId == "" { if fileID == "" {
return "", fmt.Errorf("Cannot find file %s/%s", token, filename) return "", fmt.Errorf("Cannot find file %s/%s", token, filename)
} }
return fileId, nil return fileID, nil
} }
// Type returns the storage type
func (s *GDrive) Type() string { func (s *GDrive) Type() string {
return "gdrive" return "gdrive"
} }
// Head retrieves content length of a file from storage
func (s *GDrive) Head(token string, filename string) (contentLength uint64, err error) { func (s *GDrive) Head(token string, filename string) (contentLength uint64, err error) {
var fileId string var fileID string
fileId, err = s.findId(filename, token) fileID, err = s.findID(filename, token)
if err != nil { if err != nil {
return return
} }
var fi *drive.File var fi *drive.File
if fi, err = s.service.Files.Get(fileId).Fields("size").Do(); err != nil { if fi, err = s.service.Files.Get(fileID).Fields("size").Do(); err != nil {
return return
} }
@@ -447,15 +483,16 @@ func (s *GDrive) Head(token string, filename string) (contentLength uint64, err
return return
} }
// Get retrieves a file from storage
func (s *GDrive) Get(token string, filename string) (reader io.ReadCloser, contentLength uint64, err error) { func (s *GDrive) Get(token string, filename string) (reader io.ReadCloser, contentLength uint64, err error) {
var fileId string var fileID string
fileId, err = s.findId(filename, token) fileID, err = s.findID(filename, token)
if err != nil { if err != nil {
return return
} }
var fi *drive.File var fi *drive.File
fi, err = s.service.Files.Get(fileId).Fields("size", "md5Checksum").Do() fi, err = s.service.Files.Get(fileID).Fields("size", "md5Checksum").Do()
if !s.hasChecksum(fi) { if !s.hasChecksum(fi) {
err = fmt.Errorf("Cannot find file %s/%s", token, filename) err = fmt.Errorf("Cannot find file %s/%s", token, filename)
return return
@@ -465,7 +502,7 @@ func (s *GDrive) Get(token string, filename string) (reader io.ReadCloser, conte
ctx := context.Background() ctx := context.Background()
var res *http.Response var res *http.Response
res, err = s.service.Files.Get(fileId).Context(ctx).Download() res, err = s.service.Files.Get(fileID).Context(ctx).Download()
if err != nil { if err != nil {
return return
} }
@@ -475,25 +512,27 @@ func (s *GDrive) Get(token string, filename string) (reader io.ReadCloser, conte
return return
} }
// Delete removes a file from storage
func (s *GDrive) Delete(token string, filename string) (err error) { func (s *GDrive) Delete(token string, filename string) (err error) {
metadata, _ := s.findId(fmt.Sprintf("%s.metadata", filename), token) metadata, _ := s.findID(fmt.Sprintf("%s.metadata", filename), token)
s.service.Files.Delete(metadata).Do() s.service.Files.Delete(metadata).Do()
var fileId string var fileID string
fileId, err = s.findId(filename, token) fileID, err = s.findID(filename, token)
if err != nil { if err != nil {
return return
} }
err = s.service.Files.Delete(fileId).Do() err = s.service.Files.Delete(fileID).Do()
return return
} }
// Purge cleans up the storage
func (s *GDrive) Purge(days time.Duration) (err error) { func (s *GDrive) Purge(days time.Duration) (err error) {
nextPageToken := "" nextPageToken := ""
expirationDate := time.Now().Add(-1 * days).Format(time.RFC3339) expirationDate := time.Now().Add(-1 * days).Format(time.RFC3339)
q := fmt.Sprintf("'%s' in parents and modifiedTime < '%s' and mimeType!='%s' and trashed=false", s.rootId, expirationDate, GDriveDirectoryMimeType) q := fmt.Sprintf("'%s' in parents and modifiedTime < '%s' and mimeType!='%s' and trashed=false", s.rootID, expirationDate, gdriveDirectoryMimeType)
l, err := s.list(nextPageToken, q) l, err := s.list(nextPageToken, q)
if err != nil { if err != nil {
return err return err
@@ -512,32 +551,39 @@ func (s *GDrive) Purge(days time.Duration) (err error) {
} }
l, err = s.list(l.NextPageToken, q) l, err = s.list(l.NextPageToken, q)
if err != nil {
return
}
} }
return return
} }
// IsNotExist indicates if a file doesn't exist on storage
func (s *GDrive) IsNotExist(err error) bool { func (s *GDrive) IsNotExist(err error) bool {
if err != nil { if err == nil {
if e, ok := err.(*googleapi.Error); ok { return false
return e.Code == http.StatusNotFound }
}
if e, ok := err.(*googleapi.Error); ok {
return e.Code == http.StatusNotFound
} }
return false return false
} }
// Put saves a file on storage
func (s *GDrive) Put(token string, filename string, reader io.Reader, contentType string, contentLength uint64) error { func (s *GDrive) Put(token string, filename string, reader io.Reader, contentType string, contentLength uint64) error {
dirId, err := s.findId("", token) dirID, err := s.findID("", token)
if err != nil { if err != nil {
return err return err
} }
if dirId == "" { if dirID == "" {
dir := &drive.File{ dir := &drive.File{
Name: token, Name: token,
Parents: []string{s.rootId}, Parents: []string{s.rootID},
MimeType: GDriveDirectoryMimeType, MimeType: gdriveDirectoryMimeType,
} }
di, err := s.service.Files.Create(dir).Fields("id").Do() di, err := s.service.Files.Create(dir).Fields("id").Do()
@@ -545,13 +591,13 @@ func (s *GDrive) Put(token string, filename string, reader io.Reader, contentTyp
return err return err
} }
dirId = di.Id dirID = di.Id
} }
// Instantiate empty drive file // Instantiate empty drive file
dst := &drive.File{ dst := &drive.File{
Name: filename, Name: filename,
Parents: []string{dirId}, Parents: []string{dirID},
MimeType: contentType, MimeType: contentType,
} }
@@ -567,7 +613,7 @@ func (s *GDrive) Put(token string, filename string, reader io.Reader, contentTyp
// Retrieve a token, saves the token, then returns the generated client. // Retrieve a token, saves the token, then returns the generated client.
func getGDriveClient(config *oauth2.Config, localConfigPath string, logger *log.Logger) *http.Client { func getGDriveClient(config *oauth2.Config, localConfigPath string, logger *log.Logger) *http.Client {
tokenFile := filepath.Join(localConfigPath, GDriveTokenJsonFile) tokenFile := filepath.Join(localConfigPath, gdriveTokenJSONFile)
tok, err := gDriveTokenFromFile(tokenFile) tok, err := gDriveTokenFromFile(tokenFile)
if err != nil { if err != nil {
tok = getGDriveTokenFromWeb(config, logger) tok = getGDriveTokenFromWeb(config, logger)
@@ -619,6 +665,7 @@ func saveGDriveToken(path string, token *oauth2.Token, logger *log.Logger) {
json.NewEncoder(f).Encode(token) json.NewEncoder(f).Encode(token)
} }
// StorjStorage is a storage backed by Storj
type StorjStorage struct { type StorjStorage struct {
Storage Storage
project *uplink.Project project *uplink.Project
@@ -627,6 +674,7 @@ type StorjStorage struct {
logger *log.Logger logger *log.Logger
} }
// NewStorjStorage is the factory for StorjStorage
func NewStorjStorage(access, bucket string, purgeDays int, logger *log.Logger) (*StorjStorage, error) { func NewStorjStorage(access, bucket string, purgeDays int, logger *log.Logger) (*StorjStorage, error) {
var instance StorjStorage var instance StorjStorage
var err error var err error
@@ -657,10 +705,12 @@ func NewStorjStorage(access, bucket string, purgeDays int, logger *log.Logger) (
return &instance, nil return &instance, nil
} }
// Type returns the storage type
func (s *StorjStorage) Type() string { func (s *StorjStorage) Type() string {
return "storj" return "storj"
} }
// Head retrieves content length of a file from storage
func (s *StorjStorage) Head(token string, filename string) (contentLength uint64, err error) { func (s *StorjStorage) Head(token string, filename string) (contentLength uint64, err error) {
key := storj.JoinPaths(token, filename) key := storj.JoinPaths(token, filename)
@@ -676,6 +726,7 @@ func (s *StorjStorage) Head(token string, filename string) (contentLength uint64
return return
} }
// Get retrieves a file from storage
func (s *StorjStorage) Get(token string, filename string) (reader io.ReadCloser, contentLength uint64, err error) { func (s *StorjStorage) Get(token string, filename string) (reader io.ReadCloser, contentLength uint64, err error) {
key := storj.JoinPaths(token, filename) key := storj.JoinPaths(token, filename)
@@ -694,6 +745,7 @@ func (s *StorjStorage) Get(token string, filename string) (reader io.ReadCloser,
return return
} }
// Delete removes a file from storage
func (s *StorjStorage) Delete(token string, filename string) (err error) { func (s *StorjStorage) Delete(token string, filename string) (err error) {
key := storj.JoinPaths(token, filename) key := storj.JoinPaths(token, filename)
@@ -706,11 +758,13 @@ func (s *StorjStorage) Delete(token string, filename string) (err error) {
return return
} }
// Purge cleans up the storage
func (s *StorjStorage) Purge(days time.Duration) (err error) { func (s *StorjStorage) Purge(days time.Duration) (err error) {
// NOOP expiration is set at upload time // NOOP expiration is set at upload time
return nil return nil
} }
// Put saves a file on storage
func (s *StorjStorage) Put(token string, filename string, reader io.Reader, contentType string, contentLength uint64) (err error) { func (s *StorjStorage) Put(token string, filename string, reader io.Reader, contentType string, contentLength uint64) (err error) {
key := storj.JoinPaths(token, filename) key := storj.JoinPaths(token, filename)
@@ -745,6 +799,7 @@ func (s *StorjStorage) Put(token string, filename string, reader io.Reader, cont
return err return err
} }
// IsNotExist indicates if a file doesn't exist on storage
func (s *StorjStorage) IsNotExist(err error) bool { func (s *StorjStorage) IsNotExist(err error) bool {
return errors.Is(err, uplink.ErrObjectNotFound) return errors.Is(err, uplink.ErrObjectNotFound)
} }

View File

@@ -29,12 +29,12 @@ import (
) )
const ( const (
// characters used for short-urls // SYMBOLS characters used for short-urls
SYMBOLS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" SYMBOLS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
) )
// generate a token // generate a token
func Token(length int) string { func token(length int) string {
result := "" result := ""
for i := 0; i < length; i++ { for i := 0; i < length; i++ {
x := rand.Intn(len(SYMBOLS) - 1) x := rand.Intn(len(SYMBOLS) - 1)

View File

@@ -4,12 +4,12 @@ import "testing"
func BenchmarkTokenConcat(b *testing.B) { func BenchmarkTokenConcat(b *testing.B) {
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
_ = Token(5) + Token(5) _ = token(5) + token(5)
} }
} }
func BenchmarkTokenLonger(b *testing.B) { func BenchmarkTokenLonger(b *testing.B) {
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
_ = Token(10) _ = token(10)
} }
} }

View File

@@ -50,7 +50,7 @@ func getAwsSession(accessKey, secretKey, region, endpoint string, forcePathStyle
} }
func formatNumber(format string, s uint64) string { func formatNumber(format string, s uint64) string {
return RenderFloat(format, float64(s)) return renderFloat(format, float64(s))
} }
var renderFloatPrecisionMultipliers = [10]float64{ var renderFloatPrecisionMultipliers = [10]float64{
@@ -79,7 +79,7 @@ var renderFloatPrecisionRounders = [10]float64{
0.0000000005, 0.0000000005,
} }
func RenderFloat(format string, n float64) string { func renderFloat(format string, n float64) string {
// Special cases: // Special cases:
// NaN = "NaN" // NaN = "NaN"
// +Inf = "+Infinity" // +Inf = "+Infinity"
@@ -127,7 +127,7 @@ func RenderFloat(format string, n float64) string {
// +0000 // +0000
if formatDirectiveIndices[0] == 0 { if formatDirectiveIndices[0] == 0 {
if formatDirectiveChars[formatDirectiveIndices[0]] != '+' { if formatDirectiveChars[formatDirectiveIndices[0]] != '+' {
panic("RenderFloat(): invalid positive sign directive") panic("renderFloat(): invalid positive sign directive")
} }
positiveStr = "+" positiveStr = "+"
formatDirectiveIndices = formatDirectiveIndices[1:] formatDirectiveIndices = formatDirectiveIndices[1:]
@@ -141,7 +141,7 @@ func RenderFloat(format string, n float64) string {
// 000,000.00 // 000,000.00
if len(formatDirectiveIndices) == 2 { if len(formatDirectiveIndices) == 2 {
if (formatDirectiveIndices[1] - formatDirectiveIndices[0]) != 4 { if (formatDirectiveIndices[1] - formatDirectiveIndices[0]) != 4 {
panic("RenderFloat(): thousands separator directive must be followed by 3 digit-specifiers") panic("renderFloat(): thousands separator directive must be followed by 3 digit-specifiers")
} }
thousandStr = string(formatDirectiveChars[formatDirectiveIndices[0]]) thousandStr = string(formatDirectiveChars[formatDirectiveIndices[0]])
formatDirectiveIndices = formatDirectiveIndices[1:] formatDirectiveIndices = formatDirectiveIndices[1:]
@@ -201,8 +201,8 @@ func RenderFloat(format string, n float64) string {
return signStr + intStr + decimalStr + fracStr return signStr + intStr + decimalStr + fracStr
} }
func RenderInteger(format string, n int) string { func renderInteger(format string, n int) string {
return RenderFloat(format, float64(n)) return renderFloat(format, float64(n))
} }
// Request.RemoteAddress contains port, which we want to remove i.e.: // Request.RemoteAddress contains port, which we want to remove i.e.:

View File

@@ -29,7 +29,6 @@ import (
"io" "io"
"net/http" "net/http"
_ "github.com/PuerkitoBio/ghost/handlers"
"github.com/gorilla/mux" "github.com/gorilla/mux"
virustotal "github.com/dutchcoders/go-virustotal" virustotal "github.com/dutchcoders/go-virustotal"