Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/js/libs/mysql/mysql_private.go
2070 views
1
package mysql
2
3
import (
4
"database/sql"
5
"fmt"
6
"net"
7
"net/url"
8
"strings"
9
)
10
11
type (
12
// MySQLOptions defines the data source name (DSN) options required to connect to a MySQL database.
13
// along with other options like Timeout etc
14
// @example
15
// ```javascript
16
// const mysql = require('nuclei/mysql');
17
// const options = new mysql.MySQLOptions();
18
// options.Host = 'acme.com';
19
// options.Port = 3306;
20
// ```
21
MySQLOptions struct {
22
Host string // Host is the host name or IP address of the MySQL server.
23
Port int // Port is the port number on which the MySQL server is listening.
24
Protocol string // Protocol is the protocol used to connect to the MySQL server (ex: "tcp").
25
Username string // Username is the user name used to authenticate with the MySQL server.
26
Password string // Password is the password used to authenticate with the MySQL server.
27
DbName string // DbName is the name of the database to connect to on the MySQL server.
28
RawQuery string // QueryStr is the query string to append to the DSN (ex: "?tls=skip-verify").
29
Timeout int // Timeout is the timeout in seconds for the connection to the MySQL server.
30
}
31
)
32
33
// BuildDSN builds a MySQL data source name (DSN) from the given options.
34
// @example
35
// ```javascript
36
// 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
// ```
42
func BuildDSN(opts MySQLOptions) (string, error) {
43
if opts.Host == "" || opts.Port <= 0 {
44
return "", fmt.Errorf("invalid host or port")
45
}
46
if opts.Protocol == "" {
47
opts.Protocol = "tcp"
48
}
49
// We're going to use a custom dialer when creating MySQL connections, so if we've been
50
// given "tcp" as the protocol, then quietly switch it to "nucleitcp", which we have
51
// already registered.
52
if opts.Protocol == "tcp" {
53
opts.Protocol = "nucleitcp"
54
}
55
if opts.DbName == "" {
56
opts.DbName = "/"
57
} else {
58
opts.DbName = "/" + opts.DbName
59
}
60
target := net.JoinHostPort(opts.Host, fmt.Sprintf("%d", opts.Port))
61
var dsn strings.Builder
62
dsn.WriteString(fmt.Sprintf("%v:%v", url.QueryEscape(opts.Username), opts.Password))
63
dsn.WriteString("@")
64
dsn.WriteString(fmt.Sprintf("%v(%v)", opts.Protocol, target))
65
if opts.DbName != "" {
66
dsn.WriteString(opts.DbName)
67
}
68
if opts.RawQuery != "" {
69
dsn.WriteString(opts.RawQuery)
70
}
71
return dsn.String(), nil
72
}
73
74
// @memo
75
func connectWithDSN(dsn string) (bool, error) {
76
db, err := sql.Open("mysql", dsn)
77
if err != nil {
78
return false, err
79
}
80
defer func() {
81
_ = db.Close()
82
}()
83
db.SetMaxOpenConns(1)
84
db.SetMaxIdleConns(0)
85
86
_, err = db.Exec("select 1")
87
if err != nil {
88
return false, err
89
}
90
return true, nil
91
}
92
93