util.go 25 KB

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