Skip to content

Commit fafa9f0

Browse files
committed
Initial checkin.
Copied from https://github.com/jackpal/Taipei-Torrent/blob/master/torrent/gateway.go Split into OS-specific versions.
1 parent 24a7d53 commit fafa9f0

File tree

4 files changed

+148
-0
lines changed

4 files changed

+148
-0
lines changed

LICENSE

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) 2010 Jack Palevich. All rights reserved.
2+
//
3+
// Redistribution and use in source and binary forms, with or without
4+
// modification, are permitted provided that the following conditions are
5+
// met:
6+
//
7+
// * Redistributions of source code must retain the above copyright
8+
// notice, this list of conditions and the following disclaimer.
9+
// * Redistributions in binary form must reproduce the above
10+
// copyright notice, this list of conditions and the following disclaimer
11+
// in the documentation and/or other materials provided with the
12+
// distribution.
13+
// * Neither the name of Google Inc. nor the names of its
14+
// contributors may be used to endorse or promote products derived from
15+
// this software without specific prior written permission.
16+
//
17+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18+
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19+
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20+
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21+
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23+
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25+
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

gateway_darwin.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package gateway
2+
3+
import (
4+
"bytes"
5+
"io/ioutil"
6+
"net"
7+
"os/exec"
8+
)
9+
10+
func DiscoverGateway() (ip net.IP, err error) {
11+
routeCmd := exec.Command("route", "-n", "get", "0.0.0.0")
12+
stdOut, err := routeCmd.StdoutPipe()
13+
if err != nil {
14+
return
15+
}
16+
if err = routeCmd.Start(); err != nil {
17+
return
18+
}
19+
output, err := ioutil.ReadAll(stdOut)
20+
if err != nil {
21+
return
22+
}
23+
24+
// Darwin route out format is always like this:
25+
// route to: default
26+
// destination: default
27+
// mask: default
28+
// gateway: 192.168.1.1
29+
outputLines := bytes.Split(output, []byte("\n"))
30+
for _, line := range outputLines {
31+
if bytes.Contains(line, []byte("gateway:")) {
32+
gatewayFields := bytes.Fields(line)
33+
ip = net.ParseIP(string(gatewayFields[1]))
34+
break
35+
}
36+
}
37+
38+
err = routeCmd.Wait()
39+
return
40+
}

gateway_linux.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package gateway
2+
3+
import (
4+
"bytes"
5+
"io/ioutil"
6+
"net"
7+
"os/exec"
8+
)
9+
10+
func DiscoverGateway() (ip net.IP, err error) {
11+
routeCmd := exec.Command("route", "-n")
12+
stdOut, err := routeCmd.StdoutPipe()
13+
if err != nil {
14+
return
15+
}
16+
if err = routeCmd.Start(); err != nil {
17+
return
18+
}
19+
output, err := ioutil.ReadAll(stdOut)
20+
if err != nil {
21+
return
22+
}
23+
24+
// Linux route out format is always like this:
25+
// Kernel IP routing table
26+
// Destination Gateway Genmask Flags Metric Ref Use Iface
27+
// 0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
28+
outputLines := bytes.Split(output, []byte("\n"))
29+
for _, line := range outputLines {
30+
if bytes.Contains(line, []byte("0.0.0.0")) {
31+
ipFields := bytes.Fields(line)
32+
ip = net.ParseIP(string(ipFields[1]))
33+
break
34+
}
35+
}
36+
err = routeCmd.Wait()
37+
return
38+
}

gateway_windows.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package gateway
2+
3+
import (
4+
"bytes"
5+
"io/ioutil"
6+
"net"
7+
"os/exec"
8+
)
9+
10+
func DiscoverGateway() (ip net.IP, err error) {
11+
routeCmd := exec.Command("route", "print", "0.0.0.0")
12+
stdOut, err := routeCmd.StdoutPipe()
13+
if err != nil {
14+
return
15+
}
16+
if err = routeCmd.Start(); err != nil {
17+
return
18+
}
19+
output, err := ioutil.ReadAll(stdOut)
20+
if err != nil {
21+
return
22+
}
23+
24+
// Windows route output format is always like this:
25+
// ===========================================================================
26+
// Active Routes:
27+
// Network Destination Netmask Gateway Interface Metric
28+
// 0.0.0.0 0.0.0.0 192.168.1.1 192.168.1.100 20
29+
// ===========================================================================
30+
// I'm trying to pick the active route,
31+
// then jump 2 lines and pick the third IP
32+
// Not using regex because output is quite standard from Windows XP to 8 (NEEDS TESTING)
33+
outputLines := bytes.Split(output, []byte("\n"))
34+
for idx, line := range outputLines {
35+
if bytes.Contains(line, []byte("Active Routes:")) {
36+
ipFields := bytes.Fields(outputLines[idx+2])
37+
ip = net.ParseIP(string(ipFields[2]))
38+
break
39+
}
40+
}
41+
err = routeCmd.Wait()
42+
return
43+
}

0 commit comments

Comments
 (0)