utils.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. // Package utils provides some common utility methods
  2. package utils
  3. import (
  4. "crypto/aes"
  5. "crypto/cipher"
  6. "crypto/ecdsa"
  7. "crypto/elliptic"
  8. "crypto/rand"
  9. "crypto/rsa"
  10. "crypto/x509"
  11. "encoding/hex"
  12. "encoding/pem"
  13. "errors"
  14. "fmt"
  15. "html/template"
  16. "io"
  17. "io/ioutil"
  18. "net"
  19. "os"
  20. "path"
  21. "path/filepath"
  22. "runtime"
  23. "strings"
  24. "time"
  25. "golang.org/x/crypto/ssh"
  26. "github.com/drakkan/sftpgo/logger"
  27. )
  28. const logSender = "utils"
  29. // IsStringInSlice searches a string in a slice and returns true if the string is found
  30. func IsStringInSlice(obj string, list []string) bool {
  31. for _, v := range list {
  32. if v == obj {
  33. return true
  34. }
  35. }
  36. return false
  37. }
  38. // IsStringPrefixInSlice searches a string prefix in a slice and returns true
  39. // if a matching prefix is found
  40. func IsStringPrefixInSlice(obj string, list []string) bool {
  41. for _, v := range list {
  42. if strings.HasPrefix(obj, v) {
  43. return true
  44. }
  45. }
  46. return false
  47. }
  48. // GetTimeAsMsSinceEpoch returns unix timestamp as milliseconds from a time struct
  49. func GetTimeAsMsSinceEpoch(t time.Time) int64 {
  50. return t.UnixNano() / 1000000
  51. }
  52. // GetTimeFromMsecSinceEpoch return a time struct from a unix timestamp with millisecond precision
  53. func GetTimeFromMsecSinceEpoch(msec int64) time.Time {
  54. return time.Unix(0, msec*1000000)
  55. }
  56. // GetAppVersion returns VersionInfo struct
  57. func GetAppVersion() VersionInfo {
  58. return versionInfo
  59. }
  60. // GetDurationAsString returns a string representation for a time.Duration
  61. func GetDurationAsString(d time.Duration) string {
  62. d = d.Round(time.Second)
  63. h := d / time.Hour
  64. d -= h * time.Hour
  65. m := d / time.Minute
  66. d -= m * time.Minute
  67. s := d / time.Second
  68. if h > 0 {
  69. return fmt.Sprintf("%02d:%02d:%02d", h, m, s)
  70. }
  71. return fmt.Sprintf("%02d:%02d", m, s)
  72. }
  73. // ByteCountSI returns humanized size in SI (decimal) format
  74. func ByteCountSI(b int64) string {
  75. return byteCount(b, 1000)
  76. }
  77. // ByteCountIEC returns humanized size in IEC (binary) format
  78. func ByteCountIEC(b int64) string {
  79. return byteCount(b, 1024)
  80. }
  81. func byteCount(b int64, unit int64) string {
  82. if b < unit {
  83. return fmt.Sprintf("%d B", b)
  84. }
  85. div, exp := unit, 0
  86. for n := b / unit; n >= unit; n /= unit {
  87. div *= unit
  88. exp++
  89. }
  90. if unit == 1000 {
  91. return fmt.Sprintf("%.1f %cB",
  92. float64(b)/float64(div), "KMGTPE"[exp])
  93. }
  94. return fmt.Sprintf("%.1f %ciB",
  95. float64(b)/float64(div), "KMGTPE"[exp])
  96. }
  97. // GetIPFromRemoteAddress returns the IP from the remote address.
  98. // If the given remote address cannot be parsed it will be returned unchanged
  99. func GetIPFromRemoteAddress(remoteAddress string) string {
  100. ip, _, err := net.SplitHostPort(remoteAddress)
  101. if err == nil {
  102. return ip
  103. }
  104. return remoteAddress
  105. }
  106. // NilIfEmpty returns nil if the input string is empty
  107. func NilIfEmpty(s string) *string {
  108. if len(s) == 0 {
  109. return nil
  110. }
  111. return &s
  112. }
  113. // EncryptData encrypts data using the given key
  114. func EncryptData(data string) (string, error) {
  115. var result string
  116. key := make([]byte, 16)
  117. if _, err := io.ReadFull(rand.Reader, key); err != nil {
  118. return result, err
  119. }
  120. keyHex := hex.EncodeToString(key)
  121. block, err := aes.NewCipher([]byte(keyHex))
  122. if err != nil {
  123. return result, err
  124. }
  125. gcm, err := cipher.NewGCM(block)
  126. if err != nil {
  127. return result, err
  128. }
  129. nonce := make([]byte, gcm.NonceSize())
  130. if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
  131. return result, err
  132. }
  133. ciphertext := gcm.Seal(nonce, nonce, []byte(data), nil)
  134. result = fmt.Sprintf("$aes$%s$%x", keyHex, ciphertext)
  135. return result, err
  136. }
  137. // RemoveDecryptionKey returns encrypted data without the decryption key
  138. func RemoveDecryptionKey(encryptData string) string {
  139. vals := strings.Split(encryptData, "$")
  140. if len(vals) == 4 {
  141. return fmt.Sprintf("$%v$%v", vals[1], vals[3])
  142. }
  143. return encryptData
  144. }
  145. // DecryptData decrypts data encrypted using EncryptData
  146. func DecryptData(data string) (string, error) {
  147. var result string
  148. vals := strings.Split(data, "$")
  149. if len(vals) != 4 {
  150. return "", errors.New("data to decrypt is not in the correct format")
  151. }
  152. key := vals[2]
  153. encrypted, err := hex.DecodeString(vals[3])
  154. if err != nil {
  155. return result, err
  156. }
  157. block, err := aes.NewCipher([]byte(key))
  158. if err != nil {
  159. return result, err
  160. }
  161. gcm, err := cipher.NewGCM(block)
  162. if err != nil {
  163. return result, err
  164. }
  165. nonceSize := gcm.NonceSize()
  166. nonce, ciphertext := encrypted[:nonceSize], encrypted[nonceSize:]
  167. plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)
  168. if err != nil {
  169. return result, err
  170. }
  171. return string(plaintext), nil
  172. }
  173. // GenerateRSAKeys generate rsa private and public keys and write the
  174. // private key to specified file and the public key to the specified
  175. // file adding the .pub suffix
  176. func GenerateRSAKeys(file string) error {
  177. key, err := rsa.GenerateKey(rand.Reader, 4096)
  178. if err != nil {
  179. return err
  180. }
  181. o, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
  182. if err != nil {
  183. return err
  184. }
  185. defer o.Close()
  186. priv := &pem.Block{
  187. Type: "RSA PRIVATE KEY",
  188. Bytes: x509.MarshalPKCS1PrivateKey(key),
  189. }
  190. if err := pem.Encode(o, priv); err != nil {
  191. return err
  192. }
  193. pub, err := ssh.NewPublicKey(&key.PublicKey)
  194. if err != nil {
  195. return err
  196. }
  197. return ioutil.WriteFile(file+".pub", ssh.MarshalAuthorizedKey(pub), 0600)
  198. }
  199. // GenerateECDSAKeys generate ecdsa private and public keys and write the
  200. // private key to specified file and the public key to the specified
  201. // file adding the .pub suffix
  202. func GenerateECDSAKeys(file string) error {
  203. key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
  204. if err != nil {
  205. return err
  206. }
  207. o, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
  208. if err != nil {
  209. return err
  210. }
  211. defer o.Close()
  212. keyBytes, err := x509.MarshalECPrivateKey(key)
  213. if err != nil {
  214. return err
  215. }
  216. priv := &pem.Block{
  217. Type: "EC PRIVATE KEY",
  218. Bytes: keyBytes,
  219. }
  220. if err := pem.Encode(o, priv); err != nil {
  221. return err
  222. }
  223. pub, err := ssh.NewPublicKey(&key.PublicKey)
  224. if err != nil {
  225. return err
  226. }
  227. return ioutil.WriteFile(file+".pub", ssh.MarshalAuthorizedKey(pub), 0600)
  228. }
  229. // GetDirsForSFTPPath returns all the directory for the given path in reverse order
  230. // for example if the path is: /1/2/3/4 it returns:
  231. // [ "/1/2/3/4", "/1/2/3", "/1/2", "/1", "/" ]
  232. func GetDirsForSFTPPath(p string) []string {
  233. sftpPath := CleanSFTPPath(p)
  234. dirsForPath := []string{sftpPath}
  235. for {
  236. if sftpPath == "/" {
  237. break
  238. }
  239. sftpPath = path.Dir(sftpPath)
  240. dirsForPath = append(dirsForPath, sftpPath)
  241. }
  242. return dirsForPath
  243. }
  244. // CleanSFTPPath returns a clean sftp path
  245. func CleanSFTPPath(p string) string {
  246. sftpPath := filepath.ToSlash(p)
  247. if !path.IsAbs(p) {
  248. sftpPath = "/" + sftpPath
  249. }
  250. return path.Clean(sftpPath)
  251. }
  252. // LoadTemplate wraps a call to a function returning (*Template, error)
  253. // it is just like template.Must but it writes a log before exiting
  254. func LoadTemplate(t *template.Template, err error) *template.Template {
  255. if err != nil {
  256. logger.ErrorToConsole("error loading required template: %v", err)
  257. logger.Error(logSender, "", "error loading required template: %v", err)
  258. panic(err)
  259. }
  260. return t
  261. }
  262. // IsFileInputValid returns true this is a valid file name.
  263. // This method must be used before joining a file name, generally provided as
  264. // user input, with a directory
  265. func IsFileInputValid(fileInput string) bool {
  266. cleanInput := filepath.Clean(fileInput)
  267. if cleanInput == "." || cleanInput == ".." {
  268. return false
  269. }
  270. return true
  271. }
  272. // CleanDirInput sanitizes user input for directories.
  273. // On Windows it removes any trailing `"`.
  274. // We try to help windows users that set an invalid path such as "C:\ProgramData\SFTPGO\".
  275. // This will only help if the invalid path is the last argument, for example in this command:
  276. // sftpgo.exe serve -c "C:\ProgramData\SFTPGO\" -l "sftpgo.log"
  277. // the -l flag will be ignored and the -c flag will get the value `C:\ProgramData\SFTPGO" -l sftpgo.log`
  278. // since the backslash after SFTPGO escape the double quote. This is definitely a bad user input
  279. func CleanDirInput(dirInput string) string {
  280. if runtime.GOOS == "windows" {
  281. for strings.HasSuffix(dirInput, "\"") {
  282. dirInput = strings.TrimSuffix(dirInput, "\"")
  283. }
  284. }
  285. return filepath.Clean(dirInput)
  286. }