summaryrefslogtreecommitdiffstats
path: root/tools/hole/hole.go
blob: 2834d16d7683a67658646903de93a5b450d87b1e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// The hole command is a CLI uploader tool for the roseh.moe wormhole.
//
// # Magic wormhole
//
// The name is inspired by the original "magic wormhole" library. The goal is to
// get a file from one computer to another without a USB stick, Including cases
// where scp might not be available. This includes transferring files to your
// phone, for example, although a USB cord might still be more convenient when
// moving many files. It should also work over restrictive firewalls. You should
// be able to use the wormhole as long as you can access http://roseh.moe in
// a browser.
//
// # Usage example
//
// The following shows a typical interaction with the tool
//
//	$ hole file.pdf
//	https://roseh.moe/wormhole/dv-32-ethos-aztec-shako-spoils-2y-dwarf-sand-any
//	Waiting for upload...
//	<tool hangs, waiting to upload the file>
//
// Then, on another machine:
//
//	$ curl https://roseh.moe/wormhole/dv-32-ethos-aztec-shako-spoils-2y-dwarf-sand-any > file.pdf
//
// # Web interface
//
// There's also a web interface available for the wormhole, available at
// https://roseh.moe/wormhole.
package main

import (
	"flag"
	"fmt"
	"io"
	"mime/multipart"
	"net/http"
	"os"

	"roseh.moe/pkg/roseh.moe/internal/hole"
)

var url = flag.String("url", "https://roseh.moe", "server URL")

func upload() error {
	args := flag.Args()
	if len(args) == 0 {
		return fmt.Errorf("usage error: missing filename")
	}
	if len(args) > 1 {
		return fmt.Errorf("usage error: too many positional arguments")
	}
	fileName := args[0]
	f, err := os.Open(fileName)
	if err != nil {
		return err
	}
	defer f.Close()
	hole := hole.New()
	fmt.Println(*url + "/wormhole/" + hole)
	fmt.Fprintln(os.Stderr, "Waiting for upload...")
	resp, err := http.Get(*url + "/wormhole/" + hole + "/ready")
	if err != nil {
		return fmt.Errorf("check wormhole ready: %s", err)
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return fmt.Errorf("check wormhole ready: status: %s", resp.Status)
	}
	if _, err := io.Copy(io.Discard, resp.Body); err != nil {
		return fmt.Errorf("check wormhole ready: read response: %s", err)
	}
	pipeReader, pipeWriter := io.Pipe()
	multipartWriter := multipart.NewWriter(pipeWriter)
	go func() {
		defer pipeWriter.Close()
		part, err := multipartWriter.CreateFormFile("file", fileName)
		if err != nil {
			pipeWriter.CloseWithError(fmt.Errorf("create multipart reader: %s", err))
			return
		}
		if _, err := io.Copy(part, f); err != nil {
			pipeWriter.CloseWithError(fmt.Errorf("write body: %s", err))
			return
		}
		if err := multipartWriter.Close(); err != nil {
			pipeWriter.CloseWithError(fmt.Errorf("write trailer: %s", err))
			return
		}
	}()
	resp, err = http.Post(*url+"/wormhole/"+hole, multipartWriter.FormDataContentType(), pipeReader)
	if err != nil {
		return fmt.Errorf("upload: %s", err)
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return fmt.Errorf("upload: status: %s", resp.Status)
	}
	io.Copy(os.Stderr, resp.Body)
	fmt.Fprintln(os.Stderr)
	return nil
}

func main() {
	flag.Usage = func() {
		fmt.Fprint(os.Stderr, `Usage: hole FILE
Create a one-time wormhole that allows a specific client to download a
file. Prints the URL to download the file and then hangs until the file
is downloaded.

Example:
  $ hole file.pdf
  https://roseh.moe/wormhole/dv-32-ethos-aztec-shako-spoils-2y-dwarf-sand-any
  Waiting for upload...
  <tool hangs, waiting to upload the file>

Then, on another machine:
  $ curl https://roseh.moe/wormhole/dv-32-ethos-aztec-shako-spoils-2y-dwarf-sand-any > file.pdf

Flags:
`)
		flag.PrintDefaults()
	}
	flag.Parse()
	if err := upload(); err != nil {
		fmt.Fprintf(os.Stderr, "hole: %s\n", err)
		os.Exit(1)
	}
}