Jiri Simsa | d7616c9 | 2015-03-24 23:44:30 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Vanadium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
Cosmos Nicolaou | 43b9535 | 2014-10-14 11:09:52 -0700 | [diff] [blame] | 5 | // +build linux |
| 6 | |
| 7 | package internal |
| 8 | |
| 9 | import ( |
| 10 | "net" |
| 11 | |
Cosmos Nicolaou | 0e4e392 | 2015-06-10 16:30:09 -0700 | [diff] [blame] | 12 | "v.io/v23/logging" |
Cosmos Nicolaou | 43b9535 | 2014-10-14 11:09:52 -0700 | [diff] [blame] | 13 | |
Suharsh Sivakumar | dcc11d7 | 2015-05-11 12:19:20 -0700 | [diff] [blame] | 14 | "v.io/x/ref/runtime/internal/gce" |
Cosmos Nicolaou | 43b9535 | 2014-10-14 11:09:52 -0700 | [diff] [blame] | 15 | ) |
| 16 | |
| 17 | // GCEPublicAddress returns the public IP address of the GCE instance |
| 18 | // it is run from, or nil if run from anywhere else. The returned address |
| 19 | // is the public address of a 1:1 NAT tunnel to this host. |
Cosmos Nicolaou | 0e4e392 | 2015-06-10 16:30:09 -0700 | [diff] [blame] | 20 | func GCEPublicAddress(log logging.Logger) *net.IPAddr { |
Cosmos Nicolaou | 43b9535 | 2014-10-14 11:09:52 -0700 | [diff] [blame] | 21 | if !gce.RunningOnGCE() { |
| 22 | return nil |
| 23 | } |
| 24 | var pub *net.IPAddr |
| 25 | // Determine the IP address from GCE's metadata |
| 26 | if ip, err := gce.ExternalIPAddress(); err != nil { |
| 27 | log.Infof("failed to query GCE metadata: %s", err) |
| 28 | } else { |
| 29 | // 1:1 NAT case, our network config will not change. |
| 30 | pub = &net.IPAddr{IP: ip} |
| 31 | } |
| 32 | if pub == nil { |
| 33 | log.Infof("failed to determine public IP address to publish with") |
| 34 | } |
| 35 | return pub |
| 36 | } |