|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "crypto/rsa" |
| 6 | + "flag" |
| 7 | + "fmt" |
| 8 | + "github.com/RoboCup-SSL/ssl-game-controller/internal/app/rcon" |
| 9 | + "github.com/RoboCup-SSL/ssl-game-controller/internal/app/state" |
| 10 | + "github.com/RoboCup-SSL/ssl-game-controller/pkg/client" |
| 11 | + "github.com/RoboCup-SSL/ssl-go-tools/pkg/sslconn" |
| 12 | + "github.com/golang/protobuf/proto" |
| 13 | + "log" |
| 14 | + "net" |
| 15 | + "os" |
| 16 | + "strconv" |
| 17 | + "strings" |
| 18 | + "time" |
| 19 | +) |
| 20 | + |
| 21 | +var udpAddress = flag.String("udpAddress", "224.5.23.1:10003", "The multicast address of ssl-game-controller") |
| 22 | +var autoDetectAddress = flag.Bool("autoDetectHost", true, "Automatically detect the game-controller host and replace it with the host given in address") |
| 23 | +var refBoxAddr = flag.String("address", "localhost:10011", "Address to connect to") |
| 24 | +var privateKeyLocation = flag.String("privateKey", "", "A private key to be used to sign messages") |
| 25 | +var team = flag.String("team", "YELLOW", "The team to control, either YELLOW or BLUE") |
| 26 | + |
| 27 | +var privateKey *rsa.PrivateKey |
| 28 | + |
| 29 | +type Client struct { |
| 30 | + conn net.Conn |
| 31 | + token string |
| 32 | +} |
| 33 | + |
| 34 | +func main() { |
| 35 | + flag.Parse() |
| 36 | + |
| 37 | + privateKey = client.LoadPrivateKey(*privateKeyLocation) |
| 38 | + |
| 39 | + if *autoDetectAddress { |
| 40 | + log.Print("Trying to detect host based on incoming referee messages...") |
| 41 | + host := client.DetectHost(*udpAddress) |
| 42 | + if host != "" { |
| 43 | + log.Print("Detected game-controller host: ", host) |
| 44 | + *refBoxAddr = client.GetConnectionString(*refBoxAddr, host) |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + conn, err := net.Dial("tcp", *refBoxAddr) |
| 49 | + if err != nil { |
| 50 | + log.Fatal("could not connect to game-controller at ", *refBoxAddr) |
| 51 | + } |
| 52 | + defer func() { |
| 53 | + if err := conn.Close(); err != nil { |
| 54 | + log.Printf("Could not close connection: %v", err) |
| 55 | + } |
| 56 | + }() |
| 57 | + log.Printf("Connected to game-controller at %v", *refBoxAddr) |
| 58 | + c := Client{} |
| 59 | + c.conn = conn |
| 60 | + |
| 61 | + c.register() |
| 62 | + |
| 63 | + commands := map[string]func([]string){} |
| 64 | + commands["ping"] = func(args []string) { |
| 65 | + c.sendRequest(&rcon.RemoteControlToController{ |
| 66 | + Msg: &rcon.RemoteControlToController_Request_{Request: rcon.RemoteControlToController_PING}, |
| 67 | + }) |
| 68 | + } |
| 69 | + commands["keeper"] = func(args []string) { |
| 70 | + if len(args) != 1 { |
| 71 | + log.Printf("Missing keeper id") |
| 72 | + } else if id, err := strconv.Atoi(args[0]); err != nil { |
| 73 | + log.Printf("Could not parse keeper id '%v'", args[0]) |
| 74 | + } else { |
| 75 | + c.sendDesiredKeeper(int32(id)) |
| 76 | + } |
| 77 | + } |
| 78 | + commands["substitution"] = func(args []string) { |
| 79 | + c.sendRequest(&rcon.RemoteControlToController{ |
| 80 | + Msg: &rcon.RemoteControlToController_SubstituteBot{SubstituteBot: true}, |
| 81 | + }) |
| 82 | + } |
| 83 | + commands["no_substitution"] = func(args []string) { |
| 84 | + c.sendRequest(&rcon.RemoteControlToController{ |
| 85 | + Msg: &rcon.RemoteControlToController_SubstituteBot{SubstituteBot: false}, |
| 86 | + }) |
| 87 | + } |
| 88 | + commands["timeout"] = func(args []string) { |
| 89 | + c.sendRequest(&rcon.RemoteControlToController{ |
| 90 | + Msg: &rcon.RemoteControlToController_Timeout{Timeout: true}, |
| 91 | + }) |
| 92 | + } |
| 93 | + commands["no_timeout"] = func(args []string) { |
| 94 | + c.sendRequest(&rcon.RemoteControlToController{ |
| 95 | + Msg: &rcon.RemoteControlToController_Timeout{Timeout: false}, |
| 96 | + }) |
| 97 | + } |
| 98 | + commands["challenge"] = func(args []string) { |
| 99 | + c.sendRequest(&rcon.RemoteControlToController{ |
| 100 | + Msg: &rcon.RemoteControlToController_Request_{Request: rcon.RemoteControlToController_CHALLENGE_FLAG}, |
| 101 | + }) |
| 102 | + } |
| 103 | + commands["emergency"] = func(args []string) { |
| 104 | + c.sendRequest(&rcon.RemoteControlToController{ |
| 105 | + Msg: &rcon.RemoteControlToController_EmergencyStop{EmergencyStop: true}, |
| 106 | + }) |
| 107 | + } |
| 108 | + commands["no_emergency"] = func(args []string) { |
| 109 | + c.sendRequest(&rcon.RemoteControlToController{ |
| 110 | + Msg: &rcon.RemoteControlToController_EmergencyStop{EmergencyStop: false}, |
| 111 | + }) |
| 112 | + } |
| 113 | + commands["state"] = func(args []string) { |
| 114 | + c.sendRequest(&rcon.RemoteControlToController{ |
| 115 | + Msg: &rcon.RemoteControlToController_Request_{Request: rcon.RemoteControlToController_GET_STATE}, |
| 116 | + }) |
| 117 | + } |
| 118 | + |
| 119 | + reader := bufio.NewReader(os.Stdin) |
| 120 | + for { |
| 121 | + fmt.Print("-> ") |
| 122 | + text, err := reader.ReadString('\n') |
| 123 | + if err != nil { |
| 124 | + log.Print("Can not read from stdin: ", err) |
| 125 | + for { |
| 126 | + time.Sleep(1 * time.Second) |
| 127 | + } |
| 128 | + } |
| 129 | + // convert CRLF to LF |
| 130 | + text = strings.Replace(text, "\n", "", -1) |
| 131 | + cmd := strings.Split(text, " ") |
| 132 | + if fn, ok := commands[cmd[0]]; ok { |
| 133 | + fn(cmd[1:]) |
| 134 | + } else { |
| 135 | + fmt.Println("Available commands:") |
| 136 | + for cmd := range commands { |
| 137 | + fmt.Printf(" %-20s\n", cmd) |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +func (c *Client) register() { |
| 144 | + reply := rcon.ControllerToRemoteControl{} |
| 145 | + if err := sslconn.ReceiveMessage(c.conn, &reply); err != nil { |
| 146 | + log.Fatal("Failed receiving controller reply: ", err) |
| 147 | + } |
| 148 | + if reply.GetControllerReply().NextToken == nil { |
| 149 | + log.Fatal("Missing next token") |
| 150 | + } |
| 151 | + |
| 152 | + registration := rcon.RemoteControlRegistration{} |
| 153 | + registration.Team = new(state.Team) |
| 154 | + *registration.Team = state.Team(state.Team_value[*team]) |
| 155 | + if privateKey != nil { |
| 156 | + registration.Signature = &rcon.Signature{Token: reply.GetControllerReply().NextToken, Pkcs1V15: []byte{}} |
| 157 | + registration.Signature.Pkcs1V15 = client.Sign(privateKey, ®istration) |
| 158 | + } |
| 159 | + log.Print("Sending registration") |
| 160 | + if err := sslconn.SendMessage(c.conn, ®istration); err != nil { |
| 161 | + log.Fatal("Failed sending registration: ", err) |
| 162 | + } |
| 163 | + log.Print("Sent registration, waiting for reply") |
| 164 | + reply = rcon.ControllerToRemoteControl{} |
| 165 | + if err := sslconn.ReceiveMessage(c.conn, &reply); err != nil { |
| 166 | + log.Fatal("Failed receiving controller reply: ", err) |
| 167 | + } |
| 168 | + if reply.GetControllerReply().StatusCode == nil || *reply.GetControllerReply().StatusCode != rcon.ControllerReply_OK { |
| 169 | + reason := "" |
| 170 | + if reply.GetControllerReply().Reason != nil { |
| 171 | + reason = *reply.GetControllerReply().Reason |
| 172 | + } |
| 173 | + log.Fatal("Registration rejected: ", reason) |
| 174 | + } |
| 175 | + log.Printf("Successfully registered as %v", *team) |
| 176 | + if reply.GetControllerReply().NextToken != nil { |
| 177 | + c.token = *reply.GetControllerReply().NextToken |
| 178 | + } else { |
| 179 | + c.token = "" |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +func (c *Client) sendDesiredKeeper(id int32) (accepted bool) { |
| 184 | + message := rcon.RemoteControlToController_DesiredKeeper{DesiredKeeper: id} |
| 185 | + request := rcon.RemoteControlToController{Msg: &message} |
| 186 | + return c.sendRequest(&request) |
| 187 | +} |
| 188 | + |
| 189 | +func (c *Client) sendRequest(request *rcon.RemoteControlToController) (accepted bool) { |
| 190 | + if privateKey != nil { |
| 191 | + request.Signature = &rcon.Signature{Token: &c.token, Pkcs1V15: []byte{}} |
| 192 | + request.Signature.Pkcs1V15 = client.Sign(privateKey, request) |
| 193 | + } |
| 194 | + |
| 195 | + log.Print("Sending ", proto.MarshalTextString(request)) |
| 196 | + |
| 197 | + if err := sslconn.SendMessage(c.conn, request); err != nil { |
| 198 | + log.Fatalf("Failed sending request: %v (%v)", request, err) |
| 199 | + } |
| 200 | + |
| 201 | + log.Print("Waiting for reply...") |
| 202 | + reply := rcon.ControllerToRemoteControl{} |
| 203 | + if err := sslconn.ReceiveMessage(c.conn, &reply); err != nil { |
| 204 | + log.Fatal("Failed receiving controller reply: ", err) |
| 205 | + } |
| 206 | + log.Print("Received reply: ", proto.MarshalTextString(&reply)) |
| 207 | + if reply.GetControllerReply().StatusCode == nil || *reply.GetControllerReply().StatusCode != rcon.ControllerReply_OK { |
| 208 | + log.Print("Message rejected: ", *reply.GetControllerReply().Reason) |
| 209 | + accepted = false |
| 210 | + } else { |
| 211 | + accepted = true |
| 212 | + } |
| 213 | + |
| 214 | + if reply.GetControllerReply().NextToken != nil { |
| 215 | + c.token = *reply.GetControllerReply().NextToken |
| 216 | + } else { |
| 217 | + c.token = "" |
| 218 | + } |
| 219 | + |
| 220 | + return |
| 221 | +} |
0 commit comments