util.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. // Copyright (C) 2019-2023 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. "math"
  31. "net"
  32. "net/http"
  33. "net/netip"
  34. "net/url"
  35. "os"
  36. "path"
  37. "path/filepath"
  38. "regexp"
  39. "runtime"
  40. "strconv"
  41. "strings"
  42. "time"
  43. "unicode"
  44. "github.com/google/uuid"
  45. "github.com/lithammer/shortuuid/v3"
  46. "github.com/rs/xid"
  47. "golang.org/x/crypto/ssh"
  48. "github.com/drakkan/sftpgo/v2/internal/logger"
  49. )
  50. const (
  51. logSender = "util"
  52. osWindows = "windows"
  53. )
  54. var (
  55. 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}])))\\.?$")
  56. // this can be set at build time
  57. additionalSharedDataSearchPath = ""
  58. )
  59. // IEC Sizes.
  60. // kibis of bits
  61. const (
  62. oneByte = 1 << (iota * 10)
  63. kiByte
  64. miByte
  65. giByte
  66. tiByte
  67. piByte
  68. eiByte
  69. )
  70. // SI Sizes.
  71. const (
  72. iByte = 1
  73. kbByte = iByte * 1000
  74. mByte = kbByte * 1000
  75. gByte = mByte * 1000
  76. tByte = gByte * 1000
  77. pByte = tByte * 1000
  78. eByte = pByte * 1000
  79. )
  80. var bytesSizeTable = map[string]uint64{
  81. "b": oneByte,
  82. "kib": kiByte,
  83. "kb": kbByte,
  84. "mib": miByte,
  85. "mb": mByte,
  86. "gib": giByte,
  87. "gb": gByte,
  88. "tib": tiByte,
  89. "tb": tByte,
  90. "pib": piByte,
  91. "pb": pByte,
  92. "eib": eiByte,
  93. "eb": eByte,
  94. // Without suffix
  95. "": oneByte,
  96. "ki": kiByte,
  97. "k": kbByte,
  98. "mi": miByte,
  99. "m": mByte,
  100. "gi": giByte,
  101. "g": gByte,
  102. "ti": tiByte,
  103. "t": tByte,
  104. "pi": piByte,
  105. "p": pByte,
  106. "ei": eiByte,
  107. "e": eByte,
  108. }
  109. // Contains reports whether v is present in elems.
  110. func Contains[T comparable](elems []T, v T) bool {
  111. for _, s := range elems {
  112. if v == s {
  113. return true
  114. }
  115. }
  116. return false
  117. }
  118. // Remove removes an element from a string slice and
  119. // returns the modified slice
  120. func Remove(elems []string, val string) []string {
  121. for idx, v := range elems {
  122. if v == val {
  123. elems[idx] = elems[len(elems)-1]
  124. return elems[:len(elems)-1]
  125. }
  126. }
  127. return elems
  128. }
  129. // IsStringPrefixInSlice searches a string prefix in a slice and returns true
  130. // if a matching prefix is found
  131. func IsStringPrefixInSlice(obj string, list []string) bool {
  132. for i := 0; i < len(list); i++ {
  133. if strings.HasPrefix(obj, list[i]) {
  134. return true
  135. }
  136. }
  137. return false
  138. }
  139. // RemoveDuplicates returns a new slice removing any duplicate element from the initial one
  140. func RemoveDuplicates(obj []string, trim bool) []string {
  141. if len(obj) == 0 {
  142. return obj
  143. }
  144. seen := make(map[string]bool)
  145. validIdx := 0
  146. for _, item := range obj {
  147. if trim {
  148. item = strings.TrimSpace(item)
  149. }
  150. if !seen[item] {
  151. seen[item] = true
  152. obj[validIdx] = item
  153. validIdx++
  154. }
  155. }
  156. return obj[:validIdx]
  157. }
  158. // GetTimeAsMsSinceEpoch returns unix timestamp as milliseconds from a time struct
  159. func GetTimeAsMsSinceEpoch(t time.Time) int64 {
  160. return t.UnixMilli()
  161. }
  162. // GetTimeFromMsecSinceEpoch return a time struct from a unix timestamp with millisecond precision
  163. func GetTimeFromMsecSinceEpoch(msec int64) time.Time {
  164. return time.Unix(0, msec*1000000)
  165. }
  166. // GetDurationAsString returns a string representation for a time.Duration
  167. func GetDurationAsString(d time.Duration) string {
  168. d = d.Round(time.Second)
  169. h := d / time.Hour
  170. d -= h * time.Hour
  171. m := d / time.Minute
  172. d -= m * time.Minute
  173. s := d / time.Second
  174. if h > 0 {
  175. return fmt.Sprintf("%02d:%02d:%02d", h, m, s)
  176. }
  177. return fmt.Sprintf("%02d:%02d", m, s)
  178. }
  179. // ByteCountSI returns humanized size in SI (decimal) format
  180. func ByteCountSI(b int64) string {
  181. return byteCount(b, 1000, true)
  182. }
  183. // ByteCountIEC returns humanized size in IEC (binary) format
  184. func ByteCountIEC(b int64) string {
  185. return byteCount(b, 1024, false)
  186. }
  187. func byteCount(b int64, unit int64, maxPrecision bool) string {
  188. if b <= 0 && maxPrecision {
  189. return strconv.FormatInt(b, 10)
  190. }
  191. if b < unit {
  192. return fmt.Sprintf("%d B", b)
  193. }
  194. div, exp := unit, 0
  195. for n := b / unit; n >= unit; n /= unit {
  196. div *= unit
  197. exp++
  198. }
  199. var val string
  200. if maxPrecision {
  201. val = strconv.FormatFloat(float64(b)/float64(div), 'f', -1, 64)
  202. } else {
  203. val = fmt.Sprintf("%.1f", float64(b)/float64(div))
  204. }
  205. if unit == 1000 {
  206. return fmt.Sprintf("%s %cB", val, "KMGTPE"[exp])
  207. }
  208. return fmt.Sprintf("%s %ciB", val, "KMGTPE"[exp])
  209. }
  210. // ParseBytes parses a string representation of bytes into the number
  211. // of bytes it represents.
  212. //
  213. // ParseBytes("42 MB") -> 42000000, nil
  214. // ParseBytes("42 mib") -> 44040192, nil
  215. //
  216. // copied from here:
  217. //
  218. // https://github.com/dustin/go-humanize/blob/master/bytes.go
  219. //
  220. // with minor modifications
  221. func ParseBytes(s string) (int64, error) {
  222. s = strings.TrimSpace(s)
  223. lastDigit := 0
  224. hasComma := false
  225. for _, r := range s {
  226. if !(unicode.IsDigit(r) || r == '.' || r == ',') {
  227. break
  228. }
  229. if r == ',' {
  230. hasComma = true
  231. }
  232. lastDigit++
  233. }
  234. num := s[:lastDigit]
  235. if hasComma {
  236. num = strings.Replace(num, ",", "", -1)
  237. }
  238. f, err := strconv.ParseFloat(num, 64)
  239. if err != nil {
  240. return 0, err
  241. }
  242. extra := strings.ToLower(strings.TrimSpace(s[lastDigit:]))
  243. if m, ok := bytesSizeTable[extra]; ok {
  244. f *= float64(m)
  245. if f >= math.MaxInt64 {
  246. return 0, fmt.Errorf("value too large: %v", s)
  247. }
  248. if f < 0 {
  249. return 0, fmt.Errorf("negative value not allowed: %v", s)
  250. }
  251. return int64(f), nil
  252. }
  253. return 0, fmt.Errorf("unhandled size name: %v", extra)
  254. }
  255. // GetIPFromRemoteAddress returns the IP from the remote address.
  256. // If the given remote address cannot be parsed it will be returned unchanged
  257. func GetIPFromRemoteAddress(remoteAddress string) string {
  258. ip, _, err := net.SplitHostPort(remoteAddress)
  259. if err == nil {
  260. return ip
  261. }
  262. return remoteAddress
  263. }
  264. // NilIfEmpty returns nil if the input string is empty
  265. func NilIfEmpty(s string) *string {
  266. if s == "" {
  267. return nil
  268. }
  269. return &s
  270. }
  271. // GetStringFromPointer returns the string value or empty if nil
  272. func GetStringFromPointer(val *string) string {
  273. if val == nil {
  274. return ""
  275. }
  276. return *val
  277. }
  278. // GetIntFromPointer returns the int value or zero
  279. func GetIntFromPointer(val *int64) int64 {
  280. if val == nil {
  281. return 0
  282. }
  283. return *val
  284. }
  285. // GetTimeFromPointer returns the time value or now
  286. func GetTimeFromPointer(val *time.Time) time.Time {
  287. if val == nil {
  288. return time.Now()
  289. }
  290. return *val
  291. }
  292. // GenerateRSAKeys generate rsa private and public keys and write the
  293. // private key to specified file and the public key to the specified
  294. // file adding the .pub suffix
  295. func GenerateRSAKeys(file string) error {
  296. if err := createDirPathIfMissing(file, 0700); err != nil {
  297. return err
  298. }
  299. key, err := rsa.GenerateKey(rand.Reader, 4096)
  300. if err != nil {
  301. return err
  302. }
  303. o, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
  304. if err != nil {
  305. return err
  306. }
  307. defer o.Close()
  308. priv := &pem.Block{
  309. Type: "RSA PRIVATE KEY",
  310. Bytes: x509.MarshalPKCS1PrivateKey(key),
  311. }
  312. if err := pem.Encode(o, priv); err != nil {
  313. return err
  314. }
  315. pub, err := ssh.NewPublicKey(&key.PublicKey)
  316. if err != nil {
  317. return err
  318. }
  319. return os.WriteFile(file+".pub", ssh.MarshalAuthorizedKey(pub), 0600)
  320. }
  321. // GenerateECDSAKeys generate ecdsa private and public keys and write the
  322. // private key to specified file and the public key to the specified
  323. // file adding the .pub suffix
  324. func GenerateECDSAKeys(file string) error {
  325. if err := createDirPathIfMissing(file, 0700); err != nil {
  326. return err
  327. }
  328. key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
  329. if err != nil {
  330. return err
  331. }
  332. keyBytes, err := x509.MarshalECPrivateKey(key)
  333. if err != nil {
  334. return err
  335. }
  336. priv := &pem.Block{
  337. Type: "EC PRIVATE KEY",
  338. Bytes: keyBytes,
  339. }
  340. o, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
  341. if err != nil {
  342. return err
  343. }
  344. defer o.Close()
  345. if err := pem.Encode(o, priv); err != nil {
  346. return err
  347. }
  348. pub, err := ssh.NewPublicKey(&key.PublicKey)
  349. if err != nil {
  350. return err
  351. }
  352. return os.WriteFile(file+".pub", ssh.MarshalAuthorizedKey(pub), 0600)
  353. }
  354. // GenerateEd25519Keys generate ed25519 private and public keys and write the
  355. // private key to specified file and the public key to the specified
  356. // file adding the .pub suffix
  357. func GenerateEd25519Keys(file string) error {
  358. pubKey, privKey, err := ed25519.GenerateKey(rand.Reader)
  359. if err != nil {
  360. return err
  361. }
  362. keyBytes, err := x509.MarshalPKCS8PrivateKey(privKey)
  363. if err != nil {
  364. return err
  365. }
  366. priv := &pem.Block{
  367. Type: "PRIVATE KEY",
  368. Bytes: keyBytes,
  369. }
  370. o, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
  371. if err != nil {
  372. return err
  373. }
  374. defer o.Close()
  375. if err := pem.Encode(o, priv); err != nil {
  376. return err
  377. }
  378. pub, err := ssh.NewPublicKey(pubKey)
  379. if err != nil {
  380. return err
  381. }
  382. return os.WriteFile(file+".pub", ssh.MarshalAuthorizedKey(pub), 0600)
  383. }
  384. // IsDirOverlapped returns true if dir1 and dir2 overlap
  385. func IsDirOverlapped(dir1, dir2 string, fullCheck bool, separator string) bool {
  386. if dir1 == dir2 {
  387. return true
  388. }
  389. if fullCheck {
  390. if len(dir1) > len(dir2) {
  391. if strings.HasPrefix(dir1, dir2+separator) {
  392. return true
  393. }
  394. }
  395. if len(dir2) > len(dir1) {
  396. if strings.HasPrefix(dir2, dir1+separator) {
  397. return true
  398. }
  399. }
  400. }
  401. return false
  402. }
  403. // GetDirsForVirtualPath returns all the directory for the given path in reverse order
  404. // for example if the path is: /1/2/3/4 it returns:
  405. // [ "/1/2/3/4", "/1/2/3", "/1/2", "/1", "/" ]
  406. func GetDirsForVirtualPath(virtualPath string) []string {
  407. if virtualPath == "" || virtualPath == "." {
  408. virtualPath = "/"
  409. } else {
  410. if !path.IsAbs(virtualPath) {
  411. virtualPath = CleanPath(virtualPath)
  412. }
  413. }
  414. dirsForPath := []string{virtualPath}
  415. for {
  416. if virtualPath == "/" {
  417. break
  418. }
  419. virtualPath = path.Dir(virtualPath)
  420. dirsForPath = append(dirsForPath, virtualPath)
  421. }
  422. return dirsForPath
  423. }
  424. // CleanPath returns a clean POSIX (/) absolute path to work with
  425. func CleanPath(p string) string {
  426. return CleanPathWithBase("/", p)
  427. }
  428. // CleanPathWithBase returns a clean POSIX (/) absolute path to work with.
  429. // The specified base will be used if the provided path is not absolute
  430. func CleanPathWithBase(base, p string) string {
  431. p = filepath.ToSlash(p)
  432. if !path.IsAbs(p) {
  433. p = path.Join(base, p)
  434. }
  435. return path.Clean(p)
  436. }
  437. // IsFileInputValid returns true this is a valid file name.
  438. // This method must be used before joining a file name, generally provided as
  439. // user input, with a directory
  440. func IsFileInputValid(fileInput string) bool {
  441. cleanInput := filepath.Clean(fileInput)
  442. if cleanInput == "." || cleanInput == ".." {
  443. return false
  444. }
  445. return true
  446. }
  447. // CleanDirInput sanitizes user input for directories.
  448. // On Windows it removes any trailing `"`.
  449. // We try to help windows users that set an invalid path such as "C:\ProgramData\SFTPGO\".
  450. // This will only help if the invalid path is the last argument, for example in this command:
  451. // sftpgo.exe serve -c "C:\ProgramData\SFTPGO\" -l "sftpgo.log"
  452. // the -l flag will be ignored and the -c flag will get the value `C:\ProgramData\SFTPGO" -l sftpgo.log`
  453. // since the backslash after SFTPGO escape the double quote. This is definitely a bad user input
  454. func CleanDirInput(dirInput string) string {
  455. if runtime.GOOS == osWindows {
  456. for strings.HasSuffix(dirInput, "\"") {
  457. dirInput = strings.TrimSuffix(dirInput, "\"")
  458. }
  459. }
  460. return filepath.Clean(dirInput)
  461. }
  462. func createDirPathIfMissing(file string, perm os.FileMode) error {
  463. dirPath := filepath.Dir(file)
  464. if _, err := os.Stat(dirPath); errors.Is(err, fs.ErrNotExist) {
  465. err = os.MkdirAll(dirPath, perm)
  466. if err != nil {
  467. return err
  468. }
  469. }
  470. return nil
  471. }
  472. // GenerateRandomBytes generates the secret to use for JWT auth
  473. func GenerateRandomBytes(length int) []byte {
  474. b := make([]byte, length)
  475. _, err := io.ReadFull(rand.Reader, b)
  476. if err == nil {
  477. return b
  478. }
  479. b = xid.New().Bytes()
  480. for len(b) < length {
  481. b = append(b, xid.New().Bytes()...)
  482. }
  483. return b[:length]
  484. }
  485. // GenerateUniqueID retuens an unique ID
  486. func GenerateUniqueID() string {
  487. u, err := uuid.NewRandom()
  488. if err != nil {
  489. return xid.New().String()
  490. }
  491. return shortuuid.DefaultEncoder.Encode(u)
  492. }
  493. // HTTPListenAndServe is a wrapper for ListenAndServe that support both tcp
  494. // and Unix-domain sockets
  495. func HTTPListenAndServe(srv *http.Server, address string, port int, isTLS bool, logSender string) error {
  496. var listener net.Listener
  497. var err error
  498. if filepath.IsAbs(address) && runtime.GOOS != osWindows {
  499. if !IsFileInputValid(address) {
  500. return fmt.Errorf("invalid socket address %#v", address)
  501. }
  502. err = createDirPathIfMissing(address, os.ModePerm)
  503. if err != nil {
  504. logger.ErrorToConsole("error creating Unix-domain socket parent dir: %v", err)
  505. logger.Error(logSender, "", "error creating Unix-domain socket parent dir: %v", err)
  506. }
  507. os.Remove(address)
  508. listener, err = newListener("unix", address, srv.ReadTimeout, srv.WriteTimeout)
  509. } else {
  510. CheckTCP4Port(port)
  511. listener, err = newListener("tcp", fmt.Sprintf("%s:%d", address, port), srv.ReadTimeout, srv.WriteTimeout)
  512. }
  513. if err != nil {
  514. return err
  515. }
  516. logger.Info(logSender, "", "server listener registered, address: %v TLS enabled: %v", listener.Addr().String(), isTLS)
  517. defer listener.Close()
  518. if isTLS {
  519. return srv.ServeTLS(listener, "", "")
  520. }
  521. return srv.Serve(listener)
  522. }
  523. // GetTLSCiphersFromNames returns the TLS ciphers from the specified names
  524. func GetTLSCiphersFromNames(cipherNames []string) []uint16 {
  525. var ciphers []uint16
  526. for _, name := range RemoveDuplicates(cipherNames, false) {
  527. for _, c := range tls.CipherSuites() {
  528. if c.Name == strings.TrimSpace(name) {
  529. ciphers = append(ciphers, c.ID)
  530. }
  531. }
  532. }
  533. return ciphers
  534. }
  535. // EncodeTLSCertToPem returns the specified certificate PEM encoded.
  536. // This can be verified using openssl x509 -in cert.crt -text -noout
  537. func EncodeTLSCertToPem(tlsCert *x509.Certificate) (string, error) {
  538. if len(tlsCert.Raw) == 0 {
  539. return "", errors.New("invalid x509 certificate, no der contents")
  540. }
  541. publicKeyBlock := pem.Block{
  542. Type: "CERTIFICATE",
  543. Bytes: tlsCert.Raw,
  544. }
  545. return string(pem.EncodeToMemory(&publicKeyBlock)), nil
  546. }
  547. // CheckTCP4Port quits the app if bind on the given IPv4 port fails.
  548. // This is a ugly hack to avoid to bind on an already used port.
  549. // It is required on Windows only. Upstream does not consider this
  550. // behaviour a bug:
  551. // https://github.com/golang/go/issues/45150
  552. func CheckTCP4Port(port int) {
  553. if runtime.GOOS != osWindows {
  554. return
  555. }
  556. listener, err := net.Listen("tcp4", fmt.Sprintf(":%d", port))
  557. if err != nil {
  558. logger.ErrorToConsole("unable to bind on tcp4 address: %v", err)
  559. logger.Error(logSender, "", "unable to bind on tcp4 address: %v", err)
  560. os.Exit(1)
  561. }
  562. listener.Close()
  563. }
  564. // IsByteArrayEmpty return true if the byte array is empty or a new line
  565. func IsByteArrayEmpty(b []byte) bool {
  566. if len(b) == 0 {
  567. return true
  568. }
  569. if bytes.Equal(b, []byte("\n")) {
  570. return true
  571. }
  572. if bytes.Equal(b, []byte("\r\n")) {
  573. return true
  574. }
  575. return false
  576. }
  577. // GetSSHPublicKeyAsString returns an SSH public key serialized as string
  578. func GetSSHPublicKeyAsString(pubKey []byte) (string, error) {
  579. if len(pubKey) == 0 {
  580. return "", nil
  581. }
  582. k, err := ssh.ParsePublicKey(pubKey)
  583. if err != nil {
  584. return "", err
  585. }
  586. return string(ssh.MarshalAuthorizedKey(k)), nil
  587. }
  588. // GetRealIP returns the ip address as result of parsing the specified
  589. // header and using the specified depth
  590. func GetRealIP(r *http.Request, header string, depth int) string {
  591. if header == "" {
  592. return ""
  593. }
  594. var ipAddresses []string
  595. for _, h := range r.Header.Values(header) {
  596. for _, ipStr := range strings.Split(h, ",") {
  597. ipStr = strings.TrimSpace(ipStr)
  598. ipAddresses = append(ipAddresses, ipStr)
  599. }
  600. }
  601. idx := len(ipAddresses) - 1 - depth
  602. if idx >= 0 {
  603. ip := strings.TrimSpace(ipAddresses[idx])
  604. if ip == "" || net.ParseIP(ip) == nil {
  605. return ""
  606. }
  607. return ip
  608. }
  609. return ""
  610. }
  611. // GetHTTPLocalAddress returns the local address for an http.Request
  612. // or empty if it cannot be determined
  613. func GetHTTPLocalAddress(r *http.Request) string {
  614. if r == nil {
  615. return ""
  616. }
  617. localAddr, ok := r.Context().Value(http.LocalAddrContextKey).(net.Addr)
  618. if ok {
  619. return localAddr.String()
  620. }
  621. return ""
  622. }
  623. // ParseAllowedIPAndRanges returns a list of functions that allow to find if an
  624. // IP is equal or is contained within the allowed list
  625. func ParseAllowedIPAndRanges(allowed []string) ([]func(net.IP) bool, error) {
  626. res := make([]func(net.IP) bool, len(allowed))
  627. for i, allowFrom := range allowed {
  628. if strings.LastIndex(allowFrom, "/") > 0 {
  629. _, ipRange, err := net.ParseCIDR(allowFrom)
  630. if err != nil {
  631. return nil, fmt.Errorf("given string %q is not a valid IP range: %v", allowFrom, err)
  632. }
  633. res[i] = ipRange.Contains
  634. } else {
  635. allowed := net.ParseIP(allowFrom)
  636. if allowed == nil {
  637. return nil, fmt.Errorf("given string %q is not a valid IP address", allowFrom)
  638. }
  639. res[i] = allowed.Equal
  640. }
  641. }
  642. return res, nil
  643. }
  644. // GetRedactedURL returns the url redacting the password if any
  645. func GetRedactedURL(rawurl string) string {
  646. if !strings.HasPrefix(rawurl, "http") {
  647. return rawurl
  648. }
  649. u, err := url.Parse(rawurl)
  650. if err != nil {
  651. return rawurl
  652. }
  653. return u.Redacted()
  654. }
  655. // PrependFileInfo prepends a file info to a slice in an efficient way.
  656. // We, optimistically, assume that the slice has enough capacity
  657. func PrependFileInfo(files []os.FileInfo, info os.FileInfo) []os.FileInfo {
  658. files = append(files, nil)
  659. copy(files[1:], files)
  660. files[0] = info
  661. return files
  662. }
  663. // GetTLSVersion returns the TLS version for integer:
  664. // - 12 means TLS 1.2
  665. // - 13 means TLS 1.3
  666. // default is TLS 1.2
  667. func GetTLSVersion(val int) uint16 {
  668. switch val {
  669. case 13:
  670. return tls.VersionTLS13
  671. default:
  672. return tls.VersionTLS12
  673. }
  674. }
  675. // IsEmailValid returns true if the specified email address is valid
  676. func IsEmailValid(email string) bool {
  677. return emailRegex.MatchString(email)
  678. }
  679. // PanicOnError calls panic if err is not nil
  680. func PanicOnError(err error) {
  681. if err != nil {
  682. panic(fmt.Errorf("unexpected error: %w", err))
  683. }
  684. }
  685. // GetAbsolutePath returns an absolute path using the current dir as base
  686. // if name defines a relative path
  687. func GetAbsolutePath(name string) (string, error) {
  688. if name == "" {
  689. return name, errors.New("input path cannot be empty")
  690. }
  691. if filepath.IsAbs(name) {
  692. return name, nil
  693. }
  694. curDir, err := os.Getwd()
  695. if err != nil {
  696. return name, err
  697. }
  698. return filepath.Join(curDir, name), nil
  699. }
  700. // GetLastIPForPrefix returns the last IP for the given prefix
  701. // https://github.com/go4org/netipx/blob/8449b0a6169f5140fb0340cb4fc0de4c9b281ef6/netipx.go#L173
  702. func GetLastIPForPrefix(p netip.Prefix) netip.Addr {
  703. if !p.IsValid() {
  704. return netip.Addr{}
  705. }
  706. a16 := p.Addr().As16()
  707. var off uint8
  708. var bits uint8 = 128
  709. if p.Addr().Is4() {
  710. off = 12
  711. bits = 32
  712. }
  713. for b := uint8(p.Bits()); b < bits; b++ {
  714. byteNum, bitInByte := b/8, 7-(b%8)
  715. a16[off+byteNum] |= 1 << uint(bitInByte)
  716. }
  717. if p.Addr().Is4() {
  718. return netip.AddrFrom16(a16).Unmap()
  719. }
  720. return netip.AddrFrom16(a16) // doesn't unmap
  721. }