-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add dns setting and ipv6 setting support
- Loading branch information
Showing
6 changed files
with
293 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2023 Jigsaw Operations LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//go:build linux | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
) | ||
|
||
const ( | ||
resolvConfFile = "/etc/resolv.conf" | ||
resolvConfHeadFile = "/etc/resolv.conf.head" | ||
resolvConfBackupFile = "/etc/resolv.outlinecli.backup" | ||
resolvConfHeadBackupFile = "/etc/resolv.head.outlinecli.backup" | ||
) | ||
|
||
func setSystemDNSServer(serverHost string) error { | ||
setting := []byte(`# Outline CLI DNS Setting | ||
# The original file has been renamed as resolv[.head].outlinecli.backup | ||
nameserver ` + serverHost + "\n") | ||
|
||
if err := backupAndWriteFile(resolvConfFile, resolvConfBackupFile, setting); err != nil { | ||
return err | ||
} | ||
return backupAndWriteFile(resolvConfHeadFile, resolvConfHeadBackupFile, setting) | ||
} | ||
|
||
func restoreSystemDNSServer() { | ||
restoreFileIfExists(resolvConfBackupFile, resolvConfFile) | ||
restoreFileIfExists(resolvConfHeadBackupFile, resolvConfHeadFile) | ||
} | ||
|
||
func backupAndWriteFile(original, backup string, data []byte) error { | ||
if err := os.Rename(original, backup); err != nil { | ||
return fmt.Errorf("failed to backup DNS config file '%s' to '%s': %w", original, backup, err) | ||
} | ||
if err := os.WriteFile(original, data, 0644); err != nil { | ||
return fmt.Errorf("failed to write DNS config file '%s': %w", original, err) | ||
} | ||
return nil | ||
} | ||
|
||
func restoreFileIfExists(backup, original string) { | ||
if _, err := os.Stat(backup); err != nil { | ||
fmt.Printf("[warn] failed to read DNS config backup '%s': %v\n", backup, err) | ||
return | ||
} | ||
if err := os.Rename(backup, original); err != nil { | ||
fmt.Printf("[error] failed to restore DNS config from backup '%s' to '%s': %v\n", backup, original, err) | ||
return | ||
} | ||
fmt.Printf("[info] DNS config restored from '%s' to '%s'\n", backup, original) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright 2023 Jigsaw Operations LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//go:build linux | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
) | ||
|
||
const disableIPv6ProcFile = "/proc/sys/net/ipv6/conf/all/disable_ipv6" | ||
|
||
// enableIPv6 enables or disables the IPv6 support for the Linux system. | ||
// It returns the previous setting value so the caller can restore it. | ||
// Non-nil error means we cannot find the IPv6 setting. | ||
func enableIPv6(enabled bool) (bool, error) { | ||
disabledStr, err := os.ReadFile(disableIPv6ProcFile) | ||
if err != nil { | ||
return false, fmt.Errorf("failed to read IPv6 config: %w", err) | ||
} | ||
if disabledStr[0] != '0' && disabledStr[0] != '1' { | ||
return false, fmt.Errorf("invalid IPv6 config value: %v", disabledStr) | ||
} | ||
|
||
prevEnabled := disabledStr[0] == '0' | ||
|
||
if enabled { | ||
disabledStr[0] = '0' | ||
} else { | ||
disabledStr[0] = '1' | ||
} | ||
if err := os.WriteFile(disableIPv6ProcFile, disabledStr, 0644); err != nil { | ||
return prevEnabled, fmt.Errorf("failed to write IPv6 config: %w", err) | ||
} | ||
|
||
fmt.Printf("[info] global IPv6 enabled: %v\n", enabled) | ||
return prevEnabled, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.