Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
427 views
in Technique[技术] by (71.8m points)

go - How do I dial a destination with specified gateway no matter what routing table is?

OS: macOS

Routing table:

# netstat -nr
Routing tables

Internet:
Destination        Gateway            Flags        Netif Expire
default            192.168.0.1        UGSc           en0
1.1.1.1/32         127.0.0.1          UGSc           lo0
127                127.0.0.1          UCS            lo0
127.0.0.1          127.0.0.1          UH             lo0
169.254            link#6             UCS            en0      !
192.168.0          link#6             UCS            en0      !
192.168.0.1/32     link#6             UCS            en0      !
192.168.0.1        66:c4:b4:70:cc:7d  UHLWIir        en0   1181
192.168.0.23       b6:91:26:63:85:d6  UHLWIi         en0    902
192.168.0.193/32   link#6             UCS            en0      !
224.0.0/4          link#6             UmCS           en0      !
224.0.0.251        1:0:5e:0:0:fb      UHmLWI         en0
230.230.230.230    1:0:5e:66:e6:e6    UHmLWI         en0
239.255.255.250    1:0:5e:7f:ff:fa    UHmLWI         en0
255.255.255.255/32 link#6             UCS            en0      !

As you see, the destination 1.1.1.1/32 is routed to gateway 127.0.0.1. How do I dial to 1.1.1.1 via gateway 192.168.0.1 in Go without deleting the route?

The following example code is not working:

    from := &net.TCPAddr{
        IP: net.ParseIP("192.168.0.193"),
        Port: 0,
    }
    to := &net.TCPAddr{
        IP: net.ParseIP("1.1.1.1"),
        Port: 443,
    }

    c, err := net.DialTCP("tcp", from, to)
    if err != nil {
        panic(err)
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...