|
1 | 1 | package main
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "fmt" |
5 |
| - golog "log" |
6 |
| - "os" |
7 |
| - "io" |
8 |
| - "sync" |
9 |
| - "strconv" |
10 |
| - "flag" |
11 |
| - |
12 |
| - "github.com/netsec-ethz/scion-apps/netcat/utils" |
13 |
| - "github.com/scionproto/scion/go/lib/log" |
| 4 | + "flag" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + golog "log" |
| 8 | + "os" |
| 9 | + "strconv" |
| 10 | + "sync" |
| 11 | + |
| 12 | + "github.com/netsec-ethz/scion-apps/netcat/utils" |
| 13 | + "github.com/scionproto/scion/go/lib/log" |
14 | 14 | )
|
15 | 15 |
|
16 | 16 | func printUsage() {
|
17 | 17 | fmt.Println("netcat [flags] host-address port")
|
18 | 18 | fmt.Println("The host address is specified as ISD-AS,[IP Address]")
|
19 | 19 | fmt.Println("Example SCION address: 17-ffaa:1:bfd,[127.0.0.1]:42002")
|
20 | 20 | fmt.Println("Available flags:")
|
| 21 | + fmt.Println(" -h: Show help") |
21 | 22 | fmt.Println(" -P: Use IA when resolving SCIOND socket path")
|
22 | 23 | fmt.Println(" -b: Send an extra byte before sending the actual data")
|
23 | 24 | }
|
24 | 25 |
|
25 | 26 | func main() {
|
26 |
| - log.SetupLogConsole("debug") |
27 |
| - log.Debug("Launching netcat") |
28 |
| - |
29 |
| - var ( |
30 |
| - SERVER_ADDRESS string |
31 |
| - PORT uint16 |
32 |
| - USE_IA_SCIOND_PATH bool |
33 |
| - SEND_PIPER_BYTE bool |
34 |
| - ) |
35 |
| - |
36 |
| - flag.BoolVar(&USE_IA_SCIOND_PATH, "P", false, "Use IA SCIOND Path") |
37 |
| - flag.BoolVar(&SEND_PIPER_BYTE, "b", false, "Send extra byte") |
38 |
| - flag.Parse() |
39 |
| - tail := flag.Args() |
40 |
| - if len(tail) != 2 { |
41 |
| - golog.Panicf("Number of arguments is not two! Arguments: %v", tail) |
42 |
| - } |
43 |
| - |
44 |
| - SERVER_ADDRESS = tail[0] |
45 |
| - port64, err := strconv.ParseUint(tail[1], 10, 16) |
46 |
| - if err != nil { |
47 |
| - golog.Panicf("Can't parse port string %s: %v", port64, err) |
48 |
| - } |
49 |
| - PORT = uint16(port64) |
50 |
| - |
51 |
| - |
52 |
| - // Initialize SCION library |
53 |
| - err = utils.InitSCION("", "", USE_IA_SCIOND_PATH) |
54 |
| - if err != nil { |
55 |
| - golog.Panicf("Error initializing SCION connection: %v", err) |
56 |
| - } |
57 |
| - |
58 |
| - conn, err := utils.DialSCION(fmt.Sprintf("%s:%v", SERVER_ADDRESS, PORT)) |
59 |
| - if err != nil { |
60 |
| - golog.Panicf("Error dialing remote: %v", err) |
61 |
| - } |
62 |
| - |
63 |
| - log.Debug("Connected!") |
64 |
| - |
65 |
| - if SEND_PIPER_BYTE { |
66 |
| - _, err := conn.Write([]byte {71}) |
67 |
| - if err != nil { |
68 |
| - golog.Panicf("Error writing extra byte: %v", err) |
69 |
| - } |
70 |
| - |
71 |
| - log.Debug("Sent extra byte!") |
72 |
| - } |
73 |
| - |
74 |
| - close := func() { |
75 |
| - conn.Close() |
76 |
| - } |
77 |
| - |
78 |
| - var once sync.Once |
79 |
| - go func() { |
80 |
| - io.Copy(os.Stdout, conn) |
81 |
| - once.Do(close) |
82 |
| - }() |
83 |
| - io.Copy(conn, os.Stdin) |
84 |
| - once.Do(close) |
85 |
| - |
86 |
| - log.Debug("Exiting snetcat...") |
| 27 | + log.SetupLogConsole("debug") |
| 28 | + log.Debug("Launching netcat") |
| 29 | + |
| 30 | + var ( |
| 31 | + serverAddress string |
| 32 | + port uint16 |
| 33 | + useIASCIONDPath bool |
| 34 | + extraByte bool |
| 35 | + ) |
| 36 | + flag.BoolVar(&showHelp, "h", false, "Show help") |
| 37 | + flag.BoolVar(&USE_IA_SCIOND_PATH, "P", false, "Use IA SCIOND Path") |
| 38 | + flag.BoolVar(&SEND_PIPER_BYTE, "b", false, "Send extra byte") |
| 39 | + flag.Parse() |
| 40 | + if showHelp { |
| 41 | + printUsage() |
| 42 | + return |
| 43 | + } |
| 44 | + |
| 45 | + tail := flag.Args() |
| 46 | + if len(tail) != 2 { |
| 47 | + golog.Panicf("Number of arguments is not two! Arguments: %v", tail) |
| 48 | + } |
| 49 | + |
| 50 | + SERVER_ADDRESS = tail[0] |
| 51 | + port64, err := strconv.ParseUint(tail[1], 10, 16) |
| 52 | + if err != nil { |
| 53 | + golog.Panicf("Can't parse port string %v: %v", port64, err) |
| 54 | + } |
| 55 | + PORT = uint16(port64) |
| 56 | + |
| 57 | + // Initialize SCION library |
| 58 | + err = utils.InitSCION("", "", USE_IA_SCIOND_PATH) |
| 59 | + if err != nil { |
| 60 | + golog.Panicf("Error initializing SCION connection: %v", err) |
| 61 | + } |
| 62 | + |
| 63 | + conn, err := utils.DialSCION(fmt.Sprintf("%s:%v", SERVER_ADDRESS, PORT)) |
| 64 | + if err != nil { |
| 65 | + golog.Panicf("Error dialing remote: %v", err) |
| 66 | + } |
| 67 | + |
| 68 | + log.Debug("Connected!") |
| 69 | + |
| 70 | + if SEND_PIPER_BYTE { |
| 71 | + _, err := conn.Write([]byte{71}) |
| 72 | + if err != nil { |
| 73 | + golog.Panicf("Error writing extra byte: %v", err) |
| 74 | + } |
| 75 | + |
| 76 | + log.Debug("Sent extra byte!") |
| 77 | + } |
| 78 | + |
| 79 | + close := func() { |
| 80 | + conn.Close() |
| 81 | + } |
| 82 | + |
| 83 | + var once sync.Once |
| 84 | + go func() { |
| 85 | + io.Copy(os.Stdout, conn) |
| 86 | + once.Do(close) |
| 87 | + }() |
| 88 | + io.Copy(conn, os.Stdin) |
| 89 | + once.Do(close) |
| 90 | + |
| 91 | + log.Debug("Exiting snetcat...") |
87 | 92 | }
|
88 |
| - |
89 |
| - |
|
0 commit comments