MCPcopy
hub / github.com/shadow1ng/fscan / doMySQLAuth

Method doMySQLAuth

plugins/services/mysql.go:78–115  ·  view source on GitHub ↗

doMySQLAuth 执行MySQL认证

(ctx context.Context, info *common.HostInfo, cred Credential, config *common.Config, state *common.State)

Source from the content-addressed store, hash-verified

76
77// doMySQLAuth 执行MySQL认证
78func (p *MySQLPlugin) doMySQLAuth(ctx context.Context, info *common.HostInfo, cred Credential, config *common.Config, state *common.State) *AuthResult {
79 connStr := fmt.Sprintf("%s:%s@tcp(%s:%d)/information_schema?charset=utf8&timeout=%ds",
80 cred.Username, cred.Password, info.Host, info.Port, int64(config.Timeout.Seconds()))
81
82 db, err := sql.Open("mysql", connStr)
83 if err != nil {
84 state.IncrementTCPFailedPacketCount()
85 return &AuthResult{
86 Success: false,
87 ErrorType: classifyMySQLErrorType(err),
88 Error: err,
89 }
90 }
91
92 db.SetConnMaxLifetime(config.Timeout)
93 db.SetMaxOpenConns(1)
94 db.SetMaxIdleConns(0)
95
96 err = db.PingContext(ctx)
97 if err != nil {
98 _ = db.Close()
99 state.IncrementTCPFailedPacketCount()
100 return &AuthResult{
101 Success: false,
102 ErrorType: classifyMySQLErrorType(err),
103 Error: err,
104 }
105 }
106
107 state.IncrementTCPSuccessPacketCount()
108
109 return &AuthResult{
110 Success: true,
111 Conn: &SQLDBWrapper{db},
112 ErrorType: ErrorTypeUnknown,
113 Error: nil,
114 }
115}
116
117// classifyMySQLErrorType MySQL错误分类
118func classifyMySQLErrorType(err error) ErrorType {

Callers 1

createAuthFuncMethod · 0.95

Calls 4

classifyMySQLErrorTypeFunction · 0.85
CloseMethod · 0.65

Tested by

no test coverage detected