Skip to content

Commit

Permalink
Eggbeater missed one.
Browse files Browse the repository at this point in the history
  • Loading branch information
xxxserxxx committed Aug 11, 2022
1 parent 9bf5693 commit bbaa5b7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
48 changes: 34 additions & 14 deletions cmd/cli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/spf13/viper"
)

func Init() {
func Init() error {
reportFile := viper.GetString("report_file")
listenPort := viper.GetInt("listen_port")
configFile := viper.GetString("config_file")
Expand All @@ -24,14 +24,23 @@ func Init() {
_, err := os.Stat(configFile)

if !os.IsNotExist(err) {
ExitFail("Refusing to overwrite existing %s", configFile)
return wrapError(err, fmt.Sprintf("Refusing to overwrite existing %s", configFile))
}

privateKey, err := lib.GenerateJSONPrivateKey()
check(err, "failed to generate private key")
if err != nil {
return wrapError(err, "failed to generate private key")
}

externalIPV4, err := getExternalIP()
check(err)
if err != nil {
return err
}

externalIPV6, err := getExternalIP6()
if err != nil {
return err
}

conf := &DsnetConfig{
PrivateKey: privateKey,
Expand All @@ -42,18 +51,22 @@ func Init() {
Domain: "dsnet",
ReportFile: reportFile,
ExternalIP: externalIPV4,
ExternalIP6: getExternalIP6(),
ExternalIP6: externalIPV6,
InterfaceName: interfaceName,
Networks: []lib.JSONIPNet{},
}

server := GetServer(conf)

ipv4, err := server.AllocateIP()
check(err, "failed to allocate ipv4 address")
if err != nil {
return wrapError(err, "failed to allocate ipv4 address")
}

ipv6, err := server.AllocateIP6()
check(err, "failed to allocate ipv6 address")
if err != nil {
return wrapError(err, "failed to allocate ipv6 address")
}

conf.IP = ipv4
conf.IP6 = ipv6
Expand All @@ -65,6 +78,7 @@ func Init() {
conf.MustSave()

fmt.Printf("Config written to %s. Please check/edit.\n", configFile)
return nil
}

// get a random IPv4 /22 subnet on 10.0.0.0 (1023 hosts) (or /24?)
Expand Down Expand Up @@ -120,20 +134,24 @@ func getExternalIP() (net.IP, error) {
Timeout: 5 * time.Second,
}
resp, err := client.Get("https://ipv4.icanhazip.com/")
check(err)
if err != nil {
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode == http.StatusOK {
body, err := ioutil.ReadAll(resp.Body)
check(err)
if err != nil {
return nil, err
}
IP = net.ParseIP(strings.TrimSpace(string(body)))
return IP.To4(), nil
}

return nil, errors.New("failed to determine external ip")
}

func getExternalIP6() net.IP {
func getExternalIP6() (net.IP, error) {
var IP net.IP
conn, err := net.Dial("udp", "2001:4860:4860::8888:53")
if err == nil {
Expand All @@ -144,7 +162,7 @@ func getExternalIP6() net.IP {

// check is not a ULA
if IP[0] != 0xfd && IP[0] != 0xfc {
return IP
return IP, nil
}
}

Expand All @@ -157,11 +175,13 @@ func getExternalIP6() net.IP {

if resp.StatusCode == http.StatusOK {
body, err := ioutil.ReadAll(resp.Body)
check(err)
if err != nil {
return nil, err
}
IP = net.ParseIP(strings.TrimSpace(string(body)))
return IP
return IP, nil
}
}

return net.IP{}
return net.IP{}, nil
}
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ var (
"Create %s containing default configuration + new keys without loading. Edit to taste.",
viper.GetString("config_file"),
),
Run: func(cmd *cobra.Command, args []string) {
cli.Init()
RunE: func(cmd *cobra.Command, args []string) error {
return cli.Init()
},
}

Expand Down

0 comments on commit bbaa5b7

Please sign in to comment.