2024-02-02 02:22:04 +05:30
|
|
|
package mysql
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"database/sql"
|
|
|
|
|
"fmt"
|
|
|
|
|
"net"
|
|
|
|
|
"net/url"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
2024-02-07 21:45:40 +05:30
|
|
|
type (
|
|
|
|
|
// MySQLOptions defines the data source name (DSN) options required to connect to a MySQL database.
|
|
|
|
|
// along with other options like Timeout etc
|
|
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const mysql = require('nuclei/mysql');
|
|
|
|
|
// const options = new mysql.MySQLOptions();
|
|
|
|
|
// options.Host = 'acme.com';
|
|
|
|
|
// options.Port = 3306;
|
|
|
|
|
// ```
|
|
|
|
|
MySQLOptions struct {
|
|
|
|
|
Host string // Host is the host name or IP address of the MySQL server.
|
|
|
|
|
Port int // Port is the port number on which the MySQL server is listening.
|
|
|
|
|
Protocol string // Protocol is the protocol used to connect to the MySQL server (ex: "tcp").
|
|
|
|
|
Username string // Username is the user name used to authenticate with the MySQL server.
|
|
|
|
|
Password string // Password is the password used to authenticate with the MySQL server.
|
|
|
|
|
DbName string // DbName is the name of the database to connect to on the MySQL server.
|
|
|
|
|
RawQuery string // QueryStr is the query string to append to the DSN (ex: "?tls=skip-verify").
|
|
|
|
|
Timeout int // Timeout is the timeout in seconds for the connection to the MySQL server.
|
|
|
|
|
}
|
|
|
|
|
)
|
2024-02-02 02:22:04 +05:30
|
|
|
|
|
|
|
|
// BuildDSN builds a MySQL data source name (DSN) from the given options.
|
2024-02-07 21:45:40 +05:30
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const mysql = require('nuclei/mysql');
|
|
|
|
|
// const options = new mysql.MySQLOptions();
|
|
|
|
|
// options.Host = 'acme.com';
|
|
|
|
|
// options.Port = 3306;
|
|
|
|
|
// const dsn = mysql.BuildDSN(options);
|
|
|
|
|
// ```
|
2024-02-02 02:22:04 +05:30
|
|
|
func BuildDSN(opts MySQLOptions) (string, error) {
|
|
|
|
|
if opts.Host == "" || opts.Port <= 0 {
|
|
|
|
|
return "", fmt.Errorf("invalid host or port")
|
|
|
|
|
}
|
|
|
|
|
if opts.Protocol == "" {
|
|
|
|
|
opts.Protocol = "tcp"
|
|
|
|
|
}
|
2025-01-14 13:59:30 -05:00
|
|
|
// We're going to use a custom dialer when creating MySQL connections, so if we've been
|
|
|
|
|
// given "tcp" as the protocol, then quietly switch it to "nucleitcp", which we have
|
|
|
|
|
// already registered.
|
|
|
|
|
if opts.Protocol == "tcp" {
|
|
|
|
|
opts.Protocol = "nucleitcp"
|
|
|
|
|
}
|
2024-02-02 02:22:04 +05:30
|
|
|
if opts.DbName == "" {
|
|
|
|
|
opts.DbName = "/"
|
|
|
|
|
} else {
|
|
|
|
|
opts.DbName = "/" + opts.DbName
|
|
|
|
|
}
|
|
|
|
|
target := net.JoinHostPort(opts.Host, fmt.Sprintf("%d", opts.Port))
|
|
|
|
|
var dsn strings.Builder
|
2024-09-06 23:49:55 +05:30
|
|
|
dsn.WriteString(fmt.Sprintf("%v:%v", url.QueryEscape(opts.Username), opts.Password))
|
2024-02-02 02:22:04 +05:30
|
|
|
dsn.WriteString("@")
|
|
|
|
|
dsn.WriteString(fmt.Sprintf("%v(%v)", opts.Protocol, target))
|
|
|
|
|
if opts.DbName != "" {
|
|
|
|
|
dsn.WriteString(opts.DbName)
|
|
|
|
|
}
|
|
|
|
|
if opts.RawQuery != "" {
|
|
|
|
|
dsn.WriteString(opts.RawQuery)
|
|
|
|
|
}
|
|
|
|
|
return dsn.String(), nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-01 16:10:18 +03:00
|
|
|
// @memo
|
2024-02-02 02:22:04 +05:30
|
|
|
func connectWithDSN(dsn string) (bool, error) {
|
|
|
|
|
db, err := sql.Open("mysql", dsn)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
2025-07-01 00:40:44 +07:00
|
|
|
defer func() {
|
2025-07-09 14:47:26 -05:00
|
|
|
_ = db.Close()
|
|
|
|
|
}()
|
2024-02-02 02:22:04 +05:30
|
|
|
db.SetMaxOpenConns(1)
|
|
|
|
|
db.SetMaxIdleConns(0)
|
|
|
|
|
|
|
|
|
|
_, err = db.Exec("select 1")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
return true, nil
|
|
|
|
|
}
|