@@ -4,11 +4,13 @@ import (
44 "context"
55 "database/sql"
66 "fmt"
7+ "io"
8+ "log"
79 "net"
810 "net/url"
911 "time"
1012
11- _ "github.com/go-sql-driver/mysql"
13+ "github.com/go-sql-driver/mysql"
1214 "github.com/praetorian-inc/fingerprintx/pkg/plugins"
1315 mysqlplugin "github.com/praetorian-inc/fingerprintx/pkg/plugins/services/mysql"
1416 utils "github.com/projectdiscovery/nuclei/v3/pkg/js/utils"
@@ -66,6 +68,24 @@ func (c *MySQLClient) ConnectWithDB(host string, port int, username, password, d
6668 return connect (host , port , username , password , dbName )
6769}
6870
71+ // ConnectWithDSN connects to MySQL database using given DSN.
72+ // we override mysql dialer with fastdialer so it respects network policy
73+ func (c * MySQLClient ) ConnectWithDSN (dsn string ) (bool , error ) {
74+ db , err := sql .Open ("mysql" , dsn )
75+ if err != nil {
76+ return false , err
77+ }
78+ defer db .Close ()
79+ db .SetMaxOpenConns (1 )
80+ db .SetMaxIdleConns (0 )
81+
82+ _ , err = db .Exec ("select 1" )
83+ if err != nil {
84+ return false , err
85+ }
86+ return true , nil
87+ }
88+
6989func connect (host string , port int , username , password , dbName string ) (bool , error ) {
7090 if host == "" || port <= 0 {
7191 return false , fmt .Errorf ("invalid host or port" )
@@ -78,7 +98,7 @@ func connect(host string, port int, username, password, dbName string) (bool, er
7898
7999 target := net .JoinHostPort (host , fmt .Sprintf ("%d" , port ))
80100
81- db , err := sql .Open ("mysql" , fmt .Sprintf ("%v:%v@tcp(%v)/%s" ,
101+ db , err := sql .Open ("mysql" , fmt .Sprintf ("%v:%v@tcp(%v)/%s?allowOldPasswords=1 " ,
82102 url .PathEscape (username ),
83103 url .PathEscape (password ),
84104 target ,
@@ -87,6 +107,8 @@ func connect(host string, port int, username, password, dbName string) (bool, er
87107 return false , err
88108 }
89109 defer db .Close ()
110+ db .SetMaxOpenConns (1 )
111+ db .SetMaxIdleConns (0 )
90112
91113 _ , err = db .Exec ("select 1" )
92114 if err != nil {
@@ -115,6 +137,8 @@ func (c *MySQLClient) ExecuteQuery(host string, port int, username, password, db
115137 return "" , err
116138 }
117139 defer db .Close ()
140+ db .SetMaxOpenConns (1 )
141+ db .SetMaxIdleConns (0 )
118142
119143 rows , err := db .Query (query )
120144 if err != nil {
@@ -126,3 +150,7 @@ func (c *MySQLClient) ExecuteQuery(host string, port int, username, password, db
126150 }
127151 return string (resp ), nil
128152}
153+
154+ func init () {
155+ _ = mysql .SetLogger (log .New (io .Discard , "" , 0 ))
156+ }
0 commit comments