wg0 是 wireguard 的虚拟网卡,AllowedIPs 为 0.0.0.0/0 并且 Table = off。
curl 可以实现通过 wg0 请求成功:
curl --interface wg0 http://ifconfig.me
以下代码却不行,请问为什么?应该怎么修改?
package main
import (
	"context"
	"fmt"
	"io/ioutil"
	"net"
	"net/http"
	"time"
)
func main() {
	ief, _ := net.InterfaceByName("wg0")
	addrs, _ := ief.Addrs()
	d := net.Dialer{
		LocalAddr: &net.TCPAddr{IP: addrs[0].(*net.IPNet).IP},
		Timeout:   time.Second * 5,
	}
	fmt.Println(d.LocalAddr)
	c := &http.Client{
		Transport: &http.Transport{
			DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
				return d.DialContext(ctx, network, addr)
			},
		},
	}
	r, err := c.Get("http://ifconfig.me")
	if err != nil {
		panic(err)
	}
	defer r.Body.Close()
	b, _ := ioutil.ReadAll(r.Body)
	fmt.Println(string(b))
}
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.