helpers.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. package exprhelpers
  2. import (
  3. "bufio"
  4. "encoding/base64"
  5. "fmt"
  6. "math"
  7. "net"
  8. "net/url"
  9. "os"
  10. "path/filepath"
  11. "regexp"
  12. "strconv"
  13. "strings"
  14. "time"
  15. "github.com/antonmedv/expr"
  16. "github.com/bluele/gcache"
  17. "github.com/c-robinson/iplib"
  18. "github.com/cespare/xxhash/v2"
  19. "github.com/davecgh/go-spew/spew"
  20. "github.com/prometheus/client_golang/prometheus"
  21. log "github.com/sirupsen/logrus"
  22. "github.com/umahmood/haversine"
  23. "github.com/wasilibs/go-re2"
  24. "github.com/crowdsecurity/go-cs-lib/ptr"
  25. "github.com/crowdsecurity/crowdsec/pkg/cache"
  26. "github.com/crowdsecurity/crowdsec/pkg/database"
  27. "github.com/crowdsecurity/crowdsec/pkg/fflag"
  28. "github.com/crowdsecurity/crowdsec/pkg/types"
  29. )
  30. var dataFile map[string][]string
  31. var dataFileRegex map[string][]*regexp.Regexp
  32. var dataFileRe2 map[string][]*re2.Regexp
  33. // This is used to (optionally) cache regexp results for RegexpInFile operations
  34. var dataFileRegexCache map[string]gcache.Cache = make(map[string]gcache.Cache)
  35. /*prometheus*/
  36. var RegexpCacheMetrics = prometheus.NewGaugeVec(
  37. prometheus.GaugeOpts{
  38. Name: "cs_regexp_cache_size",
  39. Help: "Entries per regexp cache.",
  40. },
  41. []string{"name"},
  42. )
  43. var dbClient *database.Client
  44. var exprFunctionOptions []expr.Option
  45. var keyValuePattern = regexp.MustCompile(`(?P<key>[^=\s]+)=(?:"(?P<quoted_value>[^"\\]*(?:\\.[^"\\]*)*)"|(?P<value>[^=\s]+)|\s*)`)
  46. func GetExprOptions(ctx map[string]interface{}) []expr.Option {
  47. if len(exprFunctionOptions) == 0 {
  48. exprFunctionOptions = []expr.Option{}
  49. for _, function := range exprFuncs {
  50. exprFunctionOptions = append(exprFunctionOptions,
  51. expr.Function(function.name,
  52. function.function,
  53. function.signature...,
  54. ))
  55. }
  56. }
  57. ret := []expr.Option{}
  58. ret = append(ret, exprFunctionOptions...)
  59. ret = append(ret, expr.Env(ctx))
  60. return ret
  61. }
  62. func Init(databaseClient *database.Client) error {
  63. dataFile = make(map[string][]string)
  64. dataFileRegex = make(map[string][]*regexp.Regexp)
  65. dataFileRe2 = make(map[string][]*re2.Regexp)
  66. dbClient = databaseClient
  67. return nil
  68. }
  69. func RegexpCacheInit(filename string, CacheCfg types.DataSource) error {
  70. //cache is explicitly disabled
  71. if CacheCfg.Cache != nil && !*CacheCfg.Cache {
  72. return nil
  73. }
  74. //cache is implicitly disabled if no cache config is provided
  75. if CacheCfg.Strategy == nil && CacheCfg.TTL == nil && CacheCfg.Size == nil {
  76. return nil
  77. }
  78. //cache is enabled
  79. if CacheCfg.Size == nil {
  80. CacheCfg.Size = ptr.Of(50)
  81. }
  82. gc := gcache.New(*CacheCfg.Size)
  83. if CacheCfg.Strategy == nil {
  84. CacheCfg.Strategy = ptr.Of("LRU")
  85. }
  86. switch *CacheCfg.Strategy {
  87. case "LRU":
  88. gc = gc.LRU()
  89. case "LFU":
  90. gc = gc.LFU()
  91. case "ARC":
  92. gc = gc.ARC()
  93. default:
  94. return fmt.Errorf("unknown cache strategy '%s'", *CacheCfg.Strategy)
  95. }
  96. if CacheCfg.TTL != nil {
  97. gc.Expiration(*CacheCfg.TTL)
  98. }
  99. cache := gc.Build()
  100. dataFileRegexCache[filename] = cache
  101. return nil
  102. }
  103. // UpdateCacheMetrics is called directly by the prom handler
  104. func UpdateRegexpCacheMetrics() {
  105. RegexpCacheMetrics.Reset()
  106. for name := range dataFileRegexCache {
  107. RegexpCacheMetrics.With(prometheus.Labels{"name": name}).Set(float64(dataFileRegexCache[name].Len(true)))
  108. }
  109. }
  110. func FileInit(fileFolder string, filename string, fileType string) error {
  111. log.Debugf("init (folder:%s) (file:%s) (type:%s)", fileFolder, filename, fileType)
  112. if fileType == "" {
  113. log.Debugf("ignored file %s%s because no type specified", fileFolder, filename)
  114. return nil
  115. }
  116. ok, err := existsInFileMaps(filename, fileType)
  117. if ok {
  118. log.Debugf("ignored file %s%s because already loaded", fileFolder, filename)
  119. return nil
  120. }
  121. if err != nil {
  122. return err
  123. }
  124. filepath := filepath.Join(fileFolder, filename)
  125. file, err := os.Open(filepath)
  126. if err != nil {
  127. return err
  128. }
  129. defer file.Close()
  130. scanner := bufio.NewScanner(file)
  131. for scanner.Scan() {
  132. if strings.HasPrefix(scanner.Text(), "#") { // allow comments
  133. continue
  134. }
  135. if len(scanner.Text()) == 0 { //skip empty lines
  136. continue
  137. }
  138. switch fileType {
  139. case "regex", "regexp":
  140. if fflag.Re2RegexpInfileSupport.IsEnabled() {
  141. dataFileRe2[filename] = append(dataFileRe2[filename], re2.MustCompile(scanner.Text()))
  142. continue
  143. }
  144. dataFileRegex[filename] = append(dataFileRegex[filename], regexp.MustCompile(scanner.Text()))
  145. case "string":
  146. dataFile[filename] = append(dataFile[filename], scanner.Text())
  147. }
  148. }
  149. if err := scanner.Err(); err != nil {
  150. return err
  151. }
  152. return nil
  153. }
  154. func existsInFileMaps(filename string, ftype string) (bool, error) {
  155. ok := false
  156. var err error
  157. switch ftype {
  158. case "regex", "regexp":
  159. if fflag.Re2RegexpInfileSupport.IsEnabled() {
  160. _, ok = dataFileRe2[filename]
  161. } else {
  162. _, ok = dataFileRegex[filename]
  163. }
  164. case "string":
  165. _, ok = dataFile[filename]
  166. default:
  167. err = fmt.Errorf("unknown data type '%s' for : '%s'", ftype, filename)
  168. }
  169. return ok, err
  170. }
  171. //Expr helpers
  172. // func Get(arr []string, index int) string {
  173. func Get(params ...any) (any, error) {
  174. arr := params[0].([]string)
  175. index := params[1].(int)
  176. if index >= len(arr) {
  177. return "", nil
  178. }
  179. return arr[index], nil
  180. }
  181. // func Atof(x string) float64 {
  182. func Atof(params ...any) (any, error) {
  183. x := params[0].(string)
  184. log.Debugf("debug atof %s", x)
  185. ret, err := strconv.ParseFloat(x, 64)
  186. if err != nil {
  187. log.Warningf("Atof : can't convert float '%s' : %v", x, err)
  188. }
  189. return ret, nil
  190. }
  191. // func Upper(s string) string {
  192. func Upper(params ...any) (any, error) {
  193. s := params[0].(string)
  194. return strings.ToUpper(s), nil
  195. }
  196. // func Lower(s string) string {
  197. func Lower(params ...any) (any, error) {
  198. s := params[0].(string)
  199. return strings.ToLower(s), nil
  200. }
  201. // func Distance(lat1 string, long1 string, lat2 string, long2 string) (float64, error) {
  202. func Distance(params ...any) (any, error) {
  203. lat1 := params[0].(string)
  204. long1 := params[1].(string)
  205. lat2 := params[2].(string)
  206. long2 := params[3].(string)
  207. lat1f, err := strconv.ParseFloat(lat1, 64)
  208. if err != nil {
  209. log.Warningf("lat1 is not a float : %v", err)
  210. return 0.0, fmt.Errorf("lat1 is not a float : %v", err)
  211. }
  212. long1f, err := strconv.ParseFloat(long1, 64)
  213. if err != nil {
  214. log.Warningf("long1 is not a float : %v", err)
  215. return 0.0, fmt.Errorf("long1 is not a float : %v", err)
  216. }
  217. lat2f, err := strconv.ParseFloat(lat2, 64)
  218. if err != nil {
  219. log.Warningf("lat2 is not a float : %v", err)
  220. return 0.0, fmt.Errorf("lat2 is not a float : %v", err)
  221. }
  222. long2f, err := strconv.ParseFloat(long2, 64)
  223. if err != nil {
  224. log.Warningf("long2 is not a float : %v", err)
  225. return 0.0, fmt.Errorf("long2 is not a float : %v", err)
  226. }
  227. //either set of coordinates is 0,0, return 0 to avoid FPs
  228. if (lat1f == 0.0 && long1f == 0.0) || (lat2f == 0.0 && long2f == 0.0) {
  229. log.Warningf("one of the coordinates is 0,0, returning 0")
  230. return 0.0, nil
  231. }
  232. first := haversine.Coord{Lat: lat1f, Lon: long1f}
  233. second := haversine.Coord{Lat: lat2f, Lon: long2f}
  234. _, km := haversine.Distance(first, second)
  235. return km, nil
  236. }
  237. // func QueryEscape(s string) string {
  238. func QueryEscape(params ...any) (any, error) {
  239. s := params[0].(string)
  240. return url.QueryEscape(s), nil
  241. }
  242. // func PathEscape(s string) string {
  243. func PathEscape(params ...any) (any, error) {
  244. s := params[0].(string)
  245. return url.PathEscape(s), nil
  246. }
  247. // func PathUnescape(s string) string {
  248. func PathUnescape(params ...any) (any, error) {
  249. s := params[0].(string)
  250. ret, err := url.PathUnescape(s)
  251. if err != nil {
  252. log.Debugf("unable to PathUnescape '%s': %+v", s, err)
  253. return s, nil
  254. }
  255. return ret, nil
  256. }
  257. // func QueryUnescape(s string) string {
  258. func QueryUnescape(params ...any) (any, error) {
  259. s := params[0].(string)
  260. ret, err := url.QueryUnescape(s)
  261. if err != nil {
  262. log.Debugf("unable to QueryUnescape '%s': %+v", s, err)
  263. return s, nil
  264. }
  265. return ret, nil
  266. }
  267. // func File(filename string) []string {
  268. func File(params ...any) (any, error) {
  269. filename := params[0].(string)
  270. if _, ok := dataFile[filename]; ok {
  271. return dataFile[filename], nil
  272. }
  273. log.Errorf("file '%s' (type:string) not found in expr library", filename)
  274. log.Errorf("expr library : %s", spew.Sdump(dataFile))
  275. return []string{}, nil
  276. }
  277. // func RegexpInFile(data string, filename string) bool {
  278. func RegexpInFile(params ...any) (any, error) {
  279. data := params[0].(string)
  280. filename := params[1].(string)
  281. var hash uint64
  282. hasCache := false
  283. matched := false
  284. if _, ok := dataFileRegexCache[filename]; ok {
  285. hasCache = true
  286. hash = xxhash.Sum64String(data)
  287. if val, err := dataFileRegexCache[filename].Get(hash); err == nil {
  288. return val.(bool), nil
  289. }
  290. }
  291. switch fflag.Re2RegexpInfileSupport.IsEnabled() {
  292. case true:
  293. if _, ok := dataFileRe2[filename]; ok {
  294. for _, re := range dataFileRe2[filename] {
  295. if re.MatchString(data) {
  296. matched = true
  297. break
  298. }
  299. }
  300. } else {
  301. log.Errorf("file '%s' (type:regexp) not found in expr library", filename)
  302. log.Errorf("expr library : %s", spew.Sdump(dataFileRe2))
  303. }
  304. case false:
  305. if _, ok := dataFileRegex[filename]; ok {
  306. for _, re := range dataFileRegex[filename] {
  307. if re.MatchString(data) {
  308. matched = true
  309. break
  310. }
  311. }
  312. } else {
  313. log.Errorf("file '%s' (type:regexp) not found in expr library", filename)
  314. log.Errorf("expr library : %s", spew.Sdump(dataFileRegex))
  315. }
  316. }
  317. if hasCache {
  318. dataFileRegexCache[filename].Set(hash, matched)
  319. }
  320. return matched, nil
  321. }
  322. // func IpInRange(ip string, ipRange string) bool {
  323. func IpInRange(params ...any) (any, error) {
  324. var err error
  325. var ipParsed net.IP
  326. var ipRangeParsed *net.IPNet
  327. ip := params[0].(string)
  328. ipRange := params[1].(string)
  329. ipParsed = net.ParseIP(ip)
  330. if ipParsed == nil {
  331. log.Debugf("'%s' is not a valid IP", ip)
  332. return false, nil
  333. }
  334. if _, ipRangeParsed, err = net.ParseCIDR(ipRange); err != nil {
  335. log.Debugf("'%s' is not a valid IP Range", ipRange)
  336. return false, nil //nolint:nilerr // This helper did not return an error before the move to expr.Function, we keep this behavior for backward compatibility
  337. }
  338. if ipRangeParsed.Contains(ipParsed) {
  339. return true, nil
  340. }
  341. return false, nil
  342. }
  343. // func IsIPV6(ip string) bool {
  344. func IsIPV6(params ...any) (any, error) {
  345. ip := params[0].(string)
  346. ipParsed := net.ParseIP(ip)
  347. if ipParsed == nil {
  348. log.Debugf("'%s' is not a valid IP", ip)
  349. return false, nil
  350. }
  351. // If it's a valid IP and can't be converted to IPv4 then it is an IPv6
  352. return ipParsed.To4() == nil, nil
  353. }
  354. // func IsIPV4(ip string) bool {
  355. func IsIPV4(params ...any) (any, error) {
  356. ip := params[0].(string)
  357. ipParsed := net.ParseIP(ip)
  358. if ipParsed == nil {
  359. log.Debugf("'%s' is not a valid IP", ip)
  360. return false, nil
  361. }
  362. return ipParsed.To4() != nil, nil
  363. }
  364. // func IsIP(ip string) bool {
  365. func IsIP(params ...any) (any, error) {
  366. ip := params[0].(string)
  367. ipParsed := net.ParseIP(ip)
  368. if ipParsed == nil {
  369. log.Debugf("'%s' is not a valid IP", ip)
  370. return false, nil
  371. }
  372. return true, nil
  373. }
  374. // func IpToRange(ip string, cidr string) string {
  375. func IpToRange(params ...any) (any, error) {
  376. ip := params[0].(string)
  377. cidr := params[1].(string)
  378. cidr = strings.TrimPrefix(cidr, "/")
  379. mask, err := strconv.Atoi(cidr)
  380. if err != nil {
  381. log.Errorf("bad cidr '%s': %s", cidr, err)
  382. return "", nil
  383. }
  384. ipAddr := net.ParseIP(ip)
  385. if ipAddr == nil {
  386. log.Errorf("can't parse IP address '%s'", ip)
  387. return "", nil
  388. }
  389. ipRange := iplib.NewNet(ipAddr, mask)
  390. if ipRange.IP() == nil {
  391. log.Errorf("can't get cidr '%s' of '%s'", cidr, ip)
  392. return "", nil
  393. }
  394. return ipRange.String(), nil
  395. }
  396. // func TimeNow() string {
  397. func TimeNow(params ...any) (any, error) {
  398. return time.Now().UTC().Format(time.RFC3339), nil
  399. }
  400. // func ParseUri(uri string) map[string][]string {
  401. func ParseUri(params ...any) (any, error) {
  402. uri := params[0].(string)
  403. ret := make(map[string][]string)
  404. u, err := url.Parse(uri)
  405. if err != nil {
  406. log.Errorf("Could not parse URI: %s", err)
  407. return ret, nil
  408. }
  409. parsed, err := url.ParseQuery(u.RawQuery)
  410. if err != nil {
  411. log.Errorf("Could not parse query uri : %s", err)
  412. return ret, nil
  413. }
  414. for k, v := range parsed {
  415. ret[k] = v
  416. }
  417. return ret, nil
  418. }
  419. // func KeyExists(key string, dict map[string]interface{}) bool {
  420. func KeyExists(params ...any) (any, error) {
  421. key := params[0].(string)
  422. dict := params[1].(map[string]interface{})
  423. _, ok := dict[key]
  424. return ok, nil
  425. }
  426. // func GetDecisionsCount(value string) int {
  427. func GetDecisionsCount(params ...any) (any, error) {
  428. value := params[0].(string)
  429. if dbClient == nil {
  430. log.Error("No database config to call GetDecisionsCount()")
  431. return 0, nil
  432. }
  433. count, err := dbClient.CountDecisionsByValue(value)
  434. if err != nil {
  435. log.Errorf("Failed to get decisions count from value '%s'", value)
  436. return 0, nil //nolint:nilerr // This helper did not return an error before the move to expr.Function, we keep this behavior for backward compatibility
  437. }
  438. return count, nil
  439. }
  440. // func GetDecisionsSinceCount(value string, since string) int {
  441. func GetDecisionsSinceCount(params ...any) (any, error) {
  442. value := params[0].(string)
  443. since := params[1].(string)
  444. if dbClient == nil {
  445. log.Error("No database config to call GetDecisionsCount()")
  446. return 0, nil
  447. }
  448. sinceDuration, err := time.ParseDuration(since)
  449. if err != nil {
  450. log.Errorf("Failed to parse since parameter '%s' : %s", since, err)
  451. return 0, nil
  452. }
  453. sinceTime := time.Now().UTC().Add(-sinceDuration)
  454. count, err := dbClient.CountDecisionsSinceByValue(value, sinceTime)
  455. if err != nil {
  456. log.Errorf("Failed to get decisions count from value '%s'", value)
  457. return 0, nil //nolint:nilerr // This helper did not return an error before the move to expr.Function, we keep this behavior for backward compatibility
  458. }
  459. return count, nil
  460. }
  461. // func LookupHost(value string) []string {
  462. func LookupHost(params ...any) (any, error) {
  463. value := params[0].(string)
  464. addresses, err := net.LookupHost(value)
  465. if err != nil {
  466. log.Errorf("Failed to lookup host '%s' : %s", value, err)
  467. return []string{}, nil
  468. }
  469. return addresses, nil
  470. }
  471. // func ParseUnixTime(value string) (time.Time, error) {
  472. func ParseUnixTime(params ...any) (any, error) {
  473. value := params[0].(string)
  474. //Splitting string here as some unix timestamp may have milliseconds and break ParseInt
  475. i, err := strconv.ParseInt(strings.Split(value, ".")[0], 10, 64)
  476. if err != nil || i <= 0 {
  477. return time.Time{}, fmt.Errorf("unable to parse %s as unix timestamp", value)
  478. }
  479. return time.Unix(i, 0), nil
  480. }
  481. // func ParseUnix(value string) string {
  482. func ParseUnix(params ...any) (any, error) {
  483. value := params[0].(string)
  484. t, err := ParseUnixTime(value)
  485. if err != nil {
  486. log.Error(err)
  487. return "", nil
  488. }
  489. return t.(time.Time).Format(time.RFC3339), nil
  490. }
  491. // func ToString(value interface{}) string {
  492. func ToString(params ...any) (any, error) {
  493. value := params[0]
  494. s, ok := value.(string)
  495. if !ok {
  496. return "", nil
  497. }
  498. return s, nil
  499. }
  500. // func GetFromStash(cacheName string, key string) (string, error) {
  501. func GetFromStash(params ...any) (any, error) {
  502. cacheName := params[0].(string)
  503. key := params[1].(string)
  504. return cache.GetKey(cacheName, key)
  505. }
  506. // func SetInStash(cacheName string, key string, value string, expiration *time.Duration) any {
  507. func SetInStash(params ...any) (any, error) {
  508. cacheName := params[0].(string)
  509. key := params[1].(string)
  510. value := params[2].(string)
  511. expiration := params[3].(*time.Duration)
  512. return cache.SetKey(cacheName, key, value, expiration), nil
  513. }
  514. func Sprintf(params ...any) (any, error) {
  515. format := params[0].(string)
  516. return fmt.Sprintf(format, params[1:]...), nil
  517. }
  518. // func Match(pattern, name string) bool {
  519. func Match(params ...any) (any, error) {
  520. var matched bool
  521. pattern := params[0].(string)
  522. name := params[1].(string)
  523. if pattern == "" {
  524. return name == "", nil
  525. }
  526. if name == "" {
  527. if pattern == "*" || pattern == "" {
  528. return true, nil
  529. }
  530. return false, nil
  531. }
  532. if pattern[0] == '*' {
  533. for i := 0; i <= len(name); i++ {
  534. matched, _ := Match(pattern[1:], name[i:])
  535. if matched.(bool) {
  536. return matched, nil
  537. }
  538. }
  539. return matched, nil
  540. }
  541. if pattern[0] == '?' || pattern[0] == name[0] {
  542. return Match(pattern[1:], name[1:])
  543. }
  544. return matched, nil
  545. }
  546. func FloatApproxEqual(params ...any) (any, error) {
  547. float1 := params[0].(float64)
  548. float2 := params[1].(float64)
  549. if math.Abs(float1-float2) < 1e-6 {
  550. return true, nil
  551. }
  552. return false, nil
  553. }
  554. func B64Decode(params ...any) (any, error) {
  555. encoded := params[0].(string)
  556. decoded, err := base64.StdEncoding.DecodeString(encoded)
  557. if err != nil {
  558. return "", err
  559. }
  560. return string(decoded), nil
  561. }
  562. func ParseKV(params ...any) (any, error) {
  563. blob := params[0].(string)
  564. target := params[1].(map[string]interface{})
  565. prefix := params[2].(string)
  566. matches := keyValuePattern.FindAllStringSubmatch(blob, -1)
  567. if matches == nil {
  568. log.Errorf("could not find any key/value pair in line")
  569. return nil, fmt.Errorf("invalid input format")
  570. }
  571. if _, ok := target[prefix]; !ok {
  572. target[prefix] = make(map[string]string)
  573. } else {
  574. _, ok := target[prefix].(map[string]string)
  575. if !ok {
  576. log.Errorf("ParseKV: target is not a map[string]string")
  577. return nil, fmt.Errorf("target is not a map[string]string")
  578. }
  579. }
  580. for _, match := range matches {
  581. key := ""
  582. value := ""
  583. for i, name := range keyValuePattern.SubexpNames() {
  584. if name == "key" {
  585. key = match[i]
  586. } else if name == "quoted_value" && match[i] != "" {
  587. value = match[i]
  588. } else if name == "value" && match[i] != "" {
  589. value = match[i]
  590. }
  591. }
  592. target[prefix].(map[string]string)[key] = value
  593. }
  594. log.Tracef("unmarshaled KV: %+v", target[prefix])
  595. return nil, nil
  596. }
  597. func Hostname(params ...any) (any, error) {
  598. hostname, err := os.Hostname()
  599. if err != nil {
  600. return "", err
  601. }
  602. return hostname, nil
  603. }