Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
ARG GO_VERSION=1.23
ARG ALPINE_VERSION=3.20

FROM golang:${GO_VERSION}-alpine${ALPINE_VERSION}
ARG USERNAME=vscode
ARG USER_UID=1000
ARG USER_GID=1000

# Setup user
RUN adduser $USERNAME -s /bin/sh -D -u $USER_UID $USER_GID && \
mkdir -p /etc/sudoers.d && \
echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME && \
chmod 0440 /etc/sudoers.d/$USERNAME

# Install packages
RUN apk add -q \
--update \
--progress \
--no-cache \
git \
sudo \
openssh-client \
zsh \
bind-tools \
bash \
make \
jq \
curl \
vim

# Install Go language server
# RUN go get -u -v golang.org/x/tools/cmd/gopls 2>&1

# Setup shell
USER $USERNAME
RUN sh -c "$(wget -O- https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)" "" --unattended &> /dev/null
ENV ENV="/home/$USERNAME/.ashrc" \
ZSH=/home/$USERNAME/.oh-my-zsh \
EDITOR=vi \
LANG=en_US.UTF-8
RUN printf 'ZSH_THEME="robbyrussell"\nENABLE_CORRECTION="false"\nplugins=(git copyfile extract colorize dotenv encode64 golang)\nsource $ZSH/oh-my-zsh.sh' > "/home/$USERNAME/.zshrc"
RUN echo "exec `which zsh`" > "/home/$USERNAME/.ashrc"
USER root
16 changes: 16 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "Go",
"dockerFile": "Dockerfile",
"forwardPorts": [80, 443, 53],
"postCreateCommand": "go mod download",
"runArgs": [
"-u",
"vscode",
"--cap-add=SYS_PTRACE",
"--security-opt",
"seccomp=unconfined",
// map SSH keys for Git
"-v", "${env:HOME}/.ssh:/home/vscode/.ssh:ro",
"--name", "needle-dev"
]
}
12 changes: 12 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for more information:
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
# https://containers.dev/guide/dependabot

version: 2
updates:
- package-ecosystem: "devcontainers"
directory: "/"
schedule:
interval: weekly
7 changes: 3 additions & 4 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:

- uses: actions/setup-go@v3
with:
go-version: 1.21.x
go-version: 1.23.x

- uses: docker/setup-qemu-action@v2
- uses: docker/setup-buildx-action@v2
Expand All @@ -32,10 +32,9 @@ jobs:
password: ${{ secrets.DOCKER_TOKEN }}

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v4
uses: goreleaser/goreleaser-action@v6
if: success() && startsWith(github.ref, 'refs/tags/')
with:
version: latest
args: release --rm-dist
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:

- uses: actions/setup-go@v3
with:
go-version: 1.21.x
go-version: 1.23.x

- name: Install step-cli
run: |
Expand Down
100 changes: 53 additions & 47 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,56 +1,61 @@
version: 2

env:
- GO111MODULE=on
before:
hooks:
- go mod tidy

builds:
- env:
- CGO_ENABLED=0
goos:
- linux
- darwin
- windows
goarch:
- 386
- amd64
- arm
- arm64
mod_timestamp: '{{ .CommitTimestamp }}'
flags:
- -trimpath
ldflags:
- -s -w
- -X go.pixelfactory.io/pkg/version.REVISION={{ .ShortCommit }}
- -X go.pixelfactory.io/pkg/version.BUILDDATE={{ .CommitDate }}
- env:
- CGO_ENABLED=0
goos:
- linux
- darwin
- windows
goarch:
- "386"
- amd64
- arm
- arm64
mod_timestamp: "{{ .CommitTimestamp }}"
flags:
- -trimpath
ldflags:
- -s -w
- -X go.pixelfactory.io/pkg/version.REVISION={{ .ShortCommit }}
- -X go.pixelfactory.io/pkg/version.BUILDDATE={{ .CommitDate }}

checksum:
name_template: '{{ .ProjectName }}_checksums.txt'
name_template: "{{ .ProjectName }}_checksums.txt"

changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'
- '^ci:'
- Merge pull request
- Merge branch
- go mod tidy
- "^docs:"
- "^test:"
- "^ci:"
- Merge pull request
- Merge branch
- go mod tidy

nfpms:
- file_name_template: '{{ .ProjectName }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}'
- file_name_template: "{{ .ProjectName }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}"
homepage: https://github.com/pixelfactoryio/needle
description: |-
Small HTTP/1.1, HTTP/2, server with TLS support, that block ads and trackers by reponsding to all requests with a transparent 1x1 gif pixel.
maintainer: Amine Benseddik <amine@pixelfactory.io>
license: MIT
vendor: pixelfactory.io
formats:
- apk
- deb
- rpm
- apk
- deb
- rpm

dockers:
- image_templates:
- 'pixelfactory/needle:{{ .Tag }}-amd64'
- "pixelfactory/needle:{{ .Tag }}-amd64"
dockerfile: Dockerfile
use: buildx
build_flag_templates:
Expand All @@ -62,7 +67,7 @@ dockers:
- "--label=org.opencontainers.image.source={{.GitURL}}"
- "--platform=linux/amd64"
- image_templates:
- 'pixelfactory/needle:{{ .Tag }}-arm64'
- "pixelfactory/needle:{{ .Tag }}-arm64"
dockerfile: Dockerfile
use: buildx
build_flag_templates:
Expand All @@ -76,32 +81,33 @@ dockers:
goarch: arm64

