Skip to content

Commit 166eda2

Browse files
authored
PSK support for initial relay connections (#98)
1 parent 42a590c commit 166eda2

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

src/cmd/configure.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type configureCmdConfig struct {
3838
mtu int
3939
disableV6 bool
4040
localhostIP string
41+
generatePSK bool
4142
}
4243

4344
// Defaults for configure command.
@@ -66,6 +67,7 @@ var configureCmdArgs = configureCmdConfig{
6667
mtu: MTU,
6768
disableV6: false,
6869
localhostIP: "",
70+
generatePSK: false,
6971
}
7072

7173
// configureCmd represents the configure command.
@@ -89,6 +91,7 @@ func init() {
8991
configureCmd.Flags().IntVarP(&configureCmdArgs.sport, "sport", "S", configureCmdArgs.sport, "listener port for server wireguard relay. Default is to copy the --outbound-endpoint port, or fallback to 51820")
9092
configureCmd.Flags().StringVarP(&configureCmdArgs.nickname, "nickname", "n", configureCmdArgs.nickname, "Server nickname to display in 'status' command")
9193
configureCmd.Flags().StringVarP(&configureCmdArgs.localhostIP, "localhost-ip", "i", configureCmdArgs.localhostIP, "[EXPERIMENTAL] Redirect wiretap packets destined for this IPv4 address to server's localhost")
94+
configureCmd.Flags().BoolVarP(&configureCmdArgs.generatePSK, "PSK", "K", configureCmdArgs.generatePSK, "generates a preshared key")
9295

9396
configureCmd.Flags().StringVarP(&configureCmdArgs.configFileRelay, "relay-output", "", configureCmdArgs.configFileRelay, "wireguard relay config output filename")
9497
configureCmd.Flags().StringVarP(&configureCmdArgs.configFileE2EE, "e2ee-output", "", configureCmdArgs.configFileE2EE, "wireguard E2EE config output filename")
@@ -223,6 +226,15 @@ func (c configureCmdConfig) Run() {
223226
Peers: []peer.PeerConfigArgs{
224227
{
225228
PublicKey: serverConfigRelay.GetPublicKey(),
229+
PresharedKey: func() string {
230+
if c.generatePSK {
231+
err = serverConfigRelay.GenPresharedKey()
232+
check("failed to generate preshared key", err)
233+
return serverConfigRelay.GetPresharedKey()
234+
} else {
235+
return ""
236+
}
237+
}(),
226238
AllowedIPs: func() []string {
227239
if c.simple {
228240
return c.allowedIPs

src/cmd/serve.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ func init() {
165165
// Deprecated flags, kept for backwards compatibility.
166166
cmd.Flags().StringP("private-relay", "", "", "wireguard private key for relay interface")
167167
cmd.Flags().StringP("public-relay", "", "", "wireguard public key of remote peer for relay interface")
168+
cmd.Flags().StringP("preshared-relay", "", "", "wireguard preshared key of remote peer for relay interface")
168169
cmd.Flags().StringP("private-e2ee", "", "", "wireguard private key for E2EE interface")
169170
cmd.Flags().StringP("public-e2ee", "", "", "wireguard public key of remote peer for E2EE interface")
170171
cmd.Flags().StringP("endpoint-relay", "", wiretapDefault.endpointRelay, "socket address of remote peer that server will connect to (example \"1.2.3.4:51820\")")
@@ -189,6 +190,8 @@ func init() {
189190

190191
err = viper.BindPFlag("Relay.Peer.publickey", cmd.Flags().Lookup("public-relay"))
191192
check("error binding flag to viper", err)
193+
err = viper.BindPFlag("Relay.Peer.presharedkey", cmd.Flags().Lookup("preshared-relay"))
194+
check("error binding flag to viper", err)
192195
err = viper.BindPFlag("Relay.Peer.endpoint", cmd.Flags().Lookup("endpoint-relay"))
193196
check("error binding flag to viper", err)
194197
err = viper.BindPFlag("Relay.Peer.allowed", cmd.Flags().Lookup("allowed"))
@@ -239,6 +242,7 @@ func init() {
239242
"ipv6-e2ee-client",
240243
"private-relay",
241244
"public-relay",
245+
"preshared-relay",
242246
"private-e2ee",
243247
"public-e2ee",
244248
"endpoint-relay",
@@ -370,6 +374,13 @@ func (c serveCmdConfig) Run() {
370374
return 0
371375
}
372376
}(),
377+
PresharedKey: func() string {
378+
if len(viper.GetString("Relay.Peer.presharedkey")) > 0 {
379+
return viper.GetString("Relay.Peer.presharedkey")
380+
} else {
381+
return ""
382+
}
383+
}(),
373384
AllowedIPs: aips,
374385
},
375386
},

src/peer/config.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type Config struct {
1919
peers []PeerConfig
2020
addresses []net.IPNet
2121
localhostIP string
22+
presharedKey *wgtypes.Key
2223
}
2324

2425
type configJSON struct {
@@ -27,6 +28,7 @@ type configJSON struct {
2728
Peers []PeerConfig
2829
Addresses []net.IPNet
2930
LocalhostIP string
31+
PresharedKey *wgtypes.Key
3032
}
3133

3234
type ConfigArgs struct {
@@ -38,6 +40,7 @@ type ConfigArgs struct {
3840
Peers []PeerConfigArgs
3941
Addresses []string
4042
LocalhostIP string
43+
PresharedKey string
4144
}
4245

4346
type Shell uint
@@ -193,6 +196,8 @@ func ParseConfig(filename string) (c Config, err error) {
193196
err = newPeer.SetAllowedIPs(strings.Split(value, ","))
194197
case "publickey":
195198
err = newPeer.SetPublicKey(value)
199+
case "presharedkey":
200+
err = newPeer.SetPresharedKey(value)
196201
case "persistentkeepalive":
197202
keepalive, e := strconv.Atoi(value)
198203
if e != nil {
@@ -233,6 +238,7 @@ func (c *Config) MarshalJSON() ([]byte, error) {
233238
c.peers,
234239
c.addresses,
235240
c.localhostIP,
241+
c.presharedKey,
236242
})
237243
}
238244

@@ -248,6 +254,7 @@ func (c *Config) UnmarshalJSON(b []byte) error {
248254
c.peers = tmp.Peers
249255
c.addresses = tmp.Addresses
250256
c.localhostIP = tmp.LocalhostIP
257+
c.presharedKey = tmp.PresharedKey
251258

252259
return nil
253260
}
@@ -266,6 +273,23 @@ func (c *Config) GetPrivateKey() string {
266273
return c.config.PrivateKey.String()
267274
}
268275

276+
func (c* Config) GenPresharedKey() error {
277+
key, err := wgtypes.GenerateKey()
278+
if err != nil {
279+
return err
280+
}
281+
c.presharedKey = &key
282+
return nil
283+
}
284+
285+
func (c* Config) GetPresharedKey() string {
286+
if c.presharedKey != nil {
287+
return c.presharedKey.String()
288+
} else {
289+
return ""
290+
}
291+
}
292+
269293
func (c *Config) SetPort(port int) error {
270294
if port < 1 || port > 65535 {
271295
return errors.New("invalid port")
@@ -440,6 +464,9 @@ func (c *Config) AsShareableFile() string {
440464

441465
s.WriteString("[Peer]\n")
442466
s.WriteString(fmt.Sprintf("PublicKey = %s\n", c.config.PrivateKey.PublicKey().String()))
467+
if c.presharedKey != nil {
468+
s.WriteString(fmt.Sprintf("PresharedKey = %s\n", c.presharedKey.String()))
469+
}
443470
s.WriteString("AllowedIPs = 0.0.0.0/32\n")
444471

445472
return s.String()
@@ -488,6 +515,11 @@ func CreateServerCommand(relayConfig Config, e2eeConfig Config, shell Shell, sim
488515
// Relay Peer.
489516
keys = append(keys, "WIRETAP_RELAY_PEER_PUBLICKEY")
490517
vals = append(vals, relayConfig.GetPeerPublicKey(0))
518+
519+
if relayConfig.presharedKey != nil {
520+
keys = append(keys, "WIRETAP_RELAY_PEER_PRESHAREDKEY")
521+
vals = append(vals, relayConfig.GetPresharedKey())
522+
}
491523

492524
if len(relayConfig.peers) > 0 && len(relayConfig.peers[0].config.AllowedIPs) > 0 {
493525
keys = append(keys, "WIRETAP_RELAY_PEER_ALLOWED")
@@ -588,6 +620,9 @@ func CreateServerFile(relayConfig Config, e2eeConfig Config, simple bool) string
588620
}
589621

590622
s.WriteString(fmt.Sprintf("PublicKey = %s\n", relayConfig.GetPeerPublicKey(0)))
623+
if relayConfig.presharedKey != nil {
624+
s.WriteString(fmt.Sprintf("PresharedKey = %s\n", relayConfig.GetPresharedKey()))
625+
}
591626
if len(relayConfig.GetPeerEndpoint(0)) > 0 {
592627
s.WriteString(fmt.Sprintf("Endpoint = %s\n", relayConfig.GetPeerEndpoint(0)))
593628
}

src/peer/peer_config.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,10 @@ func (p *PeerConfig) AsFile() string {
277277

278278
s.WriteString(fmt.Sprintf("PublicKey = %s\n", p.config.PublicKey.String()))
279279

280+
if p.config.PresharedKey != nil {
281+
s.WriteString(fmt.Sprintf("PresharedKey = %s\n", p.config.PresharedKey.String()))
282+
}
283+
280284
ips := []string{}
281285
for _, a := range p.config.AllowedIPs {
282286
ips = append(ips, a.String())
@@ -301,6 +305,9 @@ func (p *PeerConfig) AsIPC() string {
301305
var s strings.Builder
302306

303307
s.WriteString(fmt.Sprintf("public_key=%s\n", hex.EncodeToString(p.config.PublicKey[:])))
308+
if p.config.PresharedKey != nil {
309+
s.WriteString(fmt.Sprintf("preshared_key=%s\n", hex.EncodeToString(p.config.PresharedKey[:])))
310+
}
304311
if p.config.Endpoint != nil {
305312
s.WriteString(fmt.Sprintf("endpoint=%s\n", p.config.Endpoint.String()))
306313
}

0 commit comments

Comments
 (0)