IpcSetOperation implements the WireGuard configuration protocol "set" operation. See https://www.wireguard.com/xplatform/#configuration-protocol for details.
(r io.Reader)
| 142 | // IpcSetOperation implements the WireGuard configuration protocol "set" operation. |
| 143 | // See https://www.wireguard.com/xplatform/#configuration-protocol for details. |
| 144 | func (device *Device) IpcSetOperation(r io.Reader) (err error) { |
| 145 | device.ipcMutex.Lock() |
| 146 | defer device.ipcMutex.Unlock() |
| 147 | |
| 148 | defer func() { |
| 149 | if err != nil { |
| 150 | device.Log.Errorf("%v", err) |
| 151 | } |
| 152 | }() |
| 153 | |
| 154 | peer := new(ipcSetPeer) |
| 155 | deviceConfig := true |
| 156 | |
| 157 | scanner := bufio.NewScanner(r) |
| 158 | for scanner.Scan() { |
| 159 | line := scanner.Text() |
| 160 | if line == "" { |
| 161 | // Blank line means terminate operation. |
| 162 | peer.handlePostConfig() |
| 163 | return nil |
| 164 | } |
| 165 | key, value, ok := strings.Cut(line, "=") |
| 166 | if !ok { |
| 167 | return ipcErrorf(ipc.IpcErrorProtocol, "failed to parse line %q", line) |
| 168 | } |
| 169 | |
| 170 | if key == "public_key" { |
| 171 | if deviceConfig { |
| 172 | deviceConfig = false |
| 173 | } |
| 174 | peer.handlePostConfig() |
| 175 | // Load/create the peer we are now configuring. |
| 176 | err := device.handlePublicKeyLine(peer, value) |
| 177 | if err != nil { |
| 178 | return err |
| 179 | } |
| 180 | continue |
| 181 | } |
| 182 | |
| 183 | var err error |
| 184 | if deviceConfig { |
| 185 | err = device.handleDeviceLine(key, value) |
| 186 | } else { |
| 187 | err = device.handlePeerLine(peer, key, value) |
| 188 | } |
| 189 | if err != nil { |
| 190 | return err |
| 191 | } |
| 192 | } |
| 193 | peer.handlePostConfig() |
| 194 | |
| 195 | if err := scanner.Err(); err != nil { |
| 196 | return ipcErrorf(ipc.IpcErrorIO, "failed to read input: %w", err) |
| 197 | } |
| 198 | return nil |
| 199 | } |
| 200 | |
| 201 | func (device *Device) handleDeviceLine(key, value string) error { |
no test coverage detected