prompts: stderr< + misc

This commit is contained in:
Callan Bryant 2020-03-02 21:36:41 +00:00
parent 8e29d1cba7
commit 1fdf4fe71f
No known key found for this signature in database
GPG Key ID: C31FA9DF3ACBFFAA
4 changed files with 12 additions and 6 deletions

8
add.go
View File

@ -8,7 +8,6 @@ func Add(hostname string, owner string, description string) { //, publicKey stri
conf := MustLoadDsnetConfig() conf := MustLoadDsnetConfig()
privateKey := GenerateJSONPrivateKey() privateKey := GenerateJSONPrivateKey()
presharedKey := GenerateJSONKey()
publicKey := privateKey.PublicKey() publicKey := privateKey.PublicKey()
IP := conf.MustAllocateIP() IP := conf.MustAllocateIP()
@ -18,7 +17,8 @@ func Add(hostname string, owner string, description string) { //, publicKey stri
Hostname: hostname, Hostname: hostname,
Description: description, Description: description,
PublicKey: publicKey, PublicKey: publicKey,
PresharedKey: presharedKey, PrivateKey: privateKey, // omitted from server config JSON!
PresharedKey: GenerateJSONKey(),
AllowedIPs: []JSONIPNet{ AllowedIPs: []JSONIPNet{
JSONIPNet{ JSONIPNet{
IPNet: net.IPNet{ IPNet: net.IPNet{
@ -33,10 +33,10 @@ func Add(hostname string, owner string, description string) { //, publicKey stri
conf.MustSave() conf.MustSave()
} }
func GetPeerWgQuickConf(peer PeerConfig, privKey JSONKey) string { func GetPeerWgQuickConf(peer PeerConfig) string {
return `[Interface] return `[Interface]
Address = 10.50.60.2/24 Address = 10.50.60.2/24
PrivateKey=REDACTED PrivateKey={{
DNS = 8.8.8.8 DNS = 8.8.8.8
[Peer] [Peer]

View File

@ -46,6 +46,7 @@ func getRandomNetwork() JSONIPNet {
} }
} }
// TODO support IPv6
func getExternalIP() net.IP { func getExternalIP() net.IP {
conn, _ := net.Dial("udp", "8.8.8.8:80") conn, _ := net.Dial("udp", "8.8.8.8:80")
defer conn.Close() defer conn.Close()
@ -53,6 +54,10 @@ func getExternalIP() net.IP {
localAddr := conn.LocalAddr().String() localAddr := conn.LocalAddr().String()
IP := net.ParseIP(strings.Split(localAddr, ":")[0]) IP := net.ParseIP(strings.Split(localAddr, ":")[0])
// TODO detect private IP and use icanhazip.com instead if !(IP[0] == 10 || (IP[0] == 172 && IP[1] >= 16 && IP[1] <= 31) || (IP[0] == 192 && IP[1] == 168)) {
// not private, so public
return IP return IP
} }
// TODO detect private IP and use icanhazip.com instead
return net.IP{}
}

View File

@ -20,6 +20,7 @@ type PeerConfig struct {
Description string `validate:"required,gte=1,lte=255"` Description string `validate:"required,gte=1,lte=255"`
PublicKey JSONKey `validate:"required,len=44"` PublicKey JSONKey `validate:"required,len=44"`
PrivateKey JSONKey `json:"-"` // omitted from config!
PresharedKey JSONKey `validate:"required,len=44"` PresharedKey JSONKey `validate:"required,len=44"`
// TODO endpoint support // TODO endpoint support
//Endpoint net.UDPAddr `validate:"required,udp4_addr"` //Endpoint net.UDPAddr `validate:"required,udp4_addr"`

View File

@ -19,7 +19,7 @@ func MustPromptString(prompt string, required bool) string {
var err error var err error
for text == "" { for text == "" {
fmt.Printf("%s: ", prompt) fmt.Fprintf(os.Stderr, "%s: ", prompt)
text, err = reader.ReadString('\n') text, err = reader.ReadString('\n')
check(err) check(err)
text = strings.TrimSpace(text) text = strings.TrimSpace(text)