|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "downace/adb-helper-cli/internal/adb" |
| 6 | + "downace/adb-helper-cli/internal/mdns" |
| 7 | + "downace/adb-helper-cli/internal/ui" |
| 8 | + "errors" |
| 9 | + "fmt" |
| 10 | + "github.com/libp2p/zeroconf/v2" |
| 11 | + gonanoid "github.com/matoous/go-nanoid/v2" |
| 12 | + "github.com/skip2/go-qrcode" |
| 13 | + "github.com/spf13/cobra" |
| 14 | + "github.com/ttacon/chalk" |
| 15 | + "time" |
| 16 | +) |
| 17 | + |
| 18 | +var useQrCode bool |
| 19 | +var usePairingCode bool |
| 20 | + |
| 21 | +var pairCmd = &cobra.Command{ |
| 22 | + Use: "pair", |
| 23 | + Short: "Pair device using QR-code or pairing code", |
| 24 | + Run: pair, |
| 25 | + GroupID: cmdGroupApp, |
| 26 | +} |
| 27 | + |
| 28 | +func init() { |
| 29 | + rootCmd.AddCommand(pairCmd) |
| 30 | + |
| 31 | + pairCmd.Flags().BoolVar(&useQrCode, "qr", false, "use QR-code") |
| 32 | + pairCmd.Flags().BoolVar(&usePairingCode, "code", false, "use pairing code") |
| 33 | + pairCmd.MarkFlagsOneRequired("qr", "code") |
| 34 | + pairCmd.MarkFlagsMutuallyExclusive("qr", "code") |
| 35 | +} |
| 36 | + |
| 37 | +func pair(_ *cobra.Command, _ []string) { |
| 38 | + var err error |
| 39 | + |
| 40 | + if useQrCode { |
| 41 | + err = pairUsingQRCode() |
| 42 | + } else if usePairingCode { |
| 43 | + err = pairUsingPairingCode() |
| 44 | + } |
| 45 | + |
| 46 | + if err != nil { |
| 47 | + fmt.Println(chalk.Red.Color(err.Error())) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func pairUsingQRCode() error { |
| 52 | + name, password, err := genNameAndPassword() |
| 53 | + |
| 54 | + if err != nil { |
| 55 | + return err |
| 56 | + } |
| 57 | + |
| 58 | + err = printPairingQrCode(name, password) |
| 59 | + |
| 60 | + if err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + |
| 64 | + printPairingHelp("Pair device with QR code") |
| 65 | + |
| 66 | + pairingHost, err := discoverPairingHost() |
| 67 | + |
| 68 | + if pairingHost == nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + |
| 72 | + output, err := adb.ExecAdb("pair", pairingHost.String(), password) |
| 73 | + |
| 74 | + if err == nil { |
| 75 | + fmt.Println(chalk.Green.Color(output)) |
| 76 | + } |
| 77 | + |
| 78 | + return err |
| 79 | +} |
| 80 | + |
| 81 | +func pairUsingPairingCode() error { |
| 82 | + printPairingHelp("Pair device with pairing code") |
| 83 | + |
| 84 | + pairingHost, err := discoverPairingHost() |
| 85 | + |
| 86 | + if pairingHost == nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + |
| 90 | + code := ui.StringPrompt("Enter pairing code") |
| 91 | + |
| 92 | + var output string |
| 93 | + |
| 94 | + output, err = adb.ExecAdb("pair", pairingHost.String(), code) |
| 95 | + |
| 96 | + if err == nil { |
| 97 | + fmt.Println(chalk.Green.Color(output)) |
| 98 | + } |
| 99 | + |
| 100 | + return err |
| 101 | +} |
| 102 | + |
| 103 | +func printPairingHelp(lastSegment string) { |
| 104 | + fmt.Println(fmt.Sprintf("Go to %s -> %s -> %s", |
| 105 | + chalk.Cyan.Color("Developer options"), |
| 106 | + chalk.Cyan.Color("Wireless debugging"), |
| 107 | + chalk.Cyan.Color(lastSegment), |
| 108 | + )) |
| 109 | +} |
| 110 | + |
| 111 | +func discoverPairingHost() (*mdns.Host, error) { |
| 112 | + var pairingHost *mdns.Host |
| 113 | + |
| 114 | + mdns.DiscoverServices(time.Hour, "_adb-tls-pairing._tcp", func(entry *zeroconf.ServiceEntry, stop context.CancelFunc) { |
| 115 | + pairingHost = &mdns.Host{Addr: entry.AddrIPv4[0], Port: entry.Port} |
| 116 | + stop() |
| 117 | + }) |
| 118 | + |
| 119 | + if pairingHost == nil { |
| 120 | + return nil, errors.New("pairing failed") |
| 121 | + } |
| 122 | + |
| 123 | + return pairingHost, nil |
| 124 | +} |
| 125 | + |
| 126 | +func genNameAndPassword() (name string, password string, err error) { |
| 127 | + name = "" |
| 128 | + password = "" |
| 129 | + |
| 130 | + uid, err := gonanoid.New() |
| 131 | + if err != nil { |
| 132 | + return |
| 133 | + } |
| 134 | + name = "ADB_WIFI_" + uid |
| 135 | + uid, err = gonanoid.New() |
| 136 | + if err != nil { |
| 137 | + return |
| 138 | + } |
| 139 | + password = uid |
| 140 | + |
| 141 | + return |
| 142 | +} |
| 143 | + |
| 144 | +func printPairingQrCode(name string, password string) error { |
| 145 | + content := fmt.Sprintf("WIFI:T:ADB;S:%s;P:%s;;", name, password) |
| 146 | + |
| 147 | + qr, err := qrcode.New(content, qrcode.Low) |
| 148 | + |
| 149 | + if err != nil { |
| 150 | + return err |
| 151 | + } |
| 152 | + fmt.Println(qr.ToString(false)) |
| 153 | + |
| 154 | + return nil |
| 155 | +} |
0 commit comments