Skip to content

Commit

Permalink
支持Android 13中 termux 获取不到 mac 地址的情况
Browse files Browse the repository at this point in the history
  • Loading branch information
shynome committed Sep 11, 2024
1 parent 86fbf41 commit 0eee4ff
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package cmd

import (
"context"
"crypto/rand"
"encoding/hex"
"fmt"
"hash/fnv"
Expand Down Expand Up @@ -94,7 +95,22 @@ var rootCmd = &cobra.Command{
}

func getMacHashTry() string {
ifaces := try.To1(net.Interfaces())
ifaces, _ := net.Interfaces()
if len(ifaces) == 0 {
home := try.To1(os.UserHomeDir())
lcodeDir := filepath.Join(home, ".lcode")
try.To(os.MkdirAll(lcodeDir, os.ModePerm))
fakeMacFile := filepath.Join(lcodeDir, "fake-mac")
fakeMac, _ := os.ReadFile(fakeMacFile)
if len(fakeMac) == 0 {
fakeMac = make([]byte, 6)
try.To1(rand.Read(fakeMac))
try.To(os.WriteFile(fakeMacFile, fakeMac, os.ModePerm))
}
ifaces = []net.Interface{
{Flags: net.FlagUp, HardwareAddr: fakeMac},
}
}
hasher := fnv.New32a()
for _, iface := range ifaces {
if iface.Flags&net.FlagUp != 0 && iface.HardwareAddr != nil {
Expand Down

0 comments on commit 0eee4ff

Please sign in to comment.