Fix a nil pointer in Wireguard client logging (#3608)

This commit is contained in:
yuhan6665 2024-07-29 02:43:46 -04:00 committed by GitHub
parent 60553a6c26
commit a342db3e28
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -100,7 +100,7 @@ func (h *Handler) processWireGuard(ctx context.Context, dialer internet.Dialer)
} }
// bind := conn.NewStdNetBind() // TODO: conn.Bind wrapper for dialer // bind := conn.NewStdNetBind() // TODO: conn.Bind wrapper for dialer
bind := &netBindClient{ h.bind = &netBindClient{
netBind: netBind{ netBind: netBind{
dns: h.dns, dns: h.dns,
dnsOption: dns.IPOption{ dnsOption: dns.IPOption{
@ -115,15 +115,14 @@ func (h *Handler) processWireGuard(ctx context.Context, dialer internet.Dialer)
} }
defer func() { defer func() {
if err != nil { if err != nil {
_ = bind.Close() _ = h.bind.Close()
} }
}() }()
h.net, err = h.makeVirtualTun(bind) h.net, err = h.makeVirtualTun()
if err != nil { if err != nil {
return errors.New("failed to create virtual tun interface").Base(err) return errors.New("failed to create virtual tun interface").Base(err)
} }
h.bind = bind
return nil return nil
} }
@ -238,16 +237,16 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte
} }
// creates a tun interface on netstack given a configuration // creates a tun interface on netstack given a configuration
func (h *Handler) makeVirtualTun(bind *netBindClient) (Tunnel, error) { func (h *Handler) makeVirtualTun() (Tunnel, error) {
t, err := h.conf.createTun()(h.endpoints, int(h.conf.Mtu), nil) t, err := h.conf.createTun()(h.endpoints, int(h.conf.Mtu), nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
bind.dnsOption.IPv4Enable = h.hasIPv4 h.bind.dnsOption.IPv4Enable = h.hasIPv4
bind.dnsOption.IPv6Enable = h.hasIPv6 h.bind.dnsOption.IPv6Enable = h.hasIPv6
if err = t.BuildDevice(h.createIPCRequest(bind, h.conf), bind); err != nil { if err = t.BuildDevice(h.createIPCRequest(), h.bind); err != nil {
_ = t.Close() _ = t.Close()
return nil, err return nil, err
} }
@ -255,17 +254,17 @@ func (h *Handler) makeVirtualTun(bind *netBindClient) (Tunnel, error) {
} }
// serialize the config into an IPC request // serialize the config into an IPC request
func (h *Handler) createIPCRequest(bind *netBindClient, conf *DeviceConfig) string { func (h *Handler) createIPCRequest() string {
var request strings.Builder var request strings.Builder
request.WriteString(fmt.Sprintf("private_key=%s\n", conf.SecretKey)) request.WriteString(fmt.Sprintf("private_key=%s\n", h.conf.SecretKey))
if !conf.IsClient { if !h.conf.IsClient {
// placeholder, we'll handle actual port listening on Xray // placeholder, we'll handle actual port listening on Xray
request.WriteString("listen_port=1337\n") request.WriteString("listen_port=1337\n")
} }
for _, peer := range conf.Peers { for _, peer := range h.conf.Peers {
if peer.PublicKey != "" { if peer.PublicKey != "" {
request.WriteString(fmt.Sprintf("public_key=%s\n", peer.PublicKey)) request.WriteString(fmt.Sprintf("public_key=%s\n", peer.PublicKey))
} }
@ -280,7 +279,7 @@ func (h *Handler) createIPCRequest(bind *netBindClient, conf *DeviceConfig) stri
} }
addr := net.ParseAddress(address) addr := net.ParseAddress(address)
if addr.Family().IsDomain() { if addr.Family().IsDomain() {
dialerIp := bind.dialer.DestIpAddress() dialerIp := h.bind.dialer.DestIpAddress()
if dialerIp != nil { if dialerIp != nil {
addr = net.ParseAddress(dialerIp.String()) addr = net.ParseAddress(dialerIp.String())
errors.LogInfo(h.bind.ctx, "createIPCRequest use dialer dest ip: ", addr) errors.LogInfo(h.bind.ctx, "createIPCRequest use dialer dest ip: ", addr)