File dae.spec of Package dae

#
# spec file for package dae
#
# Copyright (c) 2023 SUSE LLC
#
# All modifications and additions to the file contributed by third parties
# remain the property of their copyright owners, unless otherwise agreed
# upon. The license for this file, and modifications and additions to the
# file, is the same license as for the pristine package itself (unless the
# license for the pristine package is not an Open Source License, in which
# case the license is the MIT License). An "Open Source License" is a
# license that conforms to the Open Source Definition (Version 1.9)
# published by the Open Source Initiative.

# Please submit bugfixes or comments via https://bugs.opensuse.org/
#


Name:           dae
Version:        0.2.4rc2+git.054f569
Release:        0
Summary:        A Linux high-performance transparent proxy solution based on eBPF
License:        AGPL-3.0-only
Group:          Productivity/Networking/Web/Proxy
URL:            https://github.com/daeuniverse/dae
Source0:        %{name}-%{version}.tar.xz
Source1:        vendor.tar.gz
Source2:        bpf2go.tar.xz
Source3:        config.dae

Source1000:     dae.rpmlintrc

BuildRequires:  clang
BuildRequires:  llvm
BuildRequires:  golang-packaging
BuildRequires:  golang(API) >= 1.22
BuildRequires:  systemd-rpm-macros

Requires:       v2ray-rules-dat

Suggests:       %{name}-bash-completion
Suggests:       %{name}-fish-completion
Suggests:       %{name}-zsh-completion

%{?systemd_ordering}
%{go_nostrip}
%{go_provides}

%description
In order to improve the traffic split performance as much as 
possible, dae runs the transparent proxy and traffic split suite in 
the Linux kernel by eBPF. Therefore, dae has the opportunity to make 
the direct traffic bypass the forwarding by proxy application and 
achieve true direct traffic through. Under such a magic trick, there 
is almost no performance loss and additional resource consumption 
for direct traffic.

%package devel
Summary:        A dev package with BPF binaries and Go sources for eBPF objects
Group:          Development/Languages/Other
%description devel
This is a development package that contains binary files and Go 
source files of BPF programs, used to embed and use eBPF object 
files.

%package bash-completion
Summary:        Bash Completion for %{name}
Group:          System/Shells
Requires:       %{name} = %{version}
Requires:       bash-completion
Supplements:    (%{name} and bash-completion)
BuildArch:      noarch

%description bash-completion
Bash completion script for dae CLI.

%package zsh-completion
Summary:        ZSH Completion for %{name}
Group:          System/Shells
Requires:       %{name} = %{version}
Supplements:    (%{name} and zsh)
BuildArch:      noarch

%description zsh-completion
zsh shell completions for %{name}.

%package fish-completion
Summary:        Fish Completion for %{name}
Group:          System/Shells
Requires:       %{name} = %{version}
Supplements:    (%{name} and fish)
BuildArch:      noarch

%description fish-completion
Fish command line completion support for %{name}

%prep
%autosetup -a1 -N
mkdir -p %{_builddir}/%{name}-%{version}/vendor/github.com/cilium/ebpf/cmd
tar -xf %{S:2} -C %{_builddir}/%{name}-%{version}/vendor/github.com/cilium/ebpf/cmd/
sed -i '/^github.com\/cilium\/ebpf$/agithub.com/cilium/ebpf/cmd/bpf2go' %{_builddir}/%{name}-%{version}/vendor/modules.txt
sed -i 's/-mod=mod//' %{_builddir}/%{name}-%{version}/control/control.go
sed -i 's/-mod=mod//' %{_builddir}/%{name}-%{version}/trace/trace.go

%build
%goprep .

export BPF_CLANG=clang \
    BPF_STRIP_FLAG="-strip=%{_bindir}/llvm-strip" \
    BPF_CFLAGS="-DMAX_MATCH_SET_LEN=1024 -O2 -Wall -Werror" \
    BPF_TARGET="bpfel,bpfeb" \
    BPF_TRACE_TARGET=%{go_arch}
go generate ./control/control.go
go generate ./trace/trace.go && export GO_BUILD_TAGS=trace || export GO_BUILD_TAGS=""