docker_manifests:
- name_template: 'pixelfactory/needle:{{ .Tag }}'
- name_template: "pixelfactory/needle:{{ .Tag }}"
image_templates:
- 'pixelfactory/needle:{{ .Tag }}-amd64'
- 'pixelfactory/needle:{{ .Tag }}-arm64'
- name_template: 'pixelfactory/needle:latest'
- "pixelfactory/needle:{{ .Tag }}-amd64"
- "pixelfactory/needle:{{ .Tag }}-arm64"
- name_template: "pixelfactory/needle:latest"
image_templates:
- 'pixelfactory/needle:{{ .Tag }}-amd64'
- 'pixelfactory/needle:{{ .Tag }}-arm64'
- "pixelfactory/needle:{{ .Tag }}-amd64"
- "pixelfactory/needle:{{ .Tag }}-arm64"

archives:
- name_template: '{{ .ProjectName }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}'
replacements:
darwin: Darwin
linux: Linux
windows: Windows
386: i386
amd64: x86_64
- format: tar.gz
name_template: >-
{{ .ProjectName }}_
{{- title .Os }}_
{{- if eq .Arch "amd64" }}x86_64
{{- else if eq .Arch "386" }}i386
{{- else }}{{ .Arch }}{{ end }}
{{- if .Arm }}v{{ .Arm }}{{ end }}
format_overrides:
- goos: windows
format: zip
- goos: windows
format: zip
files:
- LICENSE
- README.md

brews:
- tap:
- repository:
owner: pixelfactoryio
name: homebrew-tools
commit_author:
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ COPY needle_*.apk /tmp/
RUN apk add --allow-untrusted /tmp/needle_*.apk \
&& rm -fr /tmp/needle_*.apk

ENTRYPOINT ["/usr/local/bin/needle"]
ENTRYPOINT ["/usr/bin/needle"]
6 changes: 3 additions & 3 deletions cmd/needle.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func start(_ *cobra.Command, _ []string) error {
coredns.WithPort(corednsPort),
coredns.WithHostsFile(corednsHostsFile),
coredns.WithUpstreams(corednsUpstreams),
coredns.WithCoreFile(corednsCoreFile),
coredns.WithCoreFile(corednsCoreFile), // TODO: include only if flag is set
)

// Start CoreDNS Server
Expand Down Expand Up @@ -207,7 +207,7 @@ func start(_ *cobra.Command, _ []string) error {

router := http.NewRouter(logger, routes...)

tlsSrv, err := server.NewServer(
tlsSrv, err := server.New(
server.WithName("needle-tls"),
server.WithLogger(logger),
server.WithRouter(router),
Expand All @@ -228,7 +228,7 @@ func start(_ *cobra.Command, _ []string) error {
}
}()

httpSrv, err := server.NewServer(
httpSrv, err := server.New(
server.WithName("needle-http"),
server.WithLogger(logger),
server.WithRouter(router),
Expand Down
84 changes: 84 additions & 0 deletions cmd/parse/parse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package parse

import (
"slices"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var (
logLevel string
inputFiles []string
outputFile string
publicIPv4 string
publicIPv6 string
)

var parseHostCmd = &cobra.Command{
Use: "parse",
Short: "Parse hosts file",
RunE: start,
}

func bindFlag(flag string) error {
err := viper.BindPFlag(flag, parseHostCmd.PersistentFlags().Lookup(flag))
if err != nil {
return err
}

return nil
}

func NewCmd() (*cobra.Command, error) {
parseHostCmd.PersistentFlags().StringVar(&logLevel, "log-level", "info", "Log level (debug, info, warn, error, fatal, panic)")
if err := bindFlag("log-level"); err != nil {
return nil, err
}

parseHostCmd.PersistentFlags().StringSliceVar(&inputFiles, "file", []string{}, "Hosts file URL")
if err := bindFlag("file"); err != nil {
return nil, err
}

parseHostCmd.PersistentFlags().StringVar(&outputFile, "output", "data/hosts", "Output file")
if err := bindFlag("output"); err != nil {
return nil, err
}

parseHostCmd.PersistentFlags().StringVar(&publicIPv4, "public-ip", "0.0.0.0", "Public IP")
if err := bindFlag("public-ip"); err != nil {
return nil, err
}

parseHostCmd.PersistentFlags().StringVar(&publicIPv6, "public-ip6", "::", "Public IPv6")
if err := bindFlag("public-ip6"); err != nil {
return nil, err
}

return parseHostCmd, nil
}

func start(_ *cobra.Command, _ []string) error {
hosts := []string{}

for _, file := range inputFiles {
bs, err := getHostFile(file)
if err != nil {
return err
}

hosts = append(hosts, Hosts(bs, publicIPv4, publicIPv6)...)
}

// Remove duplicates
slices.Sort(hosts)
hosts = slices.Compact(hosts)

// Write to file
if err := WriteHosts(hosts, outputFile); err != nil {
return err
}

return nil
}
Loading