Skip to content

Instantly share code, notes, and snippets.

@wwng2333
Last active October 10, 2023 11:23
Show Gist options
  • Save wwng2333/89262d19569b1cca94da46d75ba7308d to your computer and use it in GitHub Desktop.
Save wwng2333/89262d19569b1cca94da46d75ba7308d to your computer and use it in GitHub Desktop.
adh2xxx 将Adguardhome的dhcp ip转换为dnsmasq、padavan static ip格式
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"time"
)
type Lease struct {
Mac string `json:"mac"`
Expires string `json:"expires"`
Hostname string `json:"hostname"`
IP string `json:"ip"`
Static bool `json:"static"`
}
type LeaseList struct {
Leases []Lease `json:"leases"`
}
func main() {
// 读取leases.json文件
data, err := ioutil.ReadFile("/opt/etc/AdGuardHome/data/leases.json")
if err != nil {
fmt.Println("File reading error", err)
return
}
// 解析JSON数据
var leaseList LeaseList
err = json.Unmarshal(data, &leaseList)
if err != nil {
fmt.Println("JSON unmarshalling error", err)
return
}
// 创建或打开dnsmasq.leases文件
file, err := os.Create("/tmp/dnsmasq.leases")
if err != nil {
fmt.Println("File creation error", err)
return
}
defer file.Close()
// 将数据写入dnsmasq.leases文件
for _, lease := range leaseList.Leases {
expiryTime, _ := time.Parse(time.RFC3339, lease.Expires)
unixTime := expiryTime.Unix()
fmt.Fprintf(file, "%d %s %s %s *\n", unixTime, lease.Mac, lease.IP, lease.Hostname)
}
}
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strconv"
"strings"
"sync"
)
type Lease struct {
IP string `json:"ip"`
Mac string `json:"mac"`
Hostname string `json:"hostname"`
}
type Leases struct {
Leases []Lease `json:"leases"`
}
func ping(ip string) bool {
out, err := exec.Command("ping", "-c", "2", ip).Output()
if err != nil {
return false
}
if strings.Contains(string(out), "0 packets received") {
return false
}
return true
}
func main() {
// 读取json文件
jsonFile, err := os.Open("/opt/etc/AdGuardHome/data/leases.json")
if err != nil {
fmt.Println(err)
}
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
var leases Leases
// 解析json文件
json.Unmarshal(byteValue, &leases)
var wg sync.WaitGroup
sem := make(chan bool, 4) // semaphore pattern for limiting concurrency to 4
validLeases := make([]Lease, 0)
for _, lease := range leases.Leases {
wg.Add(1)
go func(lease Lease) {
defer wg.Done()
sem <- true
if ping(lease.IP) {
validLeases = append(validLeases, lease)
}
<-sem
}(lease)
}
wg.Wait()
// 创建输出文件
file, err := os.Create("/tmp/static_ip.inf")
if err != nil {
fmt.Println(err)
}
defer file.Close()
// 创建数量文件
numFile, err := os.Create("/tmp/static_ip.num")
if err != nil {
fmt.Println(err)
}
defer numFile.Close()
// 将数据写入输出文件
for _, lease := range validLeases {
line := fmt.Sprintf("%s,%s,%s,1,0,0\n", lease.IP, lease.Mac, lease.Hostname)
_, err := file.WriteString(line)
if err != nil {
fmt.Println(err)
}
}
// 将主机数量写入数量文件
_, err = numFile.WriteString(strconv.Itoa(len(validLeases)))
if err != nil {
fmt.Println(err)
}
fmt.Println("Files written successfully")
}
@wwng2333
Copy link
Author

build on mipsel:

GOOS=linux GOARCH=mipsle go build -o adh2dnsmasq main.go
upx --lzma --best adh2dnsmasq

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment