forked from canonical/lxd
/
main_netcat.go
104 lines (83 loc) · 2.27 KB
/
main_netcat.go
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
package main
import (
"fmt"
"io"
"net"
"os"
"sync"
"github.com/spf13/cobra"
"github.com/lxc/lxd/shared"
"github.com/lxc/lxd/shared/eagain"
)
type cmdNetcat struct {
global *cmdGlobal
}
func (c *cmdNetcat) Command() *cobra.Command {
cmd := &cobra.Command{}
cmd.Use = "netcat <address> <name>"
cmd.Short = "Send stdin data to a unix socket"
cmd.Long = `Description:
Send stdin data to a unix socket
This internal command is used to forward the output of a program over
a websocket by first forwarding it to a unix socket controlled by LXD.
Its main use is when running rsync or btrfs/zfs send/receive between
two machines over the LXD websocket API.
`
cmd.RunE = c.Run
cmd.Hidden = true
return cmd
}
func (c *cmdNetcat) Run(cmd *cobra.Command, args []string) error {
// Sanity checks
if len(args) < 2 {
cmd.Help()
if len(args) == 0 {
return nil
}
return fmt.Errorf("Missing required arguments")
}
// Only root should run this
if os.Geteuid() != 0 {
return fmt.Errorf("This must be run as root")
}
logPath := shared.LogPath(args[1], "netcat.log")
if shared.PathExists(logPath) {
os.Remove(logPath)
}
logFile, logErr := os.OpenFile(logPath, os.O_WRONLY|os.O_CREATE|os.O_SYNC, 0644)
if logErr == nil {
defer logFile.Close()
}
uAddr, err := net.ResolveUnixAddr("unix", args[0])
if err != nil {
if logErr == nil {
logFile.WriteString(fmt.Sprintf("Could not resolve unix domain socket \"%s\": %s\n", args[0], err))
}
return err
}
conn, err := net.DialUnix("unix", nil, uAddr)
if err != nil {
if logErr == nil {
logFile.WriteString(fmt.Sprintf("Could not dial unix domain socket \"%s\": %s\n", args[0], err))
}
return err
}
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
_, err := io.Copy(eagain.Writer{Writer: os.Stdout}, eagain.Reader{Reader: conn})
if err != nil && logErr == nil {
logFile.WriteString(fmt.Sprintf("Error while copying from stdout to unix domain socket \"%s\": %s\n", args[0], err))
}
conn.Close()
wg.Done()
}()
go func() {
_, err := io.Copy(eagain.Writer{Writer: conn}, eagain.Reader{Reader: os.Stdin})
if err != nil && logErr == nil {
logFile.WriteString(fmt.Sprintf("Error while copying from unix domain socket \"%s\" to stdin: %s\n", args[0], err))
}
}()
wg.Wait()
return nil
}