blob: 482b53aaef99701eee68c9af672f0b353b9aa3ae [file] [log] [blame] [edit]
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build linux
package internal
import (
"net"
"v.io/x/lib/vlog"
"v.io/x/ref/profiles/internal/gce"
)
// GCEPublicAddress returns the public IP address of the GCE instance
// it is run from, or nil if run from anywhere else. The returned address
// is the public address of a 1:1 NAT tunnel to this host.
func GCEPublicAddress(log vlog.Logger) *net.IPAddr {
if !gce.RunningOnGCE() {
return nil
}
var pub *net.IPAddr
// Determine the IP address from GCE's metadata
if ip, err := gce.ExternalIPAddress(); err != nil {
log.Infof("failed to query GCE metadata: %s", err)
} else {
// 1:1 NAT case, our network config will not change.
pub = &net.IPAddr{IP: ip}
}
if pub == nil {
log.Infof("failed to determine public IP address to publish with")
}
return pub
}