%gobuild \
    -tags=$GO_BUILD_TAGS \
    -trimpath \
    -ldflags " \
        -s -w \
        -X 'github.com/daeuniverse/dae/cmd.Version=%{version}' \
        -X 'github.com/daeuniverse/dae/common/consts.MaxMatchSetLen_=1024'
    " .


%install
%goinstall

# config
install -d %{buildroot}%{_sysconfdir}/dae
install -d %{buildroot}%{_sysconfdir}/dae/config.d
install -Dm 640 %{S:3} %{buildroot}%{_sysconfdir}/dae/config.dae
install -Dm 640 example.dae %{buildroot}%{_sysconfdir}/dae/config.dae.example

# systemd
install -d %{buildroot}%{_unitdir}
install -Dm 644 install/dae.service %{buildroot}%{_unitdir}/
install -d %{buildroot}%{_sbindir}/
ln -sf %{_sbindir}/service %{buildroot}%{_sbindir}/rcdae

install -d %{buildroot}%{_datadir}/%{name}

# bpf devel
install -d %{buildroot}%{_libdir}/dae
install -Dm 664 control/bpf_bpfeb.o %{buildroot}%{_libdir}/dae/
install -Dm 664 control/bpf_bpfel.o %{buildroot}%{_libdir}/dae/
install -d %{buildroot}%{_datadir}/dae
install -Dm 664 control/bpf_bpfeb.go %{buildroot}%{_datadir}/dae/
install -Dm 664 control/bpf_bpfel.go %{buildroot}%{_datadir}/dae/

# bash completion
install -d %{buildroot}%{_datadir}/bash-completion/completions/
%{buildroot}%{_bindir}/dae completion bash > %{buildroot}%{_datadir}/bash-completion/completions/%{name}

# zsh completion
install -d %{buildroot}%{_datadir}/zsh/site-functions/
%{buildroot}%{_bindir}/dae completion zsh > %{buildroot}%{_datadir}/zsh/site-functions/_%{name}

# fish completion
install -d %{buildroot}%{_datadir}/fish/vendor_completions.d/
%{buildroot}%{_bindir}/dae completion fish > %{buildroot}%{_datadir}/fish/vendor_completions.d/%{name}.fish

%pre
%service_add_pre dae.service

%post
%service_add_post dae.service
%{__ln_s} -f %{_datadir}/v2ray-rules-dat/geoip.dat %{_datadir}/%{name}/geoip.dat
%{__ln_s} -f %{_datadir}/v2ray-rules-dat/geosite.dat %{_datadir}/%{name}/geosite.dat

%preun
%service_del_preun dae.service

%postun
%service_del_postun dae.service
# only removed on uninstall, and not upgrade, downgrade, or reinstall
case "$1" in
  0)
    %{__rm} -f %{_datadir}/%{name}/geoip.dat
    %{__rm} -f %{_datadir}/%{name}/geosite.dat
    ;;
esac

%files devel
%dir %{_libdir}/dae
%{_libdir}/dae/bpf*.o
%dir %{_datadir}/dae
%{_datadir}/dae/bpf*.go

%files
%license LICENSE
%doc README.md docs/en docs/zh
%{_bindir}/dae
%dir %{_sysconfdir}/dae
%dir %{_sysconfdir}/dae/config.d
%config %{_sysconfdir}/dae/config.dae
%ghost %{_sysconfdir}/dae/config.dae.example
%{_unitdir}/dae.service
%{_sbindir}/rcdae
# geoData
%dir %{_datadir}/%{name}
%ghost %{_datadir}/%{name}/geosite.dat
%ghost %{_datadir}/%{name}/geoip.dat

%files bash-completion
%{_datadir}/bash-completion/completions/%{name}

%files zsh-completion
%dir %{_datadir}/zsh
%dir %{_datadir}/zsh/site-functions
%{_datadir}/zsh/site-functions/_%{name}

%files fish-completion
%dir %{_datadir}/fish
%dir %{_datadir}/fish/vendor_completions.d
%{_datadir}/fish/vendor_completions.d/%{name}.fish

%changelog
openSUSE Build Service is sponsored by