aboutsummaryrefslogtreecommitdiffstats
path: root/main.go
blob: 733876e8fae1c57bb6d8734485c39c3f393d3479 (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
package main

import (
	"flag"
	"fmt"
	"github.com/tonistiigi/units"
	"io"
	"net"
	"os"
)

const (
	port    = "60321"
	network = "tcp"
	usage   = "Usage: hose <-r | -s <rhost>>"
)

var (
	r     = flag.Bool("r", false, "receive")
	rhost = flag.String("s", "", "send to remote host")
)

func main() {
	flag.Parse()
	if *r {
		if err := recv(); err != nil {
			eprintf("%v\n", err)
		}
	} else if *rhost != "" {
		if err := send(*rhost); err != nil {
			eprintf("%v\n", err)
		}
	} else {
		fmt.Println(usage)
		flag.Usage()
		os.Exit(1)
	}
}

// recv pipes data from the remote host to stdout.
func recv() error {
	laddr := net.JoinHostPort("", port)
	ln, err := net.Listen(network, laddr)
	if err != nil {
		return err
	}
	defer ln.Close()
	fmt.Fprintf(os.Stderr, "listening on %s\n", laddr)

	conn, err := ln.Accept()
	if err != nil {
		return err
	}
	defer conn.Close()
	fmt.Fprintf(os.Stderr, "accepted connection from %s\n", conn.RemoteAddr())

	n, err := io.Copy(os.Stdout, conn)
	fmt.Fprintf(os.Stderr, "received %.2f\n", units.Bytes(n)*units.B)
	return err
}

// send pipes data from stdin to the remote host.
func send(rhost string) error {
	raddr := net.JoinHostPort(rhost, port)
	fmt.Fprintf(os.Stderr, "connecting to %s...\n", raddr)
	conn, err := net.Dial(network, raddr)
	if err != nil {
		return err
	}
	defer conn.Close()
	fmt.Fprintf(os.Stderr, "connected to %s\n", raddr)

	n, err := io.Copy(conn, os.Stdin)
	fmt.Fprintf(os.Stderr, "sent %.2f\n", units.Bytes(n)*units.B)
	return err
}

func eprintf(format string, a ...any) {
	fmt.Fprintf(os.Stderr, format, a...)
	os.Exit(1)
}