Path: blob/dev/pkg/js/libs/mysql/mysql_private.go
2858 views
package mysql12import (3"context"4"database/sql"5"fmt"6"net"7"net/url"8"strings"9)1011type (12// MySQLOptions defines the data source name (DSN) options required to connect to a MySQL database.13// along with other options like Timeout etc14// @example15// ```javascript16// const mysql = require('nuclei/mysql');17// const options = new mysql.MySQLOptions();18// options.Host = 'acme.com';19// options.Port = 3306;20// ```21MySQLOptions struct {22Host string // Host is the host name or IP address of the MySQL server.23Port int // Port is the port number on which the MySQL server is listening.24Protocol string // Protocol is the protocol used to connect to the MySQL server (ex: "tcp").25Username string // Username is the user name used to authenticate with the MySQL server.26Password string // Password is the password used to authenticate with the MySQL server.27DbName string // DbName is the name of the database to connect to on the MySQL server.28RawQuery string // QueryStr is the query string to append to the DSN (ex: "?tls=skip-verify").29Timeout int // Timeout is the timeout in seconds for the connection to the MySQL server.30}31)3233// BuildDSN builds a MySQL data source name (DSN) from the given options.34// @example35// ```javascript36// const mysql = require('nuclei/mysql');37// const options = new mysql.MySQLOptions();38// options.Host = 'acme.com';39// options.Port = 3306;40// const dsn = mysql.BuildDSN(options);41// ```42func BuildDSN(opts MySQLOptions) (string, error) {43if opts.Host == "" || opts.Port <= 0 {44return "", fmt.Errorf("invalid host or port")45}46if opts.Protocol == "" {47opts.Protocol = "tcp"48}49// We're going to use a custom dialer when creating MySQL connections, so if we've been50// given "tcp" as the protocol, then quietly switch it to "nucleitcp", which we have51// already registered.52if opts.Protocol == "tcp" {53opts.Protocol = "nucleitcp"54}55if opts.DbName == "" {56opts.DbName = "/"57} else {58opts.DbName = "/" + opts.DbName59}60target := net.JoinHostPort(opts.Host, fmt.Sprintf("%d", opts.Port))61var dsn strings.Builder62dsn.WriteString(fmt.Sprintf("%v:%v", url.QueryEscape(opts.Username), opts.Password))63dsn.WriteString("@")64dsn.WriteString(fmt.Sprintf("%v(%v)", opts.Protocol, target))65if opts.DbName != "" {66dsn.WriteString(opts.DbName)67}68if opts.RawQuery != "" {69dsn.WriteString(opts.RawQuery)70}71return dsn.String(), nil72}7374// @memo75func connectWithDSN(executionId string, dsn string) (bool, error) {76db, err := sql.Open("mysql", dsn)77if err != nil {78return false, err79}80defer func() {81_ = db.Close()82}()83db.SetMaxOpenConns(1)84db.SetMaxIdleConns(0)8586ctx := context.WithValue(context.Background(), "executionId", executionId) // nolint: staticcheck87err = db.PingContext(ctx)88if err != nil {89return false, err90}91return true, nil92}939495