Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

metric-api: Add prometheus metrics #2060

Open
wants to merge 31 commits into
base: dev-next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
a3c2fa6
wireguard: Fix events chan leak
nekohasekai Aug 26, 2024
5a184b8
Update workflow to go1.23
nekohasekai Aug 20, 2024
ecdaac1
Introduce bittorrent related protocol sniffers
iosmanthus May 27, 2024
f4cd036
Introduce DTLS sniffer
nekohasekai Jul 7, 2024
10d33ee
Drop support for go1.18 and go1.19
nekohasekai Jun 7, 2024
e413f84
platform: Prepare connections list
nekohasekai Jun 11, 2024
cbadd7a
platform: Add log update interval
nekohasekai Jun 18, 2024
e889c0c
platform: Fix clash server reload on android
nekohasekai Jun 25, 2024
318c1f1
WTF is this
nekohasekai Jun 24, 2024
defa521
Implement read deadline for QUIC based UDP inbounds
nekohasekai Aug 18, 2024
6c96e81
Bump rule-set version
nekohasekai Jul 17, 2024
b67eb68
Improve usages of `json.Unmarshal`
nekohasekai Jul 22, 2024
8593c8e
Add IP address support for `rule-set match` match
nekohasekai Jul 3, 2024
b253e22
Add `rule-set decompile` command
nekohasekai Jul 3, 2024
8c60321
Add auto-redirect & Improve auto-route
nekohasekai Jun 7, 2024
f08a46f
Improve base DNS transports & Minor fixes
nekohasekai Jun 7, 2024
345cf67
Add custom options for TUN `auto-route` and `auto-redirect`
nekohasekai Jun 22, 2024
79dece9
Add accept empty DNS rule option
nekohasekai Jun 24, 2024
45eb3b9
Unique rule-set names
nekohasekai Jun 25, 2024
33ce490
Add inline rule-set & Add reload for local rule-set
nekohasekai Jun 25, 2024
75f9b22
Improve QUIC sniffer
nekohasekai Jul 7, 2024
7f45055
Add AdGuard DNS filter support
nekohasekai Jul 26, 2024
15f66ef
Write close error to log
nekohasekai Aug 10, 2024
f6a0194
Introduce SSH sniffer
nekohasekai Aug 20, 2024
7007fec
clash-api: Fix bad redirect
nekohasekai Aug 23, 2024
6dfa5e8
clash-api: Add PNA support
nekohasekai Aug 23, 2024
05c4f94
documentation: Bump version
nekohasekai Jun 7, 2024
08e72b1
metric-api: Add prometheus metrics
juvenn Aug 24, 2024
3c6173d
metric-api: Fix include
juvenn Aug 24, 2024
7363f49
metric-api: http server error handling
juvenn Aug 24, 2024
d98115d
metric-api: Add config options doc
juvenn Aug 24, 2024
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
33 changes: 16 additions & 17 deletions .github/workflows/debug.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,32 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: ^1.22
continue-on-error: true
go-version: ^1.23
- name: Run Test
run: |
go test -v ./...
build_go118:
name: Debug build (Go 1.18)
build_go120:
name: Debug build (Go 1.20)
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4
with:
fetch-depth: 0
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: ~1.18
go-version: ~1.20
- name: Cache go module
uses: actions/cache@v4
with:
path: |
~/go/pkg/mod
key: go118-${{ hashFiles('**/go.sum') }}
key: go120-${{ hashFiles('**/go.sum') }}
- name: Run Test
run: make ci_build_go118
build_go120:
name: Debug build (Go 1.20)
run: make ci_build_go120
build_go121:
name: Debug build (Go 1.21)
runs-on: ubuntu-latest
steps:
- name: Checkout
Expand All @@ -64,17 +63,17 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: ~1.20
go-version: ~1.21
- name: Cache go module
uses: actions/cache@v4
with:
path: |
~/go/pkg/mod
key: go120-${{ hashFiles('**/go.sum') }}
key: go121-${{ hashFiles('**/go.sum') }}
- name: Run Test
run: make ci_build_go120
build_go121:
name: Debug build (Go 1.21)
run: make ci_build
build_go122:
name: Debug build (Go 1.22)
runs-on: ubuntu-latest
steps:
- name: Checkout
Expand All @@ -84,13 +83,13 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: ~1.21
go-version: ~1.22
- name: Cache go module
uses: actions/cache@v4
with:
path: |
~/go/pkg/mod
key: go121-${{ hashFiles('**/go.sum') }}
key: go122-${{ hashFiles('**/go.sum') }}
- name: Run Test
run: make ci_build
cross:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
release:
types:
- released
- prereleased
workflow_dispatch:
inputs:
tag:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: ^1.22
go-version: ^1.23
- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: ^1.22
go-version: ^1.23
- name: Extract signing key
run: |-
mkdir -p $HOME/.gnupg
Expand Down
8 changes: 4 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM --platform=$BUILDPLATFORM golang:1.22-alpine AS builder
FROM --platform=$BUILDPLATFORM golang:1.23-alpine AS builder
LABEL maintainer="nekohasekai <[email protected]>"
COPY . /go/src/github.com/sagernet/sing-box
WORKDIR /go/src/github.com/sagernet/sing-box
Expand All @@ -13,15 +13,15 @@ RUN set -ex \
&& export COMMIT=$(git rev-parse --short HEAD) \
&& export VERSION=$(go run ./cmd/internal/read_tag) \
&& go build -v -trimpath -tags \
"with_gvisor,with_quic,with_dhcp,with_wireguard,with_ech,with_utls,with_reality_server,with_acme,with_clash_api" \
"with_gvisor,with_quic,with_dhcp,with_wireguard,with_ech,with_utls,with_reality_server,with_acme,with_clash_api,with_metric_api" \
-o /go/bin/sing-box \
-ldflags "-X \"github.com/sagernet/sing-box/constant.Version=$VERSION\" -s -w -buildid=" \
-ldflags "-X \"github.com/sagernet/sing-box/constant.Version=$VERSION\" -s -w -buildid= -checklinkname=0" \
./cmd/sing-box
FROM --platform=$TARGETPLATFORM alpine AS dist
LABEL maintainer="nekohasekai <[email protected]>"
RUN set -ex \
&& apk upgrade \
&& apk add bash tzdata ca-certificates \
&& apk add bash tzdata ca-certificates nftables \
&& rm -rf /var/cache/apk/*
COPY --from=builder /go/bin/sing-box /usr/local/bin/sing-box
ENTRYPOINT ["sing-box"]
11 changes: 3 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
NAME = sing-box
COMMIT = $(shell git rev-parse --short HEAD)
TAGS_GO118 = with_gvisor,with_dhcp,with_wireguard,with_reality_server,with_clash_api
TAGS_GO120 = with_quic,with_utls
TAGS_GO120 = with_gvisor,with_dhcp,with_wireguard,with_reality_server,with_clash_api,with_metric_api,with_quic,with_utls
TAGS_GO121 = with_ech
TAGS ?= $(TAGS_GO118),$(TAGS_GO120),$(TAGS_GO121)
TAGS_TEST ?= with_gvisor,with_quic,with_wireguard,with_grpc,with_ech,with_utls,with_reality_server
Expand All @@ -20,13 +19,9 @@ PREFIX ?= $(shell go env GOPATH)
build:
go build $(MAIN_PARAMS) $(MAIN)

ci_build_go118:
go build $(PARAMS) $(MAIN)
go build $(PARAMS) -tags "$(TAGS_GO118)" $(MAIN)

ci_build_go120:
go build $(PARAMS) $(MAIN)
go build $(PARAMS) -tags "$(TAGS_GO118),$(TAGS_GO120)" $(MAIN)
go build $(PARAMS) -tags "$(TAGS_GO120)" $(MAIN)

ci_build:
go build $(PARAMS) $(MAIN)
Expand Down Expand Up @@ -213,4 +208,4 @@ clean:
update:
git fetch
git reset FETCH_HEAD --hard
git clean -fdx
git clean -fdx
35 changes: 19 additions & 16 deletions adapter/experimental.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import (
"bytes"
"context"
"encoding/binary"
"io"
"net"
"time"

"github.com/sagernet/sing-box/common/urltest"
"github.com/sagernet/sing-dns"
N "github.com/sagernet/sing/common/network"
"github.com/sagernet/sing/common/rw"
"github.com/sagernet/sing/common/varbin"
)

type ClashServer interface {
Expand Down Expand Up @@ -56,16 +55,15 @@ func (s *SavedRuleSet) MarshalBinary() ([]byte, error) {
if err != nil {
return nil, err
}
err = rw.WriteUVariant(&buffer, uint64(len(s.Content)))
err = varbin.Write(&buffer, binary.BigEndian, s.Content)
if err != nil {
return nil, err
}
buffer.Write(s.Content)
err = binary.Write(&buffer, binary.BigEndian, s.LastUpdated.Unix())
if err != nil {
return nil, err
}
err = rw.WriteVString(&buffer, s.LastEtag)
err = varbin.Write(&buffer, binary.BigEndian, s.LastEtag)
if err != nil {
return nil, err
}
Expand All @@ -79,12 +77,7 @@ func (s *SavedRuleSet) UnmarshalBinary(data []byte) error {
if err != nil {
return err
}
contentLen, err := rw.ReadUVariant(reader)
if err != nil {
return err
}
s.Content = make([]byte, contentLen)
_, err = io.ReadFull(reader, s.Content)
err = varbin.Read(reader, binary.BigEndian, &s.Content)
if err != nil {
return err
}
Expand All @@ -94,7 +87,7 @@ func (s *SavedRuleSet) UnmarshalBinary(data []byte) error {
return err
}
s.LastUpdated = time.Unix(lastUpdated, 0)
s.LastEtag, err = rw.ReadVString(reader)
err = varbin.Read(reader, binary.BigEndian, &s.LastEtag)
if err != nil {
return err
}
Expand Down Expand Up @@ -125,10 +118,20 @@ func OutboundTag(detour Outbound) string {

type V2RayServer interface {
Service
StatsService() V2RayStatsService
StatsService() PacketTracking
}

type V2RayStatsService interface {
RoutedConnection(inbound string, outbound string, user string, conn net.Conn) net.Conn
RoutedPacketConnection(inbound string, outbound string, user string, conn N.PacketConn) N.PacketConn
type MetricService interface {
Service
PacketTracking
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if it is a good name, suggestions are welcome.

}

type PacketTracking interface {
WithConnCounters(inbound, outbound, user string) ConnAdapter[net.Conn]
WithPacketConnCounters(inbound, outbound, user string) ConnAdapter[N.PacketConn]
}

// ConnAdapter
// Transform a connection to another connection. The T should be either of
// net.Conn or N.PacketConn.
type ConnAdapter[T any] func(T) T
14 changes: 11 additions & 3 deletions adapter/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,16 @@ type InboundContext struct {
Network string
Source M.Socksaddr
Destination M.Socksaddr
Domain string
Protocol string
User string
Outbound string

// sniffer

Protocol string
Domain string
Client string
SniffContext any

// cache

InboundDetour string
Expand All @@ -51,7 +56,9 @@ type InboundContext struct {

// rule cache

IPCIDRMatchSource bool
IPCIDRMatchSource bool
IPCIDRAcceptEmpty bool

SourceAddressMatch bool
SourcePortMatch bool
DestinationAddressMatch bool
Expand All @@ -62,6 +69,7 @@ type InboundContext struct {

func (c *InboundContext) ResetRuleCache() {
c.IPCIDRMatchSource = false
c.IPCIDRAcceptEmpty = false
c.SourceAddressMatch = false
c.SourcePortMatch = false
c.DestinationAddressMatch = false
Expand Down
17 changes: 16 additions & 1 deletion adapter/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@ import (
"github.com/sagernet/sing-tun"
"github.com/sagernet/sing/common/control"
N "github.com/sagernet/sing/common/network"
"github.com/sagernet/sing/common/x/list"
"github.com/sagernet/sing/service"

mdns "github.com/miekg/dns"
"go4.org/netipx"
)

type Router interface {
Service
PreStarter
PostStarter
Cleanup() error

Outbounds() []Outbound
Outbound(tag string) (Outbound, bool)
Expand All @@ -45,7 +48,9 @@ type Router interface {
DefaultInterface() string
AutoDetectInterface() bool
AutoDetectInterfaceFunc() control.Func
DefaultMark() int
DefaultMark() uint32
RegisterAutoRedirectOutputMark(mark uint32) error
AutoRedirectOutputMark() uint32
NetworkMonitor() tun.NetworkUpdateMonitor
InterfaceMonitor() tun.DefaultInterfaceMonitor
PackageManager() tun.PackageManager
Expand Down Expand Up @@ -92,12 +97,22 @@ type DNSRule interface {
}

type RuleSet interface {
Name() string
StartContext(ctx context.Context, startContext RuleSetStartContext) error
PostStart() error
Metadata() RuleSetMetadata
ExtractIPSet() []*netipx.IPSet
IncRef()
DecRef()
Cleanup()
RegisterCallback(callback RuleSetUpdateCallback) *list.Element[RuleSetUpdateCallback]
UnregisterCallback(element *list.Element[RuleSetUpdateCallback])
Close() error
HeadlessRule
}

type RuleSetUpdateCallback func(it RuleSet)

type RuleSetMetadata struct {
ContainsProcessRule bool
ContainsWIFIRule bool
Expand Down
Loading