-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathseashells.go
71 lines (60 loc) · 1.28 KB
/
seashells.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
package main
import (
"bufio"
"flag"
"fmt"
"io"
"log"
"net"
"os"
"syscall"
"time"
)
var (
url = flag.String("i", "seashells.io", "URL/IP to use")
port = flag.String("p", "1337", "Port to use")
output = flag.Bool("q", false, "Suppress writing to stdout")
delay = flag.Int("d", 0, "Delay")
)
func main() {
flag.Parse()
fullUrl := *url + ":" + *port
conn, err := net.Dial("tcp", fullUrl)
if err != nil {
log.Fatal(err)
}
serverUrl, err := bufio.NewReader(conn).ReadString('\n')
fmt.Fprint(os.Stderr, serverUrl) // write url to sderr
if *delay > 0 {
time.Sleep(time.Duration(*delay) * time.Second)
}
scan := bufio.NewReader(os.Stdin)
var both io.Writer
if *output == true {
both = conn
} else {
both = io.MultiWriter(os.Stdout, conn) // will write to stdout and the net connection
}
done := make(chan error)
go func() {
for {
err := syscall.Select(1, &syscall.FdSet{[32]int32{1}}, nil, nil, nil) // check for data on stdin
done <- err
_, err = io.Copy(both, scan) // send the scan data to the multiwriter
done <- err
_, _, err = scan.ReadLine() // just want to see if we get an EOF
if err == io.EOF {
os.Exit(0)
}
done <- err
}
}()
for {
select {
case err := <-done:
if err != nil {
log.Fatal(err)
}
}
}
}