MCPcopy
hub / github.com/windtf/wireproxy / StartWireguard

Function StartWireguard

wireguard.go:63–112  ·  view source on GitHub ↗

StartWireguard creates a tun interface on netstack given a configuration

(conf *Configuration, logLevel int)

Source from the content-addressed store, hash-verified

61
62// StartWireguard creates a tun interface on netstack given a configuration
63func StartWireguard(conf *Configuration, logLevel int) (*VirtualTun, error) {
64 deviceConf := conf.Device
65 setting, err := CreateIPCRequest(deviceConf)
66 if err != nil {
67 return nil, err
68 }
69
70 tun, tnet, err := netstack.CreateNetTUN(setting.DeviceAddr, setting.DNS, setting.MTU)
71 if err != nil {
72 return nil, err
73 }
74 dev := device.NewDevice(tun, conn.NewDefaultBind(), device.NewLogger(logLevel, ""))
75 err = dev.IpcSet(setting.IpcRequest)
76 if err != nil {
77 return nil, err
78 }
79
80 err = dev.Up()
81 if err != nil {
82 return nil, err
83 }
84
85 hasV4 := false
86 hasV6 := false
87 for _, addr := range setting.DeviceAddr {
88 if addr.Is4() {
89 hasV4 = true
90 }
91 if addr.Is6() {
92 hasV6 = true
93 }
94 }
95
96 if conf.Resolve.ResolveStrategy == "auto" {
97 if hasV4 && !hasV6 {
98 conf.Resolve.ResolveStrategy = "ipv4"
99 } else {
100 conf.Resolve.ResolveStrategy = "ipv6"
101 }
102 }
103 return &VirtualTun{
104 Tnet: tnet,
105 Dev: dev,
106 Conf: deviceConf,
107 ResolveConfig: conf.Resolve,
108 SystemDNS: len(setting.DNS) == 0,
109 PingRecord: make(map[string]uint64),
110 PingRecordLock: new(sync.Mutex),
111 }, nil
112}

Callers 1

mainFunction · 0.92

Calls 1

CreateIPCRequestFunction · 0.85

Tested by

no test coverage detected