IpcGetOperation implements the WireGuard configuration protocol "get" operation. See https://www.wireguard.com/xplatform/#configuration-protocol for details.
(w io.Writer)
| 49 | // IpcGetOperation implements the WireGuard configuration protocol "get" operation. |
| 50 | // See https://www.wireguard.com/xplatform/#configuration-protocol for details. |
| 51 | func (device *Device) IpcGetOperation(w io.Writer) error { |
| 52 | device.ipcMutex.RLock() |
| 53 | defer device.ipcMutex.RUnlock() |
| 54 | |
| 55 | buf := byteBufferPool.Get().(*bytes.Buffer) |
| 56 | buf.Reset() |
| 57 | defer byteBufferPool.Put(buf) |
| 58 | sendf := func(format string, args ...any) { |
| 59 | fmt.Fprintf(buf, format, args...) |
| 60 | buf.WriteByte('\n') |
| 61 | } |
| 62 | keyf := func(prefix string, key *[32]byte) { |
| 63 | buf.Grow(len(key)*2 + 2 + len(prefix)) |
| 64 | buf.WriteString(prefix) |
| 65 | buf.WriteByte('=') |
| 66 | const hex = "0123456789abcdef" |
| 67 | for i := 0; i < len(key); i++ { |
| 68 | buf.WriteByte(hex[key[i]>>4]) |
| 69 | buf.WriteByte(hex[key[i]&0xf]) |
| 70 | } |
| 71 | buf.WriteByte('\n') |
| 72 | } |
| 73 | |
| 74 | func() { |
| 75 | // lock required resources |
| 76 | |
| 77 | device.net.RLock() |
| 78 | defer device.net.RUnlock() |
| 79 | |
| 80 | device.staticIdentity.RLock() |
| 81 | defer device.staticIdentity.RUnlock() |
| 82 | |
| 83 | device.peers.RLock() |
| 84 | defer device.peers.RUnlock() |
| 85 | |
| 86 | // serialize device related values |
| 87 | |
| 88 | if !device.staticIdentity.privateKey.IsZero() { |
| 89 | keyf("private_key", (*[32]byte)(&device.staticIdentity.privateKey)) |
| 90 | } |
| 91 | |
| 92 | if device.net.port != 0 { |
| 93 | sendf("listen_port=%d", device.net.port) |
| 94 | } |
| 95 | |
| 96 | if device.net.fwmark != 0 { |
| 97 | sendf("fwmark=%d", device.net.fwmark) |
| 98 | } |
| 99 | |
| 100 | for _, peer := range device.peers.keyMap { |
| 101 | // Serialize peer state. |
| 102 | peer.handshake.mutex.RLock() |
| 103 | keyf("public_key", (*[32]byte)(&peer.handshake.remoteStatic)) |
| 104 | keyf("preshared_key", (*[32]byte)(&peer.handshake.presharedKey)) |
| 105 | peer.handshake.mutex.RUnlock() |
| 106 | sendf("protocol_version=1") |
| 107 | peer.endpoint.Lock() |
| 108 | if peer.endpoint.val != nil { |