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 {
55 time.Sleep(time.Duration(*maxSecs) * time.Second)
59 fatal("max aliveness time is reached")
62 r := bufio.NewReader(os.Stdin)
63 b, err := r.ReadByte()
68 fatal("bad bencode: no dictionary start")
70 buf := make([]byte, 21)
74 if _, err = io.ReadFull(r, buf[:3]); err != nil {
77 switch s := string(buf[:3]); s {
79 if _, err = io.ReadFull(r, buf[:2]); err != nil {
83 fatal(`bad bencode: invalid "e" format`)
85 extLen, err := strconv.Atoi(string(buf[:1]))
89 if _, err = io.ReadFull(r, buf[:extLen]); err != nil {
92 ext = "." + string(buf[:extLen])
99 i := bytes.IndexByte(buf[:n], ':')
101 fatal(`bad bencode: invalid "v" length`)
103 size, err = strconv.ParseUint(string(buf[:i]), 10, 64)
115 fatal("bad bencode: invalid key")
117 rnd := make([]byte, 12)
118 if _, err = io.ReadFull(rand.Reader, rnd); err != nil {
121 fn = "." + base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(rnd) +
123 fd, err := os.OpenFile(fn, os.O_RDWR|os.O_CREATE|os.O_EXCL, os.FileMode(0666))
128 bfd := bufio.NewWriter(fd)
129 mr := io.MultiReader(bytes.NewReader(buf), r)
130 mw := io.MultiWriter(bfd, h)
131 if _, err = io.CopyN(mw, mr, int64(size-1)); err != nil {
134 if _, err = mr.Read(buf[:1]); err != nil {
137 if _, err = mw.Write(buf[:1]); err != nil {
140 if (ext == ".txt" || ext == ".url") && buf[0] != '\n' {
141 if err = bfd.WriteByte('\n'); err != nil {
145 if _, err = mr.Read(buf[:1]); err != nil {
150 fatal("bad bencode: no dictionary end")
152 if err = bfd.Flush(); err != nil {
155 if err = fd.Close(); err != nil {
158 if err = os.Rename(fn, fn[1:]); err != nil {
161 for _, u := range flag.Args() {
162 fmt.Println(u + fn[1:])
164 fmt.Println(hex.EncodeToString(h.Sum(nil)[:512/2/8]))
168 os.Getenv("TCPREMOTEIP"),
169 os.Getenv("TCPREMOTEPORT"),