2 paster -- paste service
3 Copyright (C) 2021 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/>.
35 func fatal(s string) {
41 maxSecs := flag.Uint("max-secs", 60, "Maximal time of aliveness (0=disable)")
42 maxSize := flag.Uint64("max-size", 1<<20, "Maximal upload size")
44 fmt.Fprintf(os.Stderr, "Usage: paster [options] URL [...]\n")
48 if len(flag.Args()) == 0 {
54 time.Sleep(time.Duration(*maxSecs) * time.Second)
55 fatal("max aliveness time is reached")
58 r := bufio.NewReader(os.Stdin)
59 b, err := r.ReadByte()
64 fatal("bad bencode: no dictionary start")
66 buf := make([]byte, 21)
70 if _, err = io.ReadFull(r, buf[:3]); err != nil {
73 switch s := string(buf[:3]); s {
75 if _, err = io.ReadFull(r, buf[:2]); err != nil {
79 fatal(`bad bencode: invalid "e" format`)
81 extLen, err := strconv.Atoi(string(buf[:1]))
85 if _, err = io.ReadFull(r, buf[:extLen]); err != nil {
88 ext = "." + string(buf[:extLen])
95 i := bytes.IndexByte(buf[:n], ':')
97 fatal(`bad bencode: invalid "v" length`)
99 size, err = strconv.ParseUint(string(buf[:i]), 10, 64)
111 fatal("bad bencode: invalid key")
113 rnd := make([]byte, 12)
114 if _, err = io.ReadFull(rand.Reader, rnd); err != nil {
117 fn := "." + base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(rnd) +
119 fd, err := os.OpenFile(fn, os.O_RDWR|os.O_CREATE|os.O_EXCL, os.FileMode(0666))
124 bfd := bufio.NewWriter(fd)
125 mr := io.MultiReader(bytes.NewReader(buf), r)
126 mw := io.MultiWriter(bfd, h)
127 if _, err = io.CopyN(mw, mr, int64(size-1)); err != nil {
130 if _, err = mr.Read(buf[:1]); err != nil {
133 if _, err = mw.Write(buf[:1]); err != nil {
136 if (ext == ".txt" || ext == ".url") && buf[0] != '\n' {
137 if err = bfd.WriteByte('\n'); err != nil {
141 if _, err = mr.Read(buf[:1]); err != nil {
146 fatal("bad bencode: no dictionary end")
148 if err = bfd.Flush(); err != nil {
151 if err = fd.Close(); err != nil {
154 if err = os.Rename(fn, fn[1:]); err != nil {
157 for _, u := range flag.Args() {
158 fmt.Println(u + fn[1:])
160 fmt.Println(hex.EncodeToString(h.Sum(nil)[:512/2/8]))
164 os.Getenv("TCPREMOTEIP"),
165 os.Getenv("TCPREMOTEPORT"),