| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- // Go MySQL Driver - A MySQL-Driver for Go's database/sql package
- //
- // Copyright 2016 The Go-MySQL-Driver Authors. All rights reserved.
- //
- // This Source Code Form is subject to the terms of the Mozilla Public
- // License, v. 2.0. If a copy of the MPL was not distributed with this file,
- // You can obtain one at http://mozilla.org/MPL/2.0/.
- package mysql
- import (
- "crypto/tls"
- "errors"
- "fmt"
- "net"
- "net/url"
- "strings"
- "time"
- )
- var (
- errInvalidDSNUnescaped = errors.New("invalid DSN: did you forget to escape a param value?")
- errInvalidDSNAddr = errors.New("invalid DSN: network address not terminated (missing closing brace)")
- errInvalidDSNNoSlash = errors.New("invalid DSN: missing the slash separating the database name")
- errInvalidDSNUnsafeCollation = errors.New("invalid DSN: interpolateParams can not be used with unsafe collations")
- )
- // Config is a configuration parsed from a DSN string
- type Config struct {
- User string // Username
- Passwd string // Password
- Net string // Network type
- Addr string // Network address
- DBName string // Database name
- Params map[string]string // Connection parameters
- Loc *time.Location // Location for time.Time values
- TLS *tls.Config // TLS configuration
- Timeout time.Duration // Dial timeout
- ReadTimeout time.Duration // I/O read timeout
- WriteTimeout time.Duration // I/O write timeout
- Collation uint8 // Connection collation
- AllowAllFiles bool // Allow all files to be used with LOAD DATA LOCAL INFILE
- AllowCleartextPasswords bool // Allows the cleartext client side plugin
- AllowOldPasswords bool // Allows the old insecure password method
- ClientFoundRows bool // Return number of matching rows instead of rows changed
- ColumnsWithAlias bool // Prepend table alias to column names
- InterpolateParams bool // Interpolate placeholders into query string
- ParseTime bool // Parse time values to time.Time
- Strict bool // Return warnings as errors
- }
- // ParseDSN parses the DSN string to a Config
- func ParseDSN(dsn string) (cfg *Config, err error) {
- // New config with some default values
- cfg = &Config{
- Loc: time.UTC,
- Collation: defaultCollation,
- }
- // [user[:password]@][net[(addr)]]/dbname[?param1=value1¶mN=valueN]
- // Find the last '/' (since the password or the net addr might contain a '/')
- foundSlash := false
- for i := len(dsn) - 1; i >= 0; i-- {
- if dsn[i] == '/' {
- foundSlash = true
- var j, k int
- // left part is empty if i <= 0
- if i > 0 {
- // [username[:password]@][protocol[(address)]]
- // Find the last '@' in dsn[:i]
- for j = i; j >= 0; j-- {
- if dsn[j] == '@' {
- // username[:password]
- // Find the first ':' in dsn[:j]
- for k = 0; k < j; k++ {
- if dsn[k] == ':' {
- cfg.Passwd = dsn[k+1 : j]
- break
- }
- }
- cfg.User = dsn[:k]
- break
- }
- }
- // [protocol[(address)]]
- // Find the first '(' in dsn[j+1:i]
- for k = j + 1; k < i; k++ {
- if dsn[k] == '(' {
- // dsn[i-1] must be == ')' if an address is specified
- if dsn[i-1] != ')' {
- if strings.ContainsRune(dsn[k+1:i], ')') {
- return nil, errInvalidDSNUnescaped
- }
- return nil, errInvalidDSNAddr
- }
- cfg.Addr = dsn[k+1 : i-1]
- break
- }
- }
- cfg.Net = dsn[j+1 : k]
- }
- // dbname[?param1=value1&...¶mN=valueN]
- // Find the first '?' in dsn[i+1:]
- for j = i + 1; j < len(dsn); j++ {
- if dsn[j] == '?' {
- if err = parseDSNParams(cfg, dsn[j+1:]); err != nil {
- return
- }
- break
- }
- }
- cfg.DBName = dsn[i+1 : j]
- break
- }
- }
- if !foundSlash && len(dsn) > 0 {
- return nil, errInvalidDSNNoSlash
- }
- if cfg.InterpolateParams && unsafeCollations[cfg.Collation] {
- return nil, errInvalidDSNUnsafeCollation
- }
- // Set default network if empty
- if cfg.Net == "" {
- cfg.Net = "tcp"
- }
- // Set default address if empty
- if cfg.Addr == "" {
- switch cfg.Net {
- case "tcp":
- cfg.Addr = "127.0.0.1:3306"
- case "unix":
- cfg.Addr = "/tmp/mysql.sock"
- default:
- return nil, errors.New("default addr for network '" + cfg.Net + "' unknown")
- }
- }
- return
- }
- // parseDSNParams parses the DSN "query string"
- // Values must be url.QueryEscape'ed
- func parseDSNParams(cfg *Config, params string) (err error) {
- for _, v := range strings.Split(params, "&") {
- param := strings.SplitN(v, "=", 2)
- if len(param) != 2 {
- continue
- }
- // cfg params
- switch value := param[1]; param[0] {
- // Disable INFILE whitelist / enable all files
- case "allowAllFiles":
- var isBool bool
- cfg.AllowAllFiles, isBool = readBool(value)
- if !isBool {
- return errors.New("invalid bool value: " + value)
- }
- // Use cleartext authentication mode (MySQL 5.5.10+)
- case "allowCleartextPasswords":
- var isBool bool
- cfg.AllowCleartextPasswords, isBool = readBool(value)
- if !isBool {
- return errors.New("invalid bool value: " + value)
- }
- // Use old authentication mode (pre MySQL 4.1)
- case "allowOldPasswords":
- var isBool bool
- cfg.AllowOldPasswords, isBool = readBool(value)
- if !isBool {
- return errors.New("invalid bool value: " + value)
- }
- // Switch "rowsAffected" mode
- case "clientFoundRows":
- var isBool bool
- cfg.ClientFoundRows, isBool = readBool(value)
- if !isBool {
- return errors.New("invalid bool value: " + value)
- }
- // Collation
- case "collation":
- collation, ok := collations[value]
- if !ok {
- // Note possibility for false negatives:
- // could be triggered although the collation is valid if the
- // collations map does not contain entries the server supports.
- err = errors.New("unknown collation")
- return
- }
- cfg.Collation = collation
- break
- case "columnsWithAlias":
- var isBool bool
- cfg.ColumnsWithAlias, isBool = readBool(value)
- if !isBool {
- return errors.New("invalid bool value: " + value)
- }
- // Compression
- case "compress":
- return errors.New("compression not implemented yet")
- // Enable client side placeholder substitution
- case "interpolateParams":
- var isBool bool
- cfg.InterpolateParams, isBool = readBool(value)
- if !isBool {
- return errors.New("invalid bool value: " + value)
- }
- // Time Location
- case "loc":
- if value, err = url.QueryUnescape(value); err != nil {
- return
- }
- cfg.Loc, err = time.LoadLocation(value)
- if err != nil {
- return
- }
- // time.Time parsing
- case "parseTime":
- var isBool bool
- cfg.ParseTime, isBool = readBool(value)
- if !isBool {
- return errors.New("invalid bool value: " + value)
- }
- // I/O read Timeout
- case "readTimeout":
- cfg.ReadTimeout, err = time.ParseDuration(value)
- if err != nil {
- return
- }
- // Strict mode
- case "strict":
- var isBool bool
- cfg.Strict, isBool = readBool(value)
- if !isBool {
- return errors.New("invalid bool value: " + value)
- }
- // Dial Timeout
- case "timeout":
- cfg.Timeout, err = time.ParseDuration(value)
- if err != nil {
- return
- }
- // TLS-Encryption
- case "tls":
- boolValue, isBool := readBool(value)
- if isBool {
- if boolValue {
- cfg.TLS = &tls.Config{}
- }
- } else if value, err := url.QueryUnescape(value); err != nil {
- return fmt.Errorf("invalid value for TLS config name: %v", err)
- } else {
- if strings.ToLower(value) == "skip-verify" {
- cfg.TLS = &tls.Config{InsecureSkipVerify: true}
- } else if tlsConfig, ok := tlsConfigRegister[value]; ok {
- if len(tlsConfig.ServerName) == 0 && !tlsConfig.InsecureSkipVerify {
- host, _, err := net.SplitHostPort(cfg.Addr)
- if err == nil {
- tlsConfig.ServerName = host
- }
- }
- cfg.TLS = tlsConfig
- } else {
- return errors.New("invalid value / unknown config name: " + value)
- }
- }
- // I/O write Timeout
- case "writeTimeout":
- cfg.WriteTimeout, err = time.ParseDuration(value)
- if err != nil {
- return
- }
- default:
- // lazy init
- if cfg.Params == nil {
- cfg.Params = make(map[string]string)
- }
- if cfg.Params[param[0]], err = url.QueryUnescape(value); err != nil {
- return
- }
- }
- }
- return
- }
|