Skip to content

Commit 82dd587

Browse files
ntozaksAptimex
andauthored
server flag to delete config files after ingesting (#80)
* delete server config after ingesting(#49) * server flag to delete config files after ingesting (#49) * -D shortcut for deleting config file --------- Co-authored-by: Adam <37312852+Aptimex@users.noreply.github.com>
1 parent af5266d commit 82dd587

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

src/cmd/serve.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import (
77
"log"
88
"net/netip"
99
"os"
10+
"slices"
1011
"strings"
1112
"sync"
1213
"time"
13-
"slices"
1414

1515
"github.com/spf13/cobra"
1616
"github.com/spf13/viper"
@@ -39,6 +39,7 @@ type serveCmdConfig struct {
3939
clientAddr6E2EE string
4040
clientAddr4Relay string
4141
clientAddr6Relay string
42+
deleteConfig bool // Add member to struct to delete server config file.
4243
quiet bool
4344
debug bool
4445
simple bool
@@ -75,6 +76,7 @@ var serveCmd = serveCmdConfig{
7576
clientAddr6E2EE: ClientE2EESubnet6.Addr().Next().String(),
7677
clientAddr4Relay: ClientRelaySubnet4.Addr().Next().Next().String(),
7778
clientAddr6Relay: ClientRelaySubnet6.Addr().Next().Next().String(),
79+
deleteConfig: false,
7880
quiet: false,
7981
debug: false,
8082
simple: false,
@@ -122,6 +124,8 @@ func init() {
122124

123125
// Flags.
124126
cmd.Flags().StringVarP(&serveCmd.configFile, "config-file", "f", serveCmd.configFile, "wireguard config file to read from")
127+
// Add flag to delete server config file.
128+
cmd.Flags().BoolVarP(&serveCmd.deleteConfig, "delete-config", "D", serveCmd.deleteConfig, "delete wireguard config file after ingesting it")
125129
cmd.Flags().IntP("port", "p", wiretapDefault.port, "listener port to use for relay connections")
126130
cmd.Flags().BoolVarP(&serveCmd.quiet, "quiet", "q", serveCmd.quiet, "silence wiretap log messages")
127131
cmd.Flags().BoolVarP(&serveCmd.debug, "debug", "d", serveCmd.debug, "enable wireguard log messages")
@@ -279,6 +283,13 @@ func (c serveCmdConfig) Run() {
279283
}
280284
}
281285

286+
// If flag --delete-config is set, delete the server config file.
287+
if c.deleteConfig && c.configFile != "" {
288+
if err := os.Remove(c.configFile); err != nil {
289+
check("error deleting config file", err)
290+
}
291+
}
292+
282293
// Synchronization vars.
283294
var (
284295
wg sync.WaitGroup
@@ -341,7 +352,7 @@ func (c serveCmdConfig) Run() {
341352
AllowedIPs: aips,
342353
},
343354
},
344-
Addresses: relayAddresses,
355+
Addresses: relayAddresses,
345356
LocalhostIP: viper.GetString("Relay.Interface.LocalhostIP"),
346357
}
347358

@@ -596,8 +607,8 @@ func configureLocalhostForwarding(localhostAddr netip.Addr, s *stack.Stack) {
596607
ipt.ForceReplaceTable(stack.NATID, newTable, false)
597608
}
598609

599-
// Adds a rule to the start of a table chain.
600-
func prependIPtableRule(table stack.Table, newRule stack.Rule, chain stack.Hook) (stack.Table) {
610+
// Adds a rule to the start of a table chain.
611+
func prependIPtableRule(table stack.Table, newRule stack.Rule, chain stack.Hook) stack.Table {
601612
insertIndex := int(table.BuiltinChains[chain])
602613
table.Rules = slices.Insert(table.Rules, insertIndex, newRule)
603614

@@ -607,7 +618,7 @@ func prependIPtableRule(table stack.Table, newRule stack.Rule, chain stack.Hook)
607618
//assumes each chain has its own unique starting rule index
608619
if ruleIndex > insertIndex {
609620
table.BuiltinChains[chainHook] = ruleIndex + 1
610-
621+
611622
}
612623
}
613624
for chainHook, ruleIndex := range table.Underflows {
@@ -617,4 +628,4 @@ func prependIPtableRule(table stack.Table, newRule stack.Rule, chain stack.Hook)
617628
}
618629

619630
return table
620-
}
631+
}

0 commit comments

Comments
 (0)