nuclei/pkg/js/libs/mysql/mysql_private.go

92 lines
2.7 KiB
Go
Raw Normal View History

package mysql
import (
"database/sql"
"fmt"
"net"
"net/url"
"strings"
)
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.
}
)
// BuildDSN builds a MySQL data source name (DSN) from the given options.
// @example
// ```javascript
// const mysql = require('nuclei/mysql');
// const options = new mysql.MySQLOptions();
// options.Host = 'acme.com';
// options.Port = 3306;
// const dsn = mysql.BuildDSN(options);
// ```
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"
}
// 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"
}
if opts.DbName == "" {
opts.DbName = "/"
} else {
opts.DbName = "/" + opts.DbName
}
target := net.JoinHostPort(opts.Host, fmt.Sprintf("%d", opts.Port))
var dsn strings.Builder
dsn.WriteString(fmt.Sprintf("%v:%v", url.QueryEscape(opts.Username), opts.Password))
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
}
// @memo
func connectWithDSN(dsn string) (bool, error) {
db, err := sql.Open("mysql", dsn)
if err != nil {
return false, err
}
defer func() {
Remove singletons from Nuclei engine (continuation of #6210) (#6296) * introducing execution id * wip * . * adding separate execution context id * lint * vet * fixing pg dialers * test ignore * fixing loader FD limit * test * fd fix * wip: remove CloseProcesses() from dev merge * wip: fix merge issue * protocolstate: stop memguarding on last dialer delete * avoid data race in dialers.RawHTTPClient * use shared logger and avoid race conditions * use shared logger and avoid race conditions * go mod * patch executionId into compiled template cache * clean up comment in Parse * go mod update * bump echarts * address merge issues * fix use of gologger * switch cmd/nuclei to options.Logger * address merge issues with go.mod * go vet: address copy of lock with new Copy function * fixing tests * disable speed control * fix nil ExecuterOptions * removing deprecated code * fixing result print * default logger * cli default logger * filter warning from results * fix performance test * hardcoding path * disable upload * refactor(runner): uses `Warning` instead of `Print` for `pdcpUploadErrMsg` Signed-off-by: Dwi Siswanto <git@dw1.io> * Revert "disable upload" This reverts commit 114fbe6663361bf41cf8b2645fd2d57083d53682. * Revert "hardcoding path" This reverts commit cf12ca800e0a0e974bd9fd4826a24e51547f7c00. --------- Signed-off-by: Dwi Siswanto <git@dw1.io> Co-authored-by: Mzack9999 <mzack9999@protonmail.com> Co-authored-by: Dwi Siswanto <git@dw1.io> Co-authored-by: Dwi Siswanto <25837540+dwisiswant0@users.noreply.github.com>
2025-07-09 14:47:26 -05:00
_ = db.Close()
}()
db.SetMaxOpenConns(1)
db.SetMaxIdleConns(0)
_, err = db.Exec("select 1")
if err != nil {
return false, err
}
return true, nil
}