-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
60 lines (47 loc) · 1005 Bytes
/
main.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
package main
import (
"flag"
"io"
"io/ioutil"
"log"
"net"
"os"
)
var (
Address = flag.String("tcp", "127.0.0.1:8080", "TCP service address")
)
func main() {
flag.Parse()
listener, err := net.Listen("tcp", *Address)
if err != nil {
log.Fatalf("Failed to open listener: %s\n", err)
}
log.Printf("Listening on: %s\n", *Address)
for {
conn, err := listener.Accept()
if err != nil {
log.Printf("Connect failed: %s\n", err)
}
file, err := ioutil.TempFile(os.TempDir(), "dumbledore")
if err != nil {
log.Printf("Failed to open file: %s\n", err)
}
log.Printf("Streaming input from %s to %s", conn.RemoteAddr(), file.Name())
go func() {
defer file.Close()
defer conn.Close()
pipe := &Pipe{source: conn, dest: file}
pipe.transfer()
}()
}
}
type Pipe struct {
source io.Reader
dest io.WriteCloser
}
func (p *Pipe) transfer() {
defer p.dest.Close()
if _, err := io.Copy(p.dest, p.source); err != nil {
log.Printf("Copy failed: %s\n", err)
}
}