util.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. // Copyright (C) 2019-2022 Nicola Murino
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Affero General Public License as published
  5. // by the Free Software Foundation, version 3.
  6. //
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU Affero General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU Affero General Public License
  13. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. // Package util provides some common utility methods
  15. package util
  16. import (
  17. "bytes"
  18. "crypto/ecdsa"
  19. "crypto/ed25519"
  20. "crypto/elliptic"
  21. "crypto/rand"
  22. "crypto/rsa"
  23. "crypto/tls"
  24. "crypto/x509"
  25. "encoding/pem"
  26. "errors"
  27. "fmt"
  28. "io"
  29. "io/fs"
  30. "net"
  31. "net/http"
  32. "net/url"
  33. "os"
  34. "path"
  35. "path/filepath"
  36. "regexp"
  37. "runtime"
  38. "strings"
  39. "time"
  40. "github.com/google/uuid"
  41. "github.com/lithammer/shortuuid/v3"
  42. "github.com/rs/xid"
  43. "golang.org/x/crypto/ssh"
  44. "github.com/drakkan/sftpgo/v2/internal/logger"
  45. )
  46. const (
  47. logSender = "util"
  48. osWindows = "windows"
  49. )
  50. var (
  51. emailRegex = regexp.MustCompile("^(?:(?:(?:(?:[a-zA-Z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])+(?:\\.([a-zA-Z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])+)*)|(?:(?:\\x22)(?:(?:(?:(?:\\x20|\\x09)*(?:\\x0d\\x0a))?(?:\\x20|\\x09)+)?(?:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f]|\\x21|[\\x23-\\x5b]|[\\x5d-\\x7e]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(?:(?:[\\x01-\\x09\\x0b\\x0c\\x0d-\\x7f]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}]))))*(?:(?:(?:\\x20|\\x09)*(?:\\x0d\\x0a))?(\\x20|\\x09)+)?(?:\\x22))))@(?:(?:(?:[a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(?:(?:[a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])(?:[a-zA-Z]|\\d|-|\\.|~|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])*(?:[a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])))\\.)+(?:(?:[a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(?:(?:[a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])(?:[a-zA-Z]|\\d|-|\\.|~|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])*(?:[a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])))\\.?$")
  52. // this can be set at build time
  53. additionalSharedDataSearchPath = ""
  54. )
  55. // Contains reports whether v is present in elems.
  56. func Contains[T comparable](elems []T, v T) bool {
  57. for _, s := range elems {
  58. if v == s {
  59. return true
  60. }
  61. }
  62. return false
  63. }
  64. // IsStringPrefixInSlice searches a string prefix in a slice and returns true
  65. // if a matching prefix is found
  66. func IsStringPrefixInSlice(obj string, list []string) bool {
  67. for i := 0; i < len(list); i++ {
  68. if strings.HasPrefix(obj, list[i]) {
  69. return true
  70. }
  71. }
  72. return false
  73. }
  74. // RemoveDuplicates returns a new slice removing any duplicate element from the initial one
  75. func RemoveDuplicates(obj []string, trim bool) []string {
  76. if len(obj) == 0 {
  77. return obj
  78. }
  79. seen := make(map[string]bool)
  80. validIdx := 0
  81. for _, item := range obj {
  82. if trim {
  83. item = strings.TrimSpace(item)
  84. }
  85. if !seen[item] {
  86. seen[item] = true
  87. obj[validIdx] = item
  88. validIdx++
  89. }
  90. }
  91. return obj[:validIdx]
  92. }
  93. // GetTimeAsMsSinceEpoch returns unix timestamp as milliseconds from a time struct
  94. func GetTimeAsMsSinceEpoch(t time.Time) int64 {
  95. return t.UnixMilli()
  96. }
  97. // GetTimeFromMsecSinceEpoch return a time struct from a unix timestamp with millisecond precision
  98. func GetTimeFromMsecSinceEpoch(msec int64) time.Time {
  99. return time.Unix(0, msec*1000000)
  100. }
  101. // GetDurationAsString returns a string representation for a time.Duration
  102. func GetDurationAsString(d time.Duration) string {
  103. d = d.Round(time.Second)
  104. h := d / time.Hour
  105. d -= h * time.Hour
  106. m := d / time.Minute
  107. d -= m * time.Minute
  108. s := d / time.Second
  109. if h > 0 {
  110. return fmt.Sprintf("%02d:%02d:%02d", h, m, s)
  111. }
  112. return fmt.Sprintf("%02d:%02d", m, s)
  113. }
  114. // ByteCountSI returns humanized size in SI (decimal) format
  115. func ByteCountSI(b int64) string {
  116. return byteCount(b, 1000)
  117. }
  118. // ByteCountIEC returns humanized size in IEC (binary) format
  119. func ByteCountIEC(b int64) string {
  120. return byteCount(b, 1024)
  121. }
  122. func byteCount(b int64, unit int64) string {
  123. if b < unit {
  124. return fmt.Sprintf("%d B", b)
  125. }
  126. div, exp := unit, 0
  127. for n := b / unit; n >= unit; n /= unit {
  128. div *= unit
  129. exp++
  130. }
  131. if unit == 1000 {
  132. return fmt.Sprintf("%.1f %cB",
  133. float64(b)/float64(div), "KMGTPE"[exp])
  134. }
  135. return fmt.Sprintf("%.1f %ciB",
  136. float64(b)/float64(div), "KMGTPE"[exp])
  137. }
  138. // GetIPFromRemoteAddress returns the IP from the remote address.
  139. // If the given remote address cannot be parsed it will be returned unchanged
  140. func GetIPFromRemoteAddress(remoteAddress string) string {
  141. ip, _, err := net.SplitHostPort(remoteAddress)
  142. if err == nil {
  143. return ip
  144. }
  145. return remoteAddress
  146. }
  147. // NilIfEmpty returns nil if the input string is empty
  148. func NilIfEmpty(s string) *string {
  149. if s == "" {
  150. return nil
  151. }
  152. return &s
  153. }
  154. // GetStringFromPointer returns the string value or empty if nil
  155. func GetStringFromPointer(val *string) string {
  156. if val == nil {
  157. return ""
  158. }
  159. return *val
  160. }
  161. // GetIntFromPointer returns the int value or zero
  162. func GetIntFromPointer(val *int64) int64 {
  163. if val == nil {
  164. return 0
  165. }
  166. return *val
  167. }
  168. // GetTimeFromPointer returns the time value or now
  169. func GetTimeFromPointer(val *time.Time) time.Time {
  170. if val == nil {
  171. return time.Now()
  172. }
  173. return *val
  174. }
  175. // GenerateRSAKeys generate rsa private and public keys and write the
  176. // private key to specified file and the public key to the specified
  177. // file adding the .pub suffix
  178. func GenerateRSAKeys(file string) error {
  179. if err := createDirPathIfMissing(file, 0700); err != nil {
  180. return err
  181. }
  182. key, err := rsa.GenerateKey(rand.Reader, 4096)
  183. if err != nil {
  184. return err
  185. }
  186. o, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
  187. if err != nil {
  188. return err
  189. }
  190. defer o.Close()
  191. priv := &pem.Block{
  192. Type: "RSA PRIVATE KEY",
  193. Bytes: x509.MarshalPKCS1PrivateKey(key),
  194. }
  195. if err := pem.Encode(o, priv); err != nil {
  196. return err
  197. }
  198. pub, err := ssh.NewPublicKey(&key.PublicKey)
  199. if err != nil {
  200. return err
  201. }
  202. return os.WriteFile(file+".pub", ssh.MarshalAuthorizedKey(pub), 0600)
  203. }
  204. // GenerateECDSAKeys generate ecdsa private and public keys and write the
  205. // private key to specified file and the public key to the specified
  206. // file adding the .pub suffix
  207. func GenerateECDSAKeys(file string) error {
  208. if err := createDirPathIfMissing(file, 0700); err != nil {
  209. return err
  210. }
  211. key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
  212. if err != nil {
  213. return err
  214. }
  215. keyBytes, err := x509.MarshalECPrivateKey(key)
  216. if err != nil {
  217. return err
  218. }
  219. priv := &pem.Block{
  220. Type: "EC PRIVATE KEY",
  221. Bytes: keyBytes,
  222. }
  223. o, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
  224. if err != nil {
  225. return err
  226. }
  227. defer o.Close()
  228. if err := pem.Encode(o, priv); err != nil {
  229. return err
  230. }
  231. pub, err := ssh.NewPublicKey(&key.PublicKey)
  232. if err != nil {
  233. return err
  234. }
  235. return os.WriteFile(file+".pub", ssh.MarshalAuthorizedKey(pub), 0600)
  236. }
  237. // GenerateEd25519Keys generate ed25519 private and public keys and write the
  238. // private key to specified file and the public key to the specified
  239. // file adding the .pub suffix
  240. func GenerateEd25519Keys(file string) error {
  241. pubKey, privKey, err := ed25519.GenerateKey(rand.Reader)
  242. if err != nil {
  243. return err
  244. }
  245. keyBytes, err := x509.MarshalPKCS8PrivateKey(privKey)
  246. if err != nil {
  247. return err
  248. }
  249. priv := &pem.Block{
  250. Type: "PRIVATE KEY",
  251. Bytes: keyBytes,
  252. }
  253. o, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
  254. if err != nil {
  255. return err
  256. }
  257. defer o.Close()
  258. if err := pem.Encode(o, priv); err != nil {
  259. return err
  260. }
  261. pub, err := ssh.NewPublicKey(pubKey)
  262. if err != nil {
  263. return err
  264. }
  265. return os.WriteFile(file+".pub", ssh.MarshalAuthorizedKey(pub), 0600)
  266. }
  267. // GetDirsForVirtualPath returns all the directory for the given path in reverse order
  268. // for example if the path is: /1/2/3/4 it returns:
  269. // [ "/1/2/3/4", "/1/2/3", "/1/2", "/1", "/" ]
  270. func GetDirsForVirtualPath(virtualPath string) []string {
  271. if virtualPath == "" || virtualPath == "." {
  272. virtualPath = "/"
  273. } else {
  274. if !path.IsAbs(virtualPath) {
  275. virtualPath = CleanPath(virtualPath)
  276. }
  277. }
  278. dirsForPath := []string{virtualPath}
  279. for {
  280. if virtualPath == "/" {
  281. break
  282. }
  283. virtualPath = path.Dir(virtualPath)
  284. dirsForPath = append(dirsForPath, virtualPath)
  285. }
  286. return dirsForPath
  287. }
  288. // CleanPath returns a clean POSIX (/) absolute path to work with
  289. func CleanPath(p string) string {
  290. return CleanPathWithBase("/", p)
  291. }
  292. // CleanPathWithBase returns a clean POSIX (/) absolute path to work with.
  293. // The specified base will be used if the provided path is not absolute
  294. func CleanPathWithBase(base, p string) string {
  295. p = filepath.ToSlash(p)
  296. if !path.IsAbs(p) {
  297. p = path.Join(base, p)
  298. }
  299. return path.Clean(p)
  300. }
  301. // IsFileInputValid returns true this is a valid file name.
  302. // This method must be used before joining a file name, generally provided as
  303. // user input, with a directory
  304. func IsFileInputValid(fileInput string) bool {
  305. cleanInput := filepath.Clean(fileInput)
  306. if cleanInput == "." || cleanInput == ".." {
  307. return false
  308. }
  309. return true
  310. }
  311. // CleanDirInput sanitizes user input for directories.
  312. // On Windows it removes any trailing `"`.
  313. // We try to help windows users that set an invalid path such as "C:\ProgramData\SFTPGO\".
  314. // This will only help if the invalid path is the last argument, for example in this command:
  315. // sftpgo.exe serve -c "C:\ProgramData\SFTPGO\" -l "sftpgo.log"
  316. // the -l flag will be ignored and the -c flag will get the value `C:\ProgramData\SFTPGO" -l sftpgo.log`
  317. // since the backslash after SFTPGO escape the double quote. This is definitely a bad user input
  318. func CleanDirInput(dirInput string) string {
  319. if runtime.GOOS == osWindows {
  320. for strings.HasSuffix(dirInput, "\"") {
  321. dirInput = strings.TrimSuffix(dirInput, "\"")
  322. }
  323. }
  324. return filepath.Clean(dirInput)
  325. }
  326. func createDirPathIfMissing(file string, perm os.FileMode) error {
  327. dirPath := filepath.Dir(file)
  328. if _, err := os.Stat(dirPath); errors.Is(err, fs.ErrNotExist) {
  329. err = os.MkdirAll(dirPath, perm)
  330. if err != nil {
  331. return err
  332. }
  333. }
  334. return nil
  335. }
  336. // GenerateRandomBytes generates the secret to use for JWT auth
  337. func GenerateRandomBytes(length int) []byte {
  338. b := make([]byte, length)
  339. _, err := io.ReadFull(rand.Reader, b)
  340. if err == nil {
  341. return b
  342. }
  343. b = xid.New().Bytes()
  344. for len(b) < length {
  345. b = append(b, xid.New().Bytes()...)
  346. }
  347. return b[:length]
  348. }
  349. // GenerateUniqueID retuens an unique ID
  350. func GenerateUniqueID() string {
  351. u, err := uuid.NewRandom()
  352. if err != nil {
  353. return xid.New().String()
  354. }
  355. return shortuuid.DefaultEncoder.Encode(u)
  356. }
  357. // HTTPListenAndServe is a wrapper for ListenAndServe that support both tcp
  358. // and Unix-domain sockets
  359. func HTTPListenAndServe(srv *http.Server, address string, port int, isTLS bool, logSender string) error {
  360. var listener net.Listener
  361. var err error
  362. if filepath.IsAbs(address) && runtime.GOOS != osWindows {
  363. if !IsFileInputValid(address) {
  364. return fmt.Errorf("invalid socket address %#v", address)
  365. }
  366. err = createDirPathIfMissing(address, os.ModePerm)
  367. if err != nil {
  368. logger.ErrorToConsole("error creating Unix-domain socket parent dir: %v", err)
  369. logger.Error(logSender, "", "error creating Unix-domain socket parent dir: %v", err)
  370. }
  371. os.Remove(address)
  372. listener, err = newListener("unix", address, srv.ReadTimeout, srv.WriteTimeout)
  373. } else {
  374. CheckTCP4Port(port)
  375. listener, err = newListener("tcp", fmt.Sprintf("%s:%d", address, port), srv.ReadTimeout, srv.WriteTimeout)
  376. }
  377. if err != nil {
  378. return err
  379. }
  380. logger.Info(logSender, "", "server listener registered, address: %v TLS enabled: %v", listener.Addr().String(), isTLS)
  381. defer listener.Close()
  382. if isTLS {
  383. return srv.ServeTLS(listener, "", "")
  384. }
  385. return srv.Serve(listener)
  386. }
  387. // GetTLSCiphersFromNames returns the TLS ciphers from the specified names
  388. func GetTLSCiphersFromNames(cipherNames []string) []uint16 {
  389. var ciphers []uint16
  390. for _, name := range RemoveDuplicates(cipherNames, false) {
  391. for _, c := range tls.CipherSuites() {
  392. if c.Name == strings.TrimSpace(name) {
  393. ciphers = append(ciphers, c.ID)
  394. }
  395. }
  396. }
  397. return ciphers
  398. }
  399. // EncodeTLSCertToPem returns the specified certificate PEM encoded.
  400. // This can be verified using openssl x509 -in cert.crt -text -noout
  401. func EncodeTLSCertToPem(tlsCert *x509.Certificate) (string, error) {
  402. if len(tlsCert.Raw) == 0 {
  403. return "", errors.New("invalid x509 certificate, no der contents")
  404. }
  405. publicKeyBlock := pem.Block{
  406. Type: "CERTIFICATE",
  407. Bytes: tlsCert.Raw,
  408. }
  409. return string(pem.EncodeToMemory(&publicKeyBlock)), nil
  410. }
  411. // CheckTCP4Port quits the app if bind on the given IPv4 port fails.
  412. // This is a ugly hack to avoid to bind on an already used port.
  413. // It is required on Windows only. Upstream does not consider this
  414. // behaviour a bug:
  415. // https://github.com/golang/go/issues/45150
  416. func CheckTCP4Port(port int) {
  417. if runtime.GOOS != osWindows {
  418. return
  419. }
  420. listener, err := net.Listen("tcp4", fmt.Sprintf(":%d", port))
  421. if err != nil {
  422. logger.ErrorToConsole("unable to bind on tcp4 address: %v", err)
  423. logger.Error(logSender, "", "unable to bind on tcp4 address: %v", err)
  424. os.Exit(1)
  425. }
  426. listener.Close()
  427. }
  428. // IsByteArrayEmpty return true if the byte array is empty or a new line
  429. func IsByteArrayEmpty(b []byte) bool {
  430. if len(b) == 0 {
  431. return true
  432. }
  433. if bytes.Equal(b, []byte("\n")) {
  434. return true
  435. }
  436. if bytes.Equal(b, []byte("\r\n")) {
  437. return true
  438. }
  439. return false
  440. }
  441. // GetSSHPublicKeyAsString returns an SSH public key serialized as string
  442. func GetSSHPublicKeyAsString(pubKey []byte) (string, error) {
  443. if len(pubKey) == 0 {
  444. return "", nil
  445. }
  446. k, err := ssh.ParsePublicKey(pubKey)
  447. if err != nil {
  448. return "", err
  449. }
  450. return string(ssh.MarshalAuthorizedKey(k)), nil
  451. }
  452. // GetRealIP returns the ip address as result of parsing either the
  453. // X-Real-IP header or the X-Forwarded-For header
  454. func GetRealIP(r *http.Request, header string, depth int) string {
  455. if header == "" {
  456. return ""
  457. }
  458. var ipAddresses []string
  459. for _, h := range r.Header.Values(header) {
  460. for _, ipStr := range strings.Split(h, ",") {
  461. ipStr = strings.TrimSpace(ipStr)
  462. ipAddresses = append(ipAddresses, ipStr)
  463. }
  464. }
  465. idx := len(ipAddresses) - 1 - depth
  466. if idx >= 0 {
  467. ip := strings.TrimSpace(ipAddresses[idx])
  468. if ip == "" || net.ParseIP(ip) == nil {
  469. return ""
  470. }
  471. return ip
  472. }
  473. return ""
  474. }
  475. // GetHTTPLocalAddress returns the local address for an http.Request
  476. // or empty if it cannot be determined
  477. func GetHTTPLocalAddress(r *http.Request) string {
  478. if r == nil {
  479. return ""
  480. }
  481. localAddr, ok := r.Context().Value(http.LocalAddrContextKey).(net.Addr)
  482. if ok {
  483. return localAddr.String()
  484. }
  485. return ""
  486. }
  487. // ParseAllowedIPAndRanges returns a list of functions that allow to find if an
  488. // IP is equal or is contained within the allowed list
  489. func ParseAllowedIPAndRanges(allowed []string) ([]func(net.IP) bool, error) {
  490. res := make([]func(net.IP) bool, len(allowed))
  491. for i, allowFrom := range allowed {
  492. if strings.LastIndex(allowFrom, "/") > 0 {
  493. _, ipRange, err := net.ParseCIDR(allowFrom)
  494. if err != nil {
  495. return nil, fmt.Errorf("given string %q is not a valid IP range: %v", allowFrom, err)
  496. }
  497. res[i] = ipRange.Contains
  498. } else {
  499. allowed := net.ParseIP(allowFrom)
  500. if allowed == nil {
  501. return nil, fmt.Errorf("given string %q is not a valid IP address", allowFrom)
  502. }
  503. res[i] = allowed.Equal
  504. }
  505. }
  506. return res, nil
  507. }
  508. // GetRedactedURL returns the url redacting the password if any
  509. func GetRedactedURL(rawurl string) string {
  510. if !strings.HasPrefix(rawurl, "http") {
  511. return rawurl
  512. }
  513. u, err := url.Parse(rawurl)
  514. if err != nil {
  515. return rawurl
  516. }
  517. return u.Redacted()
  518. }
  519. // PrependFileInfo prepends a file info to a slice in an efficient way.
  520. // We, optimistically, assume that the slice has enough capacity
  521. func PrependFileInfo(files []os.FileInfo, info os.FileInfo) []os.FileInfo {
  522. files = append(files, nil)
  523. copy(files[1:], files)
  524. files[0] = info
  525. return files
  526. }
  527. // GetTLSVersion returns the TLS version for integer:
  528. // - 12 means TLS 1.2
  529. // - 13 means TLS 1.3
  530. // default is TLS 1.2
  531. func GetTLSVersion(val int) uint16 {
  532. switch val {
  533. case 13:
  534. return tls.VersionTLS13
  535. default:
  536. return tls.VersionTLS12
  537. }
  538. }
  539. // IsEmailValid returns true if the specified email address is valid
  540. func IsEmailValid(email string) bool {
  541. return emailRegex.MatchString(email)
  542. }