/
read.go
129 lines (115 loc) · 3.51 KB
/
read.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright © 2018 Ken'ichiro Oyama <k1lowxb@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package cmd
import (
"context"
"fmt"
"io/ioutil"
"os"
"github.com/google/gopacket"
"github.com/google/gopacket/pcap"
"github.com/k1LoW/tcpdp/dumper"
"github.com/k1LoW/tcpdp/reader"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/crypto/ssh/terminal"
)
var (
readDumper string
readTarget string
)
// readCmd represents the read command
var readCmd = &cobra.Command{
Use: "read [PCAP]",
Short: "Read pcap file mode",
Long: "Read pcap format file and dump.",
Args: func(cmd *cobra.Command, args []string) error {
if terminal.IsTerminal(0) {
if len(args) != 1 {
return fmt.Errorf("Error: %s", "requires pcap file path")
}
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
viper.Set("tcpdp.dumper", readDumper) // because share with `server`
viper.Set("log.enable", false)
viper.Set("log.stdout", false)
viper.Set("dumpLog.enable", false)
viper.Set("dumpLog.stdout", true)
var pcapFile string
if terminal.IsTerminal(0) {
pcapFile = args[0]
} else {
pcap, _ := ioutil.ReadAll(os.Stdin)
tmpfile, _ := ioutil.TempFile("", "tcpdptmp")
defer func() {
tmpfile.Close()
os.Remove(tmpfile.Name())
}()
pcapFile = tmpfile.Name()
if _, err := tmpfile.Write(pcap); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
handle, err := pcap.OpenOffline(pcapFile)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer handle.Close()
var d dumper.Dumper
switch readDumper {
case "hex":
d = dumper.NewHexDumper()
case "pg":
d = dumper.NewPgDumper()
case "mysql":
d = dumper.NewMysqlDumper()
default:
d = dumper.NewHexDumper()
}
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
r := reader.NewPacketReader(
context.Background(),
packetSource,
d,
[]dumper.DumpValue{},
)
host, port, err := reader.ParseTarget(readTarget)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
err = r.ReadAndDump(host, port)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
},
}
func init() {
readCmd.Flags().StringVarP(&readTarget, "target", "t", "", "target addr. (ex. \"localhost:80\", \"3306\")")
readCmd.Flags().StringP("format", "f", "json", "STDOUT format. (\"console\", \"json\" , \"ltsv\") ")
readCmd.Flags().StringVarP(&readDumper, "dumper", "d", "hex", "dumper")
viper.BindPFlag("dumpLog.stdoutFormat", readCmd.Flags().Lookup("format"))
rootCmd.AddCommand(readCmd)
}