2023-02-02 20:22:18 +07:00
|
|
|
/*
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2024-01-04 16:28:16 +07:00
|
|
|
* Copyright (c) 2022-2024, daeuniverse Organization <dae@v2raya.org>
|
2023-02-02 20:22:18 +07:00
|
|
|
*/
|
|
|
|
|
|
|
|
package control
|
|
|
|
|
|
|
|
import (
|
|
|
|
_ "embed"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ProgField struct {
|
|
|
|
Name string
|
|
|
|
Ebpf string
|
|
|
|
}
|
|
|
|
|
2023-02-07 22:52:37 +07:00
|
|
|
//go:embed bpf_subobjects.tmpl
|
2023-02-02 20:22:18 +07:00
|
|
|
var tmpl []byte
|
|
|
|
|
|
|
|
func generate(output string) error {
|
|
|
|
var lanProgFields []ProgField
|
|
|
|
var wanProgFields []ProgField
|
|
|
|
tBpfProg := reflect.ValueOf(bpfObjects{}).FieldByName("bpfPrograms").Type()
|
|
|
|
for i := 0; i < tBpfProg.NumField(); i++ {
|
|
|
|
structField := tBpfProg.Field(i)
|
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(structField.Name, "TproxyLan"):
|
|
|
|
lanProgFields = append(lanProgFields, ProgField{
|
|
|
|
Name: structField.Name,
|
|
|
|
Ebpf: structField.Tag.Get("ebpf"),
|
|
|
|
})
|
|
|
|
case strings.HasPrefix(structField.Name, "TproxyWan"):
|
|
|
|
wanProgFields = append(wanProgFields, ProgField{
|
|
|
|
Name: structField.Name,
|
|
|
|
Ebpf: structField.Tag.Get("ebpf"),
|
|
|
|
})
|
|
|
|
default:
|
2023-02-07 20:11:12 +07:00
|
|
|
return fmt.Errorf("unexpected program name which should begin with TproxyWan or TproxyLan, but get: %v", structField.Name)
|
2023-02-02 20:22:18 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t, err := template.New("").Parse(string(tmpl))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f, err := os.OpenFile(output, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
abs, err := filepath.Abs(output)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Printf("Write to %v\n", abs)
|
|
|
|
if err = t.Execute(f, map[string]interface{}{
|
|
|
|
"WanProgFields": wanProgFields,
|
|
|
|
"LanProgFields": lanProgFields,
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GenerateObjects(output string) {
|
|
|
|
if err := generate(output); err != nil {
|
2023-02-06 12:56:43 +07:00
|
|
|
fmt.Println(err.Error())
|
2023-02-02 20:22:18 +07:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|