CreateIPCRequest serialize the config into an IPC request and DeviceSetting
(conf *DeviceConfig)
| 23 | |
| 24 | // CreateIPCRequest serialize the config into an IPC request and DeviceSetting |
| 25 | func CreateIPCRequest(conf *DeviceConfig) (*DeviceSetting, error) { |
| 26 | var request bytes.Buffer |
| 27 | |
| 28 | fmt.Fprintf(&request, "private_key=%s\n", conf.SecretKey) |
| 29 | |
| 30 | if conf.ListenPort != nil { |
| 31 | fmt.Fprintf(&request, "listen_port=%d\n", *conf.ListenPort) |
| 32 | } |
| 33 | |
| 34 | for _, peer := range conf.Peers { |
| 35 | fmt.Fprintf(&request, heredoc.Doc(` |
| 36 | public_key=%s |
| 37 | persistent_keepalive_interval=%d |
| 38 | preshared_key=%s |
| 39 | `), |
| 40 | peer.PublicKey, peer.KeepAlive, peer.PreSharedKey, |
| 41 | ) |
| 42 | if peer.Endpoint != nil { |
| 43 | fmt.Fprintf(&request, "endpoint=%s\n", *peer.Endpoint) |
| 44 | } |
| 45 | |
| 46 | if len(peer.AllowedIPs) > 0 { |
| 47 | for _, ip := range peer.AllowedIPs { |
| 48 | fmt.Fprintf(&request, "allowed_ip=%s\n", ip.String()) |
| 49 | } |
| 50 | } else { |
| 51 | request.WriteString(heredoc.Doc(` |
| 52 | allowed_ip=0.0.0.0/0 |
| 53 | allowed_ip=::0/0 |
| 54 | `)) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | setting := &DeviceSetting{IpcRequest: request.String(), DNS: conf.DNS, DeviceAddr: conf.Endpoint, MTU: conf.MTU} |
| 59 | return setting, nil |
| 60 | } |
| 61 | |
| 62 | // StartWireguard creates a tun interface on netstack given a configuration |
| 63 | func StartWireguard(conf *Configuration, logLevel int) (*VirtualTun, error) { |