|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "flag" |
| 5 | + "fmt" |
| 6 | + "io/ioutil" |
| 7 | + "net" |
| 8 | + "os" |
| 9 | + "os/exec" |
| 10 | + "path/filepath" |
| 11 | + "strconv" |
| 12 | + "strings" |
| 13 | + "syscall" |
| 14 | + "time" |
| 15 | +) |
| 16 | + |
| 17 | +func client(config Config) { |
| 18 | + |
| 19 | + // open send port |
| 20 | + writeSocket := openUDPSocket(`w`, net.UDPAddr{ |
| 21 | + IP: net.IPv4(255, 255, 255, 255), // (broadcast IPv4) |
| 22 | + Port: config.Port, |
| 23 | + }) |
| 24 | + defer writeSocket.Close() |
| 25 | + |
| 26 | + // open receive port on send port + 1 |
| 27 | + _, porttxt, _ := net.SplitHostPort(writeSocket.LocalAddr().String()) |
| 28 | + port, _ := strconv.Atoi(porttxt) |
| 29 | + readSocket := openUDPSocket(`r`, net.UDPAddr{ |
| 30 | + IP: net.IPv4(0, 0, 0, 0), |
| 31 | + Port: port + 1, |
| 32 | + }) |
| 33 | + defer readSocket.Close() |
| 34 | + |
| 35 | + // listen for reply: first start 2 channels: dataCh, and errCh |
| 36 | + data, errors := make(chan []byte), make(chan error) |
| 37 | + go func(dataCh chan []byte, errCh chan error) { |
| 38 | + keyData := make([]byte, UDP_MSG_SIZE) |
| 39 | + n, _, err := readSocket.ReadFromUDP(keyData) |
| 40 | + if err != nil { |
| 41 | + errCh <- err |
| 42 | + } |
| 43 | + dataCh <- keyData[0:n] |
| 44 | + }(data, errors) |
| 45 | + |
| 46 | + // send key request |
| 47 | + fmt.Println("Broadcasting UDP key request...") |
| 48 | + _, err := writeSocket.Write([]byte(KEY_REQUEST_TEXT)) |
| 49 | + if err != nil { |
| 50 | + fmt.Println("ERROR sending key request: ", err) |
| 51 | + os.Exit(101) |
| 52 | + } |
| 53 | + |
| 54 | + // now start the timeout channel |
| 55 | + timeout := make(chan bool, 1) |
| 56 | + go func() { |
| 57 | + time.Sleep(time.Duration(config.Wait) * time.Second) |
| 58 | + timeout <- true |
| 59 | + }() |
| 60 | + |
| 61 | + // now wait for a reply, an error, or a timeout |
| 62 | + reply := `` |
| 63 | + select { |
| 64 | + case bytes := <-data: |
| 65 | + reply = string(bytes) |
| 66 | + if strings.HasPrefix(reply, `ERROR`) == true { |
| 67 | + fmt.Println("Received error from server:", reply) |
| 68 | + os.Exit(102) |
| 69 | + } |
| 70 | + fmt.Println("Got key from server.") |
| 71 | + case err := <-errors: |
| 72 | + fmt.Println("Error reading from receive port:", err) |
| 73 | + os.Exit(103) |
| 74 | + case <-timeout: |
| 75 | + fmt.Println("WARNING: timed out waiting for response from key server.") |
| 76 | + } |
| 77 | + |
| 78 | + // create key dir and file |
| 79 | + keyWritten := false // keep track of whether the key was written |
| 80 | + if reply != `` { |
| 81 | + fmt.Printf("Writing key to %s\n", config.KeyPath) |
| 82 | + err = os.MkdirAll(filepath.Dir(config.KeyPath), 0700) |
| 83 | + if err != nil { |
| 84 | + fmt.Printf("ERROR creating directory %s: %s\n", config.KeyPath, err) |
| 85 | + os.Exit(104) |
| 86 | + } |
| 87 | + err = ioutil.WriteFile(config.KeyPath, []byte(reply), 0600) |
| 88 | + if err != nil { |
| 89 | + fmt.Printf("ERROR writing keyfile %s: %s\n", config.KeyPath, err) |
| 90 | + os.Exit(105) |
| 91 | + } |
| 92 | + keyWritten = true |
| 93 | + } |
| 94 | + // defer close and deletion of keyfile |
| 95 | + // from here on, set exitCode and call return instead of os.Exit() |
| 96 | + exitCode := 0 |
| 97 | + defer func() { |
| 98 | + if keyWritten == true { |
| 99 | + fmt.Printf("Deleting key file %s...\n", config.KeyPath) |
| 100 | + if err := os.Remove(config.KeyPath); err != nil { |
| 101 | + fmt.Printf("ERROR deleting keyfile '%s': %v\n", |
| 102 | + config.KeyPath, err) |
| 103 | + exitCode = 106 |
| 104 | + return |
| 105 | + } |
| 106 | + } |
| 107 | + if exitCode != 0 { |
| 108 | + os.Exit(exitCode) |
| 109 | + } |
| 110 | + }() |
| 111 | + |
| 112 | + // run command |
| 113 | + cmd := exec.Command(flag.Arg(0), flag.Args()[1:]...) |
| 114 | + cmdText := strings.Join(flag.Args(), " ") |
| 115 | + cmd.Stdin = os.Stdin |
| 116 | + cmd.Stdout = os.Stdout |
| 117 | + cmd.Stderr = os.Stderr |
| 118 | + fmt.Println("Running command:", cmdText) |
| 119 | + if err := cmd.Start(); err != nil { |
| 120 | + fmt.Printf("ERROR starting command '%s': %v\n", cmdText, err) |
| 121 | + exitCode = 107 |
| 122 | + return |
| 123 | + } |
| 124 | + |
| 125 | + if err = cmd.Wait(); err != nil { |
| 126 | + if exiterr, ok := err.(*exec.ExitError); ok { |
| 127 | + // The program has exited with an exit code != 0 |
| 128 | + |
| 129 | + // This works on both Unix and Windows. Although package |
| 130 | + // syscall is generally platform dependent, WaitStatus is |
| 131 | + // defined for both Unix and Windows and in both cases has |
| 132 | + // an ExitStatus() method with the same signature. |
| 133 | + if status, ok := exiterr.Sys().(syscall.WaitStatus); ok { |
| 134 | + exitCode = status.ExitStatus() |
| 135 | + fmt.Printf("ERROR: command '%s' exited with status %d\n", |
| 136 | + cmdText, exitCode) |
| 137 | + } else { |
| 138 | + fmt.Printf("ERROR: command '%s' exited with unknown status", |
| 139 | + cmdText) |
| 140 | + exitCode = 108 // problem getting command's exit status? |
| 141 | + } |
| 142 | + return |
| 143 | + } else { |
| 144 | + fmt.Printf("ERROR waiting on command '%s': %v\n", cmdText, err) |
| 145 | + exitCode = 109 |
| 146 | + return |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + fmt.Println("Command completed successfully.") |
| 151 | +} |
0 commit comments