util.go 23 KB

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