detect.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2015 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. //go:build ignore
  5. // +build ignore
  6. // detect attempts to autodetect the correct
  7. // values of the environment variables
  8. // used by go_ios_exec.
  9. // detect shells out to ideviceinfo, a third party program that can
  10. // be obtained by following the instructions at
  11. // https://github.com/libimobiledevice/libimobiledevice.
  12. package main
  13. import (
  14. "bytes"
  15. "crypto/x509"
  16. "fmt"
  17. "os"
  18. "os/exec"
  19. "strings"
  20. )
  21. func main() {
  22. udids := getLines(exec.Command("idevice_id", "-l"))
  23. if len(udids) == 0 {
  24. fail("no udid found; is a device connected?")
  25. }
  26. mps := detectMobileProvisionFiles(udids)
  27. if len(mps) == 0 {
  28. fail("did not find mobile provision matching device udids %q", udids)
  29. }
  30. fmt.Println("# Available provisioning profiles below.")
  31. fmt.Println("# NOTE: Any existing app on the device with the app id specified by GOIOS_APP_ID")
  32. fmt.Println("# will be overwritten when running Go programs.")
  33. for _, mp := range mps {
  34. fmt.Println()
  35. f, err := os.CreateTemp("", "go_ios_detect_")
  36. check(err)
  37. fname := f.Name()
  38. defer os.Remove(fname)
  39. out := output(parseMobileProvision(mp))
  40. _, err = f.Write(out)
  41. check(err)
  42. check(f.Close())
  43. cert, err := plistExtract(fname, "DeveloperCertificates:0")
  44. check(err)
  45. pcert, err := x509.ParseCertificate(cert)
  46. check(err)
  47. fmt.Printf("export GOIOS_DEV_ID=\"%s\"\n", pcert.Subject.CommonName)
  48. appID, err := plistExtract(fname, "Entitlements:application-identifier")
  49. check(err)
  50. fmt.Printf("export GOIOS_APP_ID=%s\n", appID)
  51. teamID, err := plistExtract(fname, "Entitlements:com.apple.developer.team-identifier")
  52. check(err)
  53. fmt.Printf("export GOIOS_TEAM_ID=%s\n", teamID)
  54. }
  55. }
  56. func detectMobileProvisionFiles(udids [][]byte) []string {
  57. cmd := exec.Command("mdfind", "-name", ".mobileprovision")
  58. lines := getLines(cmd)
  59. var files []string
  60. for _, line := range lines {
  61. if len(line) == 0 {
  62. continue
  63. }
  64. xmlLines := getLines(parseMobileProvision(string(line)))
  65. matches := 0
  66. for _, udid := range udids {
  67. for _, xmlLine := range xmlLines {
  68. if bytes.Contains(xmlLine, udid) {
  69. matches++
  70. }
  71. }
  72. }
  73. if matches == len(udids) {
  74. files = append(files, string(line))
  75. }
  76. }
  77. return files
  78. }
  79. func parseMobileProvision(fname string) *exec.Cmd {
  80. return exec.Command("security", "cms", "-D", "-i", string(fname))
  81. }
  82. func plistExtract(fname string, path string) ([]byte, error) {
  83. out, err := exec.Command("/usr/libexec/PlistBuddy", "-c", "Print "+path, fname).CombinedOutput()
  84. if err != nil {
  85. return nil, err
  86. }
  87. return bytes.TrimSpace(out), nil
  88. }
  89. func getLines(cmd *exec.Cmd) [][]byte {
  90. out := output(cmd)
  91. lines := bytes.Split(out, []byte("\n"))
  92. // Skip the empty line at the end.
  93. if len(lines[len(lines)-1]) == 0 {
  94. lines = lines[:len(lines)-1]
  95. }
  96. return lines
  97. }
  98. func output(cmd *exec.Cmd) []byte {
  99. out, err := cmd.Output()
  100. if err != nil {
  101. fmt.Println(strings.Join(cmd.Args, "\n"))
  102. fmt.Fprintln(os.Stderr, err)
  103. os.Exit(1)
  104. }
  105. return out
  106. }
  107. func check(err error) {
  108. if err != nil {
  109. fail(err.Error())
  110. }
  111. }
  112. func fail(msg string, v ...interface{}) {
  113. fmt.Fprintf(os.Stderr, msg, v...)
  114. fmt.Fprintln(os.Stderr)
  115. os.Exit(1)
  116. }