-
Notifications
You must be signed in to change notification settings - Fork 1
/
mysql.go
167 lines (155 loc) · 3.69 KB
/
mysql.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"fmt"
"log"
"strconv"
"strings"
)
//read more client-server protocol from http://dev.mysql.com/doc/internals/en/text-protocol.html
const (
comQuit byte = iota + 1
comInitDB
comQuery
comFieldList
comCreateDB
comDropDB
comRefresh
comShutdown
comStatistics
comProcessInfo
comConnect
comProcessKill
comDebug
comPing
comTime
comDelayedInsert
comChangeUser
comBinlogDump
comTableDump
comConnectOut
comRegiserSlave
comStmtPrepare
comStmtExecute
comStmtSendLongData
comStmtClose
comStmtReset
comSetOption
comStmtFetch
)
type query struct {
bindPort int64
client string
cport int64
server string
sport int64
sqlType string
sqlString string
}
func ipPortFromNetAddr(s string) (ip string, port int64) {
addrInfo := strings.SplitN(s, ":", 2)
ip = addrInfo[0]
port, _ = strconv.ParseInt(addrInfo[1], 10, 64)
return
}
func converToUnixLine(sql string) string {
sql = strings.Replace(sql, "\r\n", "\n", -1)
sql = strings.Replace(sql, "\r", "\n", -1)
return sql
}
func sql_escape(s string) string {
var j int = 0
if len(s) == 0 {
return ""
}
tempStr := s[:]
desc := make([]byte, len(tempStr)*2)
for i := 0; i < len(tempStr); i++ {
flag := false
var escape byte
switch tempStr[i] {
case '\r':
flag = true
escape = '\r'
break
case '\n':
flag = true
escape = '\n'
break
case '\\':
flag = true
escape = '\\'
break
case '\'':
flag = true
escape = '\''
break
case '"':
flag = true
escape = '"'
break
case '\032':
flag = true
escape = 'Z'
break
default:
}
if flag {
desc[j] = '\\'
desc[j+1] = escape
j = j + 23
} else {
desc[j] = tempStr[i]
j = j + 1
}
}
return string(desc[0:j])
}
func proxyLog(client, server string,n int,buffer []byte) {
var sqlInfo query
if n >= 5 {
var verboseStr string
switch buffer[4] {
case comQuit:
verboseStr = fmt.Sprintf("From %s To %s; Quit: %s\n", client, server, "user quit")
sqlInfo.sqlType = "Quit"
case comInitDB:
verboseStr = fmt.Sprintf("From %s To %s; schema: use %s\n", client, server, string(buffer[5:n]))
sqlInfo.sqlType = "Schema"
case comQuery:
verboseStr = fmt.Sprintf("From %s To %s; Query: %s\n", client, server, string(buffer[5:n]))
sqlInfo.sqlType = "Query"
case comFieldList:
verboseStr = fmt.Sprintf("From %s To %s; Table columns list: %s\n", client, server, string(buffer[5:n]))
sqlInfo.sqlType = "Table columns list"
case comCreateDB:
verboseStr = fmt.Sprintf("From %s To %s; CreateDB: %s\n", client, server, string(buffer[5:n]))
sqlInfo.sqlType = "CreateDB"
case comDropDB:
verboseStr = fmt.Sprintf("From %s To %s; DropDB: %s\n", client, server, string(buffer[5:n]))
sqlInfo.sqlType = "DropDB"
case comRefresh:
verboseStr = fmt.Sprintf("From %s To %s; Refresh: %s\n", client, server, string(buffer[5:n]))
sqlInfo.sqlType = "Refresh"
case comStmtPrepare:
verboseStr = fmt.Sprintf("From %s To %s; Prepare Query: %s\n",client, server, string(buffer[5:n]))
sqlInfo.sqlType = "Prepare Query"
case comStmtExecute:
verboseStr = fmt.Sprintf("From %s To %s; Prepare Args: %s\n", client, server, string(buffer[5:n]))
sqlInfo.sqlType = "Prepare Args"
case comProcessKill:
verboseStr = fmt.Sprintf("From %s To %s; Kill: kill conntion %s\n", client, server, string(buffer[5:n]))
sqlInfo.sqlType = "Kill"
default:
}
sqlInfo.sqlString = verboseStr
// log.Print(string(buffer))
log.Print(verboseStr)
if strings.EqualFold(sqlInfo.sqlType, "Quit") {
sqlInfo.sqlString = "user quit"
} else {
// sqlInfo.sqlString = converToUnixLine(sql_escape(string(buffer[5:n])))
}
//
}
//
}