custom ipfetch #63.

This commit is contained in:
Miroslav Šedivý 2021-06-26 13:44:41 +02:00
parent 3a79615a2b
commit 7aa7e0eacb
2 changed files with 13 additions and 5 deletions

View File

@ -5,6 +5,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"n.eko.moe/neko/internal/utils" "n.eko.moe/neko/internal/utils"
@ -31,6 +32,11 @@ func (WebRTC) Init(cmd *cobra.Command) error {
return err return err
} }
cmd.PersistentFlags().String("ipfetch", "http://checkip.amazonaws.com", "automatically fetch IP address from given URL when nat1to1 is not present")
if err := viper.BindPFlag("ipfetch", cmd.PersistentFlags().Lookup("ipfetch")); err != nil {
return err
}
cmd.PersistentFlags().Bool("icelite", false, "configures whether or not the ice agent should be a lite agent") cmd.PersistentFlags().Bool("icelite", false, "configures whether or not the ice agent should be a lite agent")
if err := viper.BindPFlag("icelite", cmd.PersistentFlags().Lookup("icelite")); err != nil { if err := viper.BindPFlag("icelite", cmd.PersistentFlags().Lookup("icelite")); err != nil {
return err return err
@ -68,10 +74,12 @@ func (s *WebRTC) Set() {
} }
if len(s.NAT1To1IPs) == 0 { if len(s.NAT1To1IPs) == 0 {
ip, err := utils.GetIP() ipfetch := viper.GetString("ipfetch")
if err == nil { ip, err := utils.GetIP(ipfetch)
s.NAT1To1IPs = append(s.NAT1To1IPs, ip) if err != nil {
log.Panic().Err(err).Str("ipfetch", ipfetch).Msg("failed to fetch ip address")
} }
s.NAT1To1IPs = append(s.NAT1To1IPs, ip)
} }
min := uint16(59000) min := uint16(59000)

View File

@ -10,7 +10,7 @@ import (
// dig @resolver1.opendns.com ANY myip.opendns.com +short -4 // dig @resolver1.opendns.com ANY myip.opendns.com +short -4
func GetIP() (string, error) { func GetIP(serverUrl string) (string, error) {
tr := &http.Transport{ tr := &http.Transport{
Proxy: nil, // ignore proxy Proxy: nil, // ignore proxy
DialContext: (&net.Dialer{ DialContext: (&net.Dialer{
@ -25,7 +25,7 @@ func GetIP() (string, error) {
} }
client := &http.Client{Transport: tr} client := &http.Client{Transport: tr}
rsp, err := client.Get("http://checkip.amazonaws.com") rsp, err := client.Get(serverUrl)
if err != nil { if err != nil {
return "", err return "", err
} }