2 paster -- paste service
3 Copyright (C) 2021-2022 Sergey Matveev <stargrave@stargrave.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
34 func fatal(s string) {
40 maxSize := flag.Uint64("max-size", 1<<20, "Maximal upload size")
42 fmt.Fprintf(os.Stderr, "Usage: paster [options] URL [...]\n")
46 if len(flag.Args()) == 0 {
51 r := bufio.NewReader(os.Stdin)
52 b, err := r.ReadByte()
57 fatal("bad bencode: no dictionary start")
59 buf := make([]byte, 21)
63 if _, err = io.ReadFull(r, buf[:3]); err != nil {
66 switch s := string(buf[:3]); s {
68 if _, err = io.ReadFull(r, buf[:2]); err != nil {
72 fatal(`bad bencode: invalid "e" format`)
74 extLen, err := strconv.Atoi(string(buf[:1]))
78 if _, err = io.ReadFull(r, buf[:extLen]); err != nil {
81 ext = "." + string(buf[:extLen])
88 i := bytes.IndexByte(buf[:n], ':')
90 fatal(`bad bencode: invalid "v" length`)
92 size, err = strconv.ParseUint(string(buf[:i]), 10, 64)
104 fatal("bad bencode: invalid key")
106 rnd := make([]byte, 12)
107 if _, err = io.ReadFull(rand.Reader, rnd); err != nil {
110 fn = "." + base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(rnd) +
112 fd, err := os.OpenFile(fn, os.O_RDWR|os.O_CREATE|os.O_EXCL, os.FileMode(0666))
117 bfd := bufio.NewWriter(fd)
118 mr := io.MultiReader(bytes.NewReader(buf), r)
119 mw := io.MultiWriter(bfd, h)
120 if _, err = io.CopyN(mw, mr, int64(size-1)); err != nil {
123 if _, err = mr.Read(buf[:1]); err != nil {
126 if _, err = mw.Write(buf[:1]); err != nil {
129 if (ext == ".txt" || ext == ".url") && buf[0] != '\n' {
130 if err = bfd.WriteByte('\n'); err != nil {
134 if _, err = mr.Read(buf[:1]); err != nil {
139 fatal("bad bencode: no dictionary end")
141 if err = bfd.Flush(); err != nil {
144 if err = fd.Close(); err != nil {
147 if err = os.Rename(fn, fn[1:]); err != nil {
150 for _, u := range flag.Args() {
151 fmt.Println(u + fn[1:])
153 fmt.Println(hex.EncodeToString(h.Sum(nil)[:512/2/8]))
157 os.Getenv("TCPREMOTEIP"),
158 os.Getenv("TCPREMOTEPORT"),