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